Kubernetes的ConfigMap

ConfigMap

ConfigMap作用是存储不加密的数据到etcd中,让Pod以变量或数据卷Volume挂载到容器中

应用场景:配置文件

创建配置文件

首先我们需要创建一个配置文件 redis.properties

redis.port=127.0.0.1
redis.port=6379
redis.password=123456

创建ConfigMap

我们使用命令创建configmap

kubectl create configmap redis-config --from-file=redis.properties

然后查看详细信息

kubectl describe cm redis-config

image-20201118085503534

Volume数据卷形式挂载

首先我们需要创建一个 cm.yaml

apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
  - name: busybox
    image: busybox
    command: ["/bin/sh","-c","cat /etc/config/redis.properties"]
    volumeMounts:
    - name: config-volume
      mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        name: redis-config
  restartPolicy: Never

然后使用该yaml创建我们的pod

# 创建
kubectl apply -f cm.yaml
# 查看
kubectl get pods

image-20201118090634869

最后我们通过命令就可以查看结果输出了

kubectl logs mypod

image-20201118090712780

以变量的形式挂载Pod

首先我们也有一个 myconfig.yaml文件,声明变量信息,然后以configmap创建

apiVersion: v1
kind: ConfigMap
metadata:
  name: myconfig
  namespace: default
data:
  special.level: info
  special.type: hello

然后我们就可以创建我们的配置文件

# 创建pod
kubectl apply -f myconfig.yaml
# 获取
kubectl get cm

image-20201118091042287

然后我们创建完该pod后,我们就需要在创建一个 config-var.yaml 来使用我们的配置信息

apiVersion: v1
kind: Pod
metadata:
  name: mypod1
spec:
  containers:
    - name: busybox
      image: busybox
      command: ["/bin/sh","-c","echo $(LEVEL) $(TYPE)"]
      env:
        - name: LEVEL
          valueFrom:
            configMapKeyRef:
              name: myconfig
              key: special.level
        - name: TYPE
          valueFrom:
            configMapKeyRef:
              name: myconfig
              key: special.type
  restartPolicy: Never

最后我们查看输出

kubectl logs mypod

image-20201118091448252

原文地址:https://www.cnblogs.com/fat-girl-spring/p/14134404.html