1. 程式人生 > >更新k8s鏡像版本的三種方式

更新k8s鏡像版本的三種方式

文件 ubuntu ava all type art 三種方式 url image

原文:更新k8s鏡像版本的三種方式

一、知識準備

更新鏡像版本是在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

這兩個鏡像只有版本號不同,其他的都一樣

[email protected]:~# docker run -d --rm -p 10080:80 nginx:v1
e88097841c5feef92e4285a2448b943934ade5d86412946bc8d86e262f80a050
[email protected]:~# curl http://127.0.0.1:10080
----------
version: v1
hostname: f5189a5d3ad3

四、更新鏡像的三種方法

我們首先準備一個yaml文件用於測試:

[email protected]:~# 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

簡單驗證一下:

[email protected]:~# kubectl apply -f image_update.yaml
deployment.extensions "image-deployment" created
service "nginx-service" created
[email protected]:~# kubectl get svc
NAME            TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)     AGE
nginx-service   ClusterIP   10.254.240.225   <none>        10080/TCP   1m
[email protected]:~# 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
[email protected]:~# curl http://10.254.240.225:10080
----------
version: v1
hostname: image-deployment-58b646ffb6-d4sl7

已經正常工作了,並且當前版本是v1

下面介紹修改鏡像的方法

(1)修改配置文件

這應該是最常用的方法了

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

[email protected]:~# sed -i 's/nginx:v1/nginx:v2/g' image_update.yaml

應用配置文件:

[email protected]:~# kubectl apply -f image_update.yaml
deployment.extensions "image-deployment" configured
service "nginx-service" unchanged
[email protected]:~# 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之後

[email protected]:~# 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
[email protected]:~# curl http://10.254.240.225:10080
----------
version: v2
hostname: image-deployment-55cb946d47-7tzp8

成功更新為v2

(2)使用patch命令

首先找到deployment:

[email protected]:~# kubectl get deploy
NAME               DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
image-deployment   1         1         1            1           20m

通過patch更新:

[email protected]:~# 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

等待一段時間之後:

[email protected]:~# curl http://10.254.240.225:10080
----------
version: v1
hostname: image-deployment-58b646ffb6-hbzk9

通過patch更新之後,鏡像版本更新回v1

(3)使用set image命令

使用set image命令將鏡像版本更新到v2

[email protected]:~# kubectl set image deploy image-deployment *=registry.cn-beijing.aliyuncs.com/mrvolleyball/nginx:v2
[email protected]:~# curl http://10.254.240.225:10080
----------
version: v2
hostname: image-deployment-55cb946d47-zsdc6

等待一段時間之後,版本又更新到v2

五、小結

● 本文介紹了3種方法更新鏡像版本,分別是:配置文件;patch方式;set image方式



至此,本文結束
在下才疏學淺,有撒湯漏水的,請各位不吝賜教...

更新k8s鏡像版本的三種方式