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

1、知識準備

更新鏡像版本是在k8s平常使用中很是常見的一種操做,本文主要介紹更新介紹的三種方法node


2、環境準備

組件 版本
OS Ubuntu 18.04.1 LTS
docker 18.06.0-ce


3、準備鏡像

首先準備2個不一樣版本的鏡像,用於測試(已經在阿里雲上建立好2個不一樣版本的nginx鏡像)nginx

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

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

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

4、更新鏡像的三種方法

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

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

簡單驗證一下:app

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

已經正常工做了,而且當前版本是v1curl

下面介紹修改鏡像的方法ide

(1)修改配置文件

這應該是最經常使用的方法了測試

修改配置文件,將nginx:v1改爲nginx:v2阿里雲

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

應用配置文件:url

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

5、小結

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



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

相關文章
相關標籤/搜索