我可以在 postStart 命令中使用 env

2024-04-09

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


对的,这是可能的。

使用来自的示例这篇文章创建钩子 https://blog.openshift.com/kubernetes-pods-life/,让我们读取一个秘密并将其作为环境变量传递给容器,以便稍后在postStart hook.

--- 
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,你可以看到正在显示的秘密

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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

我可以在 postStart 命令中使用 env 的相关文章

随机推荐