命令行创建configmap
指定文件,输入多个路径
kubectl create configmap <map-name> <data-source>
kubectl create configmap [configmap_name] --from-file=<my-key-name>=<path-to-file>
kubectl create configmap game-config-3 --from-file=game-special-key=configure-pod-container/configmap/game.properties
kubectl create configmap my.cnf --from-file=my.cnf=my.cnf
kubectl create configmap [configmap对象]--from-file=< key >= < path >
subPath
替换文件,必须是pod中存在的,否则为一个空文件夹
test-configmap.yaml
(无法更新)
apiVersion: v1
kind: Pod
metadata:
name: test-configmap
spec:
containers:
- name: test-configmap-volume
image: busybox
args:
- sleep
- "86400"
volumeMounts:
- name: mysqlcfmap
mountPath: "/etc/my.cnf" # 挂载到容器中目录,这个目录会自动创建
subPath: my.cnf
volumes:
- name: mysqlcfmap
configMap:
name: my.cnf # 创建 configmap 对象的名称
items:
- key: my.cnf # 创建 configmap 对象时指定的 key
path: my.cnf #
# /etc/my.cnf # 普通文件
![](https://img.haomeiwen.com/i12075269/417950fd766603ad.png)
与本地my.cnf文件内容一致
configmap.yaml
(可以更新)
1 从普通文件创建
kubectl create configmap mysql-cnf --from-file=my-cnf=./my.cnf
2 从 yaml 文件创建
# ./kustomization.yaml
configMapGenerator:
- name: mysql-cnf
files:
- my-cnf=./my.cnf
kubectl apply -f ./kustomization.yaml
Pod使用
1. 更新
更新 Etcd 中配置数据
apiVersion: v1
kind: Pod
metadata:
name: test-configmap
spec:
containers:
- name: test-configmap-volume
image: busybox
args:
- sleep
- "86400"
volumeMounts:
- name: mysqlcfmap
mountPath: "/etc/config" # 挂载到容器中目录,这个目录会自动创建
volumes:
- name: mysqlcfmap
configMap:
name: mycnf # 创建 configmap 对象的名称
items:
- key: my-cnf # 创建 configmap 对象时指定的 key
path: my.cnf # 文件名
# /etc/config/my.cnf # 软链接
![](https://img.haomeiwen.com/i12075269/ca264b315e2516c2.png)
![](https://img.haomeiwen.com/i12075269/a15db8059a848fe9.png)
场景:你需要这个挂载配置文件,但你并不希望覆盖这个目录下的其他文件
apiVersion: v1
kind: Pod
metadata:
name: test-configmap
spec:
containers:
- name: test-configmap-volume
image: busybox
args:
- sleep
- "86400"
volumeMounts:
- name: mysqlcfmap
mountPath: "/etc/nginx/conf.d/" # 挂载到容器中目录,这个目录会自动创建
subPath: my.cnf
volumes:
- name: mysqlcfmap
configMap:
name: my.cnf # 创建 configmap 对象的名称
items:
- key: my.cnf # 创建 configmap 对象时指定的 key
path: my.cnf #
不影响其他配置文件
网友评论