kubernetes/将值注入configMap的最佳实践
我是kubernetes的新员工,我想知道向其注入价值的最佳方式 现在,我定义了部署对象,它从ConfigMap文件中获取相关值。我希望将相同的kubernetes/将值注入configMap的最佳实践,kubernetes,Kubernetes,我是kubernetes的新员工,我想知道向其注入价值的最佳方式 现在,我定义了部署对象,它从ConfigMap文件中获取相关值。我希望将相同的.yml文件用于我的生产和登台环境。因此,只有configMap中的值将被更改,而文件本身将是相同的 有没有办法不用配置管理工具(如Ansible、puppet等)在kubernetes中内置完成此操作?您可以在答案的末尾找到引用文本的链接 编写应用程序时的一个好做法是将应用程序代码与配置分开。我们希望让应用程序作者能够轻松地在Kubernetes中使用
.yml
文件用于我的生产和登台环境。因此,只有configMap中的值将被更改,而文件本身将是相同的
有没有办法不用配置管理工具(如Ansible、puppet等)在kubernetes中内置完成此操作?您可以在答案的末尾找到引用文本的链接 编写应用程序时的一个好做法是将应用程序代码与配置分开。我们希望让应用程序作者能够轻松地在Kubernetes中使用此模式。尽管Secrets API允许将凭证和密钥等信息从应用程序中分离出来,但在过去,普通的非机密配置中不存在对象。在Kubernetes 1.2中,我们添加了一个名为ConfigMap的新API资源来处理此类配置数据 此外,机密数据将以base64编码的形式存储,这也适用于二进制数据,如密钥,而ConfigMaps数据将以纯文本格式存储,这对于文本文件很好 ConfigMapAPI在概念上很简单。从数据的角度来看,ConfigMap类型只是一组键值对 有几种方法可以创建配置映射:
- 在命令行中使用值列表
$ kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm
- 使用磁盘上的文件作为数据源
$ kubectl create configmap game-config-2 --from-file=docs/user-guide/configmap/kubectl/game.properties --from-file=docs/user-guide/configmap/kubectl/ui.properties $ kubectl create configmap game-config-3 --from-file=game-special-key=docs/user-guide/configmap/kubectl/game.properties
$ kubectl create configmap game-config --from-file=configure-pod-container/configmap/kubectl/
- 使用带有文件的目录作为数据源
$ kubectl create configmap game-config-2 --from-file=docs/user-guide/configmap/kubectl/game.properties --from-file=docs/user-guide/configmap/kubectl/ui.properties $ kubectl create configmap game-config-3 --from-file=game-special-key=docs/user-guide/configmap/kubectl/game.properties
$ kubectl create configmap game-config --from-file=configure-pod-container/configmap/kubectl/
- 结合前面提到的三种方法
- 将ConfigMap中的值用作环境变量
spec: containers: - name: test-container image: k8s.gcr.io/busybox command: [ "/bin/sh", "-c", "echo $(SPECIAL_LEVEL_KEY)" ] env: - name: SPECIAL_LEVEL_KEY valueFrom: configMapKeyRef: name: special-config key: SPECIAL_LEVEL
- 将ConfigMap中的数据用作卷上的文件
spec: containers: - name: test-container image: k8s.gcr.io/busybox command: [ "/bin/sh", "-c", "ls /etc/config/" ] volumeMounts: - name: config-volume mountPath: /etc/config volumes: - name: config-volume configMap: # ConfigMap containing the files name: special-config
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: k8s.gcr.io/busybox
command: [ "/bin/sh", "-c", "env" ]
envFrom:
- configMapRef:
name: special-config
restartPolicy: Never
这同样适用于机密,可由secretMapRef引用