一.简单说明
我们在运行一个mysql服务时,mysql服务有两类重要的数据,一个是存储的数据、另一个是存储的配置文件。存储数据这里我们可以使用挂载PVC来实现持久化存储,配置文件这里我们如果实现和容器的解耦,可以采用configmap来实现。
二.ConfigMap介绍
2.1 ConfigMap作用和使用场景
- ConfigMap用于保存配置数据,以键值对的形式存储
- ConfigMap资源提供了向Pod注入配置数据的方法
- ConfigMap旨在让镜像与配置文件解耦,以实现镜像的可移植性和可复用性。
- 填充环境变量的值
- 填充卷的配置文件
2.2 ConfigMap的创建
这里有以下几种创建方式:
- 使用字面值创建
kubectl create configmap my-configmap --from-literal=key1=1 --from-literal=key2=2 [root@k8s001 ~]# kubectl get cm my-configmap -o yaml apiVersion: v1 data: key1: "1" key2: "2" kind: ConfigMap metadata: creationTimestamp: "2020-08-11T03:54:00Z" name: my-configmap namespace: default resourceVersion: "4745970" selfLink: /api/v1/namespaces/default/configmaps/my-configmap uid: 4a277575-db86-11ea-bb5f-6c4b9005d934
- 使用文件创建
[root@k8s001 ~]# cat configmap-file 10.10.2.2 k8s001 [root@k8s001 ~]# kubectl create cm my-configmap-file --from-file=configmap-file configmap/my-configmap-file created [root@k8s001 ~]# kubectl get cm my-configmap-file -o yaml apiVersion: v1 data: configmap-file: | 10.10.2.2 k8s001 kind: ConfigMap metadata: creationTimestamp: "2020-08-11T03:58:23Z" name: my-configmap-file namespace: default resourceVersion: "4747102" selfLink: /api/v1/namespaces/default/configmaps/my-configmap-file uid: e6c966b8-db86-11ea-bb5f-6c4b9005d934
- 使用目录创建
[root@k8s001 ~]# mkdir configmap-dir [root@k8s001 ~]# cp configmap-file ./configmap-dir/ [root@k8s001 ~]# kubectl create cm myconfigmap-dir --from-file=configmap-dir configmap/myconfigmap-dir created [root@k8s001 ~]# kubectl get cm myconfigmap-dir -o yaml apiVersion: v1 data: configmap-file: | 10.10.2.2 k8s001 kind: ConfigMap metadata: creationTimestamp: "2020-08-11T04:01:51Z" name: myconfigmap-dir namespace: default resourceVersion: "4748006" selfLink: /api/v1/namespaces/default/configmaps/myconfigmap-dir uid: 62fde3bb-db87-11ea-ab20-6c4b9005ca34
- 编写ConfigMap的yaml文件来创建
[root@k8s001 ~]# cat configmap_mysql.yaml apiVersion: v1 kind: ConfigMap metadata: name: mysql-config namespace: business data: mysqld.cnf: |- [mysqld] pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock datadir = /var/lib/mysql symbolic-links = 0 max_allowed_packet = 50M character_set_server = utf8 collation_server = utf8_general_ci group_concat_max_len = 102400 [client] default_character_set = utf8
2.3 如何使用ConfigMap
- 可以通过环境变量的形式直接传递给Pod
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: wf-mysql
spec:
containers:
- image: mysql:5.7
imagePullPolicy: IfNotPresent
name: mysql
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
configMapKeyRef:
name: mysql-credentials
key: rootpw
- name: MYSQL_USER
valueFrom:
configMapKeyRef:
name: mysql-credentials
key: user
- name: MYSQL_PASSWORD
valueFrom:
configMapKeyRef:
name: mysql-credentials
key: password
- 通过在Pod的命令行下运行命令方式
[k8s001 ~]# cat busybox.yaml
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: wf-mysql
spec:
containers:
- image: busybox
imagePullPolicy: IfNotPresent
command: ["/bin/sh", "-c", "echo ${test_env}"]
name: busybox
envFrom:
- configMapRef:
name: configmap-test
restartPolicy: Always
- 作为volume的方式挂载到Pod内
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: wf-mysql
spec:
containers:
- image: mysql:5.7
imagePullPolicy: IfNotPresent
name: mysql
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-credentials
key: rootpw
- name: MYSQL_USER
valueFrom:
secretKeyRef:
name: mysql-credentials
key: user
- name: MYSQL_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-credentials
key: password
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: config-volume
mountPath: /etc/mysql/mysql.conf.d
volumes:
- name: config-volume
configMap:
name: mysql-config