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. For latest updates and blogs, follow us on Twitter. 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. Check out my other blog, Revive-n-Thrive.com

View Comments

Share
Published by
Ajitesh Kumar
Tags: Kubernetes

Recent Posts

Mean Squared Error vs Cross Entropy Loss Function

Last updated: 28th April, 2024 As a data scientist, understanding the nuances of various cost…

2 days ago

Cross Entropy Loss Explained with Python Examples

Last updated: 28th April, 2024 In this post, you will learn the concepts related to…

2 days ago

Logistic Regression in Machine Learning: Python Example

Last updated: 26th April, 2024 In this blog post, we will discuss the logistic regression…

3 days ago

MSE vs RMSE vs MAE vs MAPE vs R-Squared: When to Use?

Last updated: 22nd April, 2024 As data scientists, we navigate a sea of metrics to…

5 days ago

Gradient Descent in Machine Learning: Python Examples

Last updated: 22nd April, 2024 This post will teach you about the gradient descent algorithm…

1 week ago

Loss Function vs Cost Function vs Objective Function: Examples

Last updated: 19th April, 2024 Among the terminologies used in training machine learning models, the…

1 week ago