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 
[root@k8smaster ~]# kubectl create configmap redis-config --from-file=redis.properties
# 查看 configmap
[root@k8smaster ~]# kubectl get cm
# 查看 redis-config 的详情
[root@k8smaster ~]# 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文件
[root@k8smaster ~]# vi cm.yaml
# 创建pod
[root@k8smaster ~]# kubectl apply -f cm.yaml
# 启动后可以查看日志
[root@k8smaster ~]# 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
[root@k8smaster ~]# vi myconfig.yaml
[root@k8smaster ~]# kubectl apply -f myconfig.yaml
# 以变量形式进行挂载
[root@k8smaster ~]# vi config-var.yaml
[root@k8smaster ~]# kubectl apply -f config-var.yaml
posted @   VipSoft  阅读(331)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
历史上的今天:
2021-12-02 Intellij Java JNI 调用 C++
2021-12-02 Exception in thread "main" java.lang.UnsatisfiedLinkError: xxx()V
点击右上角即可分享
微信分享提示