Author: Vaishnavi Shivde

Kubernetes, the leading container orchestration platform, has transformed the way we deploy, manage, and scale applications. One of the core components that makes Kubernetes so powerful is its robust service management system. In this blog post, we will explore what services are in Kubernetes, why they are essential, and how to work with them effectively.

Service in Kubernetes:

In Kubernetes (k8s), a “Service” is an abstraction that defines a logical set of Pods and a policy by which to access them, sometimes called a microservice. Kubernetes Services enable Pods to communicate with each other and with other parts of the system, ensuring reliable network connectivity and load balancing.

Use Cases:

  • Microservices Communication: Services enable different microservices within a cluster to communicate with each other reliably.
  • External API Exposure: Exposing an internal API to external users via a LoadBalancer service.
  • Internal Load Balancing: Distributing requests among backend Pods for a scalable web application.

Types of Services šŸ§©

  1. ClusterIP (Default)Ā šŸŒ
    • Description: Exposes the Service on an internal IP in the cluster, accessible only within the cluster.
    • Use Case: Internal communication between services within the same cluster.
    • Example:
apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: MyApp
  ports:
    - protocol: TCP
      port: 80
      targetPort: 9376
  type: ClusterIP

2. NodePortĀ šŸ”“

  • Description: Exposes the Service on each Node’s IP at a static port (the NodePort). Accessible externally usingĀ <NodeIP>:<NodePort>.
  • Use Case: Direct access to a service from outside the cluster without a load balancer.
  • Example:
apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: MyApp
  ports:
    - protocol: TCP
      port: 80
      targetPort: 9376
      nodePort: 30007
  type: NodePort

3. LoadBalancerĀ āš–ļø

  • Description: Exposes the Service externally using a cloud provider’s load balancer.
  • Use Case: Automatically provision a load balancer for your service in a cloud environment.
  • Example:
    apiVersion: v1
    kind: Service
    metadata:
      name: my-service
    spec:
      selector:
        app: MyApp
      ports:
        - protocol: TCP
          port: 80
          targetPort: 9376
      type: LoadBalancer

    4. ExternalNameĀ šŸŒ

    • Description: Maps a Service to the contents of theĀ externalNameĀ field (e.g., a CNAME record), without proxying any traffic.
    • Use Case: Redirects requests to an external service (e.g., an external database or third-party service).
    • Example:
    apiVersion: v1
    kind: Service
    metadata:
      name: my-service
    spec:
      type: ExternalName
      externalName: my.external.service.com

      Task 01 : Creating a Service for your ToDo app Deployment

      • Create a Service service.yml for your todo-app Deployment
      apiVersion: v1
      kind: Service
      metadata:
        name: todo-service
        namespace: todo
      spec:
        selector:
          app: todo
        ports:
          - protocol: TCP
            port: 80
            targetPort: 8000
      • Apply the Service definition to your K8s cluster using command.
        kubectl apply -f service.yml -n <namespace-name>
      • Verify that the Service is working by accessing the todo-app using the Service’s IP and Port in your Namespace.
       kubectl get services -n <namespace-name>
      • You should see something like this:

      This shows that you have one Service named todo-service that exposes port 80 of your Pods as port 8000 on an internal IP address 10.98.16.138 in the todo Namespace.

      Task 02 : Creating a ClusterIP Service for internal cluster access

      A ClusterIP Service is the default type of Service that exposes our Pods within the cluster using an internal IP address. This type of Service is only accessible from within the cluster.

      1. Create a YAML file namedĀ cluster_ip_service.ymlĀ with the following content:
      apiVersion: v1
      kind: Service
      metadata:
        name: todo-cluster-ip-service
        namespace: todo
      spec:
        type: ClusterIP
        selector:
          app: todo
        ports:
          - protocol: TCP
            port: 80
            targetPort: 8000

      This file defines a Service object namedĀ todo-cluster-ip-serviceĀ that exposes port 80 of our Pods as port 8000 on an internal IP address. The Service belongs to theĀ todoĀ Namespace and select the Pods that have the labelĀ app: todo. TheĀ type: ClusterIPĀ specifies that this is a ClusterIP Service.

      2. Apply the ClusterIP Service definition to your K8s cluster.

      kubectl apply -f cluster_ip_service.yml -n todo

      3. Verify that the Service is created by running:

      kubectl get services -n todo

      You should see something like this:



      This shows that you have one ClusterIP Service namedĀ todo-cluster-ip-serviceĀ that exposes port 80 of your Pods as port 8000 on an internal IP addressĀ 10.109.228.211Ā in theĀ todoĀ Namespace.

      Task 03: Creating a LoadBalancer Service for external access

      A LoadBalancer Service is a type of Service that exposes our Pods using an external load balancer provided by our cloud provider or platform. This type of Service is accessible from outside the cluster.

      1. Create a YAML file namedĀ load-balancer-service.ymlĀ with the following content:
      apiVersion: v1
      kind: Service
      metadata:
        name: todo-load-balancer-service
        namespace: todo
      spec:
        type: LoadBalancer
        selector:
          app: todo
        ports:
          - protocol: TCP
            port: 80
            targetPort: 8000

      This file defines a Service object namedĀ todo-load-balancer-serviceĀ that exposes port 80 of our Pods as port 8000 on an external IP address. The Service belongs to theĀ todoĀ Namespace and select the Pods that have the labelĀ app: todo. TheĀ type: LoadBalancerĀ specifies that this is a LoadBalancer Service.

      2. Apply the YAML file to create the Service by running:

      kubectl apply -f load-balancer-service.yml -n todo

      3. Verify that the Service is created by running:

      kubectl get services -n todo

      You should see something like this:



      This shows that you have one LoadBalancer Service namedĀ todo-load-balancer-serviceĀ that exposes port 80 of your Pods as port 80 on an external IP addressĀ 10.110.153.22Ā in theĀ todoĀ Namespace.

        Conclusion

        In this blog post, we learned how to work with Services in Kubernetes, a key concept that allows us to expose and access our applications in a cluster. We also deployed and managed different types of Services for our sample ToDo app that we created on Day 32.

        I hope you enjoyed this blog post and learned something new. If you have any questions or feedback, please feel free to leave a comment below.

        Thank you for reading and stay tuned for more!

        Author: Vaishnavi Shivde

        Categorized in:

        Blog,