例如,我在公共云集群中运行一个 Pod。Pod 有一个运行应用程序的主容器。集群有一个名为 的环境变量ABC
。在主容器中,我希望访问环境变量ABC
。这样做的最佳方法是什么?
非常简单的选择
apiVersion: v1
kind: Pod
metadata:
name: envar-demo
labels:
purpose: demonstrate-envars
spec:
containers:
- name: envar-demo-container
image: gcr.io/google-samples/node-hello:1.0
env:
- name: DEMO_GREETING
value: "Hello from the environment"
- name: DEMO_FAREWELL
value: "Such a sweet sorrow"
阅读更多:https : //kubernetes.io/docs/tasks/inject-data-application/define-environment-variable-container/
选项1
如果变量不是很重要,您可以使用 configmap 来存储和 config map 将被注入到 POD 并且您的应用程序可以从操作系统访问变量。
阅读有关 configmap 的更多信息:https ://kubernetes.io/docs/concepts/configuration/configmap/
示例配置映射:
apiVersion: v1
kind: ConfigMap
metadata:
name: special-config
namespace: default
data:
SPECIAL_LEVEL: very
SPECIAL_TYPE: charm
示例吊舱
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
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:
# Provide the name of the ConfigMap containing the files you want
# to add to the container
name: special-config
restartPolicy: Never
您还可以根据需要注入带有变量列表的文件。
选项2:
如果您的变量很重要,您也可以使用秘密:https : //kubernetes.io/docs/concepts/configuration/secret/
选项 3:
如果您想采用最佳实践和选项,您可以将保管库与 Kubernetes 一起使用来管理所有不同的微服务变量管理。
保险库:https : //www.vaultproject.io/
示例:https : //github.com/travelaudience/kubernetes-vault-example
键值对管理并提供良好的安全选项。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句