Deployment介绍

一、什么是Deployment?

	用于部署无状态的服务,这个最常用的控制器。一般用于管理维护企业内部无状态的微服务,比如configserver、zuul、springboot。他可以管理多个副本的Pod实现无缝迁移、自动扩容缩容、自动灾难恢复、一键回滚等功能。

二、 创建一个Deployment

2.1、手动创建

[root@k8s-master01 ~]# kubectl create deployment nginx --image=nginx:1.15.2
deployment.apps/nginx created

2.2、使用文件创建

# 查看手动创建的nginx的yaml文件,然后把f开头的删了,且删了最后的status标签的内容,得到下面的yaml文件
[root@k8s-master01 ~]# kubectl  get deployment nginx -o yaml > nginx-deploy.yaml
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
# 使用以下命令去新建一个deployment
[root@k8s-master01 ~]# kubectl replace -f nginx-deploy.yaml 
deployment.apps/nginx replaced

# 在线更改yaml,管理deployment    ---把副本数改为2
[root@k8s-master01 ~]# kubectl edit deploy  nginx 

# 查看是否生成2个副本
[root@k8s-master01 ~]# kubectl get po
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 ~]# kubectl get deploy -owide
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 ~]# kubectl set image deploy nginx nginx=nginx:1.15.3 --record
deployment.apps/nginx image updated

3.2、查看更新过程

[root@k8s-master01 ~]# kubectl rollout status deploy nginx
Waiting for deployment "nginx" rollout to finish: 1 out of 3 new replicas have been updated...

# 或者使用describe查看
[root@k8s-master01 ~]# kubectl describe deploy nginx 

四、Deployment的回滚

3.1、回滚到上一个版本(一般都是回滚到上一个版本)

# 例如错误的更新到了一个xxx版本
[root@k8s-master01 ~]# kubectl set image deploy nginx nginx=nginx:xxx --record   
deployment.apps/nginx image updated

# 查看kubectl更新的历史命令
[root@k8s-master01 ~]# kubectl rollout history deploy nginx 
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 ~]# kubectl rollout undo deploy nginx
deployment.apps/nginx rolled back

3.2、回滚到指定版本(较少,但是得掌握)

# 多次更新错误版本
[root@k8s-master01 ~]# kubectl set image deploy nginx nginx=nginx:aa --record
deployment.apps/nginx image updated
[root@k8s-master01 ~]# kubectl set image deploy nginx nginx=nginx:bb --record
deployment.apps/nginx image updated
[root@k8s-master01 ~]# kubectl set image deploy nginx nginx=nginx:cc --record
deployment.apps/nginx image updated

# 查看kubectl更新的历史命令
[root@k8s-master01 ~]# kubectl rollout history deploy nginx 
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

# 查看指定版本的详细信息 ---看revision对应的数字即可
[root@k8s-master01 ~]# kubectl rollout history deploy nginx --revision=4
deployment.apps/nginx with revision #4
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 ~]# kubectl rollout undo deploy nginx --to-revision=4
deployment.apps/nginx rolled back

五、Deployment的扩容与缩容

5.1、Deployment的扩容

# Deployment的扩容与缩容,不会生成新的rs
[root@k8s-master01 ~]# kubectl  scale  --replicas=4  deploy nginx 
deployment.apps/nginx scaled

# --replicas  # 指定副本数
# nginx  	  # pod的名字

# 查看rs
[root@k8s-master01 ~]# kubectl get rs

5.2、Deployment的缩容

# Deployment的扩容与缩容,不会生成新的rs
[root@k8s-master01 ~]# kubectl  scale  --replicas=1  deploy nginx
deployment.apps/nginx scaled

# --replicas  # 指定副本数
# nginx  	  # pod的名字

# 查看rs
[root@k8s-master01 ~]# kubectl get rs

六、Deployment的暂停和恢复

  • deployment可以在线edit更改(可以一次性更改多个)
  • 也可以用kubectl set image更改(也可以一次性更改多个,但是需要使用到Deployment的暂停和恢复功能)

6.1、Deployment 暂停功能

# 暂停
[root@k8s-master01 ~]# kubectl rollout pause deployment nginx
deployment.apps/nginx paused

# 第一次更新
[root@k8s-master01 ~]# kubectl set image deploy nginx nginx=nginx:1.15.4 --record
deployment.apps/nginx image updated

# 第二次更新、添加内存、CPU
[root@k8s-master01 ~]# kubectl set resources deploy nginx -c nginx --limits=cpu=200m,memory=128Mi --requests=cpu=10m,memory=16Mi
deployment.apps/nginx resource requirements updated

# 查看被更改以后的nginx镜像的deployment
[root@k8s-master01 ~]# kubectl get deploy nginx -oyaml

6.2、Deployment 恢复功能

# 更新完想更新的内容后,然后恢复镜像
[root@k8s-master01 ~]# kubectl rollout resume deploy nginx 
deployment.apps/nginx resumed

# 查看rs,看到有新的
[root@k8s-master01 ~]# kubectl get rs
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
上一篇:kubernetes 重启的几种方法


下一篇:K8s常用命令和扩缩容