kubenetes的ConfigMap说明

一 .创建CofigMap

1.创建ConfigMap的方式有两种:

(1)通过yaml 文件创建

(2)通过kubectl直接在命令行下创建

yaml 创建:

  a. 配置文件以key-vlane键值对的形式的保存

  b.也可直接放在一个完整的配置文件中

  创建:

$ kubectl create -f  test-config.yaml

直接用kubectl在命令行下创建:

  其实也不用直接下面的文章,直接从kubectl create  configmap -h 的帮助信息中就可以对ConfigMap如何创建有所了解。

Examples:
  # Create a new configmap named my-config based on folder bar
  kubectl create configmap my-config --from-file=path/to/bar # Create a new configmap named my-config with specified keys instead of file basenames on disk kubectl create configmap my-config --from-file=key1=/path/to/bar/file1.txt --from-file=key2=/path/to/bar/file2.txt # Create a new configmap named my-config with key1=config1 and key2=config2 kubectl create configmap my-config --from-literal=key1=config1 --from-literal=key2=config2

a.使用目录创建:

$ ls /root/test/user-guide/configmap/kubectl

test1.properties

test2.roperties

$ kubectl create configmap test-config --from-file=/root/test/user-guide/configmap/kubectl

创建ConfigMap

$ kubectl create configmap

查看ConfigMap

$ kubectl  get configmap
$ kubectl  get configmap -o yaml
$ kubectl  describe  configmap test-config

b.使用文件创建:

刚刚使用目录创建的时候使用--from-file 指定了一个目录,也可以指定单个文件创建ConfigMap


$ kubectl get configmaps test-config --from-file=/root/test/user-guide/configmap/kubectl/test1.properties --from-file=/root/test/user-guide/configmap/kubectl/test2.properties

c.使用Literal值创建

利用--from-literal 参数传递配置信息,也可以多次使用,格式如下:

$ kubectl create configmap test-config --from-literal=db.host=10.16.12.5  --from-literal=db.port='3306'
$ kubectl get configmaps test-config -o yaml

二.使用ConfigMap

(1)通过环境变量方式,直接传递给pod

(2)通过pod 命令行下运行

(3)使volume方式载入到pod内

使用ConfigMap 来替代环境变量

首先创建ConfigMap

apiVersion: v1
kind: ConfigMap
metadata:
  name: special-config
  namespace: default
data:
  special.how: very

apiVersion: v1
kind: ConfigMap
metadata:
  name: env-config
  namespace: default
data:
  log_level: INFO

在pod 中定义环境变量

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: gcr.io/google_containers/busybox
      command: [ "/bin/sh", "-c", "env" ]
      env:
        - name: SPECIAL_LEVEL_KEY
          valueFrom:
            configMapKeyRef:
              name: special-config
              key: special.how
        - name: LOG_LEVEL
          valueFrom:
            configMapKeyRef:
              name: env-config
              key: log_level
  restartPolicy: Never

输出:

SPECIAL_LEVEL_KEY=very and LOG_LEVEL=info

在ConfigMap使用key-value(键值对)配置环境变量

创建含有key-value的ConfigMap

apiVersion: v1
kind: ConfigMap
metadata:
  name: special-config
  namespace: default
data:
  SPECIAL_LEVEL: very
  SPECIAL_TYPE: charm

使用envFrom将所有ConfigMap的数据定义为Pod环境变量。

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ "/bin/sh", "-c", "env" ]
      envFrom:
      - configMapRef:
          name: special-config
  restartPolicy: Never

输出:

SPECIAL_LEVEL=very and SPECIAL_TYPE=charm

在Pod命令中使用configmap定义的环境变量。

apiVersion: v1
kind: ConfigMap
metadata:
  name: special-config
  namespace: default
data:
  SPECIAL_LEVEL: very
  SPECIAL_TYPE: charm

为了ConfigMap中注入到命令行的参数里面,其实就是给docker run 的时候指定-e 参数修改镜像中的环境变量,然后通过CMD命令利用$(VAR_NAME)通过sed 来修改配置文件或作为命令行启动参数。

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ "/bin/sh", "-c", "echo $(SPECIAL_LEVEL_KEY) $(SPECIAL_TYPE_KEY)" ]
      env:
        - name: SPECIAL_LEVEL_KEY
          valueFrom:
            configMapKeyRef:
              name: special-config
              key: SPECIAL_LEVEL
        - name: SPECIAL_TYPE_KEY
          valueFrom:
            configMapKeyRef:
              name: special-config
              key: SPECIAL_TYPE
  restartPolicy: Never

输出:

very charm

通过数据卷使用ConfigMap

ConfigMap 可以在数据卷中被使用。

apiVersion: v1
kind: ConfigMap
metadata:
  name: special-config
  namespace: default
data:
  special.level: very
  special.type: charm

将ConfigMap数据添加到指定为volumem的目录中(一下例子的挂在路径,/etc/config)

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ "/bin/sh", "-c", "ls /etc/config/" ]
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        # Provide the name of the ConfigMap containing the files you want
        # to add to the container
        name: special-config
  restartPolicy: Never
apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ "/bin/sh","-c","cat /etc/config/keys" ]
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        name: special-config
        items:
        - key: special.level
          path: keys
  restartPolicy: Never
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值