Categories: Kubernetes

Kubernetes – Port, Targetport and NodePort

When working with Kubernetes Service, you will come across some of the following terminologies:

  • Port: Port is the port number which makes a service visible to other services running within the same K8s cluster.  In other words, in case a service wants to invoke another service running within the same Kubernetes cluster, it will be able to do so using port specified against “port” in the service spec file.
  • Target Port: Target port is the port on the POD where the service is running.
  • Nodeport: Node port is the port on which the service can be accessed from external users using Kube-Proxy.

Take a look at following spec defining a sample service:

apiVersion: v1
kind: Service
metadata:
  name: order-service
spec:
  ports:
  - port: 8080
    targetPort: 8170
    nodePort: 32222
    protocol: TCP 
  selector:
    component: order-service-app

Pay attention to some of the following in above spec:

  • The port is 8080 which represents that order-service can be accessed by other services in the cluster at port 8080.
  • The targetPort is 8170 which represents the order-service is actually running on port 8170 on pods
  • The nodePort is 32222 which represents that order-service can be accessed via kube-proxy on port 32222.

This is ipTables in Kubernetes which does the magic. It maintains the mapping of nodePort vs targetPort. K8s Kube-Proxy uses the ipTables to resolve the requests coming on a specific nodePort and redirect them to appropriate pods.

Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking.

View Comments

Share
Published by
Ajitesh Kumar
Tags: Kubernetes

Recent Posts

Mathematics Topics for Machine Learning Beginners

In this blog, you would get to know the essential mathematical topics you need to…

19 hours ago

Questions to Ask When Thinking Like a Product Leader

This blog represents a list of questions you can ask when thinking like a product…

4 days ago

Three Approaches to Creating AI Agents: Code Examples

AI agents are autonomous systems combining three core components: a reasoning engine (powered by LLM),…

1 week ago

What is Embodied AI? Explained with Examples

Artificial Intelligence (AI) has evolved significantly, from its early days of symbolic reasoning to the…

2 months ago

Retrieval Augmented Generation (RAG) & LLM: Examples

Last updated: 25th Jan, 2025 Have you ever wondered how to seamlessly integrate the vast…

5 months ago

How to Setup MEAN App with LangChain.js

Hey there! As I venture into building agentic MEAN apps with LangChain.js, I wanted to…

5 months ago