部署 调度

  1apiVersion: apps/v1
  2kind: Deployment
  3# 元数据
  4metadata:
  5  name: linuxcrypt-web
  6  labels:
  7    app: linuxcrypt-web
  8# 属性
  9spec:
 10  # 副本数量
 11  replicas: 3
 12  # 滚动升级时,容器准备就绪时间最少为30s
 13  minReadySeconds: 30 
 14  # 选择器
 15  selector:
 16    # 匹配lables, 从metadata.labels中匹配
 17    matchLabels:
 18      # 匹配lables, 从metadata.labels.app中匹配
 19      app: linuxcrypt-web
 20  strategy:
 21    # 升级方式 recreate和rollingUpdate
 22    # recreate--删除所有已存在的pod,重新创建新的; recreate策略将会在升级过程中,停止服务,但会保证应用版本一致;
 23    # rollingUpdate--滚动升级,逐步替换的策略,同时滚动升级时,支持更多的附加参数,例如设置最大不可用pod数量,最小升级间隔时间等等。使用rollingUpdate不会中断服务,但会导致调用时出现应用版本不一致的情况,输出内容不一致。
 24    #
 25    type: RollingUpdate
 26    # 由于replicas为3个, 则整个升级,pod个数在2-4个之间
 27    rollingUpdate:
 28      # 滚动升级时会先启动1个pod
 29      maxSurge: 1
 30      # 滚表示滚动升级时允许的最大Unavailable的pod个数。由于replicas为3,则整个升级,pod个数在2-4个之间
 31      maxUnavailable: 0
 32  # 滚动升级时90s后认为该pod就绪 
 33  minReadySeconds: 90 
 34  template:
 35    metadata:
 36      labels:
 37        app: linuxcrypt-web
 38    spec:
 39      # k8s将会给应用发送SIGTERM信号,可以用来正确、优雅地关闭应用,默认为30秒
 40      # 如果需要更优雅地关闭,则可以使用k8s提供的pre-stop lifecycle hook 的配置声明,将会在发送SIGTERM之前执行
 41      terminationGracePeriodSeconds: 60 
 42      containers:
 43      - name: linuxcrypt-web
 44        image: hub.docker.com/linuxcrypt-web:2.0.0-20180607.0749
 45        #command: [ "sh", "/etc/run.sh"]
 46        ports:
 47        - name: linuxcrypt-web
 48          containerPort: 8080
 49          protocol: TCP
 50        # 容器资源分配
 51        resources: 
 52          # requests主要作为pod调度时的参考依据
 53          requests: 
 54            # cpu 的频率 250m赫兹 进制1000
 55            cpu: 250m
 56            # 最低要求16M 进制1024;
 57            memory: 16Mi 
 58          # limits主要用来限制每个容器使用资源的最大值
 59          limits: 
 60            # 最多使用1个核
 61            cpu: 1 
 62            # 最多使用32M
 63            memory: 32Mi
 64        # 表示container是否以及处于可接受service请求的状态了。如果ReadinessProbe失败,endpoints controller将会从service所匹配到的endpoint列表中移除关于这个container的IP地址。
 65        # 因此对于Service匹配到的endpoint的维护其核心是ReadinessProbe。默认Readiness的初始值是Failure,如果一个container没有提供Readiness则被认为是Success。
 66        #
 67        # readinessProbe是K8S认为该pod是启动成功的,这里根据每个应用的特性,自己去判断,可以执行command,也可以进行httpGet。
 68        readinessProbe:
 69          # exec:
 70          #   command: 
 71          #   - cat
 72          #   - /tmp/health
 73          httpGet:
 74            path: /health
 75            port: 8080
 76            # host: www.linuxcrypt.cn
 77            # scheme: HTTP
 78          # 用来表示初始化延迟的时间,也就是告诉监测从多久之后开始运行,单位是秒
 79          initialDelaySeconds: 60
 80          # 用来表示监测的超时时间,如果超过这个时长后,则认为监测失败
 81          timeoutSeconds: 10
 82          # 告诉kubelet每5秒探测一次
 83          periodSeconds: 5
 84          # 探测失败后成功1次就认为是成功的
 85          successThreshold: 1
 86          # 探测失败阈值
 87          failureThreshold: 3
 88        # 表示container是否处于live状态。如果LivenessProbe失败,LivenessProbe将会通知kubelet对应的container不健康了。
 89        # 随后kubelet将kill掉container,并根据RestarPolicy进行进一步的操作。
 90        # 默认情况下LivenessProbe在第一次检测之前初始化值为Success,如果container没有提供LivenessProbe,则也认为是Success;
 91        # 
 92        # livenessProbe是K8S认为该pod是存活的,不存在则需要kill掉,然后再新启动一个,以达到RS指定的个数。
 93        livenessProbe:  
 94          httpGet:
 95            path: /health
 96            port: 8080
 97          # 用来表示初始化延迟的时间,也就是告诉监测从多久之后开始运行,单位是秒
 98          initialDelaySeconds: 60
 99          # 告诉kubelet每5秒探测一次
