6 Kubernetes Ports: A Definitive Look - Expose, NodePort, TargetPort, & More
Tags: webdev, kubernetes, devops, node
Published: 2024-01-21 14:23:08


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](https://kubernetes.io/docs/concepts/services-networking/service/#type-nodeport), a [Service type](https://kubernetes.io/docs/concepts/services-networking/service/#publishing-services-service-types) in [Kubernetes](https://kubernetes.io/) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xaxf3cl6tm957elgbjxq.png)


### 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4p4kq21eq8clqw5u3teq.png)


### 2. [Container Port](https://kubernetes.io/docs/tutorials/services/connect-applications-service/#exposing-pods-to-the-cluster) (`8001`)

App Server Port -> Container Port
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uqv2qzn6x5j2t4kqkriv.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t58wuotzxm3c6szdlzpr.png)


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](https://kubernetes.io/docs/concepts/services-networking/service/#services-in-kubernetes) (`8001`)

App Server Port -> Container Port -> Target Port
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/65n5vy6d603fiuwloifr.png)


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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d7sdt5n3x0xdtl99yhw2.png)


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>
```

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
```

Continue learning about [Internal Service Port](https://journal.hexmos.com/kubernetes-ports-overview-covering-expose-nodeport-targetport/#4-internal-service-port-5001), [Node Port](https://journal.hexmos.com/kubernetes-ports-overview-covering-expose-nodeport-targetport/#5-node-port-30904), [Web Server Port](https://journal.hexmos.com/kubernetes-ports-overview-covering-expose-nodeport-targetport/#6-web-server-or-load-balancer-port-80443).