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.
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.
“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.
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.
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.
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.
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...
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.
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.
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).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With