就绪探测失败,但端口转发到服务工作

时间:2021-05-14 14:53:34

标签: kubernetes kind

我有一个 deployment.yaml,它在容器上有一个就绪探针。 (这里准备就绪探针会失败)

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: nginx
  name: my-nginx-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx
        name: nginx
        readinessProbe:
         exec:
          command:
            - cat
            - /server/xyz.txt
         initialDelaySeconds: 50
         periodSeconds: 10
        resources: {}
status: {}

部署中的 Pod 使用 ClusterIP 类型的服务提供服务。

apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    app: nginx-service
  name: nginx-service
spec:
  ports:
  - port: 8080
    protocol: TCP
    targetPort: 80
  selector:
    app: nginx
  type: ClusterIP
status:
  loadBalancer: {}

在使用 kubectl apply 应用这些 yaml 后,Pod 中的容器永远不会准备好,因为就绪探测失败,这是意料之中的。

NAME                                     READY   STATUS    RESTARTS   AGE
my-nginx-deployment-6b788b89c6-f69j7   0/1     Running   0          9m50s
my-nginx-deployment-6b788b89c6-m5qf6   0/1     Running   0          9m50s

因此,由于这些 Pod 还没有准备好,它们不应该为流量提供服务,但是当我准备好时

kubectl port-forward services/nginx-service 8086:8080

我能够在 http://127.0.0.1:8086/ 上获得 200 响应和 nginx 主页,我还可以查看有关服务流量的 pod 日志。

问题是,当就绪探测失败时,为什么 Pod 会提供流量。

PS:我已经使用 Kind 在我的机器上创建了集群

1 个答案:

答案 0 :(得分:1)

port-forward api 用于 Pod。 kubectl port-forward 命令只是使用 service 使其易于使用,但您的端口实际上已转发到 Pod - 因此 readiness 状态不适用。< /p>