Skip to content

Latest commit

 

History

History
78 lines (56 loc) · 3.05 KB

05-expose-your-website.md

File metadata and controls

78 lines (56 loc) · 3.05 KB

Exposing the Website

create a website-service.yaml file to define a Service of type LoadBalancer for your e-commerce website. This will expose your Deployment to the internet, making it accessible via a public IP address or URL.

Here's the website-service.yaml file:

apiVersion: v1
kind: Service
metadata:
  name: ecommerce-website-service
  labels:
    app: ecommerce-website
spec:
  type: LoadBalancer
  ports:
    - port: 80
      targetPort: 80
      protocol: TCP
  selector:
    app: ecommerce-website

Let's break down this YAML file:

  1. metadata:

    • name: This is the name of your service. You'll use this name to refer to the service within your cluster.
    • labels: These help identify the service and can be used for organization and selection.
  2. spec:

    • type: LoadBalancer: This tells Kubernetes to provision an external load balancer (if available in your cloud environment) to route traffic to your service.
    • ports:
      • port: 80: The port on which the load balancer will listen.
      • targetPort: 80: The port on your pods to which traffic will be sent.
      • protocol: TCP: The network protocol to use.
    • selector: This maps the service to pods with the label app: ecommerce-website. Make sure this matches the labels in your Deployment.

To apply this Service configuration:

  1. Save the above YAML as website-service.yaml.

  2. Apply it to your Kubernetes cluster:

    kubectl apply -f website-service.yaml
    
  3. Check the status of your service:

    kubectl get services
    

    Look for ecommerce-website-service in the output.

  4. To get the external IP or hostname:

    kubectl get service ecommerce-website-service
    

    In the output, look for the EXTERNAL-IP column. It might take a few minutes for the external IP to be provisioned.

Outcome:

  • Once the external IP is provisioned, you'll be able to access your website using this IP address or hostname.
  • If you're using a cloud provider that supports DNS names for load balancers (like AWS), you might get a DNS name instead of an IP address.
  • You can access your website by navigating to http://<EXTERNAL-IP> in a web browser.

Important notes:

  1. The actual availability of a LoadBalancer and how it's implemented depends on your Kubernetes environment. Cloud providers usually provision a cloud load balancer, while bare-metal setups might require additional configuration.

  2. If you're using Minikube or a local Kubernetes setup, LoadBalancer might not work as expected. In such cases, you can use NodePort instead of LoadBalancer for testing purposes.

  3. For production environments, consider setting up an Ingress controller for more advanced routing capabilities and to manage SSL/TLS termination.

  4. Remember to secure your application appropriately when exposing it to the internet. This includes implementing proper authentication, authorization, and encryption mechanisms.

With this Service configuration, your e-commerce website should now be accessible from the internet, allowing users to interact with your application.