更新k8s镜像版本的三种方式
2019-03-25 本文已影响10人
wilsonchai
一、知识准备
更新镜像版本是在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 http://127.0.0.1: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 http://10.254.240.225: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 http://10.254.240.225: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 http://10.254.240.225: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 http://10.254.240.225:10080
----------
version: v2
hostname: image-deployment-55cb946d47-zsdc6
等待一段时间之后,版本又更新到v2
五、小结
● 本文介绍了3种方法更新镜像版本,分别是:配置文件;patch方式;set image方式
至此,本文结束
在下才疏学浅,有撒汤漏水的,请各位不吝赐教...