ConfigMap 是一種 API 對象,用來將非機密性的數據保存到鍵值對中。使用時, Pods 可以將其用作環境變量、命令行參數或者存儲卷中的配置文件。
以下以nginx鏡像提供配置文件為例鏡像演示,是將ConfigMap 以卷的形式掛載到Pod中
1.編寫congfigmap, serice,Deployment yaml文件
root@k8-deploy:~/k8s-yaml/volume# vim configmap.yml
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-config
data:
default: |
server {
listen 80;
server_name www.yanql.com;
index index.html;
location / {
root /data/nginx/html;
if (!-e $request_filename) {
rewrite ^/(.*) /index.html last;
}
}
}
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 1
selector:
matchLabels:
app: ng-deploy-80
template:
metadata:
labels:
app: ng-deploy-80
spec:
containers:
- name: ng-deploy-80
image: nginx:1.17.10
ports:
- containerPort: 80
volumeMounts:
- mountPath: /data/nginx/html
name: nginx-static-dir
- name: nginx-config
mountPath: /etc/nginx/conf.d
volumes:
- name: nginx-static-dir
hostPath:
path: /data/nginx/html
- name: nginx-config
configMap:
name: nginx-config
items:
- key: default
path: www.yanql.com.conf
---
apiVersion: v1
kind: Service
metadata:
name: ng-deploy-80
spec:
ports:
- name: http
port: 81
targetPort: 80
nodePort: 30080
protocol: TCP
type: NodePort
selector:
app: ng-deploy-80
2.使用yaml文件創建資源
root@k8-deploy:~/k8s-yaml/volume# kubectl apply -f configmap.yml
configmap/nginx-config created
deployment.apps/nginx-deployment created
service/ng-deploy-80 created
3.查看資源是否創建完成
root@k8-deploy:~/k8s-yaml/volume# kubectl get configmap
NAME DATA AGE
kube-root-ca.crt 1 26d
nginx-config 1 89s
root@k8-deploy:~/k8s-yaml/volume# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 26d
ng-deploy-80 NodePort 10.0.41.43 <none> 81:30080/TCP 97s
root@k8-deploy:~/k8s-yaml/volume# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 1/1 1 1 105s
4.驗證配置文件在pod中是否掛載了configmap提供的配置文件
root@k8-deploy:~/k8s-yaml/volume# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-deployment-55cb7cdfff-gkdcf 1/1 Running 0 14m
root@k8-deploy:~/k8s-yaml/volume# kubectl exec nginx-deployment-55cb7cdfff-gkdcf -it -- bash
root@nginx-deployment-55cb7cdfff-gkdcf:/# cat /etc/nginx/conf.d/www.yanql.com.conf
server {
listen 80;
server_name www.yanql.com;
index index.html;
location / {
root /data/nginx/html;
if (!-e $request_filename) {
rewrite ^/(.*) /index.html last;
}
}
}
5.通過訪問serice的nodePort端口驗證nginx服務是否正常()
# 3個node節點
root@k8-deploy:~/k8s-yaml/volume# curl 192.168.2.17:30080
www.yanql.com Mon Oct 11 03:31:35 UTC 2021
root@k8-deploy:~/k8s-yaml/volume# curl 192.168.2.18:30080
www.yanql.com Mon Oct 11 03:31:35 UTC 2021
root@k8-deploy:~/k8s-yaml/volume# curl 192.168.2.19:30080
www.yanql.com Mon Oct 11 03:31:35 UTC 2021
6.動態修改configmap中配置文件,pod中掛載的配置文件會同步修改。
# 找到需要修改的configmap
root@k8-deploy:~/k8s-yaml/volume# kubectl get configmap
NAME DATA AGE
kube-root-ca.crt 1 26d
nginx-config 1 31m
# 修改configmap
root@k8-deploy:~/k8s-yaml/volume# kubectl edit configmap nginx-config
configmap也可以使用create創建,使用--from-file指定配置文件的內容。
詳情參考官網文檔:https://kubernetes.io/zh/docs/tasks/configure-pod-container/configure-pod-configmap/
可以以使用 kubectl create configmap 命令基於 目錄、文件 或者字面值來創建 ConfigMap
kubectl create configmap <map-name> <data-source>
基於目錄創建 ConfigMap
# 創建本地目錄
mkdir -p configure-pod-container/configmap/
# 將實例文件下載到 `configure-pod-container/configmap/` 目錄
wget https://kubernetes.io/examples/configmap/game.properties -O configure-pod-container/configmap/game.properties
wget https://kubernetes.io/examples/configmap/ui.properties -O configure-pod-container/configmap/ui.properties
# 創建 configmap
kubectl create configmap game-config --from-file=configure-pod-container/configmap/
可以使用下面的命令顯示 ConfigMap 的詳細信息
kubectl describe configmaps game-config
基於單個文件或多個文件創建 ConfigMap
kubectl create configmap game-config-2 --from-file=configure-pod-container/configmap/game.properties
kubectl create configmap game-config-2 --from-file=configure-pod-container/configmap/game.properties --from-file=configure-pod-container/configmap/ui.properties
將configmap導出到yaml
kubectl get configmap config-multi-env-files -o yaml
定義從文件創建 ConfigMap 時要使用的鍵
kubectl create configmap game-config-3 --from-file=<my-key-name>=<path-to-file>