7.1 k8s使用configmapg 給pod內的程式提供配置檔案
阿新 • • 發佈:2021-10-20
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>