Day 34: Working with Services in Kubernetes

Day 34: Working with Services in Kubernetes

  • What are services in K8s?

    In Kubernetes, a Service is a method for exposing a network application that is running as one or more Pods in your cluster.

    The Service API, part of Kubernetes, is an abstraction to help you expose groups of Pods over a network. Each Service object defines a logical set of endpoints (usually these endpoints are Pods) along with a policy about how to make those pods accessible.

    Types of service -

    1. Cluster IP - ClusterIP services expose the service on a cluster-internal IP address, which means the service is only accessible within the cluster. This is useful for services that need to communicate exclusively within the cluster, such as databases.

    2. NodePort - NodePort services expose the service on a specific port of each node in the cluster. It allows external traffic to reach the service by accessing any node's IP address on the specified port. NodePort services are suitable for applications that need external access, but they are not typically used for production deployments due to security and scalability concerns.

    3. LoadBalancer - LoadBalancer services are similar to NodePort services but provide a cloud-specific load balancer to distribute external traffic across the nodes. This type of service is commonly used for applications that require external access and need to be highly available and scalable.

    4. ExternalName - ExternalName services do not define a selector or endpoints but serve as a way to map a service to an external DNS name. This is useful when you want to provide a Kubernetes-style DNS name for an external service, such as a database hosted outside the cluster.

Task 1 :

  1. Create a Service definition for your todo-app Deployment.

     apiVersion: v1
     kind: Service
     metadata:
       name: nginx-service
       namespace: nginx
     spec:
       selector:
         app: nginx-app   
       type: NodePort
       ports:
         - protocol: TCP
           port: 80       
           targetPort: 80 
           nodePort: 30007
    

2. Apply the Service definition to your K8s (minikube) cluster using the kubectl apply -f service.yml -n <namespace-name> command.

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

You can find the IP and Port of your Service by running the following command:

    minikube service nginx-service -n <namespace> --url

This will return the URL you can use to access the todo-app. Copy the URL and paste it into your web browser. If everything is working correctly, you should see the todo-app running in your web browser. As follows-

That's it! You now know how to create and apply a Service definition in Kubernetes, and how to verify that your Service is working correctly.

Task 2:

  • Create a ClusterIP Service for accessing the todo-app from within the cluster and

    Create a ClusterIP Service definition for your todo-app Deployment in a YAML file

      apiVersion: v1
      kind: Service
      metadata:
        name: nginx-service-cluster-ip
        labels:
          app: todo-app
      spec:
        selector:
          app: todo-app
        ports:
          - name: http
            port: 80
            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.

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

if your ClusterIP Service is named todo-service-cluster-ip, you can try running the following command in another Pod:

curl http://nginx-service-cluster-ip

Task 3:

  • Create a LoadBalancer Service for accessing the todo-app from outside the cluster and Create a LoadBalancer Service definition for your todo-app Deployment in a YAML file.
apiVersion: v1
kind: Service
metadata:
  name: nginx-service-load-balancer
  labels:
    app: todo-app
spec:
  type: LoadBalancer
  selector:
    app: todo-app
  ports:
    - name: http
      port: 80
      targetPort: 8000

  • 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.

      minikube service nginx-service-load-balancer -n mgimx-load-balancer --url
    

Conclusion:

In conclusion, services are a critical component of Kubernetes that enable decoupling of the application layer from the infrastructure layer. They provide a consistent way to access pods and provide features such as load balancing, service discovery, and health checking.