目录
[root@k8s-master-1 ~]# kubectl get deploy -owide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
web 1/1 1 1 44h nginx nginx app=web
注释:
NAME:Deployment名称
READY:Pod状态,已经Ready的个数
UP-TO-DATE:已经达到期望状态的被更新的副本数
AVAILABLE:已经可以用的副本数
AGE:应用程序运行的时间
CONTAINERS:容器名称
IMAGES:容器的镜像
SELECTOR:管理的pod的标签
????????kubernetes最小的调度单元是Pod,kubernetes主要的功能就是管理多个Pod,Pod中也可以包含一个或者多个容器,他是通过Deployment和ReplicaSet(rs)来管理Pod的。
????????Deployment并不是直接管理Pod的,而是通过管理ReplicaSet来间接管理Pod。即:deployment管理replicaset,replicaset管理pod。所以deployment比replicaset的功能更强大。
????????查看deployment控制的rs和pod,发现rs是在deployment之后加了一段字符串,而pod是在rs之后加了一段字符串
apiVersion: apps/v1 #版本号
kind: Deployment #类型
metadata: #元数据
name: #rs名称
namespace: #所属命名空间
labels: #标签
controller: deploy
spec: #详情描述
replicas: #副本数量
revisionHistoryLimit: #设置保留旧的历史版本,默认是10
paused: #暂停部署,默认是false
progressDeadlineSeconds: #部署超时时间(s),默认是600
strategy: #更新策略
type: RollingUpdate #滚动更新策略,默认
rollingUpdate: #滚动更新
maxSurge: #最大额外可以存在的副本数,可以为百分比,也可以为整数,如果该值为0,那么maxUnavaliable就不能为0
maxUnavaliable: #最大不可用状态的pod的最大值,可以为百分比,也可以为整数,如果该值为0,那么maxSurge就不能为0
type: Recreate#重建更新策略,先删除旧的pod 在创建新的pod
selector: #选择器,通过它指定该控制器管理哪些pod
matchLabels: #Labels匹配规则
app: nginx-pod
matchExpressions: #Expression匹配规则
- {key: app, operator: In, values: [nginx-pod]}
template: #模板,当副本数量不足时,会根据下面的模板创建pod副本
metadata:
labels:
app: nginx-pod
spec:
containers:
- name: nginx
image: nginx:1.17.1
ports:
- containerPort: 80
kubectl scale deploy deploy名称 --replicas=pod数量 -n 命名空间
[root@k8s-master-1 test]# kubectl scale deploy deployment --replicas=3 -n default
deployment.apps/deployment scaled
#可以看到Deployment 已经成了3个
[root@k8s-master-1 test]# kubectl get pod
NAME READY STATUS RESTARTS AGE
busybox 1/1 Running 37 6d16h
deployment-5d9c9b97bb-9b5br 1/1 Running 0 3m15s
deployment-5d9c9b97bb-hdwsd 1/1 Running 0 3m15s
deployment-5d9c9b97bb-tvxj2 1/1 Running 0 7m55s
web-96d5df5c8-sw784 1/1 Running 2 44h
kubectl edit deploy deploy名字 -n 命名空间
#找到replicas,将其数量改为需要的数量即可,edit是在线编辑的意思
[root@k8s-master-1 test]# kubectl edit deploy deployment
deployment.apps/deployment edited
[root@k8s-master-1 test]# kubectl get pod
NAME READY STATUS RESTARTS AGE
busybox 1/1 Running 37 6d16h
deployment-5d9c9b97bb-9b5br 1/1 Running 0 6m18s
deployment-5d9c9b97bb-tvxj2 1/1 Running 0 10m
web-96d5df5c8-sw784 1/1 Running 2 44h
????????Deploym支持两种镜像更新策略:重建更新和滚动更新(默认),可以通过strategy选项进行配置
????????strategy:指定新的pod替换旧的pod的策略,有两个属性
????????type:指定策略类型
????????Recretae:重建更新,在创建出新的pod之前会先杀掉所有已经存在对的pod
????????RollingUpdate:滚动更新,就是杀死一部分,就启动一部分,在更新过程中,存在两个版本的pod
????????RollingUpdate:当type为RollingUpdate时生效,用于为RollingUpdate设置参数,有两个属性:
????????maxUnavailable:用来指定在升级过程中不可用pod的最大数量,默认25%
????????maxSurge:用来指定在升级过程中可以超过期望pod的最大数量,默认25%
编辑deployment.yaml文件,在spec节点下添加更新策略
vim deployment.yaml
spec:
strategy:
type: Recreate
[root@k8s-master-1 test]# kubectl apply -f deployment.yaml
[root@k8s-master-1 test]# kubectl get pod -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
deployment-59bfd7fdbc-db5zz 1/1 Running 0 3m56s 10.244.0.45 k8s-node-1 <none> <none>
#查看deploy
[root@k8s-master-1 test]# kubectl get deploy -owide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deployment 1/1 1 1 3h58m nginx nginx:1.15.4 app=nginx-pod
#修改镜像版本
[root@k8s-master-1 test]# kubectl set image deploy deployment nginx=nginx:1.15.4 --record
deployment.apps/deployment image updated
#回滚到上一个版本
[root@k8s-master-1 test]# kubectl rollout undo deploy deployment
deployment.apps/deployment rolled back
#回滚到指定的版本
[root@k8s-master-1 test]# kubectl rollout undo deploy deployment --to-revision=3
#查看历史版本
[root@k8s-master-1 test]# kubectl rollout history deploy deployment -n default
deployment.apps/deployment
REVISION CHANGE-CAUSE
2 <none>
3 <none>
4 <none>
显示为<node>是因为apply创建时候没加 --record 表示记录修订版本
#修改镜像版本并记录
[root@k8s-master-1 test]# kubectl set image deploy deployment nginx=nginx:1.17.1 --record
#查看历史版本记录
[root@k8s-master-1 test]# kubectl rollout history deploy deployment
deployment.apps/deployment
REVISION CHANGE-CAUSE
2 kubectl apply --filename=deployment.yaml --record=true
3 kubectl set image deploy deployment nginx=nginx:1.17.1 --record=true
4 kubectl set image deploy deployment nginx=nginx:1.15.3 --record=true
修改yaml文件,在spec节点下添加滚动更新策略,默认是滚动更新
strategy:
# type: Recreate
type: RollingUpdate
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
#创建并记录
[root@k8s-master-1 test]# kubectl apply -f deployment.yaml --record
查看deploy
[root@k8s-master-1 test]# kubectl get pod -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
deployment-778c988cc8-7zf97 1/1 Running 0 37s 10.244.0.54 k8s-node-1 <none> <none>
deployment-778c988cc8-t9k8n 1/1 Running 0 37s 10.244.0.55 k8s-node-1 <none> <none>
deployment-778c988cc8-xx9d6 1/1 Running 0 37s 10.244.1.41 k8s-node-2 <none> <none>
[root@k8s-master-1 test]#
[root@k8s-master-1 test]# kubectl get deploy -owide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deployment 3/3 3 3 46s nginx nginx:1.15.3 app=nginx-pod
[root@k8s-master-1 test]#
#修改镜像版本
[root@k8s-master-1 test]# kubectl set image deploy deployment nginx=nginx:1.15.4
deployment.apps/deployment image updated
[root@k8s-master-1 test]#
#此时在查看pod 时候就会看到 旧的pod 一边停止,新的pod一边创建
[root@k8s-master-1 test]# kubectl get pod -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
deployment-59bfd7fdbc-nfg92 1/1 Running 0 2s 10.244.0.56 k8s-node-1 <none> <none>
deployment-59bfd7fdbc-pbvls 0/1 ContainerCreating 0 1s <none> k8s-node-2 <none> <none>
deployment-59bfd7fdbc-x4c92 1/1 Running 0 4s 10.244.1.42 k8s-node-2 <none> <none>
deployment-778c988cc8-7zf97 0/1 Terminating 0 116s 10.244.0.54 k8s-node-1 <none> <none>
deployment-778c988cc8-t9k8n 1/1 Terminating 0 116s 10.244.0.55 k8s-node-1 <none> <none>
deployment-778c988cc8-xx9d6 1/1 Running 0 116s 10.244.1.41 k8s-node-2 <none> <none>
[root@k8s-master-1 test]#
[root@k8s-master-1 test]# kubectl get pod -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
deployment-59bfd7fdbc-nfg92 1/1 Running 0 30s 10.244.0.56 k8s-node-1 <none> <none>
deployment-59bfd7fdbc-pbvls 1/1 Running 0 29s 10.244.1.43 k8s-node-2 <none> <none>
deployment-59bfd7fdbc-x4c92 1/1 Running 0 32s 10.244.1.42 k8s-node-2 <none> <none>
????????金丝雀发布就是灰度发布,是一种pod 发布的方式。金丝雀发布采取先发布,再删除的方式,保证pod的总量不低于期望值,并且在更新部分pod 后暂停更新,当确认新pod版本运行正常后,在进行其他版本pod 的更新
????????比如有一批新的pod资源创建完成后立即暂停更新过程,此时,仅存在一部分新版本的应用,主体部分还是旧的版本。然后,再筛选一小部分的用户请求路由到新的pod应用,继续观察能否稳定地按期望的方式运行。确定没问题之后再继续完成余下的pod资源滚动更新,否则立即回滚更新操作。这就是所谓的金丝雀发布。
????????deployment支持更新过程中的控制,如:“暂停”、或者“继续”的操作
#先查看下当前nginx 版本为1.15.3
[root@k8s-master-1 test]# kubectl get deploy -owide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deployment 3/3 3 3 19s nginx nginx:1.15.3 app=nginx-pod
#rs用的是deployment-778c988cc8 控制器
[root@k8s-master-1 test]# kubectl get rs -owide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
deployment-778c988cc8 3 3 3 29s nginx nginx:1.15.3 app=nginx-pod,pod-template-hash=778c988cc8
#接下来进行金丝雀发布
[root@k8s-master-1 test]# kubectl set image deploy deployment nginx=nginx:1.15.4 && kubectl rollout pause deploy deployment
deployment.apps/deployment image updated
deployment.apps/deployment paused
#或者先暂停 在更新
[root@k8s-master-1 ~]# kubectl rollout pause deploy deployment
[root@k8s-master-1 test]# kubectl set image deploy deployment nginx=nginx:1.15.4
#进行二次更新 添加CPU配置(可以查看pod详解中的资源限制和请求 resources{limits,requests}})
[root@k8s-master-1 ~]# kubectl set resources deploy deployment --limits=cpu=200m,memory=128mi --requests=cpu=10m,memory=16mi
[root@k8s-master-1 test]# kubectl get deploy -owide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deployment 4/3 1 4 9m3s nginx nginx:1.15.4 app=nginx-pod
#从上面可以看到,nginx 的镜像为 1.15.4 了,但 READY 的有 4/3,其中的 4 表示我们有 4 个可以对外提供服务的 pod,3 表示我们期望的是三个,
UP-TO-DATE 为 1 ,表示最新版本的 pod 数量为 1 个,AVAILABLE 为 4 ,表示当前可用的 pod 数量为 4 个
[root@k8s-master-1 test]# kubectl get rs -owide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
deployment-59bfd7fdbc 1 1 1 4m47s nginx nginx:1.15.4 app=nginx-pod,pod-template-hash=59bfd7fdbc
deployment-778c988cc8 3 3 3 13m nginx nginx:1.15.3 app=nginx-pod,pod-template-hash=778c988cc8
#观察没问题的话继续更新操作
[root@k8s-master-1 test]# kubectl rollout resume deploy deployment
deployment.apps/deployment resumed
#deploy 版本已经修改
[root@k8s-master-1 test]# kubectl get deploy -owide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deployment 3/3 3 3 20m nginx nginx:1.15.4 app=nginx-pod
#如果有问题 立马回滚
[root@k8s-master-1 test]# kubectl rollout undo deploy deployment
deployment.apps/deployment rolled back
#回滚到指定的版本
[root@k8s-master-1 test]# kubectl rollout undo deploy deployment --to-revision=3
[root@k8s-master-1 test]#
[root@k8s-master-1 test]# kubectl get deploy -owide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deployment 3/3 3 3 22m nginx nginx:1.15.3 app=nginx-pod