https://kubernetes.io/docs/concepts/configuration/configmap/
定义一个ConfigMap
apiVersion: v1 # ConfigMap的 api version
kind: ConfigMap # 资源类型
metadata: # 元数据
name: game-demo # 名称
data: # data是配置文件的内容。有两种形式,键/值形式和文件形式
# property-like keys; each key maps to a simple value
# 键: 值 形式
player_initial_lives: "3"
ui_properties_file_name: "user-interface.properties"
# file-like keys
# 文件形式,文件形式需要注意的是,文件名冒号后有一个“|”符号,说明另起一行才是配置文件内容。
game.properties: |
enemy.types=aliens,monsters
player.maximum-lives=5
user-interface.properties: |
color.good=purple
color.bad=yellow
allow.textmode=true
Pod中引用ConfigMap
apiVersion: v1
kind: Pod
metadata:
name: configmap-demo-pod
spec:
containers:
- name: demo
image: alpine
command: ["sleep", "3600"]
env:
# 以env环境变量的形式引用ConfigMap中键值形式的配置。
# Define the environment variable
# - name 自定义一个容器内的环境变量PLAYER_INITIAL_LIVES
- name: PLAYER_INITIAL_LIVES # Notice that the case is different here
# from the key name in the ConfigMap.
# PLAYER_INITIAL_LIVES自定义的容器内的环境变量名称
valueFrom: # valueFrom 字段配置这个环境变量的值从哪里来
configMapKeyRef: # configMapKeyRef指出是从ConfigMap继承值
name: game-demo # ConfigMap的名称.
key: player_initial_lives # ConfigMap game-demo 中配置的键值形式的键名.
- name: UI_PROPERTIES_FILE_NAME
valueFrom:
configMapKeyRef:
name: game-demo
key: ui_properties_file_name
volumeMounts:
# volumeMounts 字段定义要用哪个volumes
- name: config # 引用名为config的volumes
mountPath: "/config" # 挂载到容器内的路径
readOnly: true # 只读
# ConfigMap在Pod中引用的时候是利用volumes字段中配置,volumes是和containers字段平级的
volumes:
# You set volumes at the Pod level, then mount them into containers inside that Pod
# 定义一个卷
- name: config # 给卷起一个名,containers里引用ConfigMap时需要通过这个卷名引用
configMap: # 卷类型,这里指定 ConfigMap
# Provide the name of the ConfigMap you want to mount.
# name 字段配置我们需要用到的ConfigMap的名字,这里是game-demo
name: game-demo
# An array of keys from the ConfigMap to create as files
# items里可以配置configMap中的key或者配置文件在挂到容器内部使用什么名称或者权限
items:
- key: "game.properties" # configMap中的key名称,要挂载谁?
path: "game.properties" # 挂到容器内的名称,可自定义,要挂载的名称叫什么?
mode: 0644 # 挂载容器内的文件的权限,优先级高于defaultMode
- key: "user-interface.properties"
path: "user-interface.properties"
defaulMode: 0666 # 挂到容器内之后配置文件的权限