Java OpenResty Spring Spring Boot MySQL Redis MongoDB PostgreSQL Linux Android Nginx 面试 小程序 Arthas JVM AQS juc Kubernetes Docker DevOps


Kubernetes 设置 Pod 环境变量

Kubernetes 大约 3732 字

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

阅读 77 · 发布于 2022-11-04

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb

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

扫描二维码关注我
昵称:
随便看看 换一批