Kubernetes(K8S) 配置管理-ConfigMap 介紹

作用:存儲不加密數據到 etcd,讓 Pod 以變量或者 Volume 掛載到容器中
場景:配置文件

創建配置文件

redis.properties

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

創建 ConfigMap

# 根據 redis.properties 創建 redis-config 
[[email protected] ~]# kubectl create configmap redis-config --from-file=redis.properties
# 查看 configmap
[[email protected] ~]# kubectl get cm
# 查看 redis-config 的詳情
[[email protected] ~]# kubectl describe cm redis-config

以 Volume 的形式進行掛載到 pod 容器中

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文件
[[email protected] ~]# vi cm.yaml
# 創建pod
[[email protected] ~]# kubectl apply -f cm.yaml
# 啓動後可以查看日誌
[[email protected] ~]# kubectl logs mypod

以變量的形式進行掛載

創建 yaml,聲明變量信息 configmap 創建
myconfig.yaml

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

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:
            configMapKeyRef:
              name: myconfig
              key: special.level
        - name: TYPE
          valueFrom:
            configMapKeyRef:
              name: myconfig
              key: special.type
  restartPolicy: Never
[[email protected] ~]# vi myconfig.yaml
[[email protected] ~]# kubectl apply -f myconfig.yaml
# 以變量形式進行掛載
[[email protected] ~]# vi config-var.yaml
[[email protected] ~]# kubectl apply -f config-var.yaml
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章