Kubernetes configMap(配置文件存储)

Kubernetes configMap(配置文件存储)

官方文档:https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/

与Secret类似,区别在于ConfigMap保存的是不需要加密配置信息。
应用场景:应用配置

创建测试配置文件

1、创建测试配置文件
vim redis.properties

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

2、通过命令创建引用配置文件

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

3、查看创建的配置文件
kubectl get cm

NAME DATA AGE
redis-config 1 8s

4、查看详细信息
kubectl describe cm redis-config

Name: redis-config
Namespace: default
Labels: <none>
Annotations: <none>

Data
====
redis.properties:
----
redis.host=127.0.0.1
redis.port=6379
redis.password=123456

Events: <none>

通过volume导入方式

1、创建yaml文件
vim 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

2、创建容器

kubectl create -f cm.yaml 

3、查看结果
kubectl logs mypod

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

通过变量名方式

1、创建yaml文件
vim myconfig.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  # 指定命名空间
  name: myconfig
  namespace: default
# 指定level type
data:
  # 指定变量 value则是配置文件的配置
  special.level: info
  special.type: hello

2、创建容器

kubectl create -f myconfig.yaml

3、查看创建
kubectl get cm

NAME DATA AGE
myconfig 2 23s
redis-config 1 11m

4、创建pod yaml
vim config-var.yaml

apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
    - name: busybox
      image: busybox
      command: [ "/bin/sh", "-c", "echo $(LEVEL) $(TYPE)" ]
      env:
        - name: LEVEL
          valueFrom:
            # 通过key加载配置文件
            configMapKeyRef:
              # 使用的key
              name: myconfig
              key: special.level
        - name: TYPE
          valueFrom:
            configMapKeyRef:
              # 使用的type
              name: myconfig
              key: special.type
  restartPolicy: Never

5、查看验证
kubectl logs mypod

info hello

 

posted @ 2019-08-28 15:27  kevin.Xiang  阅读(2063)  评论(0编辑  收藏  举报