Day 34 Task: Working with Services in Kubernetes

ยท

4 min read

Day 34 Task: Working with Services in Kubernetes

Services in Kubernetes

The Kubernetes Service API simplifies the process of exposing groups of Pods over a network. Each Service defines a logical set of endpoints, typically represented by Pods, and specifies how to make those Pods accessible.

For instance, consider a stateless image-processing backend with 3 replicas. The frontend clients don't need to be aware of the specific backend Pods; the Service abstraction decouples them. You can define the set of Pods targeted by a Service using a selector.

If your workload uses HTTP, Ingress can control web traffic access. While not a Service type, Ingress serves as the entry point for your cluster, allowing you to consolidate routing rules for multiple workload components running separately.

Beyond Ingress and Service, Kubernetes offers the Gateway API, which extends capabilities for configuring network service access in your cluster. You can add Gateway to your cluster using CustomResourceDefinitions to configure these services.

Service Types in Kubernetes

Kubernetes supports four types of services, each with a different purpose:

  1. ClusterIP: This is the default service type in Kubernetes. It provides a stable IP address and DNS name for accessing pods within the cluster. The ClusterIP service is only accessible from within the cluster.

  2. NodePort: This service type exposes the service on a static port on each node in the cluster. It provides a way to access the service from outside the cluster.

  3. LoadBalancer: This service type provides a load balancer for the service in cloud environments that support load balancers. It automatically creates a load balancer and assigns a public IP address to the service.

  4. ExternalName: This service type provides a way to access an external service by creating a DNS CNAME record. It does not create any endpoints or perform any load balancing.

Task 1: Create a Service for your todo-app Deployment

  1. Create a Service definition YAML file: Begin by creating a YAML file named service.yml to define your Service for the todo-app Deployment.

      apiVersion: v1
      kind: Service
      metadata:
        name: todo-app-deployment
        namespace: todo-app
      spec:
        type: NodePort
        selector:
          app: todo-app
        ports:
          - port: 80
            targetPort: 8000
    
    1. Apply the Service definition: Execute the following command to apply the Service definition to your Kubernetes cluster.

Don't forget to replace <namespace-name> with the actual namespace where your todo-app Deployment resides.

     kubectl apply -f service.yml -n <namespace-name>
  1. Verification: To ensure that the Service is working correctly, access the todo-app using the Service's IP and Port within your specified Namespace.
     kubectl get svc -n <namespace-name>

Task 2: Create a ClusterIP Service for accessing the todo-app within the cluster

Create a ClusterIP Service definition YAML file: Now, create another YAML file named cluster-ip-service.yml to define a ClusterIP Service for your todo-app Deployment.

 apiVersion: v1
 kind: Service
 metadata:
   name: todo-app-clusterip
   labels:
     app: todo-app
 spec:
   selector:
     app: todo-app
   ports:
     - protocol: TCP
       port: 8000
       targetPort: 8080
   type: ClusterIP
  1. Apply the ClusterIP Service definition: Use the following command to apply the ClusterIP Service definition to your Kubernetes cluster within the specified namespace.

      kubectl apply -f cluster-ip-service.yml -n <namespace-name>
    

  2. Verification: To confirm that the ClusterIP Service is functioning correctly, access the todo-app from another Pod within the cluster, still in your specified Namespace.

    Create a YAML file named test-pod.yml for a testing pod:

      apiVersion: v1
      kind: Pod
      metadata:
        name: test-pod
      spec:
        containers:
          - name: busybox
            image: busybox
            command: ['sh', '-c', 'while true; do wget -q -O- clusterip:8000; done']'
    

    Apply the ClusterIP Service definition to your K8s cluster using the command & verify todo-app is running

      kubectl apply -f test-pod.yml -n <namespace-name>
    

Tsk 3: Create a LoadBalancer Service for accessing the todo-app from outside the cluster

  1. Create a LoadBalancer Service definition YAML file: Create a YAML file named load-balancer-service.yml to define a LoadBalancer Service for your todo-app Deployment.

    w you can do it:

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

  2. Apply the LoadBalancer Service definition: Apply the LoadBalancer Service definition to your Kubernetes cluster within the specified namespace using the following command:

      kubectl apply -f load-balancer-service.yml -n <namespace-name>
    
  3. Verification: To verify the functionality of the LoadBalancer Service, access the todo-app from outside the cluster, within your specified Namespace with the command:

      kubectl get svc -n <namespace-name>
    

Conclusion

Today, you've acquired essential skills in managing Kubernetes Services, a critical component for establishing dependable network communication within your cluster. Through these practical tasks, you've honed your abilities in both creating and setting up various Service types like ClusterIP and LoadBalancer. This expertise will serve as a valuable asset as you progress further in your Kubernetes exploration.

Did you find this article valuable?

Support DevOps_journey by becoming a sponsor. Any amount is appreciated!

ย