ConfigMap 的用法幾乎與 Secret 完全相同:你可以使用 kubectl create configmap 從文件或者目錄創(chuàng)建 ConfigMap嵌纲,也可以直接編寫 ConfigMap 對象的 YAML 文件父虑。
-
創(chuàng)建configmap
centos-mysql:latest
kubectl create configmap mysqlcfmap --from-file=./my.cnf
image.png -
查看configmap
kubectl get configmaps
image.png -
get yaml文件
kubectl get configmaps mysqlcfmap -o yaml
image.png my.cnf
文件中
創(chuàng)建一個my.cnf文件
[mysqld]
user=oyzx
port=3307
- 創(chuàng)建
mysql.yaml
文件
注意:(請仔細(xì)閱讀如下注意事項9窘!!)
- 由于考慮到簡單實驗,并未拉取真實mysql鏡像,在網(wǎng)絡(luò)良好的狀態(tài)下蛤高,可將鏡像
image: busybox
改為image: mysql:5.7
類似鏡像- 掛載目錄
mountPath: "/mysqlcfmap"
,如果掛載的是文件夾肮塞,當(dāng)該文件夾襟齿,如/mysqlcfmap
中有內(nèi)容時,將會把該文件夾下所有文件覆蓋枕赵,只留下my.cnf
猜欺,如果掛載的是mountPath: "/mysqlcfmap/my.cnf"
,則會自動創(chuàng)建一個my.cnf的文件夾拷窜,再將my.cnf文件放進(jìn)去开皿。
apiVersion: v1
kind: Pod
metadata:
name: test-configmap
spec:
containers:
- name: test-configmap-volume
image: busybox
args:
- sleep
- "86400"
volumeMounts:
- name: mysqlcfmap
mountPath: "/mysqlcfmap"
readOnly: true
volumes:
- name: mysqlcfmap
configMap:
name: mysqlcfmap
創(chuàng)建
kubectl create -f mysql.yaml
-
查看是否創(chuàng)建成功
kubectl get pods
image.png -
進(jìn)入容器
kubectl exec -it test-configmap sh
可看到my.cnf文件
image.png
修改掛載覆蓋目錄的問題
- 刪除原始容器
kubectl delete -f mysql.yam
image.png - 刪除configmap中的
mysqlcnf
kubectl delete configmaps mysqlcnf
image.png - 自定義key方式重新創(chuàng)建configmap
kubectl create configmap mysqlcnf --from-file=mysqlcnf=./my.cnf
image.png - 查看創(chuàng)建的configmap
kubectl get configmaps
image.png - 修改yaml文件
apiVersion: v1
kind: Pod
metadata:
name: test-configmap
spec:
containers:
- name: test-configmap-volume
image: busybox
args:
- sleep
- "86400"
volumeMounts:
- name: mysqlcfmap
mountPath: "/mysqlcfmap" # 掛載到容器中目錄,這個目錄會自動創(chuàng)建
volumes:
- name: mysqlcfmap
configMap:
name: mysqlcnf # 創(chuàng)建 configmap 對象的名稱
items:
- key: my.cnf # 創(chuàng)建 configmap 對象時指定的 key
path: my.cnf # 容器 a 目錄中的文件名
- 創(chuàng)建容器
kubectl create -f mysql.yaml
image.png
1 從普通文件創(chuàng)建
kubectl create configmap mysql-cnf --from-file=my-cnf=./my.cnf
2 從 yaml 文件創(chuàng)建
# ./kustomization.yaml
configMapGenerator:
- name: mysql-cnf
files:
- my-cnf=./my.cnf
kubectl apply -f ./kustomization.yaml
Pod使用
1. 更新
更新 Etcd 中配置數(shù)據(jù)
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" # 掛載到容器中目錄篮昧,這個目錄會自動創(chuàng)建
volumes:
- name: mysqlcfmap
configMap:
name: mysql-cnf # 創(chuàng)建 configmap 對象的名稱
items:
- key: my-cnf # 創(chuàng)建 configmap 對象時指定的 key
path: my.cnf # 容器 a 目錄中的文件名
# /etc/config/my.cnf # 軟鏈接
-------------------
不更新
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" # 掛載到容器中目錄赋荆,這個目錄會自動創(chuàng)建
subPath: my.cnf
volumes:
- name: mysqlcfmap
configMap:
name: mysql-cnf # 創(chuàng)建 configmap 對象的名稱
items:
- key: my-cnf # 創(chuàng)建 configmap 對象時指定的 key
path: my.cnf # 容器 a 目錄中的文件名
# /etc/my.cnf # 普通文件
image.png
-
更新
image.png
修改配置文件my.cnf,再重新生成configmap后可自動更新在容器中得到新的my.cnf文件內(nèi)容
image.png -
不更新
image.png