Connecting Pods with Kubernetes Services

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


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL kubernetes(("`Kubernetes`")) -.-> kubernetes/TroubleshootingandDebuggingCommandsGroup(["`Troubleshooting and Debugging Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/BasicCommandsGroup(["`Basic Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/AdvancedCommandsGroup(["`Advanced Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/ConfigurationandVersioningGroup(["`Configuration and Versioning`"]) kubernetes/TroubleshootingandDebuggingCommandsGroup -.-> kubernetes/exec("`Exec`") kubernetes/BasicCommandsGroup -.-> kubernetes/delete("`Delete`") kubernetes/BasicCommandsGroup -.-> kubernetes/run("`Run`") kubernetes/AdvancedCommandsGroup -.-> kubernetes/apply("`Apply`") kubernetes/ConfigurationandVersioningGroup -.-> kubernetes/label("`Label`") subgraph Lab Skills kubernetes/exec -.-> lab-15815{{"`Connecting Pods with Kubernetes Services`"}} kubernetes/delete -.-> lab-15815{{"`Connecting Pods with Kubernetes Services`"}} kubernetes/run -.-> lab-15815{{"`Connecting Pods with Kubernetes Services`"}} kubernetes/apply -.-> lab-15815{{"`Connecting Pods with Kubernetes Services`"}} kubernetes/label -.-> lab-15815{{"`Connecting Pods with Kubernetes Services`"}} end

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:

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:

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: nginx
      command:
        - sleep
        - "3600"

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

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, you will exec into the container and use curl to access the Service. Run the following command to exec into the container:

kubectl exec -it test-pod-1 -- sh

This will open a shell inside the container. From the shell, run the following command to access the Service:

curl http://my-service

This will return 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: 8

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

kubectl apply -f service.yaml

This will update the Service to target Pods with the label app=busybox.

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:

kubectl run my-pod-2 --image=nginx --restart=Never

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

Exec into the container and use curl to access the Service as you did in Step 3. This time, you should get an error indicating that the connection was refused.

This is because the Service is now targeting a different set of Pods than the ones that the test Pod is running. To fix this, you can update the label of the Pod to match the new selector in the Service.

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

kubectl label pod my-pod-2 app=busybox

This will add the label app=busybox to the test Pod.

Now, if you run the curl command again, you should get the default Nginx page, indicating that the Service is working correctly.

Summary

In this lab, you learned how to network Pods with Services in Kubernetes. You created a simple Pod running the Nginx image, created a Service to target the Pod, and tested the Service by accessing it from another Pod. You also learned how to update the Service to target a different set of Pods and how to update the label of a Pod to match the new selector in the Service. Services are an essential component of networking in Kubernetes and allow you to connect to Pods in a reliable and scalable way.

Other Kubernetes Tutorials you may like