k8s 中 configMap 的日常使用

在和 k8s 的日常玩耍中,经常需要变更某些服务容器的配置文件的啦,Kubernetes 就提供了非常灵活的模块化方式,即 configMap

介绍

ConfigMap 是一种 API 对象,用来将非机密性的数据保存到键值对中。使用时, Pods 可以将其用作环境变量、命令行参数或者存储卷中的配置文件。

ConfigMap 将您的环境配置信息和容器镜像解耦,便于应用配置的修改。

当然 ConfigMap 不保证数据的隐私性,如果有重要数据请存放在 Secret 中

创建 ConfigMap

使用 kubectl create configmap 命令

来基于目录、文件、或者字面值来创建 ConfigMap:

1
kubectl create configmap <映射名称> <数据源>

其中,<映射名称> 是为 ConfigMap 指定的名称,<数据源> 是要从中提取数据的目录、 文件或者字面值。 ConfigMap 对象的名称必须是合法的 DNS 子域名

在你基于文件来创建 ConfigMap 时,<数据源> 中的键名默认取自文件的基本名, 而对应的值则默认为文件的内容

示例:创建 synthetic-monitoring(grafana 的一个插件)的配置文件的 configMap

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#先创建这个配置文件
cat >> synthetic-monitoring-app-grafana.yaml << EOF
apiVersion: 1
apps:
- type: grafana-synthetic-monitoring-app
name: grafana-synthetic-monitoring-app
disabled: false
jsonData:
apiHost: https://synthetic-monitoring-api.grafana.net/
stackId: xxxxxx
logs:
grafanaName: grafanacloud-sunnyrian-logs
hostedId: xxxxxx
metrics:
grafanaName: grafanacloud-sunnyrian-prom
hostedId: xxxxxx
secureJsonData:
publisherToken: <your Token>

datasources:
- name: grafanacloud-sunnyrian-logs
type: loki
access: proxy
url: https://logs-prod-us-central1.grafana.net
basicAuth: true
basicAuthUser: xxxxxx
jsonData:
maxLines: 1000
secureJsonData:
basicAuthPassword: <your Token>
version: 1

- name: grafanacloud-sunnyrian-prom
type: prometheus
access: proxy
url: https://prometheus-us-central1.grafana.net/api/prom
basicAuth: true
basicAuthUser: xxxxxx
jsonData:
timeInterval: 1s
secureJsonData:
basicAuthPassword: <your Token>
version: 1
EOF

然后创建 configMap

1
kubectl create configmap synthetic-configmap --from-file=./synthetic-monitoring-app-grafana.yaml -n kube-system

(optional) 也可以同时将多个文件生成一个 configMap: –from-env-file=file1 –from-env-file=file2

其中 synthetic-configmap 为该 configMap 的名字,然后选择从文件生成,生成到 kube-system 的命名空间

这里指定命名空间很重要

基于现有 configMap 生成 yaml

下面的指令可以把刚刚生成的 configMap 打印成yaml格式

1
kubectl get configmap synthetic-monitoring-app-grafana -o yaml -n kube-system

然后再导成yaml就可以啦

直接写 configMap.yaml

格式参照

1
2
3
4
5
6
7
8
9
10
11
12
13
apiVersion: v1
kind: ConfigMap
metadata:
creationTimestamp: 2017-12-27T18:36:28Z
name: game-config-env-file
namespace: default
resourceVersion: "809965"
selfLink: /api/v1/namespaces/default/configmaps/game-config-env-file
uid: d9d1ca5b-eb34-11e7-887b-42010a8002b8
data:
allowed: '"true"'
enemies: aliens
lives: "3"

应用 configMap

1
kubectl apply -f configMap.yaml

使用存储在 ConfigMap 中的数据填充卷

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: k8s.gcr.io/busybox
command: [ "/bin/sh", "-c", "ls /etc/config/" ]
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
# 提供包含要添加到容器中的文件的 ConfigMap 的名称
name: special-config
#将该键放在 /etc/config/keys 下
items:
- key: SPECIAL_LEVEL
path: keys
restartPolicy: Never

apply 之后,启动容器,就可以在相应目录看到配置文件啦