Logu
Logu

Reputation: 1024

what does --port option do in a k8s pod

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

Upvotes: 1

Views: 466

Answers (2)

Logu
Logu

Reputation: 1024

When port option is already given to pod,

  1. expose can be run without --port option and it will use what is defined for the pod
  2. expose can be run with --port option and it will override the option given in the pod

When --port option is neither defined in the pod nor in expose it will be an error.

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

Upvotes: 1

gohm'c
gohm'c

Reputation: 15500

...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.

Upvotes: 3

Related Questions