Day 34 Task: Working with Services in Kubernetes
Table of contents
🔹What are Services in K8s?
Services: Services are a K8s resource that defines a logical set of pods and a policy for accessing them. They ensure that your application remains discoverable and available to other parts of your application or to external users.
Services come in several types:
ClusterIP: This service type exposes the service on an internal cluster IP. It's suitable for communication between pods within the same cluster.
NodePort: NodePort services expose the service on a static port on each node's IP address. It's commonly used for accessing services from outside the cluster during development or testing.
LoadBalancer: LoadBalancer services are typically used in cloud environments where they provision a load balancer to distribute traffic across multiple pods. This allows for external access to services.
ExternalName: This service type maps a service to an external DNS name. It's used for accessing external services from within the cluster.
Services provide an essential layer of networking abstraction in Kubernetes, allowing applications to communicate reliably and consistently, regardless of where pods are located or how they are scaled.
🔹Task-1:
Create a Service for your todo-app Deployment from Day-32
Create a Service definition for your todo-app Deployment in a YAML file.
Apply the Service definition to your K8s (minikube) cluster using the
kubectl apply -f service.yml -n <namespace-name>
command.Verify that the Service is working by accessing the todo-app using the Service's IP and Port in your Namespace.
🔹Task-2:
Create a ClusterIP Service for accessing the todo-app from within the cluster
Create a ClusterIP Service definition for your todo-app Deployment in a YAML file.
Apply the ClusterIP Service definition to your K8s (minikube) cluster using the
kubectl apply -f cluster-ip-service.yml -n <namespace-name>
command.Verify that the ClusterIP Service is working by accessing the todo-app from another Pod in the cluster in your Namespace.
🔹Task-3:
Create a LoadBalancer Service for accessing the todo-app from outside the cluster
Create a LoadBalancer Service definition for your todo-app Deployment in a YAML file.
Apply the LoadBalancer Service definition to your K8s (minikube) cluster using the
kubectl apply -f load-balancer-service.yml -n <namespace-name>
command.Verify that the LoadBalancer Service is working by accessing the todo-app from outside the cluster in your Namespace.
To create a LoadBalancer service, you can edit the service definition:
kubectl edit svc/todo-app-service -n devopspro
Change
type: ClusterIP
totype: LoadBalancer
, save the file, and exit. After a short time, Kubernetes will allocate an external IP address, and you can access the service using that IP.