Working with Services in Kubernetes

Working with Services in Kubernetes

#90 Days of DevOps Challenge - Day 34

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.

Task 1:Create a Service for accessing todo-app

  • Create a Service for your todo-app Deployment from Day-32. Create a Service definition for your todo-app Deployment in a YAML file. Now create a service.yml where we will deploy NodePort.
 apiVersion: v1
 kind: Service
 metadata:
   name: my-django-app-service
   namespace: my-app
 spec:
   type: NodePort
   selector:
     app: django-app
   ports:
     - port: 80
       targetPort: 8000
       nodePort: 30010

  • Apply the Service definition to your K8s (minikube) cluster using the kubectl apply -f service.yml -n <namespace-name> command.
kubectl apply -f service.yml -n my-svc-app
#Below command use to check the particular service 
kubectl get svc -n=my-svc-app

  • 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

  • Create a ClusterIP Service for accessing the todo-app from within the cluster
 apiVersion: v1
 kind: Service
 metadata:
   name: my-django-app-cluster
   namespace: my-app
 spec:
   type: ClusterIP
   selector:
     app: django-app
   ports:
     - name: http
       protocol: TCP
       port: 8000
       targetPort: 8000

  • 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 cluster-svc.yml -n my-app

  • Verify that the service is running by running the below command
 kubectl get svc -n my-app

  • Deploy another Pod in the my-app namespace to test the service. You can use the following YAML definition to create a simple test Pod
 apiVersion: v1
 kind: Pod
 metadata:
   name: test-pod
   namespace: my-app
 spec:
   containers:
   - name: busybox
     image: busybox
     command: ['sh', '-c', 'while true; do wget -q -O- my-app-cluster-ip:8000; done']

  • Now we need to create a pod using the below command
 kubectl apply -f test.yml -n my-app

  • Now we need to go inside the pod and execute the wget command
kubectl exec -it test-pod -n my-app -- /bin/sh

# Inside the test pod execute the wget commad.
 wget -qO- http://<ip-of-my-django-app-cluster-ip>:<port
 Ex:- wget -qO- http://10.107.36.50:8000

Now we can say that 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

  • Create a LoadBalancer Service definition for your todo-app Deployment in a YAML file
 apiVersion: v1
 kind: Service
 metadata:
   name: my-django-app-cluster-ip
   namespace: my-app
 spec:
   selector:
     app: django-app
   ports:
     - port: 80
       targetPort: 8000
   type: LoadBalancer

  • Now we need to create LoadBalancer Service definition for your K8s cluster using the below command
 kubectl apply -f load-balancer-svc.yml -n my-app

  • Verify that the LoadBalancer Service is working by accessing the todo-app from outside the cluster in your namespace.
 kubectl get service -n=my-app

  • copy the Public IPv4 DNS and access through the browser by providing the port number in the URL.
 Public-IPv4-DNS:<loadbalancer-port-number>
 Ex:- ec2-18-204-228-100.compute-1.amazonaws.com:31067

Devops#devops,#90daysofDevOps

Thank you for reading!! I hope you find this article helpful!!

if any queries or corrections to be done to this blog please let me know.

Happy Learning!!

Saikat Mukherjee

Did you find this article valuable?

Support Saikat Mukherjee's blog by becoming a sponsor. Any amount is appreciated!