美文网首页
configMap(存储不敏感信息)

configMap(存储不敏感信息)

作者: 魔曦帝天 | 来源:发表于2019-12-10 19:33 被阅读0次

命令行创建configmap
指定文件,输入多个路径

kubectl create configmap <map-name> <data-source>

kubectl create configmap [configmap_name] --from-file=<my-key-name>=<path-to-file>

kubectl create configmap game-config-3 --from-file=game-special-key=configure-pod-container/configmap/game.properties

kubectl create configmap my.cnf --from-file=my.cnf=my.cnf
kubectl create configmap [configmap对象]--from-file=< key >= < path >

subPath替换文件,必须是pod中存在的,否则为一个空文件夹
test-configmap.yaml(无法更新)

apiVersion: v1
kind: Pod
metadata:
  name: test-configmap
spec:
  containers:
  - name: test-configmap-volume
    image: busybox
    args:
    - sleep
    - "86400"
    volumeMounts:
    - name: mysqlcfmap
      mountPath: "/etc/my.cnf" # 挂载到容器中目录,这个目录会自动创建
      subPath: my.cnf
  volumes:
  - name: mysqlcfmap
    configMap:
       name: my.cnf  # 创建 configmap 对象的名称
       items:
       - key: my.cnf  # 创建  configmap 对象时指定的 key
         path: my.cnf  # 
  # /etc/my.cnf  # 普通文件

与本地my.cnf文件内容一致

configmap.yaml(可以更新)

1 从普通文件创建
  kubectl  create  configmap mysql-cnf  --from-file=my-cnf=./my.cnf
  
2 从 yaml 文件创建

# ./kustomization.yaml
configMapGenerator:
- name:  mysql-cnf
  files:
  - my-cnf=./my.cnf


kubectl apply -f ./kustomization.yaml


Pod使用

1. 更新
  更新 Etcd 中配置数据

apiVersion: v1
kind: Pod
metadata:
  name: test-configmap
spec:
  containers:
  - name: test-configmap-volume
    image: busybox
    args:
    - sleep
    - "86400"
    volumeMounts:
    - name: mysqlcfmap
      mountPath: "/etc/config" # 挂载到容器中目录,这个目录会自动创建
  volumes:
  - name: mysqlcfmap
    configMap:
       name: mycnf  # 创建 configmap 对象的名称
       items:
       - key: my-cnf   # 创建  configmap 对象时指定的 key
         path: my.cnf  # 文件名

    # /etc/config/my.cnf  # 软链接
image.png image.png

场景:你需要这个挂载配置文件,但你并不希望覆盖这个目录下的其他文件

apiVersion: v1
kind: Pod
metadata:
  name: test-configmap
spec:
  containers:
  - name: test-configmap-volume
    image: busybox
    args:
    - sleep
    - "86400"
    volumeMounts:
    - name: mysqlcfmap
      mountPath: "/etc/nginx/conf.d/" # 挂载到容器中目录,这个目录会自动创建
      subPath: my.cnf
  volumes:
  - name: mysqlcfmap
    configMap:
       name: my.cnf  # 创建 configmap 对象的名称
       items:
       - key: my.cnf  # 创建  configmap 对象时指定的 key
         path: my.cnf  # 

不影响其他配置文件

相关文章

网友评论

      本文标题:configMap(存储不敏感信息)

      本文链接:https://www.haomeiwen.com/subject/ujawgctx.html