Day 34 of 90 Days of DevOps Challenge: Working with Services in Kubernetes
Welcome to Day 34! 🎉 After learning about Deployments and Namespaces in Kubernetes, it’s time to focus on Services. Services play a critical role in connecting Pods with each other, as well as exposing your applications to external clients. Let’s dive into today’s tasks and explore the different types of Services in Kubernetes. 🚀
What are Services in Kubernetes?
In Kubernetes, Services are responsible for providing stable network access to a group of Pods. Pods can come and go, but Services offer a fixed point of communication that doesn’t change. Services abstract away the dynamic nature of Pods and allow other Pods, external systems, or users to interact with your application.
Types of Services:
ClusterIP: Exposes the service internally to the cluster.
NodePort: Exposes the service on a static port on each node.
LoadBalancer: Exposes the service externally, typically with a cloud provider's load balancer.
Task 1: Create a Service for Your Todo-App Deployment
Let’s start by creating a Service for your todo-app deployment from Day 32. This will allow external clients or other Pods within the cluster to access the application.
Step 1: Create a Service YAML File
Here’s a simple YAML definition for a Service:
apiVersion: v1
kind: Service
metadata:
name: todo-app-service
namespace: my-namespace
spec:
selector:
app: todo-app
ports:
- protocol: TCP
port: 80
targetPort: 5000
type: NodePort
selector: Ensures that traffic is directed to Pods with the label
app: todo-app
.port: The port exposed by the Service.
targetPort: The port on which your application (todo-app) is listening inside the Pod.
Step 2: Apply the Service to Your Kubernetes Cluster
Use the following command to apply the Service definition to your minikube cluster:
kubectl apply -f service.yml -n my-namespace
Step 3: Verify the Service
You can verify that the Service is working by accessing the todo-app using the Service’s NodePort and IP. To find the IP and port, run:
kubectl get service -n my-namespace
Use the displayed IP and port to access the todo-app from your browser or a curl command.
Task 2: Create a ClusterIP Service
The ClusterIP service allows access to the application from within the cluster. This is useful when other Pods or internal systems need to communicate with your todo-app.
Step 1: Create a ClusterIP Service YAML File
apiVersion: v1
kind: Service
metadata:
name: todo-app-clusterip-service
namespace: my-namespace
spec:
selector:
app: todo-app
ports:
- protocol: TCP
port: 80
targetPort: 5000
type: ClusterIP
Step 2: Apply the ClusterIP Service to Your Kubernetes Cluster
Run the following command to apply the ClusterIP Service definition:
kubectl apply -f cluster-ip-service.yml -n my-namespace
Step 3: Verify the ClusterIP Service
You can verify the service by creating another Pod in the same namespace and accessing the todo-app via the service:
kubectl run test-pod --rm -i --tty --image busybox -- /bin/sh
From the busybox shell, run:
wget -qO- http://todo-app-clusterip-service:80
This should display the HTML content from your todo-app.
Task 3: Create a LoadBalancer Service
A LoadBalancer service exposes your application to external users. This is especially useful when your Kubernetes cluster is running on a cloud platform like AWS, GCP, or Azure, but Minikube also supports LoadBalancer services.
Step 1: Create a LoadBalancer Service YAML File
apiVersion: v1
kind: Service
metadata:
name: todo-app-loadbalancer-service
namespace: my-namespace
spec:
selector:
app: todo-app
ports:
- protocol: TCP
port: 80
targetPort: 5000
type: LoadBalancer
Step 2: Apply the LoadBalancer Service
To apply the LoadBalancer service, run:
kubectl apply -f load-balancer-service.yml -n my-namespace
Step 3: Verify the LoadBalancer Service
Once the LoadBalancer service is running, it will allocate an external IP address. You can check the external IP by running:
kubectl get service todo-app-loadbalancer-service -n my-namespace
After the external IP is assigned, you can access your todo-app using the LoadBalancer’s IP address.
Conclusion
Today, you explored the three main types of Services in Kubernetes: NodePort, ClusterIP, and LoadBalancer. Each type serves a different purpose, whether you need internal access, external access, or load balancing across your application’s Pods.
By mastering services in Kubernetes, you can ensure that your applications are reachable and resilient, no matter how complex your infrastructure becomes.
Don’t forget to share your progress on LinkedIn with #90DaysOfDevOps! 🚀