Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we need a port/containerPort in a Kuberntes deployment/container definition?

When I define e.g. a deployment in kubernetes there is a section with a list of containers and each of them contains an array of ports, e.g.:

apiVersion: apps/v1
kind: Deployment
spec:
  template:
    spec:
      containers:
      - name: my-nginx
        image: nginx
        ports:
        - containerPort: 80

Now the documentation here explicitly says it does not affect connectivity:

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. Any port which is listening on the default "0.0.0.0" address inside a container will be accessible from the network. Cannot be updated.

Now it seems it does not really affect anything and only informational, but what does that really mean, where is that used?

I have found one use of that is that if port defines a name, it can be referenced from a service by that name.

Is that it or there are some other uses for this specification?

like image 628
Ilya Chernomordik Avatar asked Jul 25 '19 08:07

Ilya Chernomordik


People also ask

What is the use of containerPort 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.

Is containerPort mandatory?

containerPort. The containerPort option is optional and tells the service to forward the traffic from the service port (defined in port ) to a different containerPort of a pod.

Why port forwarding is required in Kubernetes?

Port forwarding is simply one way to access your Pods. As mentioned above, port forwarding is a useful method for testing accessibility, debugging, and other investigative tasks on your Pods in Kubernetes. It is not meant to be used for exposing applications to external traffic from end users.

What is containerPort in Yaml?

ContainerPort is used to decide which of the container's ports should be used by the service in case a container has multiple ports.


1 Answers

As you quote the documentation,

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. Any port which is listening on the default "0.0.0.0" address inside a container will be accessible from the network. Cannot be updated.

the purpose of defining the containerPorts is purely for documentation. It is only used by other developers to understand the port that the container listens to. Kubernetes borrows this idea from docker which does the same with EXPOSE command as mentioned here.

like image 105
Malathi Avatar answered Sep 18 '22 14:09

Malathi