一、创建ConfigMap
apiVersion: v1
kind: ConfigMap
metadata:
name: dataflow-config
data:
loglevel: info
env: test
二、引入指令配置到容器环境变量
apiVersion: v1
kind: Pod
metadata:
name: test-configmap-pod
labels:
name: test-configmap-pod
spec:
containers:
- name: test-configmap-pod
image: busybox
resources:
limits:
memory: "128Mi"
cpu: "500m"
command: ['/bin/sh','-c','env']
ports:
- containerPort: 80
env: # 单个环境变量
- name: loglevel
valueFrom:
configMapKeyRef:
name: dataflow-config
key: loglevel
- name: profile
valueFrom:
configMapKeyRef:
name: dataflow-config
key: env
三、引入所有配置到容器环境变量
apiVersion: v1
kind: Pod
metadata:
name: test-configmap-pod2
labels:
name: test-configmap-pod2
spec:
containers:
- name: test-configmap-pod2
image: busybox
resources:
limits:
memory: "128Mi"
cpu: "500m"
command: ['/bin/sh','-c','env']
ports:
- containerPort: 80
envFrom: # 批量引入环境变量
- configMapRef:
name: dataflow-config
prefix: dataflow.
restartPolicy: Never
四、配置挂载每个配置为一个文件
将会在 /etc/dataflow/loglevel
的文件中记录配置的内容
apiVersion: v1
kind: Pod
metadata:
name: test-configmap-pod3
labels:
name: test-configmap-pod3
spec:
containers:
- name: test-configmap-pod3
image: busybox
resources:
limits:
memory: "128Mi"
cpu: "500m"
command: ['/bin/sh','-c','cat /etc/dataflow/loglevel']
ports:
- containerPort: 80
volumeMounts:
- mountPath: /etc/dataflow
name: frontend-vol
restartPolicy: Never
volumes:
- name: frontend-vol
configMap: # 使用挂载volume方式引入配置
name: dataflow-config
items:
- key: loglevel
path: loglevel
指定了items将会只创建指定的配置文件,如果不指定items,将会configMap中所有的配置项都分别创建配置文件。
五、将POD信息作为环境变量
apiVersion: v1
kind: Pod
metadata:
name: test-configmap-pod5
labels:
name: test-configmap-pod5
spec:
containers:
- name: test-configmap-pod5
image: busybox
resources:
limits:
memory: "128Mi"
cpu: "500m"
command: ['/bin/sh','-c','env']
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
supports metadata.name
, metadata.namespace
, metadata.labels['<KEY>']
, metadata.annotations['<KEY>']
, spec.nodeName
, spec.serviceAccountName
, status.hostIP
, status.podIP
, status.podIPs
.
六、将POD资源作为环境变量
apiVersion: v1
kind: Pod
metadata:
name: test-configmap-pod6
labels:
name: test-configmap-pod6
spec:
containers:
- name: test-configmap-pod6
image: busybox
resources:
requests:
memory: "32Mi"
limits:
memory: "128Mi"
cpu: "500m"
command: ['/bin/sh','-c','env']
env:
- name: CPU_LIMIT
valueFrom:
resourceFieldRef:
resource: limits.cpu
- name: MEMORY_REQUSETS
valueFrom:
resourceFieldRef:
resource: requests.memory
- name: MEMORY_LIMIT
valueFrom:
resourceFieldRef:
resource: limits.memory
- name: POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
七、将POD 信息挂载为文件
apiVersion: v1
kind: Pod
metadata:
name: test-configmap-pod7
labels:
name: test-configmap-pod7
owner: yangyan
spec:
containers:
- name: test-configmap-pod7
image: busybox
resources:
requests:
memory: "32Mi"
limits:
memory: "128Mi"
cpu: "500m"
command: ['/bin/sh','-c','cat /etc/pod/*']
volumeMounts:
- mountPath: /etc/pod
name: podinfo
readOnly: false
volumes:
- name: podinfo
downwardAPI:
items:
- path: labels
fieldRef:
fieldPath: metadata.labels
- path: annotations
fieldRef:
fieldPath: metadata.annotations
restartPolicy: Never
了解 工作生活心情记忆 的更多信息
Subscribe to get the latest posts sent to your email.