【容器应用系列教程】Kubernetes配置映射ConfigMap

一、关于ConfigMap

以键值对存储业务的配置数据(密码、配置文件内容)

二、创建并使用ConfigMap

1.在ConfigMap通过键值对的方式定义

[root@k8s-master wordpress]# vim config-db.yml

apiVersion: v1
kind: ConfigMap
metadata:
    name: mysql-config
    namespace: wordpress
data:
    db_password: redhat
    db_name: jiaowu
[root@k8s-master wordpress]# kubectl create -f config-db.yml 
configmap/mysql-config created

查看ConfigMap

[root@k8s-master wordpress]# kubectl get cm -n wordpress
NAME               DATA   AGE
kube-root-ca.crt   1      3h19m
mysql-config       2      46s

查看ConfigMap的详细信息

[root@k8s-master wordpress]# kubectl describe cm mysql-config -n wordpress

Name:         mysql-config
Namespace:    wordpress
Labels:       <none>
Annotations:  <none>

Data
====
db_name:	#键
----
jiaowu	#对应的值

db_password:	#键
----
redhat	#对应的值
Events:  <none>
[root@k8s-master wordpress]# 

使用刚刚存的ConfigMap创建数据库Pod

[root@k8s-master wordpress]# vim config-mysql.yml

apiVersion: apps/v1
kind: Deployment
metadata:
    name: wp-mysql
    namespace: wordpress
spec:
    replicas: 1
    selector:
        matchLabels:
            app: wp-mysql
    strategy:
        type: RollingUpdate
        rollingUpdate:
            maxSurge: 1
            maxUnavailable: 0
    template:
        metadata:
            labels:
                app: wp-mysql
        spec:
            containers:
            - name: wp-mysql
              image: mysql:5.7
              imagePullPolicy: IfNotPresent
              ports:
              - containerPort: 3306
              env:
              - name: MYSQL_ROOT_PASSWORD
                valueFrom:
                    configMapKeyRef:	#配置变量取自ConfigMap
                            name: mysql-config	#刚刚创建的ConfigMap服务的名字
                            key: db_password	#对应的键
              - name: MYSQL_DATABASE
                valueFrom:
                    configMapKeyRef:
                            name: mysql-config
                            key: db_name
              resources:
                requests:
                  memory: "1G"
                  cpu: "1"
                limits:
                  memory: "2G"
                  cpu: "2"
---
apiVersion: v1
kind: Service
metadata:
    name: wp-mysql
    namespace: wordpress
spec:
    ports:
    - port: 3306
    selector:
      app: wp-mysql
[root@k8s-master wordpress]# kubectl create -f config-mysql.yml 
deployment.apps/wp-mysql created
service/wp-mysql created
[root@k8s-master wordpress]# kubectl get pods -n wordpress
NAME                        READY   STATUS    RESTARTS   AGE
wp-mysql-55858fbc65-m8vnl   1/1     Running   0          82s

进入容器内部验证

[root@k8s-master wordpress]# kubectl exec -it wp-mysql-55858fbc65-m8vnl -n wordpress -- bash
bash-4.2# mysql -uroot -predhat
mysql> 	#密码正确

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| jiaowu             |       #jiaowu库
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

2.在ConfigMap中定义配置文件

[root@k8s-master wordpress]# vim config-db.yml

apiVersion: v1
kind: ConfigMap
metadata:
    name: mysql-config
    namespace: wordpress
data:
    db_password: redhat
    db_name: jiaowu
    my.cnf: |	#定义配置文件名
        [mysqld]
        server_id=10
        log_bin=master
        gtid_mode=on	#开启事务
        enforce_gtid_consistency=true
        collation-server = utf8_general_ci
        character-set-server = utf8	#定义默认创建库的字符集为utf8
[root@k8s-master wordpress]# kubectl apply -f config-db.yml

查看ConfigMap详细信息

[root@k8s-master wordpress]# kubectl describe cm mysql-config -n wordpress
Name:         mysql-config
Namespace:    wordpress
Labels:       <none>
Annotations:  <none>

Data
====
db_name:
----
jiaowu

db_password:
----
redhat

my.cnf:
----
[mysqld]
server_id=10
log_bin=master
gtid_mode=on
enforce_gtid_consistency=true
collation-server = utf8_general_ci
character-set-server = utf8

挂载配置文件,创建Pod

[root@k8s-master wordpress]# vim config-mysql.yml

apiVersion: apps/v1
kind: Deployment
metadata:
    name: wp-mysql
    namespace: wordpress
spec:
    replicas: 1
    selector:
        matchLabels:
            app: wp-mysql
    strategy:
        type: RollingUpdate
        rollingUpdate:
            maxSurge: 1
            maxUnavailable: 0
    template:
        metadata:
            labels:
                app: wp-mysql
        spec:
            containers:
            - name: wp-mysql
              image: mysql:5.7
              imagePullPolicy: IfNotPresent
              ports:
              - containerPort: 3306
              env:
              - name: MYSQL_ROOT_PASSWORD
                valueFrom:
                    configMapKeyRef:
                            name: mysql-config
                            key: db_password
              resources:
                requests:
                  memory: "1G"
                  cpu: "1"
                limits:
                  memory: "2G"
                  cpu: "2"
              volumeMounts:	#挂载数据卷
                  - name: config-mysql-volume	#挂载自己定义的数据卷名
                    mountPath: "/etc/mysql/conf.d"	#挂载路径
                    readOnly: true	#开启只读
            volumes:
                - name: config-mysql-volume	#创建一个数据卷名
                  projected:
                      sources:
                      - configMap:	#定义ConfigMap类型
                           name: mysql-config	#指定数据卷挂载到哪个ConfigMap上
---
apiVersion: v1
kind: Service
metadata:
    name: wp-mysql
    namespace: wordpress
spec:
    ports:
    - port: 3306
    selector:
      app: wp-mysql
[root@k8s-master wordpress]# kubectl create -f config-mysql.yml 
deployment.apps/wp-mysql created
service/wp-mysql created
[root@k8s-master wordpress]# kubectl get pods -n wordpress
NAME                        READY   STATUS    RESTARTS   AGE
wp-mysql-68b5d74d89-gr25n   1/1     Running   0          4s

进入容器内部检查

[root@k8s-master wordpress]# kubectl exec -it wp-mysql-68b5d74d89-gr25n -n wordpress -- bash
bash-4.2# mysql -uroot -predhat

mysql> show master status\G;	#二进制日志是开启状态
*************************** 1. row ***************************
             File: master.000003
         Position: 194
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 799fd813-0b74-11ee-95c8-6e94f7388cb8:1-5
1 row in set (0.00 sec)

mysql> create database AA;
Query OK, 1 row affected (0.00 sec)

mysql> select schema_name,default_character_set_name from information_schema.schemata;
+--------------------+----------------------------+
| schema_name        | default_character_set_name |
+--------------------+----------------------------+
| information_schema | utf8                       |
| AA                 | utf8                       |	#所有库都是utf8字符集
| mysql              | utf8                       |
| performance_schema | utf8                       |
| sys                | utf8                       |
+--------------------+----------------------------+
5 rows in set (0.00 sec)