DEV Community

Cover image for 6 Kubernetes Ports: A Definitive Look - Expose, NodePort, TargetPort, & More
Athreya aka Maneshwar
Athreya aka Maneshwar

Posted on

6 Kubernetes Ports: A Definitive Look - Expose, NodePort, TargetPort, & More

Ever felt confused about server, docker, service, container, target or node ports in Kubernetes? This article breaks it down for you, explaining every port in your workflow, from development to deployment Dive in and simplify the complexity today!

Recently I was trying to setup a deployment pipeline on top of our Kubernetes infrastructure.

I was searching for a proper guide on the types of ports and how traffic is navigated between them, and I couldn’t find any ready-made solutions.

After learning about it and solving my problem, I have written this article to help you gain clarity on ports in a simple way and foster discussions.
Perfect for both self-study and helping out a friend!

Communication of Ports:

In the below method, I'm using NodePort, a Service type in Kubernetes to demonstrate how traffic flows between an Application Server and a Web Server.

This article is focused on providing conecptual clarity of ports in Kubernetes.

Image description

1. Application Server Port (8001)

You probably know this already.

You write your code in your framework of choice.

Either Django, or Node, or Gin, or any of the other options.

These frameworks have their respective run commands.

For example, in Django python manage.py runserver

And we see the Django app being accessible at port 8001

Image description

2. Container Port (8001)

App Server Port -> Container Port
Image description

You probably might know this as well already.

In Kubernetes, a "container" is like a compact and portable package that holds everything your application needs to run. Imagine it as a virtual box that contains your app, its dependencies, and even the environment it requires.

Now, let's talk about ports. Think of them as doors or entry points to your application. When we create a Docker image (a snapshot of your app and its environment), we also decide which port our application should use. If your app runs on port 3000, Docker exposes that same port.

Image description

When we start the Docker image, it transforms into a "container" - a running instance of your application.

Since we've already exposed a port, the container is ready to accept the incoming traffic and forward it to the Application inside.

3. Target Port (8001)

App Server Port -> Container Port -> Target Port
Image description

The Target Port refers to the port on your Pod which forwards the traffic to the Container Port.

The Target Port is highlighted in red colour below.

Image description

The Service forwards the traffic from the Internal Service Port to the Target Port on the Pod.

The App Server Port, Container Port and Target Port were pretty straightforward and understandable as they were all meant to be the same. That means the service is going to redirect traffic to the Target Port which reaches the App Server.

Extras

You can view the Pod’s description by using the kubectl command



kubectl get pods
kubectl describe pod <pod-name>


Enter fullscreen mode Exit fullscreen mode

The Target Port can be set in the service.yaml



apiVersion: v1
kind: Service
metadata:
  name: fb-backends
spec:
  type: NodePort
  ports:
    - targetPort: 8001
      protocol: TCP
  selector:
    app: fb-backends


Enter fullscreen mode Exit fullscreen mode

Continue learning about Internal Service Port, Node Port, Web Server Port.

Top comments (0)