how to change the port of a kubernetes container/pod?

8/27/2019

I am displaying the output of the "docker ps -a command" to list all the containers to my Html page. I want to change the port of these containers using a button in the page itself. In docker normally if the container is running, I would run a docker stop on the container-id and restart it by adding the -p HOSTPORT:CONTAINERPORT to the command. But since all the containers running are Kubernetes containers/pods, stopping them will re-create a new pod/container with a different name. So how do I change the port of the container/pod in such cases?

output of "docker ps -a command"

 NAMES                              CONTAINER ID  STATUS     
 k8s_nginx_nginx-6cdb6c86d4-z7m7m   56711e6de1be  Up 2 seconds
 k8s_POD_nginx-6cdb6c86d4-z7m7m_d   70b21761cb74  Up 3 seconds
 k8s_coredns_coredns-5c98db65d4-7   dfb21bb7c7f4  Up 7 days
 k8s_POD_coredns-5c98db65d4-7djs8   a336be8230ce  Up 7 days
 k8s_POD_kube-proxy-9722h_kube-sy   5e290420dec4  Up 7 days
 k8s_POD_kube-apiserver-wootz_kub   a23dea72b38b  Exited (255) 7 days ago

nginx.yaml

apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  type: NodePort
  ports:
  - name: nginxport
    port: 80
    targetPort: 80
    nodePort: 30000
  selector:
    app: nginx
    tier: frontend

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  selector:
    matchLabels:
      app: nginx
      tier: frontend
  template:
    metadata:
      labels:
        app: nginx
        tier: frontend
    spec:

      containers:
      - image: suji165475/devops-sample:mxgraph
        name: nginx
        ports:
        - containerPort: 80
          name: nginxport

So how can I change the port of any of the containers/pod ?

-- Vignesh Swaminathan
docker
kubernetes

2 Answers

8/28/2019

Answering OP's question, as per his comments.

I want to change the port on which my kubernetes containers run. I want to change the nodeport,container port,targetport for it. so how can do this using kubectl patch command for both the service and deployment?

kubectl patch deployment nginx  --type json   -p='[{"op": "replace", "path": "/spec/template/spec/containers/0/ports/0/containerPort", "new port"}]' && \
kubectl patch service nginx --type json   -p='[{"op": "replace", "path": "/spec/type/spec/ports/0/targetPort", "new port"}]' && \
kubectl patch service nginx --type json   -p='[{"op": "replace", "path": "/spec/type/spec/ports/0/nodePort", "new port"}]'

Here is how to change pod specs,

kubectl patch pod valid-pod --type='json' -p='[{"op": "replace", "path": "/spec/containers/0/ports/0/port", "value":"new port"}]'

As David said, Pods aren't really used directly without a deployment.

What you would normally do, have a deployment with deploys the pods and that configuration can be then edited using kubectl.

Try using something like this,

kubectl patch deployment valid-deployment  --type json   -p='[{"op": "replace", "path": "/spec/template/spec/containers/0/ports/0/port", "new port"}]'

If you patch the deployment, the pods automatically restart.

That being said, if you change the port of the container, the service targetport would have to be changed too. The simple fix for that would to make sure all your container ports have name attribute filled which are mapped to their appropriate k8s services.

-- Sufiyan Parkar
Source: StackOverflow

8/27/2019

Most of the attributes of a PodSpec cannot be changed once the pod has been created. The port information is inside the containers array, and the linked documentation explicitly notes that containers "Cannot be updated." You must delete and recreate the pod if you want to change the ports it makes visible (or most of its other properties); there is no other way to do it.

You almost never directly deal with Pods (and for that matter you almost never mix plain Docker containers and Kubernetes on the same host). Typically you create a Deployment object, which can be updated in place, and it takes responsibility for creating and deleting Pods for you.

(The corollary to this is that if you're trying to manually delete and recreate Pods, in isolation, changing their properties, but these Pods are also managed by Deployments or StatefulSets or DaemonSets, the controller will notice that a replica is missing when you delete it and recreate it, with its original settings.)

-- David Maze
Source: StackOverflow