Kubernetes Networking with Services | Pods | Containers

labby

Labby

Posted on August 10, 2024

Kubernetes Networking with Services | Pods | Containers

Introduction

This article covers the following tech skills:

Skills Graph

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.

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
Enter fullscreen mode Exit fullscreen mode

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

kubectl apply -f /home/labex/project/myapp-pod.yaml
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

kubectl apply -f /home/labex/project/service.yaml
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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

kubectl apply -f /home/labex/project/test-pod-1.yaml
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

curl http://my-service
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

kubectl apply -f service.yaml
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

MindMap


🚀 Practice Now: Connecting Pods with Kubernetes Services


Want to Learn More?

💖 💪 🙅 🚩
labby
Labby

Posted on August 10, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related