Kubernetes ConfigMap

什么是ConfigMap

ConfigMap为Pod中的容器提供了配置文件、环境变量等非敏感信息,通过ConfigMap可以将Pod和其他组件分开,这将使得Pod更加有移植性,使得配置更加容器更改及管理,也使得Pod更加规范。

Config基本操作

通过kubectl create configmap
kubectl create configmap nginx-configmap --from-literal=password=123456
在这里插入图片描述
通过yaml资源配置清单
kubectl apply -f nginx-configmap.yaml

apiVersion: v1
kind: List
metadata:
items:
- apiVersion: v1
  data:
    password: "123456"
  kind: ConfigMap
  metadata:
    name: nginx-configmap

通过kubectl get configmap查看详细信息
在这里插入图片描述
通过kubectl describe configmap查看详细信息
在这里插入图片描述

ConfigMap作为存储卷被Pod调用

创建ConfigMap
kubectl create configmap database-config --from-literal=user=root --from-literal=password=123456
在这里插入图片描述
通过YAML资源定义清单创建Pod并绑定ConfigMap为存储卷
kubectl apply -f nginx-pod-configmap-volume.yaml

apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
spec:
  containers:
    - name: nginx
      image: nginx:1.16
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        name: database-config 

查看Pod容器挂载的ConfigMap存储卷
kubectl exec -it nginx-pod /bin/bash
在这里插入图片描述
查看Pod详细信息,挂载了ConfigMap类型的Volumes
kubectl describe pod nginx-pod
在这里插入图片描述

ConfigMap作为环境变量被Pod调用

kubectl create configmap database-config --from-literal=user=root --from-literal=password=123456
kubectl create configmap env-config --from-literal=LOG_LEVEL=ERROR
在这里插入图片描述
kubectl apply -f nginx-pod-configmap-env.yaml

apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
spec:
  containers:
    - name: nginx
      image: nginx:1.16
      env:
        - name: DB_USER_CONFIG
          valueFrom:
            configMapKeyRef:
              name: database-config
              key: user
        - name: DB_PASSWORD_CONFIG
          valueFrom:
            configMapKeyRef:
              name: database-config
              key: password
      envFrom:
        - configMapRef:
            name: env-config

查看Pod容器的环境变量
kubectl exec -it nginx-pod /bin/bash
在这里插入图片描述
查看Pod容器详细信息
kubectl describe pod nginx-pod
在这里插入图片描述

Config注意的事项
  1. ConfigMap文件大小限制: 1MB (etcd的限制)
  2. ConfigMap必须在Pod引用它之前创建
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值