部署中的 Pod 无法完成就绪检查

Pod in Deployment won't fulfill readiness check

我做了一个 nginx pod 的简单部署,然后编辑部署以通过 TCP 添加一个 readinessProbe 和一个 livenessProbe,就像在 official docs.

中一样

保存后,部署创建了一个新的副本集并启动了新的 pod,但探测从未完成。

这里是 describe 命令的部署 yaml 输出:

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "2"
  creationTimestamp: "2020-09-21T18:51:13Z"
  generation: 2
  labels:
    app: dep1
  name: dep1
  namespace: default
  resourceVersion: "1683893"
  selfLink: /apis/apps/v1/namespaces/default/deployments/dep1
  uid: b23bceff-aca5-4c89-84c0-5882cf2df217
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: dep1
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: dep1
    spec:
      containers:
      - image: nginx
        imagePullPolicy: Always
        livenessProbe:
          failureThreshold: 3
          initialDelaySeconds: 15
          periodSeconds: 20
          successThreshold: 1
          tcpSocket:
            port: 8080
          timeoutSeconds: 1
        name: nginx
        ports:
        - containerPort: 8080
          protocol: TCP
        readinessProbe:
          failureThreshold: 3
          initialDelaySeconds: 5
          periodSeconds: 10
          successThreshold: 1
          tcpSocket:
            port: 8080
          timeoutSeconds: 1
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
status:
  availableReplicas: 1
  conditions:
  - lastTransitionTime: "2020-09-21T18:51:16Z"
    lastUpdateTime: "2020-09-21T18:51:16Z"
    message: Deployment has minimum availability.
    reason: MinimumReplicasAvailable
    status: "True"
    type: Available
  - lastTransitionTime: "2020-09-21T18:51:13Z"
    lastUpdateTime: "2020-09-21T19:16:07Z"
    message: ReplicaSet "dep1-5d66c67794" is progressing.
    reason: ReplicaSetUpdated
    status: "True"
    type: Progressing
  observedGeneration: 2
  readyReplicas: 1
  replicas: 2
  unavailableReplicas: 1
  updatedReplicas: 1

下面是 pod 的事件:

Events:
  Type     Reason     Age                     From                     Message
  ----     ------     ----                    ----                     -------
  Normal   Scheduled  <unknown>               default-scheduler        Successfully assigned default/dep1-5d66c67794-qd48q to docker-desktop
  Normal   Pulling    13m (x2 over 14m)       kubelet, docker-desktop  Pulling image "nginx"
  Normal   Killing    13m                     kubelet, docker-desktop  Container nginx failed liveness probe, will be restarted
  Normal   Pulled     13m (x2 over 14m)       kubelet, docker-desktop  Successfully pulled image "nginx"
  Normal   Created    13m (x2 over 14m)       kubelet, docker-desktop  Created container nginx
  Normal   Started    13m (x2 over 14m)       kubelet, docker-desktop  Started container nginx
  Warning  Unhealthy  12m (x5 over 14m)       kubelet, docker-desktop  Liveness probe failed: dial tcp 10.1.0.174:8080: connect: connection refused
  Warning  Unhealthy  9m48s (x30 over 14m)    kubelet, docker-desktop  Readiness probe failed: dial tcp 10.1.0.174:8080: connect: connection refused
  Warning  BackOff    4m42s (x11 over 8m36s)  kubelet, docker-desktop  Back-off restarting failed container

为什么我打开以下端口时连接被拒绝?

ports:
  - containerPort: 8080
    protocol: TCP

默认情况下,nginx 网络服务器公开端口 80,不仅您的健康检查不起作用,而且您的应用程序永远不会在端口 8080 上打开。此 tutorial 中使用的 docker 图像是k8s.gcr.io/goproxy:0.1,您正在使用 nginx。尝试此配置或将您的图像部署更改为 k8s.gcr.io/goproxy:0.1:

spec:
  containers:
  - image: nginx
    imagePullPolicy: Always
    livenessProbe:
      failureThreshold: 3
      initialDelaySeconds: 15
      periodSeconds: 20
      successThreshold: 1
      tcpSocket:
        port: 80
      timeoutSeconds: 1
    name: nginx
    ports:
    - containerPort: 80
      protocol: TCP
    readinessProbe:
      failureThreshold: 3
      initialDelaySeconds: 5
      periodSeconds: 10
      successThreshold: 1
      tcpSocket:
        port: 80
      timeoutSeconds: 1