1 概述

存储不加密数据到etcd,让pod以变量或者Volune过载到容器中。

2 场景1: 配置文件

2.1 新建redis配置文件

  1. redis.host=127.0.0.1
  2. redis.port=6379
  3. redis.password=123456

2.2 创建configmap

  1. // 1 创建名为redis-config的configmap
  2. kubectl create configmap redis-config --from-file=[文件路径]
  3. // 2 查询configmap
  4. kubectl get configmap/cm
  5. // 3 查看详细信息
  6. kubectl describe configmap/cm redis-config
  1. apiVersion: v1
  2. kind: Secret
  3. metadata:
  4. name: mysecret
  5. type: Opaque
  6. data:
  7. username: test(需base64加密 echo test | base64)
  8. password: test(需base64加密)

2.3 挂载

2.3.1 以volume形式挂载到pod容器中

2.3.1.1 配置文件configmapvolume.yaml

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: mypod
  5. spec:
  6. containers:
  7. - name: busybox
  8. image: busybox
  9. command: ["/bash/sh", "-c", "cat /ect/config/redis.properties"]
  10. volumeMounts:
  11. - name: config-volume
  12. mountPath: /etc/config
  13. volumes:
  14. - name: config-volume
  15. configMap:
  16. name: redis-config
  17. restartPolicy: Never

2.3.1.2 运行

  1. // 1 部署
  2. kubectl apply configmapvolume.yaml
  3. // 2 查看mypod日志
  4. kubectl logs mypod

2.3.2 以变量形式挂载到pod容器中

2.3.2.1 配置文件myconfig.yaml

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: myconfig
  5. namespece: default
  6. data:
  7. special.level: info
  8. special.type: hello

2.3.2.2 pod文件configmap.yaml

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: mypod
  5. spec:
  6. containers:
  7. - name: busybox
  8. image: busybox
  9. command: ["/bash/sh", "-c", "echo ${LEVEL} ${TYPE}"]
  10. env:
  11. - name: LEVEL
  12. valueFrom:
  13. secretKeyRef:
  14. name: myconfig
  15. key: special.level
  16. - name: TYPE
  17. valueFrom:
  18. secretKeyRef:
  19. name: myconfig
  20. key: special.type
  21. restartPolicy: Never

2.3.2.3 运行

  1. // 1 创建configmap
  2. kubectl apply -f myconfig.yaml
  3. // 2 查看configmap
  4. kubectl get cm
  5. // 3 创建pod
  6. kubectl apply -f configmap.yaml
  7. // 4 查看日志
  8. kubectl logs mypod