有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么活动性/就绪性探测失败?

我正试图通过Helm charts将一个应用程序部署到Kubernetes集群。每次尝试部署应用程序时,我都会得到

"Liveness probe failed: Get http://172.17.0.7:80/: dial tcp 172.17.0.7:80: connect: connection refused" and "Readiness probe failed: Get http://172.17.0.7:80/: dial tcp 172.17.0.7:80: connect: connection refused"

这是我的部署。亚马尔:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "mychart.fullname" . }}
  labels:
    {{- include "mychart.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "mychart.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "mychart.selectorLabels" . | nindent 8 }}
    spec:
    {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
    {{- end }}
      serviceAccountName: {{ include "mychart.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: nikovlyubomir/docker-spring-boot:latest
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - name: http
              containerPort: 80
              protocol: TCP
          livenessProbe:
            initialDelaySeconds: 200
            httpGet:
              path: /
              port: 80
          readinessProbe:
            initialDelaySeconds: 200
            httpGet:
              path: /
              port: http
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
    {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
    {{- end }}



我读到可能的解决方案是在两个探测中添加更多的initialDelaySecond,但这仍然没有解决我的问题

有什么意见吗


共 (2) 个答案

  1. # 1 楼答案

    连接被拒绝意味着容器没有监听端口80。同样,当您设置http就绪探测或活动探测时,如下所示

    apiVersion: v1
    kind: Pod
    metadata:
      labels:
        test: liveness
      name: liveness-http
    spec:
      containers:
      - name: liveness
        image: k8s.gcr.io/liveness
        args:
        - /server
        livenessProbe:
          httpGet:
            path: /
            port: 80
          initialDelaySeconds: 3
          periodSeconds: 3
    

    为了执行探测,kubelet向在容器中运行并侦听端口80的服务器发送HTTP GET请求。如果服务器的/路径的处理程序返回一个成功代码,则kubelet会认为容器是活动的、健康的。如果处理程序返回失败代码,kubelet将终止容器并重新启动它

    因此,代码中没有返回路径^{成功代码的处理程序。由于它是一个spring boot应用程序,假设您在pom中有spring boot actuator依赖项,您可以将路径更改为/actuator/health,这应该可以解决问题

  2. # 2 楼答案

    既然我能把图像拉出来,我试了一下

    $ docker run -d nikovlyubomir/docker-spring-boot:latest
    9ac42a1228a610ae424217f9a2b93cabfe1d3141fe49e0665cc71cb8b2e3e0fd
    

    我有日志

    $ docker logs 9ac
    ...
    2020-03-08 02:02:30.552  INFO 1  - [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 1993 (http) with context path ''
    

    似乎申请是从1993港开始的,而不是80港

    然后检查容器中的端口和连接:

    $ docker exec -ti 9ac bash
    root@9ac42a1228a6:/# curl localhost:1993
    {"timestamp":"2020-03-08T02:03:12.104+0000","status":404,"error":"Not Found","message":"No message available","path":"/"}
    root@9ac42a1228a6:/# curl localhost:1993/actuator/health
    {"timestamp":"2020-03-08T02:04:01.348+0000","status":404,"error":"Not Found","message":"No message available","path":"/actuator/health"}
    root@9ac42a1228a6:/# curl localhost:80
    curl: (7) Failed to connect to localhost port 80: Connection refused
    root@9ac42a1228a6:/# curl localhost:80/actuator/health
    curl: (7) Failed to connect to localhost port 80: Connection refused
    

    因此,请确保正确设置了检查路径/或其他路径,并且端口801993已准备就绪