英文:
Accessing environment variables from a pod
问题
我写了一个使用export var name = somevalue设置的环境变量的golang程序。
cloudType = os.Getenv("CLOUD_TYPE")
clusterRegion = os.Getenv("CLUSTER_REGION")
clusterType = os.Getenv("CLUSTER_TYPE")
clusterName = os.Getenv("CLUSTER_NAME")
clusterID = os.Getenv("CLUSTER_ID")
如上所述,我的程序尝试使用getenv函数从系统中设置的环境变量中获取值。如果直接运行程序并获取环境变量的值,它可以正常工作。但是,当我尝试构建一个镜像并在pod中运行时,它无法获取环境变量的值,而是返回空值。有没有办法从pod中访问本地环境变量?
英文:
I wrote golang program which fetches values from environment variable set in my system using export var name = somevalue.
cloudType = os.Getenv("CLOUD_TYPE")
clusterRegion = os.Getenv("CLUSTER_REGION")
clusterType = os.Getenv("CLUSTER_TYPE")
clusterName = os.Getenv("CLUSTER_NAME")
clusterID = os.Getenv("CLUSTER_ID")
As mentioned above my program tries to fetch values from env var set in system using getenv func.The program is working good if run it and fetching values from env variables. But When I tried building a image and running it inside a pod it was able to fetch values from the env var. It is giving empty values. Is there any way to access the local env var from the pod?
答案1
得分: 1
创建一个类似这样的yaml文件来定义一个配置映射:
apiVersion: v1
data:
CLOUD_TYPE: "$CLOUD_TYPE"
CLUSTER_REGION: "$CLUSTER_REGION"
CLUSTER_TYPE: "$CLUSTER_TYPE"
CLUSTER_NAME: "$CLUSTER_NAME"
CLUSTER_ID: "$CLUSTER_ID"
kind: ConfigMap
metadata:
creationTimestamp: null
name: foo
确保你的配置变量已设置,然后将其应用到你的集群,首先进行环境变量替换:
envsubst < foo.yaml | kubectl apply -f
然后在Pod定义中使用该配置映射:
spec:
containers:
- name: mypod
envFrom:
- configMapRef:
name: foo
英文:
Make a yaml file like this to define a config map
apiVersion: v1
data:
CLOUD_TYPE: "$CLOUD_TYPE"
CLUSTER_REGION: "$CLUSTER_REGION"
CLUSTER_TYPE: "$CLUSTER_TYPE"
CLUSTER_NAME: "$CLUSTER_NAME"
CLUSTER_ID: "$CLUSTER_ID"
kind: ConfigMap
metadata:
creationTimestamp: null
name: foo
Ensure your config vars are set then apply it to your cluster, with env substitution first
envsubst < foo.yaml | kubectl apply -f
Then in the pod definition use the config map
spec:
containers:
- name: mypod
envFrom:
- configMapRef:
name: foo
答案2
得分: 0
您好!以下是翻译好的内容:
在 Pod 中,您在主机上设置的环境变量不会自动传递给 Pod。您可以在 Pod 的规范中设置环境变量,然后在容器中访问。一种常见的方法是使用 envsubst < draft-spec.yaml > final-spec.yaml
将规范中的环境变量替换为主机上的变量。例如,如果您有以下规范:
apiVersion: v1
kind: Pod
metadata:
labels:
run: busybox
name: busybox
spec:
restartPolicy: Never
containers:
- name: busybox
image: busybox
imagePullPolicy: IfNotPresent
command: ["ash","-c","echo ${CONTAINER_MESSAGE}"]
env:
- name: CONTAINER_MESSAGE
value: $HOST_MESSAGE
您可以运行 HOST_MESSAGE='hello, world!' envsubst '{$HOST_MESSAGE}' < busybox.yaml | kubectl apply -f -
。这将用"hello, world!"替换$HOST_MESSAGE,但不会影响${CONTAINER_MESSAGE}。这种方法不依赖于 ConfigMap,并且允许您在部署后使用 kubectl set env
更新变量。
英文:
...haven't set the env var in the pod. I set it locally in my system
Environment variables set on your host are not automatically pass on to the Pod. You can set the env in your spec and access by your container. A common approach to substitute environment variables in the spec with variables on the host is using envsubst < draft-spec.yaml > final-spec.yaml
. Example if you have spec:
apiVersion: v1
kind: Pod
metadata:
labels:
run: busybox
name: busybox
spec:
restartPolicy: Never
containers:
- name: busybox
image: busybox
imagePullPolicy: IfNotPresent
command: ["ash","-c","echo ${CONTAINER_MESSAGE}"]
env:
- name: CONTAINER_MESSAGE
value: $HOST_MESSAGE
You can run HOST_MESSAGE='hello, world!' envsubst '{$HOST_MESSAGE}' < busybox.yaml | kubectl apply -f -
. This will substitute $HOST_MESSAGE with "hello, world!" but will not touch ${CONTAINER_MESSAGE}. This approach does not depends on ConfigMap and it allows you to use kubectl set env
to update the variable after deployed.
答案3
得分: 0
似乎你没有在镜像中设置环境变量。
首先,你需要确保在你的镜像或者Pod中设置了环境变量。在镜像中,你需要在Dockerfile中使用ENV命令。文档。在Kubernetes的Pod中,文档。
其次,你提到你想从你的Pod中获取运行时环境变量,你可以运行以下命令。
kubectl exec -it ${POD_NAME} -- printenv
英文:
It seems you set the env var not in the image.
First, you need ensure that you set up env in your image or pods. In image, you need to use ENV in your Dockerfile. doc. In Kubernetes pod, doc.
Second, you mentioned you want to get runtime env vars from your pod, you can run below command.
kubectl exec -it ${POD_NAME} -- printenv
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论