一、deployment资源在滚动升级时,服务的访问不会中断,这是相比于rc资源的优点

二、定义deployment文件

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 3
  strategy:     #滚动升级策略
    rollingUpdate:
      maxSurge: 1  #多启动的pod数量
      maxUnavailable: 1  #最多不可用的pod数量
    type: RollingUpdate
  minReadySeconds: 30 #升级间隔
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: 10.0.0.11:5000/nginx:1.13
        ports:
        - containerPort: 80
        resources:   #资源限制
          limits:
            cpu: 100m
          requests:
            cpu: 100m

三、查看资源

[root@k8s-master zabbix]# kubectl get deployment 
NAME      DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
nginx     3         3         3            3           21m
[root@k8s-master zabbix]# kubectl get pod -o wide
NAME                     READY     STATUS    RESTARTS   AGE       IP            NODE
nginx-2807576163-3td60   1/1       Running   0          23m       172.18.78.8   10.0.0.13
nginx-2807576163-5rzth   1/1       Running   0          23m       172.18.57.5   10.0.0.12
nginx-2807576163-msl6c   1/1       Running   0          23m       172.18.78.7   10.0.0.13
[root@k8s-master zabbix]# curl -I 172.18.78.8
HTTP/1.1 200 OK
Server: nginx/1.13.12
Date: Tue, 02 Jun 2020 15:49:13 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 09 Apr 2018 16:01:09 GMT
Connection: keep-alive
ETag: "5acb8e45-264"
Accept-Ranges: bytes

四、deployment有2个标签

[root@k8s-master zabbix]# kubectl get pod --show-labels 
NAME                     READY     STATUS    RESTARTS   AGE       LABELS
nginx-2807576163-3td60   1/1       Running   0          26m       app=nginx,pod-template-hash=2807576163
nginx-2807576163-5rzth   1/1       Running   0          26m       app=nginx,pod-template-hash=2807576163
nginx-2807576163-msl6c   1/1       Running   0          26m       app=nginx,pod-template-hash=2807576163

五、修改配置文件、立即滚动升级

 spec:
      containers:
      - image: 10.0.0.11:5000/nginx:1.15 #1.13改成1.15

六、查看 deployment在滚动升级时2个版本的资源都可以访问,升级完成后只能访问1.15版本

[root@k8s-master zabbix]# curl -I 172.18.78.3  
HTTP/1.1 200 OK
Server: nginx/1.15.5
Date: Tue, 02 Jun 2020 15:57:30 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 02 Oct 2018 14:49:27 GMT
Connection: keep-alive
ETag: "5bb38577-264"
Accept-Ranges: bytes

七、再修改版本为1.13,查看

[root@k8s-master zabbix]# kubectl get pod --show-labels 
NAME                     READY     STATUS    RESTARTS   AGE       LABELS
nginx-2807576163-9d8n3   1/1       Running   0          8s        app=nginx,pod-template-hash=2807576163
nginx-2807576163-pfhsg   1/1       Running   0          8s        app=nginx,pod-template-hash=2807576163
nginx-3014407781-259wj   1/1       Running   0          12m       app=nginx,pod-template-hash=3014407781
nginx-3014407781-cqbm6   1/1       Running   0          12m       app=nginx,pod-template-hash=3014407781

八、它会新起2个容器,等到30s后没问题,3个会自动全部更新版本

[root@k8s-master zabbix]# kubectl get pod --show-labels 
NAME                     READY     STATUS    RESTARTS   AGE       LABELS
nginx-2807576163-9d8n3   1/1       Running   0          1m        app=nginx,pod-template-hash=2807576163
nginx-2807576163-pfhsg   1/1       Running   0          1m        app=nginx,pod-template-hash=2807576163
nginx-2807576163-z6z5p   1/1       Running   0          1m        app=nginx,pod-template-hash=2807576163

九、没有把资源删除掉,只是改了rs的副本数

[root@k8s-master zabbix]# kubectl get all
NAME           DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
deploy/nginx   3         3         3            3           45m

NAME             CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
svc/kubernetes   10.254.0.1   <none>        443/TCP   6h

NAME                  DESIRED   CURRENT   READY     AGE
rs/nginx-2807576163   3         3         3         45m  #副本数
rs/nginx-3014407781   0         0         0         15m  #副本数

NAME                        READY     STATUS    RESTARTS   AGE
po/nginx-2807576163-9d8n3   1/1       Running   0          3m
po/nginx-2807576163-pfhsg   1/1       Running   0          3m
po/nginx-2807576163-z6z5p   1/1       Running   0          3m

十、deployment相当于rc的优势:服务不会中断,不依赖配置文件,修改配置文件立即生效,创建,回滚,升级等操作命令行执行

十一、命令行创建deployment 记录下来升级版本

[root@k8s-master development]# kubectl run   nginx  --image=10.0.0.11:5000/nginx:1.13 --replicas=3 --record
deployment "nginx" created

十一、命令行更新,升级到1.15

[root@k8s-master development]# kubectl set image deployment nginx nginx=10.0.0.11:5000/nginx:1.15
deployment "nginx" image updated

十二、升级到1.17

[root@k8s-master development]# kubectl set image deployment nginx nginx=10.0.0.11:5000/nginx:1.17
deployment "nginx" image updated

十三、查看升级历史信息

[root@k8s-master development]# kubectl rollout history deployment nginx
deployments "nginx"
REVISION	CHANGE-CAUSE
1		kubectl run nginx --image=10.0.0.11:5000/nginx:1.13 --replicas=3 --record
2		kubectl set image deployment nginx nginx=10.0.0.11:5000/nginx:1.15
3		kubectl set image deployment nginx nginx=10.0.0.11:5000/nginx:1.17

十四、执行回滚操作,会回滚到revision2,并删除revison2

[root@k8s-master development]# kubectl rollout history deployment nginx
deployments "nginx"
REVISION	CHANGE-CAUSE
1		kubectl run nginx --image=10.0.0.11:5000/nginx:1.13 --replicas=3 --record
3		kubectl set image deployment nginx nginx=10.0.0.11:5000/nginx:1.17
4		kubectl set image deployment nginx nginx=10.0.0.11:5000/nginx:1.15

十五、再次回滚上个版本,会回滚到revision3,并删除revision3

[root@k8s-master development]# kubectl rollout undo deployment nginx
deployment "nginx" rolled back
[root@k8s-master development]# kubectl rollout history deployment nginx
deployments "nginx"
REVISION	CHANGE-CAUSE
1		kubectl run nginx --image=10.0.0.11:5000/nginx:1.13 --replicas=3 --record
4		kubectl set image deployment nginx nginx=10.0.0.11:5000/nginx:1.15
5		kubectl set image deployment nginx nginx=10.0.0.11:5000/nginx:1.17

十六、回滚到指定revision版本

[root@k8s-master development]# kubectl rollout undo deployment nginx --to-revision=1
deployment "nginx" rolled back
[root@k8s-master development]# kubectl rollout history deployment nginx
deployments "nginx"
REVISION	CHANGE-CAUSE
4		kubectl set image deployment nginx nginx=10.0.0.11:5000/nginx:1.15
5		kubectl set image deployment nginx nginx=10.0.0.11:5000/nginx:1.17
6		kubectl run nginx --image=10.0.0.11:5000/nginx:1.13 --replicas=3 --record

Categories: docker&k8s

0 Comments

发表评论

Avatar placeholder

邮箱地址不会被公开。 必填项已用*标注