1. Overview

Kubernetes is a popular container orchestration system used to deploy and manage containerized applications. As a crucial component of this ecosystem, Kubernetes Services provide a stable endpoint for accessing pods running within a Kubernetes cluster.

A Service eliminates the need to know IP addresses or ports. However, to ensure proper traffic routing to the responsible pods, it’s important to understand the difference between the port and targetPort fields when defining a Kubernetes Service. Although these fields may appear similar, they serve distinct purposes, making it essential to configure them properly.

In this tutorial, we’ll discuss the differences between port and targetPort and understand how to configure them correctly to ensure effective communication between Services and pods.

2. What Is port?

A Service definition specifies the port number that the Service will listen on for incoming traffic using the port field. The Service uses this port to route traffic to the pods it’s responsible for.

Here’s an example of the YAML definition for a Service with the port field set to 8080:

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: my-app
  ports:
    - name: http
      port: 8080
      protocol: TCP
      targetPort: 8080

We set the port field to 8080, which directs incoming traffic to this Service‘s port 8080. Similarly, we also set the targetPort field to 8080, which routes the traffic to the pods that this Service is responsible for, specifically to their port 8080.

3. What Is targetPort?

In a Service definition, the targetPort field plays a crucial role in routing traffic to the Service‘s pods. Specifically, this field is set to the pod’s port number that the Service is responsible for routing traffic to. By doing so, we can ensure that traffic is directed to the appropriate pods and that our Service functions as intended.

To illustrate, let’s suppose that a pod is running a web server on port 8080. In this case, the targetPort field in the Service definition would be set to 8080, which enables traffic to be routed to that specific pod. Correctly setting the targetPort field is essential for maintaining the reliability and availability of our application.

Here’s an example of the YAML definition for a Service with the targetPort field set to 8080:

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

The targetPort field routes traffic to port 8080 on the Service‘s pods. The port field directs incoming traffic to port 80 on the Service.

4. The Difference Between port and targetPort

The primary distinction between port and targetPort is that port specifies the Service‘s listening port for incoming traffic. The targetPort field specifies the port number for routing traffic to the Service‘s pods.

To put it simply, port is used to listen for incoming traffic from external clients, while targetPort is the Service‘s internal communication port with the pods responsible for handling that traffic.

5. Conclusion

In this article, we learned the importance of port and targetPort fields in Kubernetes Service definitions. Understanding their differences is crucial for effective traffic routing to responsible pods. targetPort should be set to the pod’s port number and port to the client-accessible external port for Service access.

2 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.