DevOps Blog

Using Kubernetes Port, TargetPort, and NodePort

Curl elasticsearch commands.
3 minute read
BMC Software

(This article is part of our Kubernetes Guide. Use the right-hand menu to navigate.)

Kubernetes and ports

Kubernetes is an open-source tool that orchestrates containerized applications. The Kubernetes tool makes sure that containers are running correctly by monitoring pods across different servers or nodes.

It automatically manages responses to container failure, restarting or replacing pods when necessary. It scales them up or down, based on demand, and distributes the computing load across containers to balance the work. It eliminates the manual work of managing, scaling, and maintaining large and complex applications.

Kubernetes uses networking, including ports and services, to facilitate communication between pods and other resources.

Kubernetes Services port configurations: Port vs TargetPort vs NodePort

In Kubernetes there are several different port configurations for Kubernetes services:

  • Port exposes the Kubernetes service on the specified port within the cluster. Other pods within the cluster can communicate with this server on the specified port.
  • TargetPort is the port on which the service will send requests to, that your pod will be listening on. Your application in the container will need to be listening on this port also.
  • NodePort exposes a service externally to the cluster by means of the target nodes IP address and the NodePort. NodePort is the default setting if the port field is not specified.

Let’s look at how to use these ports in your Kubernetes manifest.

Using Port, TargetPort, and NodePort in Kubernetes

apiVersion: v1
kind: Service
metadata:
name: hello-world
spec:
type: NodePort
selector:
app: hello-world
ports:
- protocol: TCP
port: 8080
targetPort: 80
nodePort: 30036

From the above examples the hello-world service will be exposed internally to cluster applications on port 8080 and externally to the cluster on the node IP address on 30036. It will also forward requests to pods with the label “app: hello-world” on port 80.

The configuration of the above settings can be verified with the command:

$ kubectl describe service hello-world

Kubernetes NodePort service description

Create a pod running nginx to which the service will forward requests to:

apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app: hello-world
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80

To test and demonstrate the above configuration, we can create a pod running an ubuntu container to execute some curl commands to verify connectivity.

$ kubectl run -i --tty ubuntu --image=ubuntu --restart=Never -- sh 

From this pod run the following commands:

Curl the service on the ‘port’ defined in the Kubernetes manifest for the service.

$ curl hello-world:8080

This proves that curling the Kubernetes service on port 80 forwards the request to our nginx pod listening on port 80.

To test the NodePort on your machine (not in the ubuntu pod) you will need to find the IP address of the node that your pod is running on.

$ kubectl describe pod nginx

Kubernetes NodePort IP address

Now, you can curl the Node IP Address and the NodePort and should reach the nginx container running behind the Kubernetes service.

Kubernets service port vs targetport vs nodeport

Conclusion

Mastering Kubernetes, especially the use of pods and ports, is essential for efficiently managing containerized applications at scale. As Kubernetes continues to evolve and its adoption grows across industries, understanding how to configure and optimize networking within the platform will empower you to deploy, manage, and scale applications with greater precision and reliability.

Beginning Kubernetes: Knowledge & Tutorials for Getting Started

In this comprehensive e-book, we take a deep dive into the distributed computing platform Kubernetes, also known as K8s.


These postings are my own and do not necessarily represent BMC's position, strategies, or opinion.

See an error or have a suggestion? Please let us know by emailing [email protected].

About Us

As BMC and BMC Helix, we are committed to a shared purpose for customers in every industry and around the globe. BMC empowers 86% of the Forbes Global 50 to accelerate business value faster than humanly possible by automating critical applications, systems, and services to take advantage of cloud, data, and emerging AI technologies. BMC Helix, now operating as an independent company, helps the world’s most forward-thinking IT organizations turn AI into action—unlocking human potential to multiply productivity so teams can focus on the work that matters most.
Learn more about BMC and BMC Helix ›

About the author

BMC Software

BMC works with 86% of the Forbes Global 50 and customers and partners around the world to create their future. With our history of innovation, industry-leading automation, operations, and service management solutions, combined with unmatched flexibility, we help organizations free up time and space to become an Autonomous Digital Enterprise that conquers the opportunities ahead.