最近,想在生产环境使用k8s。但是涉及到多套环境,突如其来的想知道下configmap挂载的时机。
猜想:
configmap和pod的关系
- configmap更新会自动同步到pod里面
- configmap只是在pod创建的时候挂载,configmap后续不论怎么变化都是最初挂载的值
configmap和deployment的关系
- 创建deployment,后续所有pod都与创建deployment时的configmap相同
- deployment创建pod时,根据configmap当时的值挂载
- deployment创建的pod会随configmap变化而变化
怀着这疑问,做了下实验:
这是实验yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: game-demo
data:
player_initial_lives: "3"
ui_properties_file_name: "user-interface.properties"
game.properties: |
enemy.types=aliens,monsters
player.maximum-lives=5
user-interface.properties: |
color.good=purple
color.bad=yellow
allow.textmode=true
---
apiVersion: v1
kind: Pod
metadata:
name: configmap-demo-pod
spec:
containers:
- name: demo
image: alpine
imagePullPolicy: IfNotPresent
command: ["sleep", "3600"]
env:
- name: PLAYER_INITIAL_LIVES
valueFrom:
configMapKeyRef:
name: game-demo
key: player_initial_lives
- name: UI_PROPERTIES_FILE_NAME
valueFrom:
configMapKeyRef:
name: game-demo
key: ui_properties_file_name
volumeMounts:
- name: config
mountPath: "/config"
readOnly: true
volumes:
- name: config
configMap:
name: game-demo
items:
- key: "game.properties"
path: "game.properties"
- key: "user-interface.properties"
path: "user-interface.properties"
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
env:
- name: PLAYER_INITIAL_LIVES
valueFrom:
configMapKeyRef:
name: game-demo
key: player_initial_lives
- name: UI_PROPERTIES_FILE_NAME
valueFrom:
configMapKeyRef:
name: game-demo
key: ui_properties_file_name
volumeMounts:
- name: config
mountPath: "/config"
readOnly: true
volumes:
- name: config
configMap:
name: game-demo
items:
- key: "game.properties"
path: "game.properties"
- key: "user-interface.properties"
path: "user-interface.properties"
启动
验证pod,这时候的cm是3
改成4
pod结果还是3
所以pod中第二个猜想是正确的
验证deployment,当时cm是3,这时候cm已经改成4了但是没变
删除一个pod,红色为新生成的
进入新生产的pod,发现是4