Home > Blockchain >  what does --port option do in a k8s pod
what does --port option do in a k8s pod

Time:01-07

if I create a pod imperatively like this, what does the --port option do?

kubectl run mypod --image=nginx --port 9090

nginx application by default is going to listen on port 80. Why do we need this option? The documentation says

--port='': The port that this container exposes.

If it is exposed using kubectl expose pod mypod --port 9090, it is going to create service on port 9090 and target port 9090. But in the above case it neither creates a service

CodePudding user response:

...nginx application by default is going to listen on port 80. Why do we need this option?

The use of --port 80 means the same if you write in spec:

...
containers:
- name: ...
  image: nginx
  ports:
  - containerPort: 80
...

It doesn't do any port mapping but inform that this container will expose port 80.

...in the above case it neither creates a service

You can add --expose to kubectl run which will create a service, in this case is the same if you write in spec:

kind: Service
...
spec:
  ports:
  - port: 80
    targetPort: 80
...

Note you can only specify one port with --port, even if you write multiple --port, only the last one will take effect.

CodePudding user response:

Looks like this is primarily informational and does not have any effect on the container/pod.

https://kubernetes.io/docs/reference/kubernetes-api/workload-resources/pod-v1/#ports

CodePudding user response:

In Docker when you run a container like so:

docker run --name mycontainer -p 80 -d nginx

this will actually tell docker to run an nginx image and docker will look for an open port and map it for you like this:

df09fbe82b13     nginx     "/docker-entrypoint.…"     6 minutes ago     Up 6 minutes     0.0.0.0:50986->80/tcp

Now in the other hand running:

kubectl run mypod --image=nginx --port 9090

will create you a pod called mypod if you try to run:

kubectl describe po mypod

you will need to understand these two lines

    Port:           9090/TCP
    Host Port:      0/TCP

That means we are trying to map port 9090 from pod to 0 on our host machine which seems odd where port 0 seems to be invalid.

TL;DR the port 0 means that we doesn't know the port and kubernetes will dynamically assign a host port.

Make sure that if you need an app with a port to be running in your web browser you will have to work with services

  •  Tags:  
  • Related