skip to Main Content

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


  1. Chosen as BEST ANSWER

    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


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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search