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

时间:2018-01-08 12:16:59

标签: kubernetes

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

1 个答案:

答案 0 :(得分:3)

是的,这是可能的。

使用this post to create hooks中的示例,让我们读取一个秘密并将其作为环境变量传递给容器,稍后在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的内容,您可以看到显示的秘密

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