带有秘密的 livenessProbe 在 Kubernetes 中不起作用

mag*_*gic 7 spring-boot kubernetes kubernetes-secrets

我正在尝试在我的 kubernetes 部署 yaml 文件中传递 livenessProbe 以执行我的应用程序的运行状况。所以,我用令牌值创建了一个秘密,并按如下方式传递

      livenessProbe:
        httpGet:
          path: test/actuator/health
          port: 9001
          httpHeaders:
          - name: Authorization
            valueFrom:
              secretKeyRef:
                name: actuator-token
                value: token
Run Code Online (Sandbox Code Playgroud)

但我收到以下错误

error: error validating "deployment.yaml": error validating data: [ValidationError(Deployment.spec.template.spec.containers[0].livenessProbe.httpGet.httpHeaders[0]): unknown field "valueFrom" in io.k8s.api.core.v1.HTTPHeader, ValidationError(Deployment.spec.template.spec.containers[0].livenessProbe.httpGet.httpHeaders[0]): missing required field "value" in io.k8s.api.core.v1.HTTPHeader, ValidationError(Deployment.spec.template.spec.containers[0].readinessProbe.httpGet.httpHeaders): invalid type for io.k8s.api.core.v1.HTTPGetAction.httpHeaders: got "map", expected "array"]; if you choose to ignore these errors, turn validation off with --validate=false

请建议并感谢您的帮助。

另外让我们知道他们有什么更好的处理令牌的方法,因为我不想直接在我的部署 yaml 文件上提供令牌值。

Sta*_*kov 7

不确定@DT 答案是否有效,没有该功能的文档。

我还做了一些测试,下面的例子对我不起作用:

spec:
  containers:
  - name: mycontainer
    image: myimage
    env:
      - name: TOKEN
        value: '12345'
    livenessProbe:
      httpGet:
        path: /v1/health
        port: 80
        httpHeaders:
        - name: Authorization
          value: Apikey $TOKEN
Run Code Online (Sandbox Code Playgroud)

我的应用程序收到 401 错误,因为它无法用 env 变量替换标头值。我什至尝试了许多其他带有单/双​​引号、括号的选项,但没有一个起作用。

否则,您可以使用exec代替httpGet,但它需要在您的 docker 映像中安装curl。

spec:
  containers:
  - name: mycontainer
    image: myimage
    env:
      - name: TOKEN
        value: '12345'
    livenessProbe:
      exec:
        command:
        - bash
        - -c
        - 'curl --fail http://localhost/v1/health --header "Authorization: Apikey $TOKEN"'
    initialDelaySeconds: 30
    periodSeconds: 15
Run Code Online (Sandbox Code Playgroud)

如果您想valueFrom从您的秘密中使用,则无需解码容器内的变量。我已经被解码了。

如果您无法将curl包添加到您的映像中,最好考虑根据您的应用程序开发的语言编写自定义脚本。这是js的示例: https: //blog.sixeyed.com/docker-healthchecks-why-not-to-use-curl-or-iwr/

另外,检查这个问题,有一个类似的答案How to use basic authentication in a HTTP livenessprobe in Kubernetes?


DT.*_*DT. 6

httpHeaders只支持valuename字段不处理valueFrom

$ kubectl explain pod.spec.containers.livenessProbe.httpGet.httpHeaders

KIND:     Pod
VERSION:  v1

RESOURCE: httpHeaders <[]Object>

DESCRIPTION:
     Custom headers to set in the request. HTTP allows repeated headers.

     HTTPHeader describes a custom header to be used in HTTP probes

FIELDS:
   name <string> -required-
     The header field name

   value        <string> -required-
     The header field value
Run Code Online (Sandbox Code Playgroud)

您可以尝试使用 env 变量之类的。

spec:
  containers:
  - name: mycontainer
    image: myimage
    env:
      - name: MY_SECRET
        valueFrom:
          secretKeyRef:
            name: actuator-token
            key: token
    livenessProbe:
        httpGet:
          path: test/actuator/health
          port: 9001
          httpHeaders:
          - name: Authorization
            value: $SECRET
Run Code Online (Sandbox Code Playgroud)