ConfigMap

基础理论

ConfigMap 是一种 API 对象,用来将非机密性的数据保存到健值对中。使用时, Pods 可以将其用作环境变量、命令行参数或者存储卷中的配置文件。
ConfigMap 将您的环境配置信息和 容器镜像 解耦,便于应用配置的修改。
注意:ConfigMap 并不提供保密或者加密功能。 如果你想存储的数据是机密的,请使用 Secret, 或者使用其他第三方工具来保证你的数据的私密性,而不是用 ConfigMap。

使用 ConfigMap 来将你的配置数据和应用程序代码分开。
ConfigMap 在设计上不是用来保存大量数据的。在 ConfigMap 中保存的数据不可超过 1 MiB。如果你需要保存超出此尺寸限制的数据,你可能希望考虑挂载存储卷 或者使用独立的数据库或者文件服务。

创建ConfigMap

创建ConfigMap

  1. [root@clientvm ~]# kubectl create configmap -h
  2. Usage:
  3. kubectl create configmap NAME [--from-file=[key=]source] [--from-literal=key1=value1] [--dry-run=server|client|none]
  4. [options]
  5. Examples:
  6. # Create a new configmap named my-config based on folder bar
  7. kubectl create configmap my-config --from-file=path/to/bar
  8. # Create a new configmap named my-config with specified keys instead of file basenames on disk
  9. kubectl create configmap my-config --from-file=key1=/path/to/bar/file1.txt --from-file=key2=/path/to/bar/file2.txt
  10. # Create a new configmap named my-config with key1=config1 and key2=config2
  11. kubectl create configmap my-config --from-literal=key1=config1 --from-literal=key2=config2
  12. # Create a new configmap named my-config from the key=value pairs in the file
  13. kubectl create configmap my-config --from-file=path/to/bar
  14. # Create a new configmap named my-config from an env file
  15. kubectl create configmap my-config --from-env-file=path/to/bar.env

从配置文件创建

  1. [root@clientvm ~]# kubectl create configmap haproxy --from-file=/etc/haproxy/haproxy.cfg
  2. configmap/haproxy created
  3. [root@clientvm ~]# kubectl describe configmaps haproxy
  4. Name: haproxy
  5. Namespace: default
  6. Labels: <none>
  7. Annotations: <none>
  8. Data
  9. ====
  10. haproxy.cfg:
  11. ......
  1. kubectl create configmap my-config --from-literal=db_name=wordpress --from-literal=db_user=redhat

使用configMap

通过容器的环境变量使用ConfigMap

https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#define-a-container-environment-variable-with-data-from-a-single-configmap

  1. [root@clientvm ~]# cat configmap-env-pod.yaml
  2. apiVersion: v1
  3. kind: Pod
  4. metadata:
  5. name: test-pod-env
  6. spec:
  7. containers:
  8. - name: test-container
  9. image: busybox
  10. imagePullPolicy: IfNotPresent
  11. command: [ "/bin/sh", "-c", "env" ]
  12. env:
  13. - name: DB_USER
  14. valueFrom:
  15. configMapKeyRef:
  16. name: my-config
  17. key: db_user
  18. - name: DB_NAME
  19. valueFrom:
  20. configMapKeyRef:
  21. name: my-config
  22. key: db_name
  23. restartPolicy: Never
  1. [root@clientvm ~]# kubectl apply -f configmap-env-pod.yaml
  2. pod/test-pod-env created
  3. [root@clientvm ~]# kubectl get pod
  4. NAME READY STATUS RESTARTS AGE
  5. test-pod-env 0/1 Completed 0 23s
  6. [root@clientvm ~]# kubectl logs test-pod-env | grep DB
  7. DB_NAME=wordpress
  8. DB_USER=redhat

将 ConfigMap 以卷的形式进行挂载的 Pod 示例:

https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#add-configmap-data-to-a-specific-path-in-the-volume

  1. [root@clientvm ~]# cat configmap-file-pod.yaml
  2. apiVersion: v1
  3. kind: Pod
  4. metadata:
  5. name: mypod-configmap-file
  6. spec:
  7. containers:
  8. - name: mypod
  9. image: nginx
  10. imagePullPolicy: IfNotPresent
  11. volumeMounts:
  12. - name: foo
  13. mountPath: "/mnt/conf"
  14. readOnly: true
  15. volumes:
  16. - name: foo
  17. configMap:
  18. name: haproxy
  1. [root@clientvm ~]# kubectl get pod
  2. NAME READY STATUS RESTARTS AGE
  3. mypod-configmap-file 1/1 Running 0 4s
  4. [root@clientvm ~]# kubectl exec mypod-configmap-file -it -- bash
  5. root@mypod-configmap-file:/# ls /mnt/conf/
  6. haproxy.cfg

Secret

基础理论

Secret 对象类型用来保存敏感信息,例如密码、OAuth 令牌和 SSH 密钥。 将这些信息放在 secret 中比放在 Pod 的定义或者 容器镜像 中来说更加安全和灵活。

