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

Min*_*ong 3 kubernetes

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

Jos*_*sto 6

对的,这是可能的。

使用这篇文章中的示例创建钩子,让我们读取一个秘密并将其作为环境变量传递给容器,以便稍后在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
Run Code Online (Sandbox Code Playgroud)

如果您检查 的内容/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
Run Code Online (Sandbox Code Playgroud)