configmap在容器中的使用问题

configmap示例

apiVersion: v1
kind: ConfigMap
metadata:
  name: test-configmap
data:
  # 简单的key-value键值对
  username: "zhangsan"
  password: "password"

  # 文件名作为key,文件内容为value  
  test.conf: |
    host  127.0.0.1
    user  root
    password 123  

configmap日常用法

apiVersion: v1
kind: Pod
metadata:
  name: test-pod
spec:
  containers:
    - name: test
      image: centos
      command:
      - /bin/bash
      args:
      - '-c'
      - while true; do echo hello; sleep 1; done
      env:
        # 环境变量中使用configmap
        - name: USERNAME
          valueFrom:
            configMapKeyRef:
              name: test-configmap  # 设置需要从哪个configmap获取数据
              key: username         # 获取configmap中哪个key的数据
      volumeMounts:
      # 1. 直接将test.conf挂载到/config下,如果/config不为空,目录内容会被覆盖
      - name: config
        mountPath: /config
        readOnly: true
      # 2. 使用subPath,不会覆盖/config目录中原有的内容
      #- name: config
      #  mountPath: /config/test.conf
      #  subPath: test.conf
      #  readOnly: true
  volumes:
    - name: config
      configMap:
        name: test-configmap
        items:
        - key: "test.conf"
          path: "test.conf"

你可能感兴趣的:(kubernetes)