Kubernetes configMap(配置文件存儲)

Kubernetes configMap(配置文件存儲)

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

與Secret相似,區別在於ConfigMap保存的是不須要加密配置信息。
應用場景:應用配置vim

建立測試配置文件

一、建立測試配置文件
vim redis.propertiesapi

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

二、經過命令建立引用配置文件測試

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

三、查看建立的配置文件
kubectl get cm加密

NAME DATA AGE
redis-config 1 8s

四、查看詳細信息
kubectl describe cm redis-configspa

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導入方式

一、建立yaml文件
vim cm.yamlrest

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

二、建立容器code

kubectl create -f cm.yaml 

三、查看結果
kubectl logs mypodblog

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

經過變量名方式

一、建立yaml文件
vim myconfig.yamlci

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

二、建立容器

kubectl create -f myconfig.yaml

三、查看建立
kubectl get cm

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

四、建立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

五、查看驗證
kubectl logs mypod

info hello
相關文章
相關標籤/搜索