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.
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.
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
Query the information of the pod.
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>
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
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
View the information of the pod after you update the image.
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>
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