Deployment
一、什么是Deployment?
用于部署无状态的服务,这个最常用的控制器。一般用于管理维护企业内部无状态的微服务,比如configserver、zuul、springboot。他可以管理多个副本的Pod实现无缝迁移、自动扩容缩容、自动灾难恢复、一键回滚等功能。
用于部署无状态的服务! ! !
二、 创建一个Deployment
2.1、手动创建
[ root@k8s-master01 ~]
deployment.apps/nginx created
2.2、使用文件创建
[ root@k8s-master01 ~]
cat > nginx-deploy.yaml << EFO
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "1"
creationTimestamp: "2020-12-22T00:07:49Z"
generation: 1
labels:
app: nginx
name: nginx
namespace: default
resourceVersion: "73782"
uid: 6186f4c7-50bc-45d0-9ed4-916b311802eb
spec:
progressDeadlineSeconds: 600
replicas: 1 # 副本数
revisionHistoryLimit: 10 # 历史记录保留的个数
selector:
matchLabels:
app: nginx
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
app: nginx
spec:
containers:
- image: nginx:1.15.2
imagePullPolicy: IfNotPresent
name: nginx
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
EFO
[ root@k8s-master01 ~]
deployment.apps/nginx replaced
[ root@k8s-master01 ~]
[ root@k8s-master01 ~]
NAME READY STATUS RESTARTS AGE
nginx-66bbc9fdc5-c6l6t 1/1 Running 0 57s
nginx-66bbc9fdc5-hsv4d 1/1 Running 0 34m
2.3、状态解析
[ root@k8s-master01 ~]
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
nginx 2/2 2 1 35m nginx nginx:1.15.2 app= nginx
NAME: Deployment名称
READY:Pod的状态,已经Ready的个数
UP-TO-DATE:已经达到期望状态的被更新的副本数
AVAILABLE:已经可以用的副本数
AGE:显示应用程序运行的时间
CONTAINERS:容器名称
IMAGES:容器的镜像
SELECTOR:管理的Pod的标签
三、Deployment的更新
3.1、更改deployment的镜像并记录
[ root@k8s-master01 ~]
deployment.apps/nginx image updated
3.2、查看更新过程
[ root@k8s-master01 ~]
Waiting for deployment "nginx" rollout to finish: 1 out of 3 new replicas have been updated.. .
[ root@k8s-master01 ~]
四、Deployment的回滚
4.1、回滚到上一个版本(一般都是回滚到上一个版本)
[ root@k8s-master01 ~]
deployment.apps/nginx image updated
[ root@k8s-master01 ~]
deployment.apps/nginx
REVISION CHANGE-CAUSE
1 < none>
2 kubectl set image deploy nginx nginx= nginx:1.15.3 --record= true
3 kubectl set image deploy nginx nginx= nginx:xxx --record= true
[ root@k8s-master01 ~]
deployment.apps/nginx rolled back
4.2、回滚到指定版本(较少,但是得掌握)
[ root@k8s-master01 ~]
deployment.apps/nginx image updated
[ root@k8s-master01 ~]
deployment.apps/nginx image updated
[ root@k8s-master01 ~]
deployment.apps/nginx image updated
[ root@k8s-master01 ~]
deployment.apps/nginx
REVISION CHANGE-CAUSE
1 < none>
3 kubectl set image deploy nginx nginx= nginx:xxx --record= true
4 kubectl set image deploy nginx nginx= nginx:1.15.3 --record= true
5 kubectl set image deploy nginx nginx= nginx:aa --record= true
6 kubectl set image deploy nginx nginx= nginx:bb --record= true
7 kubectl set image deploy nginx nginx= nginx:cc --record= true
[ root@k8s-master01 ~]
deployment.apps/nginx with revision
Pod Template:
Labels: app= nginx
pod-template-hash= 5dfc8689c6
Annotations: kubernetes.io/change-cause: kubectl set image deploy nginx nginx= nginx:1.15.3 --record= true
Containers:
nginx:
Image: nginx:1.15.3
Port: < none>
Host Port: < none>
Environment: < none>
Mounts: < none>
Volumes: < none>
[ root@k8s-master01 ~]
deployment.apps/nginx rolled back
五、Deployment的扩容与缩容
5.1、Deployment的扩容
[ root@k8s-master01 ~]
deployment.apps/nginx scaled
[ root@k8s-master01 ~]
5.2、Deployment的缩容
[ root@k8s-master01 ~]
deployment.apps/nginx scaled
[ root@k8s-master01 ~]
六、Deployment的暂停和恢复
deployment可以在线edit更改(可以一次性更改多个)
也可以用kubectl set image更改(也可以一次性更改多个,但是需要使用到Deployment的暂停和恢复功能)
6.1、Deployment 暂停功能
[ root@k8s-master01 ~]
deployment.apps/nginx paused
[ root@k8s-master01 ~]
deployment.apps/nginx image updated
[ root@k8s-master01 ~]
deployment.apps/nginx resource requirements updated
[ root@k8s-master01 ~]
6.2、Deployment 恢复功能
[ root@k8s-master01 ~]
deployment.apps/nginx resumed
[ root@k8s-master01 ~]
NAME DESIRED CURRENT READY AGE
nginx-5b6bc78b67 1 1 0 41s
七、Deployment注意事项
.spec.revisionHistoryLimit:设置保留RS旧的revision的个数,设置为0的话,不保留历史数据
.spec.minReadySeconds:可选参数,指定新创建的Pod在没有任何容器崩溃的情况下视为Ready最小的秒数,默认为0,即一旦被创建就视为可用。
滚动更新的策略:
.spec.strategy.type:更新deployment的方式,默认是RollingUpdate
RollingUpdate:滚动更新,可以指定maxSurge和maxUnavailable
maxUnavailable:指定在回滚或更新时最大不可用的Pod的数量,可选字段,默认25%,可以设置成数字或百分比,如果该值为0,那么maxSurge就不能0
maxSurge:可以超过期望值的最大Pod数,可选字段,默认为25%,可以设置成数字或百分比,如果该值为0,那么maxUnavailable不能为0
Recreate:重建,先删除旧的Pod,在创建新的Pod