Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Container port and targetport in Kubernetes?

How is container port different from targetports in a container in Kubernetes? Are they used interchangeably, if so why?

I came across the below code snippet where containerPort is used to denote the port on a pod in Kubernetes.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres-deployment
  labels:
    app: demo-voting-app
spec:
  replicas: 1
  selector:
    matchLabels:
      name: postgres-pod
      app: demo-voting-app
  template:
    metadata:
      name: postgres-pod
      labels:
        name: postgres-pod
        app: demo-voting-app

    spec:
      containers:
      - name: postgres
        image: postgres:9.4
        ports:
        - containerPort: 5432
        

In the above code snippet, they have given 5432 for the containerPort parameter (in the last line). So, how is this containerPort different from targetport?

As far as I know, the term port in general refers to the port on the service (Kubernetes). Correct me if I'm incorrect.

like image 214
Purushothaman Srikanth Avatar asked Aug 17 '20 09:08

Purushothaman Srikanth


People also ask

What is difference between port and TargetPort in Kubernetes?

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.

What is container port in Kubernetes?

“containerPort” defines the port on which app can be reached out inside the container. once your container is spun up and the pod is running. You may need to expose the POD as a service to the external world sometimes. Kubernetes service comes into the picture here.

What is host port and container port?

containerPort: A container port specifies a port within a container. This is only necessary as part of a port mapping when using BRIDGE or USER mode networking with a Docker container. hostPort: A host port specifies a port on the host to bind to.

What is container port in Yaml file?

containerPortList of ports to expose from the container. Exposing a port here gives the system additional information about the network connections a container uses, but is primarily informational. Not specifying a port here DOES NOT prevent that port from being exposed. Any port which is listening on the default "0.0.

What is targetport in Kubernetes?

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.

What are the different types of ports in Kubernetes?

Kubernetes – Port, Targetport and NodePort. 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. The port is 8080 which represents that order-service can be accessed by other services in...

What is the difference between targetport and nodeport in K8s?

Summary: all requests end up in the targetport. nodeport is used if request from outside k8s network & port if from within. "Target port" is the port on which your container is running. Port : port redirects the traffic to the container from the service.

What is the difference between targetport and containerport?

In a nutshell: targetPort and containerPort basically refer to the same port (so if both are used they are expected to have the same value) but they are used in two different contexts and have entirely different purposes.


3 Answers

In a nutshell: targetPort and containerPort basically refer to the same port (so if both are used they are expected to have the same value) but they are used in two different contexts and have entirely different purposes.

They cannot be used interchangeably as both are part of the specification of two distinct kubernetes resources/objects: Service and Pod respectively. While the purpose of containerPort can be treated as purely informational, targetPort is required by the Service which exposes a set of Pods.

It's important to understand that by declaring containerPort with the specific value in your Pod/Deployment specification you cannot make your Pod to expose this specific port e.g. if you declare in containerPort field that your nginx Pod exposes port 8080 instead of default 80, you still need to configure your nginx server in your container to listen on this port.

Declaring containerPort in Pod specification is optional. Even without it your Service will know where to direct the request based on the info it has declared in its targetPort.

It's good to remember that it's not required to declare targetPort in the Service definition. If you omit it, it defaults to the value you declared for port (which is the port of the Service itself).

like image 175
mario Avatar answered Nov 17 '22 08:11

mario


ContainerPort in pod spec

List of ports to expose from the container. Exposing a port here gives the system additional information about the network connections a container uses, but is primarily informational. Not specifying a port here DOES NOT prevent that port from being exposed

targetPort in service spec

Number or name of the port to access on the pods targeted by the service. Number must be in the range 1 to 65535. Name must be an IANA_SVC_NAME. If this is a string, it will be looked up as a named port in the target Pod's container ports. If this is not specified, the value of the 'port' field is used (an identity map).

Hence targetPort in service needs to match the containerPort in pod spec because that's how service knows which container port is destination to forward the traffic to.

like image 45
Arghya Sadhu Avatar answered Nov 17 '22 08:11

Arghya Sadhu


containerPort is the port, which app inside the container can be reached on.

targetPort is the port, which is exposed in the cluster and the service connects the pod to other services or users.

like image 36
Prakash Ramasamy Avatar answered Nov 17 '22 06:11

Prakash Ramasamy