目录

kubectl-ConfigMap

ConfigMap概述

ConfigMap是什么

  • ConfigMap是在pod中映射(文件、目录)的一种方式,允许将配置文件与镜像文件分离,以使容器化的应用具有可移植性

    PS :ConfigMap的文件大小最大为1MB

ConfigMap的作用

  • 在日常工作中经常要修改各种配置文件的参数、数据库的地址、用户名密码等,这些操作在容器内非常麻烦,POD在重启或迁移时又会恢复到初始状态,使用ConfigMap就可以解决这样的问题

    PS:configmap一旦创建,后面源文件修改后configmap的内容不会跟随改变,可删除重新创建

例:将nginx配置文件映射到容器应用内

  1. 在本地创建、修改nginx的配置文件

       [root@k8s-master ~]# mkdir -p /var/webconf/
       [root@k8s-master var]# cd /var/webconf/
       [root@k8s-master webconf]# vim default.conf
    
  2. 创建configmap

       [root@k8s-master webconf]# kubectl create configmap nginx-conf --from-file=/var/webconf
       configmap/nginx-conf created
       [root@k8s-master webconf]# kubectl get configmaps
       NAME               DATA   AGE
       kube-root-ca.crt   1      13d
       nginx-conf         1      11s
    
  3. 使用configmap

    在资源文件中声明

       ---
       apiVersion: apps/v1
       kind: Deployment
       metadata:
         name: webnginx
         annotations:
           kubernetes.io/change-cause: nginx.v1
       spec:
         replicas: 4
         selector:
           matchLabels:
             app: nginx
         template:
           metadata:
             labels:
               app: nginx
           spec:
             volumes:                #pod定义存储卷
             - name: nginx-php           #定义存储卷名称
               configMap:
                 name: nginx-conf        #选择configMap
             containers:
             - name: nginx
               image: hub.godone.top/pub/nginx:v1
               imagePullPolicy: IfNotPresent
               volumeMounts:           #使用存储卷
               - name: nginx-php         #选择存储卷
                 subPath: default.conf       #选择子路径(如果映射目录可不填)
                 mountPath: /etc/nginx/conf.d/default.conf   #映射到容器内的路径
               - name: nginx-php
                 subPath: fastcgi.conf
                 mountPath: /etc/nginx/fastcgi.conf   #映射到容器内的路径
               ports:
               - containerPort: 80
    

推荐文章

kubernetes-kubectl