前言

ConfigMap组件可以实现应用和配置分离,避免因为修改配置项而重新构建镜像。
ConfigMap 用于保存配置数据的键值对,可以用来保存单个属性,也可以用来保存配置文件。


一、创建ConfigMap

1、从字符串创建

[root@k8s-master k8s]# kubectl create configmap  config-string --from-literal=testKey=testValue
configmap/special-config created
[root@k8s-master k8s]# kubectl get configmap  config-string -o go-template='{{.data}}'
map[testKey:testValue]

2、从文件创建

[root@k8s-master k8s]# echo -e "a=b\nc=d" | tee config.env
a=b
c=d
[root@k8s-master k8s]# kubectl create configmap config-env --from-env-file=config.env
configmap/config-env created
[root@k8s-master k8s]# kubectl get configmap config-env -o go-template='{{.data}}'
map[a:b c:d]

3、从目录创建

[root@k8s-master k8s]# mkdir config
[root@k8s-master k8s]# echo a > config/a
[root@k8s-master k8s]# echo b > config/b
[root@k8s-master k8s]#  kubectl create configmap config-dir --from-file=config/
configmap/config-dir created
[root@k8s-master k8s]# kubectl get configmap config-dir -o go-template='{{.data}}'
map[a:a
 b:b
]

4、根据yaml文件创建

[root@k8s-master k8s]# cat config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: config-yaml
  namespace: default
data:
  key1: value1
  key2: value2
[root@k8s-master k8s]# kubectl create -f config.yaml
configmap/config-yaml created
[root@k8s-master k8s]# kubectl get configmap config-yaml -o go-template='{{.data}}'
map[key1:value1 key2:value2]

二、使用ConfigMap

1、设置环境变量

(1)config-env.yaml

[root@k8s-master k8s]# cat config-env.yaml
apiVersion: v1
kind: Pod
metadata:
  name: pod-01
spec:
  containers:
  - name: busybox-container-01
    image: busybox:latest
    command: [ "/bin/sh", "-c", "env" ]
    env:
    - name: KEY1
      valueFrom:
        configMapKeyRef:
          name: config-yaml
          key: key1
    - name: KEY2
      valueFrom:
        configMapKeyRef:
          name: config-yaml
          key:  key2
    envFrom:
    - configMapRef:
        name: config-yaml
  restartPolicy: Never

(2)创建

[root@k8s-master k8s]# kubectl create -f config-env.yaml
pod/pod-01 created

(3)查看日志

[root@k8s-master k8s]# kubectl logs pod-01
KEY1=value1
KEY2=value2
NGINX_SERVICE_NODEPORT_PORT=tcp://10.106.65.70:80
KUBERNETES_PORT=tcp://10.96.0.1:443
NGINX_SERVICE_NODEPORT_SERVICE_PORT=80
...

2、用作命令行参数

(1)config-cmd.yaml

[root@k8s-master k8s]# cat config-cmd.yaml
apiVersion: v1
kind: Pod
metadata:
  name: pod-02
spec:
  containers:
  - name: busybox-container-02
    image: busybox:latest
    command: [ "/bin/sh", "-c", "echo ${KEY1} ${KEY2}" ]
    env:
    - name: KEY1
      valueFrom:
        configMapKeyRef:
          name: config-yaml
          key: key1
    - name: KEY2
      valueFrom:
        configMapKeyRef:
          name: config-yaml
          key:  key2
    envFrom:
    - configMapRef:
        name: config-yaml
  restartPolicy: Never

(2)创建

[root@k8s-master k8s]# kubectl create -f config-cmd.yaml
pod/pod-02 created

(3)查看日志

[root@k8s-master k8s]# kubectl logs pod-02
value1 value2

3、作为文件挂载

(1)config-file.yaml

[root@k8s-master k8s]# cat config-file.yaml
apiVersion: v1
kind: Pod
metadata:
  name: pod-03
spec:
  containers:
  - name: busybox-container-03
    image: busybox:latest
    command: [ "/bin/sh", "-c", "cat /etc/config/key1" ]
    volumeMounts:
    - name: config-file
      mountPath: /etc/config
  volumes:
  - name: config-file
    configMap:
      name: config-yaml
  restartPolicy: Never

(2)创建

[root@k8s-master k8s]# kubectl create -f config-file.yaml
pod/pod-03 created

(3)查看日志

[root@k8s-master k8s]# kubectl logs pod-03
value1

4、作为目录挂载

(1)config-dir.yaml

[root@k8s-master k8s]# cat config-dir.yaml
apiVersion: v1
kind: Pod
metadata:
  name: pod-04
spec:
  containers:
  - name: busybox-container-04
    image: busybox:latest
    command: [ "/bin/sh", "-c", "cat /etc/config/keys/key1" ]
    volumeMounts:
    - name: config-file
      mountPath: /etc/config
  volumes:
  - name: config-file
    configMap:
      name: config-yaml
      items:
      - key: key1
        path: keys/key1
  restartPolicy: Never

(2)创建

[root@k8s-master k8s]# kubectl create -f config-dir.yaml
pod/pod-04 created
```

(3)查看日志

```c
[root@k8s-master k8s]# kubectl logs pod-04
value1
```
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