Connecting Pods with Kubernetes Services

KubernetesBeginner
Practice Now

Introduction

In Kubernetes, Pods are ephemeral and can be terminated and recreated at any time. This presents a challenge when it comes to networking, as it is difficult to connect to a Pod directly. To solve this problem, Kubernetes provides a higher-level abstraction called a Service. A Service provides a stable IP address and DNS name for a set of Pods, allowing other components to connect to them easily. In this lab, you will learn how to network Pods with Services in Kubernetes.

Start the Minikube Cluster

Before creating resources, you need a running Kubernetes cluster. Minikube is a lightweight Kubernetes environment that runs on your local machine.

  1. Navigate to your working directory:

    Open the terminal and navigate to the default project folder:

    cd /home/labex/project
    
  2. Start Minikube:

    Start Minikube to initialize a Kubernetes cluster:

    minikube start
    
    • This command sets up a single-node Kubernetes cluster on your local machine.
    • Minikube may take a few minutes to start depending on your system's performance.
  3. Verify Minikube is running:

    Check the status of the Minikube cluster:

    minikube status
    
    • Look for components like kubelet and apiserver listed as Running.
    • If the cluster is not running, rerun minikube start.

If you encounter issues starting Minikube. Use minikube delete to reset the environment if needed.

Create a Pod

The first step is to create a simple Pod. Create a file named /home/labex/project/myapp-pod.yaml with the following contents:

apiVersion: v1
kind: Pod
metadata:
  name: my-pod-1
  labels:
    app: nginx
spec:
  containers:
    - name: my-container
      image: nginx

Save the file and create the Pod by running the following command:

minikube kubectl -- apply -f /home/labex/project/myapp-pod.yaml

This will create a Pod named my-pod-1 with a single container running the Nginx image.

Create a Service

The second step is to create a Service that targets the Pod you created in the previous step. Create a file named /home/labex/project/service.yaml with the following contents:

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: nginx
  ports:
    - name: http
      port: 80
      targetPort: 80

Save the file and create the Service by running the following command:

minikube kubectl -- apply -f /home/labex/project/service.yaml

This will create a Service named my-service that targets Pods with the label app=nginx and exposes port 80.

Test the Service

The third step is to test the Service by accessing it from another Pod. Create a file named /home/labex/project/test-pod-1.yaml with the following contents:

apiVersion: v1
kind: Pod
metadata:
  name: test-pod-1
spec:
  containers:
    - name: my-container
      image: busybox
      command:
        - sleep
        - "3600"

Save the file and create the test Pod by running the following command:

minikube kubectl -- apply -f /home/labex/project/test-pod-1.yaml

This will create a Pod named test-pod-1 with a single container running the Busybox image.

Next, access the Service from the Busybox Pod with wget:

minikube kubectl -- exec test-pod-1 -- wget -qO- http://my-service

This command returns the default Nginx page, indicating that the Service is working correctly.

Update the Service

The fourth step is to update the Service to target a different set of Pods. Update the selector field in the /home/labex/project/service.yaml file to the following:

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: busybox
  ports:
    - name: http
      port: 80
      targetPort: 80

Save the file and update the Service by running the following command:

minikube kubectl -- apply -f /home/labex/project/service.yaml

This will update the Service to target Pods with the label app=busybox. At this point, the Service has no endpoints because none of the existing Pods use that label yet.

Test the Updated Service

The fifth step is to test the updated Service by accessing it from another Pod. Create a new test Pod with the following command:

minikube kubectl -- run my-pod-2 --image=busybox --restart=Never -- sleep 3600

This will create a new Pod named my-pod-2 with a single container running the Busybox image.

Before changing any labels, confirm that the Service has no matching endpoints:

minikube kubectl -- get endpoints my-service

The output should show <none> because no Pod currently has the app=busybox label.

This happens because the Service selector now looks for app=busybox, but your Nginx server Pod is still labeled app=nginx. To fix this, relabel the original server Pod so that the Service points to the Nginx container again.

Run the following command to update the label of the server Pod:

minikube kubectl -- label pod my-pod-1 app=busybox --overwrite

This updates the label on my-pod-1 so it matches the Service selector.

Now test the Service again from my-pod-2:

minikube kubectl -- exec my-pod-2 -- wget -qO- http://my-service

This command should return the default Nginx page, indicating that the Service is working correctly again.

Summary

In this lab, you learned how to network Pods with Services in Kubernetes. You created an Nginx Pod, exposed it with a Service, and tested that Service from Busybox client Pods. You also updated the Service selector and relabeled the original Nginx Pod so that the Service once again targeted a working web server. Services are an essential component of networking in Kubernetes and allow you to connect to Pods in a reliable and scalable way.