How to create an environment variable in kubernetes container

9/21/2019

I am trying to pass an environment variable in kubernetes container.

What have I done so far ?

  • Create a deployment

    kubectl create deployment foo --image=foo:v1

  • Create a NODEPORT service and expose the port

    kubectl expose deployment/foo --type=NodePort --port=9000

  • See the pods

    kubectl get pods

  • dump the configurations (so to add the environment variable)

    kubectl get deployments -o yaml > dev/deployment.yaml

    kubectl get svc -o yaml > dev/services.yaml

    kubectl get pods -o yaml > dev/pods.yaml

  • Add env variable to the pods env:

    • name: FOO_KEY value: "Hellooooo"
  • Delete the svc,pods,deployments

    kubectl delete -f dev/ --recursive

  • Apply the configuration

    kubectl apply -f dev/ --recursive

  • Verify env parameters

    kubectl describe pods

Something weird

If I manually changed the meta information of the pod yaml and hard code the name of the pod. It gets the env variable. However, this time two pods come up one with the hard coded name and other with the hash with it. For example, if the name I hardcoded was "foo", two pods namely foo and foo-12314faf (example) would appear in "kubectl get pods". Can you explain why ?

Question

Why does the verification step does not show the environment variable ?

-- cafebabe1991
kubernetes

1 Answer

9/21/2019

As the issue is resolved in the comment section.

If you want to set env to pods I would suggust you to use set sub commend

kubectl set env --help will provide more detail such as list the env and create new one

Examples:
  # Update deployment 'registry' with a new environment variable
  kubectl set env deployment/registry STORAGE_DIR=/local

  # List the environment variables defined on a deployments 'sample-build'
  kubectl set env deployment/sample-build --list

Deployment enables declarative updates for Pods and ReplicaSets. Pods are not typically directly launched on a cluster. Instead, pods are usually managed by replicaSet which is managed by deployment.

following thread discuss what-is-the-difference-between-a-pod-and-a-deployment

-- Suresh Vishnoi
Source: StackOverflow