陈同学
微服务
Accelerator
About
# K8s 普通存储:ConfigMap ConfigMap 用于存储普通数据,Secret 用于存储敏感数据。通过K8s 提供的存储,一是可以将镜像和配置分离,二是可以摆脱对磁盘的依赖。 ## 创建 ConfigMap ### 将文件打包为ConfigMap 准备两个测试文件。 ```bash echo file1 >> file1 echo file2 >> file2 ``` 通过 `--from-file` 参数可以打包N个文件。 ```bash kubectl create configmap demo-config --from-file=file1 --from-file=file2 kubectl get configmap/demo-config -o yaml ``` 创建的ConfigMap yaml 如下: ```yaml apiVersion: v1 data: file1: | file1 file2: | file2 kind: ConfigMap metadata: creationTimestamp: "2020-03-15T01:44:02Z" name: demo-config namespace: default resourceVersion: "28168337" selfLink: /api/v1/namespaces/default/configmaps/demo-config uid: cc4bd366-8f60-4844-a6db-0f84284fa89f ``` ### 将目录打包为ConfigMap ```bash mkdir configmap echo file3 >> configmap/file3 kubectl create configmap demo-dir-config --from-file=configmap/ kubectl get configmap/demo-dir-config -o yaml ``` 其yaml如下: ```yaml apiVersion: v1 data: file3: | file3 kind: ConfigMap metadata: creationTimestamp: "2020-03-15T01:57:25Z" name: demo-dir-config namespace: default resourceVersion: "28169829" selfLink: /api/v1/namespaces/default/configmaps/demo-dir-config uid: 686cebd8-4abe-40e7-ac48-7c5be7038cfd ``` ### 通过literal 创建ConfigMap ```bash kubectl create configmap demo-literal-config \ --from-literal=user.name=kitty \ --from-literal=user.age=18 ``` 获取yaml: ```bash kubectl get configmap/demo-literal-config -o yaml ``` 数据如下: ```yaml apiVersion: v1 data: user.age: "18" user.name: kitty kind: ConfigMap metadata: creationTimestamp: "2020-03-15T01:59:45Z" name: demo-literal-config namespace: default resourceVersion: "28170092" selfLink: /api/v1/namespaces/default/configmaps/demo-literal-config uid: 5921c2a3-9eed-49f3-bf4c-671dd7e18c50 ``` ### 使用 yaml 创建 下面为Demo Yaml。 ```yaml apiVersion: v1 kind: ConfigMap metadata: name: demo-yaml-config data: user.name: kitty ``` ## 应用 ConfigMap 以上面的 `demo-literal-config` 作为例子: ### 作为环境变量 利用以下yaml创建Pod,将`demo-literal-config` 中 `user.name` 字段分配给环境变量 `USER_NAME`. ```yaml apiVersion: v1 kind: Pod metadata: name: config-env-demo spec: containers: - name: busybox image: busybox command: [ "/bin/sh", "-c", "env" ] env: - name: USER_NAME valueFrom: configMapKeyRef: name: demo-literal-config key: user.name ``` 查看Pod日志可看到环境变量。 ```bash $ kubectl logs pod/config-env-demo USER_NAME=kitty ``` 也可以将整个ConfigMap作为环境变量。 ```yaml apiVersion: v1 kind: Pod metadata: name: config-env-demo spec: containers: - name: busybox image: busybox command: [ "/bin/sh", "-c", "env" ] envFrom: - configMapRef: name: demo-literal-config ``` 运行Pod后查看环境变量: ```bash $ kubectl logs pod/config-env-demo HOSTNAME=config-env-demo user.name=kitty user.age=18 ``` ### 作为Volume挂载到容器 以上面的 `demo-config` 为例。 ```yaml apiVersion: v1 kind: Pod metadata: name: nginx spec: containers: - name: nginx-container image: nginx volumeMounts: - name: config-volume mountPath: /etc/config volumes: - name: config-volume configMap: name: demo-config ``` 运行Pod,进入容器后查看 `/etc/config` 目录,可以看到 file1、file2两个文件。 > 注意:如果 /etc/config 目录原先有文件,将被删除 ```bash $ ls -l /etc/config/ total 0 lrwxrwxrwx 1 root root 12 Mar 15 04:41 file1 -> ..data/file1 lrwxrwxrwx 1 root root 12 Mar 15 04:41 file2 -> ..data/file2 ```
本文由
cyj
创作,可自由转载、引用,但需署名作者且注明文章出处。
文章标题:
K8s 普通存储:ConfigMap
文章链接:
https://chenyongjun.vip/articles/151
扫码或搜索 cyjrun 关注微信公众号, 结伴学习, 一起努力