1、Deployment与ReplicaSet之间的关系
2、Deployment的自动升级更新的结构图
3、Deployment需要注意的点
4、Deployment手动升级更新的方法—指定更新的方式为Recreate
[root@master manifests]# kubectl explain deploy.spec.strategy
rollingUpdate
类似于ReplicaSet
5、Deployment自动滚动升级更新的方法—指定更新的方式为RollingUpdate
Deployment能定义在自动升级滚动更新过程中,能多出期望副本数的个数,还能定义少出期望副本数的个数。
(1)如果定义的是多出期望副本数(假设期望副本数的个数为5)的1个,那么更新的过程中:加1个新的,删1个旧的,就这样,共5次;
(2)如果定义的是多出期望副本数(假设期望副本数的个数为5)的2个,那么更新的过程中:加2个新的,删2个旧的,加2个新的,删2个旧的,加1个新的,删1个旧的(2,2,1);
(3)如果定义的是少出期望副本数(假设期望副本数的个数为5)的1个,那么更新的过程中:删1个旧的,加1个新的,就这样,共5次;
(4)如果定义的是少出期望副本数(假设期望副本数的个数为5)的2个,那么更新的过程中:删2个旧的,加2个新的,删2个旧的,加2个新的,删1个旧的,加1个新的(2,2,1);
(5)如果定义的是既可以多出期望副本数(假设期望副本数的个数为5)的1个,也可以少出期望副本数的1个。也就是最多是6个,最少是4个。那么更新的过程中:加1个新的,删2个旧的,加2个新的,删2个旧的,加2个新的,删1个旧的;
6、Deployment的使用
[root@master manifests]# vim deploy-demo.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deploy
namespace: default
spec:
replicas: 2
selector:
matchLabels:
app: myapp
release: canary
template:
metadata:
labels: #这里不用指定Pod的名字,即使指定了,也不会生效。
app: myapp
release: canary
spec:
containers:
- name: myapp
image: nginx:latest
ports:
- name: http
containerPort: 80
[root@master manifests]# kubectl apply -f deploy-demo.yaml #使用apply,而不使用create。apply既可以创建,也可以声明式的更新
[root@master manifests]# kubectl get deploy -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
myapp-deploy 2/2 2 2 55s myapp nginx:latest app=myapp,release=canary
[root@master manifests]# kubectl get rs -o wide #下面的rs是deploy自动创建的,其中myapp-deploy是deply的名字,后面的字串其实是Pod模板的hash值,而不是随机的。
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
myapp-deploy-7f9f5b6964 2 2 2 64s myapp nginx:latest app=myapp,pod-template-hash=7f9f5b6964,release=canary
[root@master manifests]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myapp-deploy-7f9f5b6964-bkscp 1/1 Running 0 71s 10.244.1.17 node01
myapp-deploy-7f9f5b6964-tj9df 1/1 Running 0 71s 10.244.2.24 node02
(1)Deployment的声明式更新
[root@master manifests]# vim deploy-demo.yaml #将文件中的replicas的值由2改为3
spec:
replicas: 3
[root@master manifests]# kubectl apply -f deploy-demo.yaml #apply可以使用多次,而create只能使用1次。
[root@master manifests]# kubectl get deploy -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
myapp-deploy 3/3 3 3 7m56s myapp nginx:latest app=myapp,release=canary
[root@master manifests]# kubectl get rs -o wide #7f9f5b6964并没有发生变化,这是因为Pod模板没有发生变化
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
myapp-deploy-7f9f5b6964 3 3 3 8m5s myapp nginx:latest app=myapp,pod-template-hash=7f9f5b6964,release=canary
[root@master manifests]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myapp-deploy-7f9f5b6964-bkscp 1/1 Running 0 8m11s 10.244.1.17 node01
myapp-deploy-7f9f5b6964-pk8nh 1/1 Running 0 24s 10.244.1.18 node01
myapp-deploy-7f9f5b6964-tj9df 1/1 Running 0 8m11s 10.244.2.24 node02
(2)Deployment的自动滚动更新升级
(3)Deployment的自动扩缩容
[root@master manifests]# kubectl patch deployment myapp-deploy -p '{"spec":{"replicas":5}}' #注意:最外面用单引号,里面用双引号
[root@master manifests]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myapp-deploy-84df5cd67c-5db7w 1/1 Running 0 43m 10.244.1.19 node01
myapp-deploy-84df5cd67c-q2mc4 1/1 Running 0 44m 10.244.2.26 node02
myapp-deploy-84df5cd67c-vfrnr 1/1 Running 0 51s 10.244.1.20 node01
myapp-deploy-84df5cd67c-vkjlh 1/1 Running 0 51s 10.244.2.27 node02
myapp-deploy-84df5cd67c-w5ppg 1/1 Running 0 44m 10.244.2.25 node02
(4)控制Deployment的更新节奏—在Deployment自动滚动更新升级的过程中,设置暂停键,模拟canary(金丝雀版本的生成)
[root@master manifests]# kubectl patch deployment myapp-deploy -p '{"spec":{"strategy":{"rollingUpdate":{"maxSurge":1,"maxUnavailable":0}}}}' #修改myapp-deploy的更新策略
[root@master manifests]# kubectl describe deployment myapp-deploy #可以看到更新策略发生了变化
RollingUpdateStrategy: 0 max unavailable, 1 max surge
[root@master manifests]# kubectl set image deployment myapp-deploy myapp=nginx:1.15 && kubectl rollout pause deployment myapp-deploy #设置myapp-deploy中的myapp容器的镜像为nginx:1.15,并且一设置完,就暂停(即只更新1个)
在另外一个窗口输入下面的命令来监控Pod的变化过程
[root@master ~]# kubectl get pods -w -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myapp-deploy-585bbbd678-t2clk 0/1 ContainerCreating 0 2m27s node01
myapp-deploy-84df5cd67c-5db7w 1/1 Running 0 67m 10.244.1.19 node01
myapp-deploy-84df5cd67c-q2mc4 1/1 Running 0 67m 10.244.2.26 node02
myapp-deploy-84df5cd67c-vfrnr 1/1 Running 0 24m 10.244.1.20 node01
myapp-deploy-84df5cd67c-vkjlh 1/1 Running 0 24m 10.244.2.27 node02
myapp-deploy-84df5cd67c-w5ppg 1/1 Running 0 68m 10.244.2.25 node02
myapp-deploy-585bbbd678-t2clk 1/1 Running 0 6m25s node01
[root@master manifests]# kubectl rollout status deployment myapp-deploy #输入命令查看滚动更新的过程,因为没有更新完(更新1个之后,就暂停),所以输入这个命令之后,一直在等待
Waiting for deployment "myapp-deploy" rollout to finish: 1 out of 5 new replicas have been updated...
上面的canary版本已经构造完成,现在想要继续更新下面所有的Pod中的容器的镜像版本
[root@master ~]# kubectl rollout resume deployment myapp-deploy
[root@master manifests]# kubectl rollout status deployment myapp-deploy #查看自动滚动更新升级的状态
Waiting for deployment "myapp-deploy" rollout to finish: 1 out of 5 new replicas have been updated...
Waiting for deployment spec update to be observed...
Waiting for deployment spec update to be observed...
Waiting for deployment "myapp-deploy" rollout to finish: 1 out of 5 new replicas have been updated...
Waiting for deployment "myapp-deploy" rollout to finish: 1 out of 5 new replicas have been updated...
Waiting for deployment "myapp-deploy" rollout to finish: 2 out of 5 new replicas have been updated...
Waiting for deployment "myapp-deploy" rollout to finish: 2 out of 5 new replicas have been updated...
Waiting for deployment "myapp-deploy" rollout to finish: 3 out of 5 new replicas have been updated...
Waiting for deployment "myapp-deploy" rollout to finish: 3 out of 5 new replicas have been updated...
Waiting for deployment "myapp-deploy" rollout to finish: 3 out of 5 new replicas have been updated...
Waiting for deployment "myapp-deploy" rollout to finish: 4 out of 5 new replicas have been updated...
Waiting for deployment "myapp-deploy" rollout to finish: 4 out of 5 new replicas have been updated...
Waiting for deployment "myapp-deploy" rollout to finish: 2 old replicas are pending termination...
Waiting for deployment "myapp-deploy" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "myapp-deploy" rollout to finish: 1 old replicas are pending termination...
deployment "myapp-deploy" successfully rolled out
[root@master manifests]# kubectl get rs -o wide #自动滚动更新了2次,所以现在rs的个数为3
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
myapp-deploy-585bbbd678 5 5 5 27m myapp nginx:1.15 app=myapp,pod-template-hash=585bbbd678,release=canary
myapp-deploy-7f9f5b6964 0 0 0 3h43m myapp nginx:latest app=myapp,pod-template-hash=7f9f5b6964,release=canary
myapp-deploy-84df5cd67c 0 0 0 94m myapp nginx:1.14 app=myapp,pod-template-hash=84df5cd67c,release=canary
(5)Deployment的自动回滚
[root@master manifests]# kubectl rollout history deployment myapp-deploy
deployment.apps/myapp-deploy
REVISION CHANGE-CAUSE
1
2
3
[root@master manifests]# kubectl rollout undo deployment myapp-deploy --to-revision=1 #指定自动回滚到第一个版本
[root@master manifests]# kubectl get rs -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
myapp-deploy-585bbbd678 0 0 0 37m myapp nginx:1.15 app=myapp,pod-template-hash=585bbbd678,release=canary
myapp-deploy-7f9f5b6964 5 5 5 3h52m myapp nginx:latest app=myapp,pod-template-hash=7f9f5b6964,release=canary
myapp-deploy-84df5cd67c 0 0 0 103m myapp nginx:1.14 app=myapp,pod-template-hash=84df5cd67c,release=canary
启动一个Deployment,用来提供redis服务。
[root@master manifests]# vim ds-demo.yaml #一个配置文件中可以有多个资源定义,不同的资源定义之间用"---"分割
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: redis
role: logstor
template:
metadata:
labels:
app: redis
role: logstor
spec:
containers:
- name: redis
image: redis:4.0-alpine
ports:
- name: redis
containerPort: 6379
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: filebeat-ds
namespace: default
spec:
selector:
matchLabels:
app: filebeat
release: stable
template:
metadata:
labels:
app: filebeat
release: stable
spec:
containers:
- name: filebeat
image: ikubernetes/filebeat:5.6.5-alpine
env:
- name: REDIS_HOST
value: redis.default.svc.cluster.local
- name: REDIS_LOG_LEVEL
value: info
[root@master manifests]# kubectl get deploy -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
redis 1/1 1 1 5m28s redis redis:4.0-alpine app=redis,role=logstor
[root@master manifests]# kubectl get rs -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
redis-588694bf8c 1 1 1 5m35s redis redis:4.0-alpine app=redis,pod-template-hash=588694bf8c,role=logstor
[root@master manifests]# kubectl get ds -o wide
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE CONTAINERS IMAGES SELECTOR
filebeat-ds 2 2 2 2 2 5m46s filebeat ikubernetes/filebeat:5.6.5-alpine app=filebeat,release=stable
[root@master manifests]# kubectl get pods -o wide #可以看到运行了filebeat-ds这个DaemonSet有2个Pod,这是因为集群中有2个节点(node节点),master节点上不运行此类Pod。这是因为master节点上有污点,此类Pod不能容忍污点。除非设置此类Pod的容忍度。
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
filebeat-ds-jc94g 1/1 Running 0 5m58s 10.244.2.36 node02
filebeat-ds-s689z 1/1 Running 0 5m57s 10.244.1.33 node01
redis-588694bf8c-2d6t7 1/1 Running 0 5m58s 10.244.2.35 node02
为redis这个Deployment提供一个Service服务
[root@master manifests]# kubectl expose deployment redis --port=6379
service/redis exposed
[root@master manifests]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 443/TCP 10d
redis ClusterIP 10.111.36.11 6379/TCP 3s
1、DaemonSet也支持滚动更新
[root@master manifests]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
filebeat-ds-qdjgs 1/1 Running 0 41s 10.244.1.66 node01
filebeat-ds-swbsv 1/1 Running 0 33s 10.244.2.71 node02
redis-588694bf8c-4szsb 1/1 Running 0 58m 10.244.1.60 node01
[root@master manifests]# kubectl set image daemonset filebeat-ds filebeat=ikubernetes/filebeat:5.6.6-alpine #更新filebeat容器的镜像版本
daemonset.apps/filebeat-ds image updated
[root@master manifests]# kubectl get pods -w
NAME READY STATUS RESTARTS AGE
filebeat-ds-qdjgs 1/1 Running 0 47s
filebeat-ds-swbsv 1/1 Running 0 39s
redis-588694bf8c-4szsb 1/1 Running 0 58m
filebeat-ds-qdjgs 1/1 Terminating 0 59s
filebeat-ds-qdjgs 0/1 Terminating 0 61s
filebeat-ds-qdjgs 0/1 Terminating 0 62s
filebeat-ds-qdjgs 0/1 Terminating 0 62s
filebeat-ds-84vxb 0/1 Pending 0 0s
filebeat-ds-84vxb 0/1 Pending 0 0s
filebeat-ds-84vxb 0/1 ContainerCreating 0 0s
filebeat-ds-84vxb 1/1 Running 0 2s
filebeat-ds-swbsv 1/1 Terminating 0 56s
filebeat-ds-swbsv 0/1 Terminating 0 58s
filebeat-ds-swbsv 0/1 Terminating 0 59s
filebeat-ds-swbsv 0/1 Terminating 0 59s
filebeat-ds-n5tp5 0/1 Pending 0 0s
filebeat-ds-n5tp5 0/1 Pending 0 0s
filebeat-ds-n5tp5 0/1 ContainerCreating 0 0s
filebeat-ds-n5tp5 1/1 Running 0 4s