100          periodSeconds: 5
101          # 探测失败后成功1次就认为是成功的
102          successThreshold: 1
103          # 探测失败阈值
104          failureThreshold: 3
105          # 用来表示监测的超时时间,如果超过这个时长后,则认为监测失败
106          timeoutSeconds: 10

livenessProbe、readinessProbe: 二者结合,通过健康监测,在滚动升级时会验证pod是否可用。如果不可用,在升级时会杀掉所有进程自动创建,然后自动检测时,如果不可用会根据策略自动处理,当前是重建

常用命令

  • 查看部署状态
1kubectl rollout status deployment/linuxcrypt-web
2kubectl describe deployment linuxcrypt-web
 1  Name:                   linuxcrypt-web
 2  Namespace:              default
 3  CreationTimestamp:      Thu, 07 Jun 2018 14:45:08 +0800
 4  Labels:                 app=linuxcrypt-web
 5  Annotations:            deployment.kubernetes.io/revision=6
 6  Selector:               app=linuxcrypt-web
 7  Replicas:               4 desired | 4 updated | 4 total | 4 available | 0 unavailable
 8  StrategyType:           RollingUpdate
 9  MinReadySeconds:        0
10  RollingUpdateStrategy:  0 max unavailable, 1 max surge
11  Pod Template:
12    Labels:  app=linuxcrypt-web
13    Containers:
14     linuxcrypt-web:
15      Image:        hub.docker.com/linuxcrypt-web:2.0.0-20180607.0910
16      Port:         8080/TCP
17      Host Port:    0/TCP
18      Environment:  <none>
19      Mounts:       <none>
20    Volumes:        <none>
21  Conditions:
22    Type           Status  Reason
23    ----           ------  ------
24    Progressing    True    NewReplicaSetAvailable
25    Available      True    MinimumReplicasAvailable
26  OldReplicaSets:  <none>
27  NewReplicaSet:   linuxcrypt-web-d5f8c55c9 (4/4 replicas created)
28  Events:
29    Type    Reason             Age   From                   Message
30    ----    ------             ----  ----                   -------
31    Normal  ScalingReplicaSet  1h    deployment-controller  Scaled up replica set linuxcrypt-web-d5f8c55c9 to
  • 升级
1kubectl set image deployment linuxcrypt-web linuxcrypt-web=image-name:tag
  • 暂停升级
1kubectl rollout pause deployment linuxcrypt-web
  • 继续升级
1kubectl rollout resume deployment linuxcrypt-web
  • 回滚
1kubectl rollout undo deployment linuxcrypt-web
  • 查看deployments版本
1kubectl rollout history deployment
2kubectl rollout history deployment linuxcrypt-web
1  deployments "linuxcrypt-web"
2  REVISION  CHANGE-CAUSE
3  1         <none>
4  2         <none>
5  3         <none>
  • 回滚到指定版本
1kubectl rollout undo deployment linuxcrypt-web --to-revision=2 # 2 从查看deployments版本查看
  • 更改副本数
1kubectl scale deployment xxx-m --replicas 1 -n linuxcrypt-web