Kubernetes资源对象--Configmap

configmap与secret类似,只是configmap用来处理不包含敏感信息的数据,用于Pod自定义配置。


1.创建Configmap

Configmap也有四种方式创建:

  • 通过--from-literal
    kubectl create configmap myconfigmap1 --from-literal=config1=123 --from-literal=config2=456
    一个--from-literal对应一个条目,查看如下:
[root@ceph1 kube-dashboard]# kubectl describe configmap myconfigmap1
Name:         myconfigmap1
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
config1:
----
123
config2:
----
456
Events:  <none>
  • 通过--from-file
    kubectl create configmap myconfigmap2 --from-file=configmap1 --from-file=configmap2
  • 通过--from-env-file
    kubectl create configmap myconfigmap3 --from-env-file=configmap-env.txt
  • 通过yaml配置文件
kind: ConfigMap
metadata:
  name: myconfigmap4
data:
  config1: aaa
  config2: bbb

kubectl create -f myconfigmap4.yaml


2. 使用ConfigMap

ConfigMap也有两种方式使用:环境变量和Volume。

  • Volume方式
apiVersion: v1
kind: Pod
metadata:
  name: configmap-volume
spec:
  containers:
  - name: configmap-volume
    image: busybox
    args:
     - /bin/sh
     - -c
     - sleep 10; touch /tmp/test; sleep 30000
    volumeMounts:
    - name: configmap
      mountPath: /etc/configmap
      readOnly: true
  volumes:
  - name: configmap
    configMap:
      name: myconfigmap4
  • 环境变量的方式
apiVersion: v1
kind: Pod
metadata:
  name: configmap-env
spec:
  containers:
  - name: configmap-env
    image: busybox
    args:
     - /bin/sh
     - -c
     - sleep 10; touch /etc/configmap; sleep 30000
    env:
     - name: CONFIG1
       valueFrom:
         configMapKeyRef:
           name: myconfigmap4
           key: config1
       name: CONFIG2
       valueFrom:
         configMapKeyRef:
           name: myconfigmap4
           key: config2

普遍情况下,配置信息都是以文件形式存在,所以在创建configmap时候采用--from-file或者yaml文件比较多,使用configmap则是volume的方式居多。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 生产环境中很多应用程序的配置可能需要通过配置文件,命令行参数和环境变量的组合配置来完成。这些配置应该从image中...
    sjyu_eadd阅读 1,298评论 0 1
  • ConfigMap在K8S中经常会使用到,它能使容器镜像和配置内容解耦,它可以绑定在volume上供容器访问,它的...
    乱七八糟谈技术阅读 1,801评论 0 1
  • 1.kubernetes介绍 Kubernetes容器集群管理系统,是Google开源的一个项目,目标是管理跨多个...
    Rami阅读 1,058评论 0 3
  • 应用部署的一个最佳实践是将应用所需的配置信息与程序进行分离,这样可以使得应用程序被更好地复用,通过不同的配置也能实...
    王勇1024阅读 20,565评论 0 5
  • 学到现在,前面的七章其实已经足够开发使用,但如果想要对K8S有个进阶的认知,从现在开始才是真正的核心,很多第三方的...
    Suny____阅读 410评论 0 1