第七章 Rolling update

7.1 实践

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: httpd
spec:
  replicas: 3
  template:
    metadata:
      labels:
        run: httpd
    spec:
      containers:
      - name: httpd
        image: httpd:2.2.31
        ports:
        - containerPort: 80

使用 kubectl  apply 进行部署。

kubeusr@GalaxyKubernetesMaster:~$ kubectl get deployment httpd -o wide
NAME      DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE       CONTAINERS   IMAGES         SELECTOR
httpd     3         3         3            2           3m        httpd        httpd:2.2.31   run=httpd
kubeusr@GalaxyKubernetesMaster:~$ kubectl get replicaset -o wide
NAME                 DESIRED   CURRENT   READY     AGE       CONTAINERS   IMAGES         SELECTOR
busybox-577868d55b   1         1         1         1d        busybox      busybox        pod-template-hash=1334248116,run=busybox
httpd-9658687dd      3         3         2         4m        httpd        httpd:2.2.31   pod-template-hash=521424388,run=httpd

然后将配置文件中的2.2.31 替换为 2.2.32,再执行kubectl apply,

kubeusr@GalaxyKubernetesMaster:~$ kubectl get deployment -o wide
NAME      DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE       CONTAINERS   IMAGES         SELECTOR
busybox   1         1         1            1           1d        busybox      busybox        run=busybox
httpd     3         4         2            3           11m       httpd        httpd:2.2.32   run=httpd         # 更新成了新版本


kubeusr@GalaxyKubernetesMaster:~$ kubectl get replicaset -o wide
NAME                 DESIRED   CURRENT   READY     AGE       CONTAINERS   IMAGES         SELECTOR
busybox-577868d55b   1         1         1         1d        busybox      busybox        pod-template-hash=1334248116,run=busybox
httpd-76c8bd9f65     2         2         1         5m        httpd        httpd:2.2.32   pod-template-hash=3274685921,run=httpd
httpd-9658687dd      2         2         2         12m       httpd        httpd:2.2.31   pod-template-hash=521424388,run=httpd

 

kubeusr@GalaxyKubernetesMaster:~$ kubectl describe deployment httpd
Name:                   httpd
Namespace:              default
CreationTimestamp:      Sun, 30 Dec 2018 07:37:56 -0500
Labels:                 run=httpd
Annotations:            deployment.kubernetes.io/revision=2
                        kubectl.kubernetes.io/last-applied-configuration={"apiVersion":"apps/v1beta1","kind":"Deployment","metadata":{"annotations":{},"name":"httpd","namespace":"default"},"spec":{"replicas":2,"template":{"m...
Selector:               run=httpd
Replicas:               2 desired | 2 updated | 3 total | 2 available | 1 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  run=httpd
  Containers:
   httpd:
    Image:        httpd:2.2.32
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    ReplicaSetUpdated
OldReplicaSets:  httpd-9658687dd (1/1 replicas created)
NewReplicaSet:   httpd-76c8bd9f65 (2/2 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  12m   deployment-controller  Scaled up replica set httpd-9658687dd to 2
  Normal  ScalingReplicaSet  53s   deployment-controller  Scaled up replica set httpd-76c8bd9f65 to 1
  Normal  ScalingReplicaSet  42s   deployment-controller  Scaled down replica set httpd-9658687dd to 1
  Normal  ScalingReplicaSet  42s   deployment-controller  Scaled up replica set httpd-76c8bd9f65 to 2

7.2 回滚

httpd.v1.yml

 

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: httpd
spec:
  revisionHistoryLimit: 10
  replicas: 3
  template:
    metadata:
      labels:
        run: httpd
    spec:
      containers:
      - name: httpd
        image: httpd:2.4.16
        ports:
        - containerPort: 80

 

httpd.v2.yml

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: httpd
spec:
  revisionHistoryLimit: 10
  replicas: 3
  template:
    metadata:
      labels:
        run: httpd
    spec:
      containers:
      - name: httpd
        image: httpd:2.4.17
        ports:
        - containerPort: 80

 

httpd.v3.yml

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: httpd
spec:
  revisionHistoryLimit: 10
  replicas: 3
  template:
    metadata:
      labels:
        run: httpd
    spec:
      containers:
      - name: httpd
        image: httpd:2.4.18
        ports:
        - containerPort: 80

 

 

kubeusr@GalaxyKubernetesMaster:~$ kubectl apply -f httpd.v1.yml --record       # record的作用是将命令记录到revision记录中。
deployment.apps "httpd" configured
kubeusr@GalaxyKubernetesMaster:~$ kubectl get deployment httpd -o wide
NAME      DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE       CONTAINERS   IMAGES         SELECTOR
httpd     3         4         2            3           20m       httpd        httpd:2.4.16   run=httpd
kubeusr@GalaxyKubernetesMaster:~$ kubectl apply -f httpd.v2.yml --record
deployment.apps "httpd" configured
kubeusr@GalaxyKubernetesMaster:~$ kubectl get deployment httpd -o wide
NAME      DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE       CONTAINERS   IMAGES         SELECTOR
httpd     3         4         1            3           28m       httpd        httpd:2.4.17   run=httpd
kubeusr@GalaxyKubernetesMaster:~$ kubectl apply -f httpd.v3.yml --record
deployment.apps "httpd" configured
kubeusr@GalaxyKubernetesMaster:~$ kubectl get deployment httpd -o wide
NAME      DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE       CONTAINERS   IMAGES         SELECTOR
httpd     3         4         2            3           29m       httpd        httpd:2.4.18   run=httpd

# 查看历史部署版本 kubeusr@GalaxyKubernetesMaster:~$ kubectl rollout history deployment httpd deployments "httpd" REVISION CHANGE-CAUSE 1
<none> # 因为之前部署命令中没有record,所以就显示 <none> 2 <none> 3 kubectl apply --filename=httpd.v1.yml --record=true 4 kubectl apply --filename=httpd.v2.yml --record=true 5 kubectl apply --filename=httpd.v3.yml --record=true

如果想回滚到某个版本:

kubeusr@GalaxyKubernetesMaster:~$ kubectl rollout undo deployment httpd --to-revision=3            # 回退到revision=3的那个版本
deployment.apps "httpd"
kubeusr@GalaxyKubernetesMaster:~$ kubectl get deployment httpd -o wide           
NAME      DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE       CONTAINERS   IMAGES         SELECTOR
httpd     3         4         2            3           37m       httpd        httpd:2.4.16   run=httpd

 

posted @ 2018-12-30 20:53  刘大飞  阅读(929)  评论(0编辑  收藏  举报