Kubernetes 设置 Pod 环境变量

Kubernetes About 3,732 words

name-value

apiVersion: v1
kind: Pod
metadata:
  name: pod-nginx
  namespace: dev
spec:
  containers:
    - image: nginx:1.23.1-alpine
      name: container-nginx
      ports:
        - name: port-nginx
          containerPort: 80
          protocol: TCP
      env:
        - name: MY_NAME
          value: "ZHANG SAN"

valueFrom

fieldRef

apiVersion: v1
kind: Pod
metadata:
  name: pod-nginx
  namespace: dev
spec:
  containers:
    - image: nginx:1.23.1-alpine
      name: container-nginx
      ports:
        - name: port-nginx
          containerPort: 80
          protocol: TCP
      env:
        - name: MY_POD_SERVICE_ACCOUNT
          valueFrom:
            fieldRef:
              fieldPath: spec.serviceAccountName

resourceFieldRef

apiVersion: v1
kind: Pod
metadata:
  name: pod-nginx
  namespace: dev
spec:
  containers:
    - image: nginx:1.23.1-alpine
      name: container-nginx
      ports:
        - name: port-nginx
          containerPort: 80
          protocol: TCP
      env:
        - name: MY_CPU_REQUEST
          valueFrom:
            resourceFieldRef:
              containerName: container-nginx
              resource: requests.cpu
        - name: MY_CPU_LIMIT
          valueFrom:
            resourceFieldRef:
              containerName: container-nginx
              resource: limits.cpu      

configMapKeyRef

apiVersion: v1
kind: Pod
metadata:
  name: pod-nginx
  namespace: dev
spec:
  containers:
    - image: nginx:1.23.1-alpine
      name: container-nginx
      ports:
        - name: port-nginx
          containerPort: 80
          protocol: TCP
      env:
        - name: CONFIGMAP_USERNAME
          valueFrom:
            configMapKeyRef:
              name: myconfigmap
              key: username
        - name: CONFIGMAP_PASSWORD
          valueFrom:
            configMapKeyRef:
              name: myconfigmap
              key: password

---

apiVersion: v1
kind: ConfigMap
metadata:
  name: myconfigmap
data:
  username: zhangsan
  password: pwd123

secretKeyRef

apiVersion: v1
kind: Pod
metadata:
  name: pod-nginx
  namespace: dev
spec:
  containers:
    - image: nginx:1.23.1-alpine
      name: container-nginx
      ports:
        - name: port-nginx
          containerPort: 80
          protocol: TCP
      env:
        - name: SECRET_USERNAME
          valueFrom:
            secretKeyRef:
              name: mysecret
              key: username
              optional: false
        - name: SECRET_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysecret
              key: password
              optional: false

---

apiVersion: v1
kind: Secret
metadata:
  name: mysecret
type: Opaque
data:
  username: YWRtaW4=
  password: MWYyZDFlMmU2N2Rm

envFrom

configMapRef secretRef

apiVersion: v1
kind: Pod
metadata:
  name: pod-nginx
  namespace: dev
spec:
  containers:
    - image: nginx:1.23.1-alpine
      name: container-nginx
      ports:
        - name: port-nginx
          containerPort: 80
          protocol: TCP
      envFrom:
        - configMapRef:
            name: yourconfigmap
        - secretRef:
            name: yoursecret

---

apiVersion: v1
kind: Secret
metadata:
  name: mysecret
type: Opaque
data:
  username: YWRtaW4=
  password: MWYyZDFlMmU2N2Rm

---

apiVersion: v1
kind: Secret
metadata:
  name: yoursecret
type: Opaque
data:
  age: YWRtaW4=
  address: MWYyZDFlMmU2N2Rm

参考

https://kubernetes.io/docs/tasks/inject-data-application/define-environment-variable-container

https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information

https://kubernetes.io/docs/concepts/configuration/secret/#using-secrets-as-environment-variables

https://kubernetes.io/docs/concepts/configuration/configmap

Views: 1,073 · Posted: 2022-11-04

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh