All Products
Search
Document Center

Elastic Container Instance:Update the image of a pod without changing the IP address of the pod

Last Updated:Mar 19, 2024

When you update a pod to troubleshoot issues or to improve features of the 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 kubectl commands to update the container image of a pod and keep the IP address of the pod unchanged.

Prerequisites

The image to be updated is prepared and uploaded to an image repository.

Procedure

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

Note

To perform the operations in the following example, make sure that an Internet NAT gateway is created and SNAT is configured for the VPC to which the cluster belongs. This way, the image can be pulled over the Internet.

  1. Create an Elastic Container Instance-based 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
      labels:
        alibabacloud.com/eci: "true" 
    spec:
      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 sample 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 sample output shows that the container image of the pod is nginx:1.7.9.

      IMAGE
      nginx:1.7.9
  3. Use one of the following methods to update the container image:

    • kubectl patch

      Run the kubectl command to modify the image tag.

      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 image tag.

      kubectl edit pod/nginx -o yaml 
    • kubectl apply

      Open the nginx.yaml configuration file of the pod, modify the image tag, 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 update the image.

    1. Query the IP address of the pod.

      kubectl get pod/nginx -n default -o wide

      The following sample 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 you update the image.

      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 sample output shows that the container image of the pod is updated to nginx:1.9.6.

      IMAGE
      nginx:1.9.6