Working with Services in Kubernetes

·

3 min read

🕸️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 - NodePort Service for 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.

    • use vim / nano to create a service.yaml file and save it
    apiVersion: v1
    kind: Service
    metadata:
      name: todo-service
      namespace: django
    spec:
      type: NodePort
      selector:
        app: todo-app
      ports:

        - port: 80
          targetPort: 8000
          nodePort: 30007
  • Apply the Service definition to your K8s (minikube) cluster using the kubectl apply -f service.yml -n <namespace-name> command.

  • Verify that the Service is working by accessing the todo-app using the Service's IP and Port in your Namespace.

    • To get services , we use kubectl get svc -n <namespace> and for wide overview we can use kubectl get svc -o wide -n <namespace>

  • To understand about nodeport (type) service you can refer this - blog

Task 2 - ClusterIP Service for todo-app

  • 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-app-cluster-ip
        labels:
          app: todo-app
      spec:
        selector:
          app: todo-app
        ports:
          - protocol: TCP
            port: 8000
            targetPort: 8080
        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.

  • Verify that the ClusterIP Service is working by accessing the todo-app from another Pod in the cluster in your Namespace.

    • Create YAML file for a testing pod:

        apiVersion: v1
        kind: Pod
        metadata:
          name: test-pod
        spec:
          containers:
            - name: test-container
              image: busybox
              command: ["sh", "-c", "while true; do wget -q -O- http://todo-app-cluster-ip:80; sleep 1; done"]
      
    • Apply it to your K8s (minikube) cluster using the kubectl apply -f cluster-ip-service.yml -n <namespace-name> command.

    • You can also use kubectl log test-pod -n <namespace>

Task 3 - LoadBalancer Service for todo-app

  • 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-app-loadbalancer
      spec:
        selector:
          app: todo-app
        ports:
          - protocol: TCP
            port: 80
            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.

  • Verify that the LoadBalancer Service is working by accessing the todo-app from outside the cluster in your Namespace.

I hope you understood about the types of services , how and where to use them !!

Stay tuned for more content ..!

Happy Learning :D