What are Services in K8s
In Kubernetes, Services are objects that provide stable network identities to Pods and abstract away the details of Pod IP addresses. Services allow Pods to receive traffic from other Pods, Services, and external clients.
In Kubernetes, imagine you have a bunch of workers (Pods) doing different jobs for a big project (your application). Now, you want people (other parts of your app or users) to easily reach these workers.
A Service is like a receptionist who helps direct people to the right worker (Pod). Instead of everyone finding and bothering individual workers, they go to the receptionist (Service) and say, "I need to talk to the team working on 'Feature A'," and the receptionist guides them to the right place.
This receptionist does a few things:
Gives a Stable Address: It provides a stable address (like a phone number) that doesn't change even if workers (Pods) come and go. This way, people can always reach the project team.
Shares the Workload: When many people are looking for a certain team, the receptionist (Service) distributes them evenly to available workers (Pods). Imagine it like a busy restaurant - instead of everyone crowding the kitchen, the receptionist organizes who goes where.
Chooses the Right Team: The receptionist uses labels (like 'Feature A' or 'Frontend Team') to know which team (Pods with certain labels) to send people to. This ensures the right people talk to the right team.
Has Different Desks (Service Types): There are different types of reception desks - some for everyone in the building (ClusterIP), some reachable from outside (NodePort), and even a public entrance (LoadBalancer) where lots of people can come in!
So, Services in Kubernetes simplify how different parts of your application or users can talk to the right parts of your application, making everything organized and efficient, just like a good receptionist at a busy office!
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.
apiVersion: v1
kind: Service
metadata:
name: todo-service
namespace: todo
labels:
app: todo
spec:
selector:
app: todo
ports:
- protocol: TCP
port: 80
targetPort: 8000
type: NodePort
- Apply the Service definition to your K8s (minikube) cluster using the
kubectl apply -f service.yml -n <namespace-name>
command.
ubuntu@ip-172-31-60-234:~$ kubectl apply -f service.yml -n todo
service/todo-service configured
- Verify that the Service is working by accessing the todo-app using the Service's IP and Port in your Namespace.
We are getting the output to curl, the service is working
Commands -
kubectl get ns todo
minikube service todo-service -n=todo --url
curl -L http://192.168.49.2:31443
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.
apiVersion: v1
kind: Service
metadata:
name: todo-service
namespace: todo
labels:
app: todo
spec:
selector:
app: todo
ports:
- protocol: TCP
port: 8000
targetPort: 8000
type: ClusterIP
- Apply the ClusterIP Service definition to your K8s (minikube) cluster using the
kubectl apply -f cluster-ip-service.yml -n <namespace-name>
command.
kubectl apply -f service_clusterIP.yml -n todo
- Verify that the ClusterIP Service is working by accessing the todo-app from another Pod in the cluster in your Namespace. - We can use wget <IP:port> to check if we are able to access the service from different pods.
ubuntu@ip-172-31-60-234:~$ kubectl get pods -n todo
NAME READY STATUS RESTARTS AGE
todo-app-587d5cdcd8-hkbrn 1/1 Running 0 30m
todo-app-587d5cdcd8-rx6jx 1/1 Running 0 30m
todo-app-587d5cdcd8-vqpns 1/1 Running 0 30m
todo-app-587d5cdcd8-xnb4d 1/1 Running 0 30m
ubuntu@ip-172-31-60-234:~$ kubectl get services -n todo
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
todo-service ClusterIP 10.99.192.130 <none> 8000/TCP 48m
ubuntu@ip-172-31-60-234:~$ kubectl exec -it todo-app-587d5cdcd8-rx6jx -n todo -- sh
/app #
/app # wget http://10.99.192.130:8000
Connecting to 10.99.192.130:8000 (10.99.192.130:8000)
index.html 100% |****************************************************************************************************************************************************************************************************| 667 0:00:00 ETA
/app #
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.
apiVersion: v1
kind: Service
metadata:
name: todo-service-lb
namespace: todo
labels:
app: todo
spec:
selector:
app: todo
ports:
- protocol: TCP
port: 8000
targetPort: 8000
type: LoadBalancer
- Apply the LoadBalancer Service definition to your K8s (minikube) cluster using the
kubectl apply -f load-balancer-service.yml -n <namespace-name>
command.
ubuntu@ip-172-31-60-234:~$ kubectl apply -f service_LB.yml -n todo
service/todo-service-lb created
ubuntu@ip-172-31-60-234:~$ minikube service todo-service-lb -n=todo --url
http://192.168.49.2:31873
ubuntu@ip-172-31-60-234:~$ minikube service list
|-------------|-----------------|--------------|---------------------------|
| NAMESPACE | NAME | TARGET PORT | URL |
|-------------|-----------------|--------------|---------------------------|
| default | kubernetes | No node port | |
| kube-system | kube-dns | No node port | |
| todo | todo-service | 8000 | http://192.168.49.2:30140 |
| todo | todo-service-lb | 8000 | http://192.168.49.2:31873 |
|-------------|-----------------|--------------|---------------------------|
- Verify that the LoadBalancer Service is working by accessing the todo-app from outside the cluster in your Namespace.
We are getting the output to the command - curl -L http://192.168.49.2:31873 which states that the application is accessible.
Thank you :)