1

For example, I run a Pod in a public cloud cluster. The Pod has a main container running the app. The cluster has an environment variable named ABC. Within the main container, I wish to access the environment variable ABC. What is the best way to do so?

1
  • When you say "the cluster has an environment variable", where is that variable and how is it set? Typically a cluster would consist of several nodes (or hundreds) and an individual pod has pretty limited access to things outside the container level. Commented Sep 14, 2021 at 10:26

1 Answer 1

2

Very simple option

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"

Read more : https://kubernetes.io/docs/tasks/inject-data-application/define-environment-variable-container/

Option 1

If variables is not much important you can use the configmap to store the and config map will get injected to POD and your app can access the variables from OS.

Read more about configmap : https://kubernetes.io/docs/concepts/configuration/configmap/

Example configmap :

apiVersion: v1
kind: ConfigMap
metadata:
  name: special-config
  namespace: default
data:
  SPECIAL_LEVEL: very
  SPECIAL_TYPE: charm 

example pod

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

You can also inject files with list of variables as per requirement.

Option 2 :

You can also use secret if your variable is important : https://kubernetes.io/docs/concepts/configuration/secret/

Option 3 :

If you want to go with best practices and options you can use the vault with Kubernetes for managing all different microservice variables management.

Vault : https://www.vaultproject.io/

Example : https://github.com/travelaudience/kubernetes-vault-example

it key-value pair management and provides good security options also.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks! I was looking at a way that doesn't explicitly require usage of ConfigMap, Secret, or Vault, but it looks like there is no such way. ConfigMap should be the best option for my use cases. :)
yes, or else you can use first option adding env, env: - name: DEMO_GREETING value: "Hello from the environment"
if you dont want explicit you can mention in the application it self in .env file or in config file that's only option.
Noted, thanks for the information of the alternative options. :)
thanks, glad to hear that, you can also update the status of question if the answer resolves your issue. that's how it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.