Day 34 Task: Working with Services in Kubernetes

Task 1: Creating a Service for the todo-app Deployment

Service YAML File: service.yml

apiVersion: v1
kind: Service
metadata:
  name: todo-app-service
  namespace: node-app
spec:
  selector:
    app: todo
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8000
  type: NodePort

Steps:

  1. Save the above YAML content in a file named service.yml.

  2. Apply the Service definition:

     kubectl apply -f service.yml -n node-app
    
  3. Verify the Service:

     kubectl get svc -n node-app
    
  4. Access the app using the Node IP and the NodePort (retrieved from kubectl get svc -n node-app):

     curl <Node-IP>:<NodePort>
    

Task 2: Creating a ClusterIP Service

ClusterIP YAML File: cluster-ip-service.yml

apiVersion: v1
kind: Service
metadata:
  name: todo-app-cluster-ip-service
  namespace: node-app
spec:
  selector:
    app: todo
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8000
  type: ClusterIP

Steps:

  1. Save the above YAML content in a file named cluster-ip-service.yml.

  2. Apply the ClusterIP Service definition:

     kubectl apply -f cluster-ip-service.yml -n node-app
    
  3. Verify the Service:

     kubectl get svc -n node-app
    
  4. Access the Service from another Pod in the cluster. Create a temporary Pod for testing:

     kubectl run test-pod --image=busybox -it --rm --namespace=node-app -- /bin/sh
    
  5. Inside the Pod, test the Service:

     wget -qO- todo-app-cluster-ip-service
    

Task 3: Creating a LoadBalancer Service

LoadBalancer YAML File: load-balancer-service.yml

apiVersion: v1
kind: Service
metadata:
  name: todo-app-load-balancer
  namespace: node-app
spec:
  selector:
    app: todo
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8000
  type: LoadBalancer

Steps:

  1. Save the above YAML content in a file named load-balancer-service.yml.

  2. Apply the LoadBalancer Service definition:

     kubectl apply -f load-balancer-service.yml -n node-app
    
  3. Verify the Service:

     kubectl get svc -n node-app
    
  4. Use the External IP provided by the LoadBalancer to access the todo-app:

     curl <External-IP>:80
    

Final Verification

Run the following command to ensure all services are up and running:

kubectl get svc -n node-app

Check connectivity to each Service as per their configuration.


Key Notes

  • NodePort: Accessible from outside the cluster on the Node’s IP and exposed port.

  • ClusterIP: Accessible only within the cluster for inter-Pod communication.

  • LoadBalancer: Provides external access using a cloud load balancer (in Kind, a LoadBalancer simulation is possible using tools like MetalLB).

With these steps, you’ll have all three types of Services configured and tested for your todo-app. 🚀