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:
-
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.
-
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 labelapp: ecommerce-website
. Make sure this matches the labels in your Deployment.
To apply this Service configuration:
-
Save the above YAML as
website-service.yaml
. -
Apply it to your Kubernetes cluster:
kubectl apply -f website-service.yaml
-
Check the status of your service:
kubectl get services
Look for
ecommerce-website-service
in the output. -
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:
-
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.
-
If you're using Minikube or a local Kubernetes setup, LoadBalancer might not work as expected. In such cases, you can use
NodePort
instead ofLoadBalancer
for testing purposes. -
For production environments, consider setting up an Ingress controller for more advanced routing capabilities and to manage SSL/TLS termination.
-
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.