Day 34 Working with Services in Kubernetes π
You've made it to Day 34 of your Kubernetes learning journey! Yesterday, you deployed an application in Kubernetes, and today, weβre taking things further by setting up Services to manage how our app can be accessed. Let's dive in!
π What Are Services in Kubernetes?
In Kubernetes (K8s), Services are objects that act like a bridge, providing stable network identities to Pods (the smallest deployable units in Kubernetes). They help your app receive traffic, regardless of Pod IP changes, and ensure seamless communication between Pods, Services, and even external clients.
π§ In Simple Terms: Imagine Services as doors that let you reach your app without worrying about which room (Pod) it's in. Services manage this for you, so traffic always reaches the right place!
π§ Hands-On Tasks: Setting Up Services for Your todo-app
Let's get practical! Weβll create and use three types of Kubernetes Services to access the todo-app
deployed on Day 32:
Task 1: Create a Service for Your todo-app
Deployment πͺ
here the project link
git clone https://github.com/LondheShubham153/django-todo-cicd.git
This Service will give your app a stable identity within the cluster.
Define the Service: Create a YAML file named
service.yml
with this basic setup:apiVersion: v1 kind: Service metadata: name: todo-app-service spec: selector: app: todo-app ports: - protocol: TCP port: 80 targetPort: 8080
Apply the Service Definition: Use the following command to apply the Service to your Kubernetes cluster (replace
<namespace-name>
with your actual namespace):kubectl apply -f service.yml -n <namespace-name>
Verify the Service: Check if your Service is working by accessing the
todo-app
using the Service's IP and port:kubectl get svc -n <namespace-name>
Task 2: Create a ClusterIP Service for Internal Access π
ClusterIP is the default Service type that only allows access within the cluster.
Define the ClusterIP Service: Create a YAML file named
cluster-ip-service.yml
:apiVersion: v1 kind: Service metadata: name: todo-app-clusterip-service spec: type: ClusterIP selector: app: todo-app ports: - protocol: TCP port: 80 targetPort: 8080
Apply the ClusterIP Service Definition:
kubectl apply -f cluster-ip-service.yml -n <namespace-name>
Verify the ClusterIP Service: Access the
todo-app
from another Pod within the cluster. You can use the Podβs terminal to confirm internal access:kubectl exec -it <pod-name> -n <namespace-name> -- curl todo-app-clusterip-service
Task 3: Create a LoadBalancer Service for External Access π
A LoadBalancer Service allows your app to be accessed from outside the cluster.
Define the LoadBalancer Service: Create a YAML file named
load-balancer-service.yml
:yamlCopy codeapiVersion: v1 kind: Service metadata: name: todo-app-loadbalancer-service spec: type: LoadBalancer selector: app: todo-app ports: - protocol: TCP port: 80 targetPort: 8080
Apply the LoadBalancer Service Definition:
kubectl apply -f load-balancer-service.yml -n <namespace-name>
Verify the LoadBalancer Service: Once the LoadBalancer is set up, access the
todo-app
from outside the cluster using the external IP provided:kubectl get svc -n <namespace-name>
π Note: LoadBalancer might not work on Minikube, as it's designed for cloud environments like AWS or GCP. In Minikube, you can use the
minikube service
command to access LoadBalancer Services.
π Summary
Here's what we've covered:
Kubernetes Services provide stable access to your app within and outside the cluster.
We created three types of Services for your
todo-app
:Standard Service for basic access.
ClusterIP Service for internal access only.
LoadBalancer Service for external access.
π Congratulations on Completing Day 34!
Youβre becoming a Kubernetes pro! Keep practicing and exploring more about Services in Kubernetes. The more you work with these concepts, the more confident you'll become.
Subscribe to my newsletter
Read articles from Dhruv Moradiya directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by