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
2
Answers
When port option is already given to 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
...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: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
tokubectl run
which will create a service, in this case is the same if you write in spec:Note you can only specify one port with
--port
, even if you write multiple--port
, only the last one will take effect.