51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

env注入configMap环境变量及字段引用 实战案例 valueFrom 字段

env注入cm环境变量及字段引用实战

需求,我们在 环境变量中 env 中需要引用 configMap 里边的一些值怎么办?

1-我们先来创建一个 configMap-env的 cm

[root@master231 /manifests/pod/cm/cm-env]#cat cm-env.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: cm-env
data:
  school: baimei

  class: linux86

  student.txt: |
     崔晨阳
     邓祥丞
     赵志勇

相关命令
	kubectl get cm nginx-conf 
	kubectl describe cm cm-env

2- 在 env中引用 configmap

[root@master231 /manifests/pod/cm/cm-env]#cat 19-pods-volumes-configMaps-env.yaml


apiVersion: v1
kind: Pod
metadata:
  name: baimei-games-cm-env-003
spec:
  nodeName: worker233
  containers:
  - name: games
    image: harbor.baimei.com/baimei-games/games:v0.5
    resources:
      requests:
        cpu: "500m"
        memory: "200M"
      limits:
        cpu: 1.5
        memory: "400M"
    env:
    - name: baimei_LINUX86_SHAHE_JIAOSHI02_CLASS
      valueFrom:
        configMapKeyRef:
           name: cm-env
           key: class
    - name: baimei_LINUX86_SHAHE_JIAOSHI03_school
      valueFrom:
        configMapKeyRef:
           name: cm-env
           key: school
    - name: baimei_linux86_shahe_jiaoshi03_student
      valueFrom:
        configMapKeyRef:
           name: cm-env
           key: student.txt
    - name: baimei_linux86_shahe_jiaoshi03_INFO_POD_NAME
      valueFrom:
        fieldRef:
           fieldPath: "metadata.name"
    - name: baimei_linux86_shahe_jiaoshi03_INFO_POD_NODENAME
      valueFrom:
        fieldRef:
           fieldPath: "spec.nodeName"
    - name: baimei_linux86_shahe_jiaoshi03_INFO_POD_IP
      valueFrom:
        fieldRef:
           fieldPath: "status.podIP"
    - name: baimei_linux86_shahe_jiaoshi03_INFO_RESOURCE_CPU
      valueFrom:
        resourceFieldRef:
           resource: "limits.cpu"
    - name: baimei_linux86_shahe_jiaoshi03_INFO_RESOURCE_MEMORY
      valueFrom:
        resourceFieldRef:
           resource: "requests.memory"

运行一下:

kubectl apply  -f 19-pods-volumes-configMaps-env.yaml

查看 pods 运行状态:

kubectl  get pods -o wide

查看 引用 confing 是否成功

kubectl exec -it baimei-games-cm-env-003  -- env

更多的 帮助信息:

kubectl explain po.spec.containers.env.valueFrom
赞(0)
未经允许不得转载:工具盒子 » env注入configMap环境变量及字段引用 实战案例 valueFrom 字段