一、Deployment

1.1 Deployment API 版本对照表

Kubernetes 版本 Deployment 版本
v1.5-v1.15 extensions/v1beta1
v1.7-v1.15 apps/v1beta1
v1.8-v1.15 apps/v1beta2
v1.9+ apps/v1

1.2 Deployment 一个典型的用例

一个典型的用例如下:

  • 使用 Deployment 来创建 ReplicaSet。ReplicaSet 在后台创建 pod。检查启动状态,看它是成功还是失败。
  • 然后,通过更新 Deployment 的 PodTemplateSpec 字段来声明 Pod 的新状态。这会创建一个新的 ReplicaSet,Deployment 会按照控制的速率将 pod 从旧的 ReplicaSet 移动到新的 ReplicaSet 中。
  • 如果当前状态不稳定,回滚到之前的 Deployment revision。每次回滚都会更新 Deployment 的 revision。
  • 扩容 Deployment 以满足更高的负载。
  • 暂停 Deployment 来应用 PodTemplateSpec 的多个修复,然后恢复上线。
  • 根据 Deployment 的状态判断上线是否 hang 住了。
  • 清除旧的不必要的 ReplicaSet。

1.3 创建 Deployment

Deployment yaml文件包含四个部分:

  • apiVersion: 表示版本
  • kind: 表示资源
  • metadata: 表示元信息
  • spec: 资源规范字段

Deployment yaml 名词解释:

  1. apiVersion: apps/v1 # 指定api版本,此值必须在kubectl api-versions中
  2. kind: Deployment # 指定创建资源的角色/类型
  3. metadata: # 资源的元数据/属性
  4. name: demo # 资源的名字,在同一个namespace中必须唯一
  5. namespace: default # 部署在哪个namespace中
  6. labels: # 设定资源的标签
  7. app: demo
  8. version: stable
  9. spec: # 资源规范字段
  10. replicas: 1 # 声明副本数目
  11. revisionHistoryLimit: 3 # 保留历史版本
  12. selector: # 选择器
  13. matchLabels: # 匹配标签
  14. app: demo
  15. version: stable
  16. strategy: # 策略
  17. rollingUpdate: # 滚动更新
  18. maxSurge: 30% # 最大额外可以存在的副本数,可以为百分比,也可以为整数
  19. maxUnavailable: 30% # 示在更新过程中能够进入不可用状态的 Pod 的最大值,可以为百分比,也可以为整数
  20. type: RollingUpdate # 滚动更新策略
  21. template: # 模版
  22. metadata: # 资源的元数据/属性
  23. annotations: # 自定义注解列表
  24. sidecar.istio.io/inject: "false" # 自定义注解名字
  25. labels: # 设定资源的标签
  26. app: demo
  27. version: stable
  28. spec: # 资源规范字段
  29. containers:
  30. - name: demo # 容器的名字
  31. image: demo:v1 # 容器使用的镜像地址
  32. imagePullPolicy: IfNotPresent # 每次Pod启动拉取镜像策略,三个选择 Always、Never、IfNotPresent
  33. # Always,每次都检查;Never,每次都不检查(不管本地是否有);IfNotPresent,如果本地有就不检查,如果没有就拉取
  34. resources: # 资源管理
  35. limits: # 最大使用
  36. cpu: 300m # CPU,1核心 = 1000m
  37. memory: 500Mi # 内存,1G = 1000Mi
  38. requests: # 容器运行时,最低资源需求,也就是说最少需要多少资源容器才能正常运行
  39. cpu: 100m
  40. memory: 100Mi
  41. livenessProbe: # pod 内部健康检查的设置
  42. httpGet: # 通过httpget检查健康,返回200-399之间,则认为容器正常
  43. path: /healthCheck # URI地址
  44. port: 8080 # 端口
  45. scheme: HTTP # 协议
  46. # host: 127.0.0.1 # 主机地址
  47. initialDelaySeconds: 30 # 表明第一次检测在容器启动后多长时间后开始
  48. timeoutSeconds: 5 # 检测的超时时间
  49. periodSeconds: 30 # 检查间隔时间
  50. successThreshold: 1 # 成功门槛
  51. failureThreshold: 5 # 失败门槛,连接失败5次,pod杀掉,重启一个新的pod
  52. readinessProbe: # Pod 准备服务健康检查设置
  53. httpGet:
  54. path: /healthCheck
  55. port: 8080
  56. scheme: HTTP
  57. initialDelaySeconds: 30
  58. timeoutSeconds: 5
  59. periodSeconds: 10
  60. successThreshold: 1
  61. failureThreshold: 5
  62. #也可以用这种方法
  63. #exec: 执行命令的方法进行监测,如果其退出码不为0,则认为容器正常
  64. # command:
  65. # - cat
  66. # - /tmp/health
  67. #也可以用这种方法
  68. #tcpSocket: # 通过tcpSocket检查健康
  69. # port: number
  70. ports:
  71. - name: http # 名称
  72. containerPort: 8080 # 容器开发对外的端口
  73. protocol: TCP # 协议
  74. imagePullSecrets: # 镜像仓库拉取密钥
  75. - name: harbor-certification
  76. affinity: # 亲和性调试
  77. nodeAffinity: # 节点亲和力
  78. requiredDuringSchedulingIgnoredDuringExecution: # pod 必须部署到满足条件的节点上
  79. nodeSelectorTerms: # 节点满足任何一个条件就可以
  80. - matchExpressions: # 有多个选项,则只有同时满足这些逻辑选项的节点才能运行 pod
  81. - key: beta.kubernetes.io/arch
  82. operator: In
  83. values:
  84. - amd64

Service yaml 名词解释:

apiVersion: v1 # 指定api版本,此值必须在kubectl api-versions中 
kind: Service # 指定创建资源的角色/类型 
metadata: # 资源的元数据/属性
  name: demo # 资源的名字,在同一个namespace中必须唯一
  namespace: default # 部署在哪个namespace中
  labels: # 设定资源的标签
    app: demo
spec: # 资源规范字段
  type: ClusterIP # ClusterIP 类型
  ports:
    - port: 8080 # service 端口
      targetPort: http # 容器暴露的端口
      protocol: TCP # 协议
      name: http # 端口名称
  selector: # 选择器
    app: demo

1.4 一个Deployment例子

more nginx-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80

二、StatefulSet

2.1 创建一个StatefulSet

more web.yaml

---
apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels: 
    app: nginx
spec:
  ports:
  - port: 80
    name: web
  clusterIP: None
  selector:
    app: nginx
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: web
  labels: 
    app: nginx
spec:
  serviceName: "nginx"
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels: 
        app: nginx
    spec:
      containers: 
      - name: nginx
        image: gcr.io/google_containers/nginx-slim:latest
        ports:
        - containerPort: 80
          name: web
        volumeMounts:
        - name: www
          mountPath: /usr/share/nginx/html
  volumeClaimTemplates:
  - metadata:
     name: www
     annotations:
       volume.alpha.kubernetes.io/storage-class: anything
    spec:
     accessModes: [ "ReadWriteOnce" ]
     resources:
       requests:
         storage: 200Mi