7.1 k8s使用configmapg 给pod内的程序提供配置文件


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>


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM