All Products
Search
Document Center

Change the image of a pod without changing the IP address

Last Updated: Sep 07, 2021

When you update a pod to troubleshoot issues or to improve functions of a pod, you may need to modify the image of the pod. During this process, the IP address of the pod cannot be changed to ensure business continuity. This topic describes how to use the kubectl command to modify the container image of a pod and keep the IP address of the pod unchanged.

Prerequisites

kubectl is installed and kubectl can interact with your kubernetes cluster.

Procedure

The following example describes how to use the kubectl command to change the container image from nginx:1.7.9 to nginx:1.9.6 and keep the IP address of the pod that runs NGINX unchanged.

  1. Create a pod.

    kubectl create -f nginx.yaml

    The following code provides an example of the content of nginx.yaml. The container image used is nginx:1.7.9.

    apiVersion: v1
    kind: Pod
    metadata:
      name: nginx
      namespace: default
    spec:
      nodeName: virtual-kubelet
      containers:
      - image: nginx:1.7.9
        imagePullPolicy: Always
        name: nginx
  2. Query the information of the pod.

    1. Query the IP address of the pod.

      kubectl get pod/nginx -n default -o wide

      The following example output shows that the IP address of the pod is 172.16.22.193.

      NAME    READY   STATUS    RESTARTS   AGE    IP              NODE                           NOMINATED NODE   READINESS GATES
      nginx   1/1     Running   0          5m5s   172.16.22.193   virtual-kubelet-cn-beijing-e   <none>           <none>
    2. Query the image tag of the pod.

      kubectl get pod/nginx -n default -o=custom-columns='IMAGE:spec.containers[*].image'

      The following example output shows that the container image of the pod is nginx:1.7.9.

      IMAGE
      nginx:1.7.9
  3. Use the following methods to change the container image:

    • kubectl patch

      Run the kubectl command to modify the tag of the container image.

      kubectl patch pod nginx -p '{"spec":{"containers":[{"name": "nginx","image": "nginx:1.9.6"}]}}'
    • kubectl edit

      Run the kubectl edit command to edit the pod and modify the tag of the container image.

      kubectl edit pod/nginx -o yaml 
    • kubectl apply

      Open the nginx.yaml configuration file of the pod, modify the tag of the container image, and then run the kubectl apply command to redeploy the pod.

      kubectl apply -f nginx.yaml 
  4. View the information of the pod after you change the image.

    1. Query the IP address of the pod.

      kubectl get pod/nginx -n default -o wide

      The following example output shows that the IP address of the pod is 172.16.22.193, which is the same as the IP address of the pod before the image was changed.

      NAME    READY   STATUS    RESTARTS   AGE   IP              NODE                           NOMINATED NODE   READINESS GATES
      nginx   1/1     Running   1          19m   172.16.22.193   virtual-kubelet-cn-beijing-e   <none>           <none>
    2. Query the image tag of the pod.

      kubectl get pod/nginx -n default -o=custom-columns='IMAGE:spec.containers[*].image'

      The following example output shows that the container image of the pod is changed to nginx:1.9.6.

      IMAGE
      nginx:1.9.6