Kubernetes-Deployment

Deployment

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

$ kubectl create deployment  nginx --image=nginx1.20 
deployment.apps/nginx created

从文件创建

$ cat >>nginx-deployment.yaml<<-EOF 
apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "1"
  generation: 1
  labels:
    app: nginx
  name: nginx
  namespace: default
spec:
  progressDeadlineSeconds: 600
  replicas: 2  # 副本数
  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.20
        imagePullPolicy: Always
        name: nginx
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
EOF
$ kubectl get deployment  -o wide
NAME    READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS        IMAGES       SELECTOR
nginx   2/2     2            2           16m   nginx1-20-5k2fk   nginx:1.20   app=nginx

NAME:Deployment名称
READY:Pod的状态,已经Ready的个数
UP-TO-DATE:已经达到期望状态的被更新的副本数
AVAILABLE:已经可以用的副本数
AGE:显示应用程序运行的时间
CONTAINERS:容器名称
IMAGES:容器的镜像
SELECTOR:管理的Pod的标签

更新

$ kubectl set image deployment nginx nginx=nginx:1.19 --record
deployment.apps/nginx image updated

查看应用升级状态

$ kubectl rollout status deployment nginx
deployment "nginx" successfully rolled out

回滚

查看历史版本

$ kubectl rollout history deployment nginx
deployment.apps/nginx 
REVISION  CHANGE-CAUSE
1         <none>
2         kubectl set image deployment nginx nginx=nginx:1.20 --record=true
3         kubectl set image deployment nginx nginx=nginx:1.21 --record=true

回滚到上一个版本

$ kubectl rollout undo deployment nginx
deployment.apps/nginx rolled back

查看回滚状态

$ kubectl rollout status deployment nginx
Waiting for deployment "nginx" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "nginx" rollout to finish: 1 old replicas are pending termination...
deployment "nginx" successfully rolled out

查看历史记录

$ kubectl rollout history deployment nginx 
deployment.apps/nginx 
REVISION  CHANGE-CAUSE
1         <none>
3         kubectl set image deployment nginx nginx=nginx:1.21 --record=true
4         kubectl set image deployment nginx nginx=nginx:1.20 --record=true

查看指定版本的详细信息

$ kubectl rollout history deployment nginx --revision=4
deployment.apps/nginx with revision #4
Pod Template:
  Labels:	app=nginx
	pod-template-hash=7766cbf97c
  Annotations:	kubernetes.io/change-cause: kubectl set image deployment nginx nginx=nginx:1.20 --record=true
  Containers:
   nginx:
    Image:	nginx:1.20
    Port:	<none>
    Host Port:	<none>
    Environment:	<none>
    Mounts:	<none>
  Volumes:	<none>

回滚到指定版本

$ kubectl rollout undo deployment nginx --to-revision=3
deployment.apps/nginx rolled back

扩容缩容

将已存在的资源进行扩容

$ kubectl scale deployment nginx --replicas=3
deployment.apps/nginx scaled
$ kubectl get pods
NAME                     READY   STATUS              RESTARTS   AGE
nginx-58f8c48d58-9j4kh   1/1     Running             0          3m55s
nginx-58f8c48d58-gsmm2   1/1     Running             0          3m36s
nginx-58f8c48d58-pffrw   0/1     ContainerCreating   0          17s

将已存在的资源进行缩容

$ kubectl scale deployment nginx --replicas=1
deployment.apps/nginx scaled
$ kubectl get pods
NAME                     READY   STATUS        RESTARTS   AGE
nginx-58f8c48d58-9j4kh   1/1     Running       0          5m51s
nginx-58f8c48d58-gsmm2   1/1     Terminating   0          5m32s
nginx-58f8c48d58-pffrw   1/1     Terminating   0          2m13

更新暂停和恢复

暂停更新

$ kubectl rollout pause deployment nginx
deployment.apps/nginx paused
$ kubectl set image deployment nginx nginx=nginx:1.15 --record
deployment.apps/nginx image updated
$ kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-58f8c48d58-9j4kh   1/1     Running   0          10m

恢复更新

$ kubectl rollout resume deployment nginx
deployment.apps/nginx resumed
$ kubectl get rs
NAME               DESIRED   CURRENT   READY   AGE
nginx-5759cb8dcc   0         0         0       27m
nginx-58f8c48d58   1         1         1       25m
nginx-7766cbf97c   0         0         0       27m
nginx-8695495f75   1         1         0       20s  # 20秒前创建了一个rs
apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "6"
    kubernetes.io/change-cause: kubectl set image deployment nginx nginx=nginx:1.15
      --record=true
  creationTimestamp: "2022-01-11T06:41:17Z"
  generation: 11
  labels:
    app: nginx
  name: nginx
  namespace: default
  resourceVersion: "105521"
  uid: 661de325-16ff-4851-bbd2-bc56d5dae807
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
        imagePullPolicy: Always
        name: nginx
        resources:
          limits:
            cpu: 200m
            memory: 128Mi
          requests:
            cpu: 10m
            memory: 16Mi
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30

.spec.revisionHistoryLimit: 10 //设置保留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
posted @ 2022-01-21 15:12  Cai_HL  阅读(22)  评论(0编辑  收藏  举报
>