Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Kubernetes 我可以在postStart命令中使用env吗_Kubernetes - Fatal编程技术网

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

Kubernetes 我可以在postStart命令中使用env吗,kubernetes,Kubernetes,我可以在lifecycl.postStart.exe.command中使用环境变量吗? 我有一个脚本必须在postStart命令中运行。 该命令包含一个密码,我是否可以使用valueFrom将密码获取到env,并在postStart命令中使用env?是的,这是可能的 使用中的示例,让我们读取一个secret并将其作为环境变量传递给容器,以便稍后在postStarthook中读取它 --- apiVersion: apps/v1beta1 kind: Deployment metadata:

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

是的,这是可能的

使用中的示例,让我们读取一个secret并将其作为环境变量传递给容器,以便稍后在
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

谢谢你!