gpt4 book ai didi

kubernetes - 我可以在 postStart 命令中使用 env 吗

转载 作者:行者123 更新时间:2023-12-01 06:15:24 29 4
gpt4 key购买 nike

我可以在 lifecycl.postStart.exe.command 中使用环境变量吗?
我有一个必须在 postStart 命令中运行的脚本。
该命令包含一个 secret ,我可以使用 valueFrom 获取 env 的 secret ,并在 postStart 命令中使用 env 吗?

最佳答案

对的,这是可能的。

使用来自 this post to create hooks 的示例,让我们读取一个 secret 并将其作为环境变量传递给容器,以便稍后在 postStart 中读取它钩。

--- 
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: loap
spec:
replicas: 1
template:
metadata:
labels:
app: loap
spec:
containers:
-
command:
- sh
- "-c"
- "echo $(date +%s): START >> /loap/timing; sleep 10; echo $(date +%s): END >> /loap/timing;"
image: busybox
env:
- name: SECRET_THING
valueFrom:
secretKeyRef:
name: supersecret
key: password
lifecycle:
postStart:
exec:
command:
- sh
- "-c"
- "echo ${SECRET_THING} $(date +%s): POST-START >> /loap/timing"
preStop:
exec:
command:
- sh
- "-c"
- "echo $(date +%s): PRE-HOOK >> /loap/timing"
livenessProbe:
exec:
command:
- sh
- "-c"
- "echo $(date +%s): LIVENESS >> /loap/timing"
name: main
readinessProbe:
exec:
command:
- sh
- "-c"
- "echo $(date +%s): READINESS >> /loap/timing"
volumeMounts:
-
mountPath: /loap
name: timing
initContainers:
-
command:
- sh
- "-c"
- "echo $(date +%s): INIT >> /loap/timing"
image: busybox
name: init
volumeMounts:
-
mountPath: /loap
name: timing
volumes:
-
hostPath:
path: /tmp/loap
name: timing

如果您查看 /tmp/loap/timings 的内容,您可以看到正在显示的 secret
my-secret-password 1515415872: POST-START
1515415873: READINESS
1515415879: LIVENESS
1515415882: END
1515415908: START
my-secret-password 1515415908: POST-START
1515415909: LIVENESS
1515415913: READINESS
1515415918: END

关于kubernetes - 我可以在 postStart 命令中使用 env 吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48150137/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com