要使用 Secret,Pod 需要引用 Secret。 Pod 可以用三种方式之一来使用 Secret:

创建generic Secret

  1. [root@clientvm ~]# kubectl create secret -h
  2. Create a secret using specified subcommand.
  3. Available Commands:
  4. docker-registry Create a secret for use with a Docker registry
  5. generic Create a secret from a local file, directory or literal value
  6. tls Create a TLS secret
  7. Usage:
  8. kubectl create secret [flags] [options]
  1. [root@clientvm ~]# echo root>username.txt
  2. [root@clientvm ~]# echo redhat>password.txt
  3. [root@clientvm ~]# kubectl create secret generic my-secret1 --from-file=username.txt --from-file=password.txt
  4. secret/my-secret1 created
  5. [root@clientvm ~]# kubectl describe secrets my-secret1
  6. Name: my-secret1
  7. Namespace: default
  8. Labels: <none>
  9. Annotations: <none>
  10. Type: Opaque
  11. Data
  12. ====
  13. password.txt: 7 bytes
  14. username.txt: 5 bytes
  1. [root@clientvm ~]# kubectl create secret generic my-secret2 --from-file=user=username.txt --from-file=passwd=password.txt
  2. secret/my-secret2 created
  3. [root@clientvm ~]# kubectl describe secrets my-secret2
  4. Name: my-secret2
  5. Namespace: default
  6. Labels: <none>
  7. Annotations: <none>
  8. Type: Opaque
  9. Data
  10. ====
  11. passwd: 7 bytes
  12. user: 5 bytes
  1. [root@clientvm ~]# kubectl create secret generic my-secret3 --from-literal=user=root --from-literal=passwd='redhat'
  2. secret/my-secret3 created
  3. [root@clientvm ~]# kubectl edit secrets my-secret3
  4. ......
  5. apiVersion: v1
  6. data:
  7. passwd: cmVkaGF0
  8. user: cm9vdA==
  9. kind: Secret
  10. ......
  11. [root@clientvm ~]# echo cmVkaGF0 |base64 --decode
  12. redhat

创建docker-registry Secret

  1. kubectl create secret docker-registry my-secret --docker-server=DOCKER_REGISTRY_SERVER --docker-username=DOCKER_USER
  2. --docker-password=DOCKER_PASSWORD
  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: private-reg
  5. spec:
  6. containers:
  7. - name: private-reg-container
  8. image: <your-private-image>
  9. imagePullSecrets:
  10. - name: my-secret

创建TLS Secret

  1. kubectl create secret tls my-tls-secret --cert=path/to/cert/file --key=path/to/key/file
  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: mynginx
  5. spec:
  6. containers:
  7. - name: mypod
  8. image: nginx
  9. imagePullPolicy: IfNotPresent
  10. volumeMounts:
  11. - name: foo
  12. mountPath: "/etc/nginx/ssl"
  13. readOnly: true
  14. volumes:
  15. - name: foo
  16. secret:
  17. secretName: my-tls-secret

使用Secret

以环境变量方式使用secret

  1. [root@clientvm ~]# cat secret-env-pod.yaml
  2. apiVersion: v1
  3. kind: Pod
  4. metadata:
  5. name: secret-test-pod-env
  6. spec:
  7. containers:
  8. - name: test-container
  9. image: busybox
  10. imagePullPolicy: IfNotPresent
  11. command: [ "/bin/sh", "-c", "env" ]
  12. envFrom:
  13. - secretRef:
  14. name: my-secret3
  15. restartPolicy: Never
  16. [root@clientvm ~]# kubectl apply -f secret-env-pod.yaml
  17. pod/secret-test-pod-env created
  18. [root@clientvm ~]# kubectl get pod
  19. NAME READY STATUS RESTARTS AGE
  20. secret-test-pod-env 0/1 Completed 0 8s
  21. [root@clientvm ~]# kubectl logs secret-test-pod-env
  22. user=root
  23. passwd=redhat

已卷形式挂载Secret

  1. [root@clientvm ~]# cat secret-env-volume.yaml
  2. apiVersion: v1
  3. kind: Pod
  4. metadata:
  5. name: test-pod-secret-volume
  6. spec:
  7. containers:
  8. - name: mypod
  9. image: nginx
  10. imagePullPolicy: IfNotPresent
  11. volumeMounts:
  12. - name: foo
  13. mountPath: "/mnt/aaa"
  14. readOnly: true
  15. volumes:
  16. - name: foo
  17. secret:
  18. secretName: my-secret1
  19. [root@clientvm ~]# kubectl apply -f secret-env-volume.yaml
  20. pod/test-pod-secret-volume created
  21. [root@clientvm ~]# kubectl exec test-pod-secret-volume -it -- bash
  22. root@test-pod-secret-volume:/# cat /mnt/aaa/username.txt
  23. root
  24. root@test-pod-secret-volume:/# cat /mnt/aaa/password.txt
  25. redhat