美文网首页
ConfigMap使用

ConfigMap使用

作者: Oooyzx | 来源:发表于2019-12-10 11:45 被阅读0次

ConfigMap 的用法几乎与 Secret 完全相同:你可以使用 kubectl create configmap 从文件或者目录创建 ConfigMap,也可以直接编写 ConfigMap 对象的 YAML 文件。

  • 创建configmap
    centos-mysql:latest
    kubectl create configmap mysqlcfmap --from-file=./my.cnf

    image.png
  • 查看configmap
    kubectl get configmaps

    image.png
  • get yaml文件
    kubectl get configmaps mysqlcfmap -o yaml

    image.png
  • my.cnf文件中
    创建一个my.cnf文件

[mysqld]
user=oyzx
port=3307
  • 创建mysql.yaml文件
注意:(请仔细阅读如下注意事项!!!)
  1. 由于考虑到简单实验,并未拉取真实mysql镜像,在网络良好的状态下,可将镜像image: busybox改为image: mysql:5.7类似镜像
  2. 挂载目录mountPath: "/mysqlcfmap",如果挂载的是文件夹,当该文件夹,如/mysqlcfmap中有内容时,将会把该文件夹下所有文件覆盖,只留下my.cnf,如果挂载的是mountPath: "/mysqlcfmap/my.cnf",则会自动创建一个my.cnf的文件夹,再将my.cnf文件放进去。
apiVersion: v1
kind: Pod
metadata:
 name: test-configmap
spec:
 containers:
 - name: test-configmap-volume
   image: busybox
   args:
   - sleep
   - "86400"
   volumeMounts:
   - name: mysqlcfmap
     mountPath: "/mysqlcfmap"
     readOnly: true
 volumes:
 - name: mysqlcfmap
   configMap:
      name: mysqlcfmap
  • 创建
    kubectl create -f mysql.yaml

  • 查看是否创建成功
    kubectl get pods

    image.png
  • 进入容器
    kubectl exec -it test-configmap sh
    可看到my.cnf文件

    image.png

修改挂载覆盖目录的问题

  • 删除原始容器
    kubectl delete -f mysql.yam
    image.png
  • 删除configmap中的mysqlcnf
    kubectl delete configmaps mysqlcnf
    image.png
  • 自定义key方式重新创建configmap
    kubectl create configmap mysqlcnf --from-file=mysqlcnf=./my.cnf
    image.png
  • 查看创建的configmap
    kubectl get configmaps
    image.png
  • 修改yaml文件
apiVersion: v1
kind: Pod
metadata:
  name: test-configmap
spec:
  containers:
  - name: test-configmap-volume
    image: busybox
    args:
    - sleep
    - "86400"
    volumeMounts:
    - name: mysqlcfmap
      mountPath: "/mysqlcfmap" # 挂载到容器中目录,这个目录会自动创建
  volumes:
  - name: mysqlcfmap
    configMap:
       name: mysqlcnf  # 创建 configmap 对象的名称
       items:
       - key: my.cnf  # 创建  configmap 对象时指定的 key
         path: my.cnf  # 容器 a 目录中的文件名
  • 创建容器
    kubectl create -f mysql.yaml
image.png
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: mysql-cnf  # 创建 configmap 对象的名称
       items:
       - key: my-cnf   # 创建  configmap 对象时指定的 key
         path: my.cnf  # 容器 a 目录中的文件名

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


-------------------

不更新
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: mysql-cnf  # 创建 configmap 对象的名称
       items:
       - key: my-cnf   # 创建  configmap 对象时指定的 key
         path: my.cnf  # 容器 a 目录中的文件名

  # /etc/my.cnf  # 普通文件
image.png
  • 更新


    image.png

    修改配置文件my.cnf,再重新生成configmap后可自动更新在容器中得到新的my.cnf文件内容


    image.png
  • 不更新


    image.png

相关文章

网友评论

      本文标题:ConfigMap使用

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