一个比较完整的deployment的样例

以下是一个较为丰富且包含完整生命周期钩子的Kubernetes Deployment示例。这个示例展示了一个带有初始化容器(initContainers)、生命周期钩子(lifecycle)、就绪探针(readinessProbe)和存活探针(livenessProbe)的Deployment。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: example-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: example
  template:
    metadata:
      labels:
        app: example
    spec:
      affinity:
        nodeAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 1
            preference:
              matchExpressions:
              - key: high-memory
                operator: In
                values:
                - "true"
      initContainers:
      - name: init-myservice
        image: busybox:1.28
        command: ['sh', '-c', 'echo "Init Container doing some work" && sleep 60']
      containers:
      - name: main-container
        image: nginx:1.17.1
        ports:
        - containerPort: 80
        lifecycle:
          postStart:
            exec:
              command: ["/bin/sh", "-c", "echo 'PostStart Hook: Container started'"]
          preStop:
            exec:
              command: ["/bin/sh", "-c", "echo 'PreStop Hook: Container stopping' && sleep 10"]
        readinessProbe:
          httpGet:
            path: /index.html
            port: 80
          initialDelaySeconds: 15
          periodSeconds: 10
        livenessProbe:
          httpGet:
            path: /index.html
            port: 80
          initialDelaySeconds: 20
          periodSeconds: 10
      restartPolicy: Always

Kubernetes Deployment 配置的逐行解释:

apiVersion: apps/v1
- apiVersion: 指定了使用的 Kubernetes API 版本,这里是 apps/v1,适用于 Deployment 资源类型。

kind: Deployment
- kind: 定义了资源类型,这里是 Deployment,表示这是一个部署对象。

metadata:
  name: example-deployment
- metadata: 提供了关于资源的元数据。 - name: 资源的名称,这里是 example-deployment

spec:
  replicas: 3
- spec: 定义了部署的详细规格。 - replicas: 指定了应该运行的 Pod 副本数量,这里设置为3。

  selector:
    matchLabels:
      app: example
- selector: 定义了如何选择 Pod,用于确定哪些 Pod 属于这个 Deployment。 - matchLabels: 设置了选择标准,这里是选择带有 app: example 标签的 Pod。

  template:
    metadata:
      labels:
        app: example
- template: 定义了 Pod 的模板。 - metadata: 模板的元数据。 - labels: 设置了 Pod 的标签,这里为 app: example

    spec:
      affinity:
        nodeAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 1
            preference:
              matchExpressions:
              - key: high-memory
                operator: In
                values:
                - "true"
- affinity: 定义了 Pod 与节点的亲和性设置。 - nodeAffinity: 设置了节点亲和性规则。 - preferredDuringSchedulingIgnoredDuringExecution: 表明这是一个优先级规则,不是强制性的。 - weight: 为这个规则设置了权重,这里是1。 - preference: 定义了匹配表达式。 - matchExpressions: 设置了节点选择条件。 - key: 节点标签的键,这里是 high-memory。 - operator: 操作符,这里是 In,表示节点标签的值应该在下面的列表中。 - values: 值的列表,这里是 "true"

      initContainers:
      - name: init-myservice
        image: busybox:1.28
        command: ['sh', '-c', 'echo "Init Container doing some work" && sleep 60']
- initContainers: 定义了初始化容器。 - name: 初始化容器的名称,这里是 init-myservice。 - image: 容器使用的镜像,这里是 busybox:1.28。 - command: 容器启动时执行的命令。

      containers:
      - name: main-container
        image: nginx:1.17.1
        ports:
        - containerPort: 80
- containers: 定义了应用容器。 - name: 容器的名称,这里是 main-container。 - image: 容器使用的镜像,这里是 nginx:1.17.1。 - ports: 容器暴露的端口。 - containerPort: 指定容器内部监听的端口号,这里是80。

        lifecycle:
          postStart:
            exec:
              command: ["/bin/sh", "-c", "echo 'PostStart Hook: Container started'"]
          preStop:
            exec:
              command: ["/bin/sh", "-c", "echo 'PreStop Hook: Container stopping' && sleep 10"]
- lifecycle: 定义了容器生命周期的钩子。 - postStart: 容器启动后执行的命令。 - preStop: 容器停止前执行的命令。

        readinessProbe:
          httpGet:
            path: /index.html
            port: 80
          initialDelay

Seconds: 15
          periodSeconds: 10
- readinessProbe: 定义了就绪探针。 - httpGet: 通过HTTP GET请求检查容器是否准备好服务。 - pathport: 指定了检查的路径和端口。 - initialDelaySeconds: 容器启动后延迟多久开始探针。 - periodSeconds: 探针的检查频率。

        livenessProbe:
          httpGet:
            path: /index.html
            port: 80
          initialDelaySeconds: 20
          periodSeconds: 10
- livenessProbe: 定义了存活探针。 - 类似于 readinessProbe,用于检查容器是否还在运行。

      restartPolicy: Always
- restartPolicy: 定义了 Pod 的重启策略,这里设置为 Always,意味着总是重启失败的容器。

这个配置为 Kubernetes 部署提供了一个详细的设置,包括节点亲和性、初始化容器、容器生命周期钩子以及探针来确保容器的健康和正确启动。