简介
本文介绍K8S的ConfigMap的用法。
ConfigMap介绍
ConfigMap是k8s的配置管理组件,可以将配置以key-value的形式传递,通常用来保存不需要加密的配置信息,加密信息则需用到Secret。
ConfigMap可以保存单个属性,也可以保存配置文件。
使用ConfigMap的好处是:
- ConfigMap受K8S管理,可以在管理页面进行展示
- ConfigMap可以挂载单个文件,但PV不能挂载单个文件,只能挂载目录。(hostPath也可以挂载单个文件)。
推荐使用ConfigMap的场景
公共容器的部署,比如:mysql、redis等。
不推荐使用ConfigMap的场景
SpringCloud应用项目。SpringCloud项目推荐使用Nacos,功能更加强大。
用法1:用key-value配置
apiVersion: v1 kind: ConfigMap metadata: name: nacos-cm data: mysql.host: "mysql" mysql.db.name: "nacos_devtest" mysql.port: "3306" mysql.user: "nacos" mysql.password: "nacos" --- apiVersion: apps/v1 kind: StatefulSet metadata: name: nacos spec: podManagementPolicy: Parallel serviceName: nacos-headless replicas: 3 template: metadata: labels: app: nacos spec: containers: - name: nacos imagePullPolicy: Always image: nacos/nacos-server:latest resources: requests: memory: "2Gi" cpu: "500m" ports: - containerPort: 8848 name: client-port - containerPort: 9848 name: client-rpc - containerPort: 9849 name: raft-rpc - containerPort: 7848 name: old-raft-rpc env: - name: NACOS_REPLICAS value: "3" - name: SERVICE_NAME value: "nacos-headless" - name: DOMAIN_NAME value: "cluster.local" - name: POD_NAMESPACE valueFrom: fieldRef: apiVersion: v1 fieldPath: metadata.namespace - name: MYSQL_SERVICE_HOST valueFrom: configMapKeyRef: name: nacos-cm key: mysql.host - name: MYSQL_SERVICE_DB_NAME valueFrom: configMapKeyRef: name: nacos-cm key: mysql.db.name - name: MYSQL_SERVICE_PORT valueFrom: configMapKeyRef: name: nacos-cm key: mysql.port - name: MYSQL_SERVICE_USER valueFrom: configMapKeyRef: name: nacos-cm key: mysql.user - name: MYSQL_SERVICE_PASSWORD valueFrom: configMapKeyRef: name: nacos-cm key: mysql.password - name: SPRING_DATASOURCE_PLATFORM value: "mysql" - name: NACOS_SERVER_PORT value: "8848" - name: NACOS_APPLICATION_PORT value: "8848" - name: PREFER_HOST_MODE value: "hostname" volumeMounts: - name: data mountPath: /home/nacos/plugins/peer-finder subPath: peer-finder - name: data mountPath: /home/nacos/data subPath: data - name: data mountPath: /home/nacos/logs subPath: logs
请先
!