为什么“活力/准备就绪”探针失败?

时间:2020-03-07 12:41:30

标签: java spring-boot kubernetes deployment kubernetes-helm

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

“活动探测失败:获取http://172.17.0.7:80/:拨打tcp 172.17.0.7:80:连接:连接被拒绝”和“就绪探测器失败:获取http://172.17.0.7:80/:拨打TCP 172.17.0.7:80:连接: 连接被拒绝”

这是我的deployment.yaml:

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 个答案:

答案 0 :(得分:1)

因为我可以拉出图像,所以我尝试了

$ 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已准备就绪。

答案 1 :(得分:0)

连接被拒绝意味着容器没有在端口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将HTTP GET请求发送到运行在Container中并侦听端口80的服务器。如果服务器的/路径的处理程序返回成功代码,则kubelet认为使容器保持健康。如果处理程序返回失败代码,则kubelet将杀死Container并重新启动它。

因此,您的代码中没有处理程序,该处理程序返回路径/的成功代码。由于它是Spring Boot应用程序,因此如果您在pom中具有spring boot致动器依赖性,则可以将路径更改为/actuator/health,这应该可以解决该问题。