[英]livenessProbe with secret not working in kubernetes
我正在嘗試在我的 kubernetes 部署 yaml 文件中傳遞 livenessProbe 以執行我的應用程序的運行狀況。 所以,我用令牌值創建了一個秘密,並按如下方式傳遞
livenessProbe:
httpGet:
path: test/actuator/health
port: 9001
httpHeaders:
- name: Authorization
valueFrom:
secretKeyRef:
name: actuator-token
value: token
但我收到以下錯誤
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 文件上提供令牌值。
httpHeaders
只支持value
和name
字段不處理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
您可以嘗試使用 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
不確定@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
我的應用程序得到 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
如果你想從你的秘密中使用valueFrom
,你不需要解碼容器內的變量。 我會已經被解碼了。
如果您無法將 curl 包添加到您的圖像,最好考慮根據您的應用程序開發的語言編寫自定義腳本。 這是 js 的示例: https : //blog.sixeyed.com/docker-healthchecks-why-not-to-use-curl-or-iwr/
另外,檢查這個問題,有一個類似的答案How to use basic authentication in a HTTP liveness probe in Kubernetes?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.