Deployment(简写为deploy)是kubernetes控制器的又一种实现.只需要在 Deployment 中描述您想要的目标状态是什么,Deployment controller 就会帮您将 Pod 和ReplicaSet 的实际状态改变到您的目标状态.
比如期望运行二个nginx pod,当一个pod因为意外下线时, Deployment 会根据你描述的状态再起一个pod,这就是所谓的控制循环。Deployment会根据申明的RS控制器配置,控制对应的pod的数量和状态。
下面是Deploymen的更新机制
1.首先,创建-一个新的RS控制器,版本为v2 ;
2接着将旧控制器的pod陆续下线,同时新的RS控制器同步上线对应Pod;
3. Pod更新完成后,弃用旧的RS控制器,滚动发布就此完成。
部署nginx应用,以下面模板进行部署,滚动策略,最多新增一个 (maxSurge),最少下线一个(maxUnavailable)
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 3
minReadySeconds: 10
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
type: RollingUpdate
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.10-alpine
ports:
- containerPort: 80
name: http
readinessProbe:
periodSeconds: 1
httpGet:
path: /
port: http
kubectl apply -f 进行部署,kubectl get rs -o wide进行查看
[root@master ~]# kubectl get rs -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
deploy-nginx-5745bb45d7 3 3 3 30s nginx nginx:1.10-alpine app=nginx,pod-template-hash=5745bb45d7
升级到nginx1.13,再进行查看
[root@master ~]# kubectl get rs -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
deploy-nginx-567c45c748 2 2 0 3s nginx nginx:1.13-alpine app=nginx,pod-template-hash=567c45c748
deploy-nginx-5745bb45d7 2 2 2 3m5s nginx nginx:1.10-alpine app=nginx,pod-template-hash=5745bb45d7
[root@master ~]# kubectl get rs -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
deploy-nginx-567c45c748 3 3 2 30s nginx nginx:1.13-alpine app=nginx,pod-template-hash=567c45c748
deploy-nginx-5745bb45d7 0 0 0 3m32s nginx nginx:1.10-alpine app=nginx,pod-template-hash=5745bb45d7
用下面命令查看历史版本
[root@master ~]# kubectl rollout history deployment/deploy-nginx
deployment.apps/deploy-nginx
REVISION CHANGE-CAUSE
1
2
版本回滚 kubectl rollout undo deployment/deploy-nginx --to-revision=0
回滚结果
[root@master ~]# kubectl get rs -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
deploy-nginx-567c45c748 0 0 0 15m nginx nginx:1.13-alpine app=nginx,pod-template-hash=567c45c748
deploy-nginx-5745bb45d7 3 3 3 18m nginx nginx:1.10-alpine app=nginx,pod-template-hash=5745bb45d7
金丝雀发布
金丝雀发布一般先发 1 台,或者一个小比例,例如 2% 的服务器,主要做流量验证用,也称为金丝雀 (Canary) 测试(国内常称灰度测试)如果金丝测试通过,则把剩余的 V1 版本全部升级为 V2 版本。如果金丝雀测试失败,则直接回退金丝雀,发布失败
kubectl set image deployment deploy-nginx nginx=nginx:1.14-alpine && kubectl rollout pause deployment deploy-nginx
[root@master ~]# kubectl get rs -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
deploy-nginx-567c45c748 0 0 0 34m nginx nginx:1.13-alpine app=nginx,pod-template-hash=567c45c748
deploy-nginx-5745bb45d7 2 2 2 37m nginx nginx:1.10-alpine app=nginx,pod-template-hash=5745bb45d7
deploy-nginx-754874567 2 2 2 94s nginx nginx:1.14-alpine app=nginx,pod-template-hash=754874567
如果有问题,可以通过版本回滚 kubectl rollout undo deployment/deploy-nginx --to-revision=0进行回滚
或者测试几天正常,可以继续更新
[root@master ~]# kubectl get rs -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
deploy-nginx-567c45c748 0 0 0 37m nginx nginx:1.13-alpine app=nginx,pod-template-hash=567c45c748
deploy-nginx-5745bb45d7 0 0 0 40m nginx nginx:1.10-alpine app=nginx,pod-template-hash=5745bb45d7
deploy-nginx-754874567 3 3 3 5m23s nginx nginx:1.14-alpine app=nginx,pod-template-hash=754874567