更新k8s镜像版本的三种方式


二、环境准备 组件 版本
OS   Ubuntu 18.04.1 LTS  
docker   18.06.0-ce  


三、准备镜像

首先准备2个不同版本的镜像,用于测试(已经在阿里云上创建好2个不同版本的nginx镜像)

docker pull registry.cn-beijing.aliyuncs.com/mrvolleyball/nginx:v1 docker pull registry.cn-beijing.aliyuncs.com/mrvolleyball/nginx:v2

这两个镜像只有版本号不同,其他的都一样

root@k8s-master:~# docker run -d --rm -p 10080:80 nginx:v1 e88097841c5feef92e4285a2448b943934ade5d86412946bc8d86e262f80a050 root@k8s-master:~# curl :10080 ---------- version: v1 hostname: f5189a5d3ad3 四、更新镜像的三种方法

我们首先准备一个yaml文件用于测试:

root@k8s-master:~# more image_update.yaml apiVersion: extensions/v1beta1 kind: Deployment metadata: name: image-deployment spec: replicas: 1 template: metadata: labels: app: image-update spec: containers: - name: nginx image: registry.cn-beijing.aliyuncs.com/mrvolleyball/nginx:v1 imagePullPolicy: Always --- apiVersion: v1 kind: Service metadata: name: nginx-service spec: selector: app: image-update ports: - protocol: TCP port: 10080 targetPort: 80

简单验证一下:

root@k8s-master:~# kubectl apply -f image_update.yaml deployment.extensions "image-deployment" created service "nginx-service" created root@k8s-master:~# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE nginx-service ClusterIP 10.254.240.225 <none> 10080/TCP 1m root@k8s-master:~# kubectl get pod -owide NAME READY STATUS RESTARTS AGE IP NODE image-deployment-58b646ffb6-d4sl7 1/1 Running 0 1m 10.10.169.131 k8s-node2 root@k8s-master:~# curl :10080 ---------- version: v1 hostname: image-deployment-58b646ffb6-d4sl7

已经正常工作了,并且当前版本是v1

下面介绍修改镜像的方法

(1)修改配置文件

这应该是最常用的方法了

修改配置文件,将nginx:v1改成nginx:v2:

root@k8s-master:~# sed -i 's/nginx:v1/nginx:v2/g' image_update.yaml

应用配置文件:

root@k8s-master:~# kubectl apply -f image_update.yaml deployment.extensions "image-deployment" configured service "nginx-service" unchanged root@k8s-master:~# kubectl get pod -owide NAME READY STATUS RESTARTS AGE IP NODE image-deployment-55cb946d47-7tzp8 0/1 ContainerCreating 0 16s <none> k8s-node1 image-deployment-58b646ffb6-d4sl7 1/1 Terminating 0 11m 10.10.169.131 k8s-node2

等待一段时间之后,v2版本ready之后

root@k8s-master:~# kubectl get pod -owide NAME READY STATUS RESTARTS AGE IP NODE image-deployment-55cb946d47-7tzp8 1/1 Running 0 1m 10.10.36.119 k8s-node1 root@k8s-master:~# curl :10080 ---------- version: v2 hostname: image-deployment-55cb946d47-7tzp8

成功更新为v2

(2)使用patch命令

首先找到deployment:

root@k8s-master:~# kubectl get deploy NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE image-deployment 1 1 1 1 20m

通过patch更新:

root@k8s-master:~# kubectl patch deployment image-deployment --patch '{"spec": {"template": {"spec": {"containers": [{"name": "nginx","image":"registry.cn-beijing.aliyuncs.com/mrvolleyball/nginx:v1"}]}}}}' deployment.extensions "image-deployment" patched

等待一段时间之后:

root@k8s-master:~# curl :10080 ---------- version: v1 hostname: image-deployment-58b646ffb6-hbzk9

通过patch更新之后,镜像版本更新回v1

(3)使用set image命令

使用set image命令将镜像版本更新到v2

root@k8s-master:~# kubectl set image deploy image-deployment *=registry.cn-beijing.aliyuncs.com/mrvolleyball/nginx:v2 root@k8s-master:~# curl :10080 ---------- version: v2 hostname: image-deployment-55cb946d47-zsdc6

等待一段时间之后,版本又更新到v2

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpfxdw.html