<p>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?</p>

<p>output of "docker ps -a command"</p>

<pre><code> 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
</code></pre>

<p>nginx.yaml</p>

<pre><code>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
</code></pre>

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

Vignesh Swaminathan
<p>Most of the attributes of a <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.15/#podspec-v1-core" rel="nofollow noreferrer">PodSpec</a> cannot be changed once the pod has been created.  The port information is inside the <code>containers</code> array, and the linked documentation explicitly notes that <code>containers</code> "Cannot be updated."  You <em>must</em> 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.</p>

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

<p>(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.)</p>
