您现在的位置是:首页 > 文章详情

Kubernetes - 6.1 Config and Storage - ConfigMap

日期:2020-03-01点击:475

什么是ConfigMap

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

Config基本操作

通过kubectl create configmap
kubectl create configmap nginx-configmap --from-literal=password=123456
image

通过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查看详细信息
image

通过kubectl describe configmap查看详细信息
image

ConfigMap作为存储卷被Pod调用

创建ConfigMap
kubectl create configmap database-config --from-literal=user=root --from-literal=password=123456
image

通过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
image

查看Pod详细信息,挂载了ConfigMap类型的Volumes
kubectl describe pod nginx-pod
image

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
image

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
image

查看Pod容器详细信息
kubectl describe pod nginx-pod
image

Config注意的事项

  1. ConfigMap文件大小限制: 1MB (etcd的限制)
  2. ConfigMap必须在Pod引用它之前创建
原文链接:https://yq.aliyun.com/articles/747561
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章