cafebabe1991
cafebabe1991

Reputation: 5186

How to create an environment variable in kubernetes container

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

What have I done so far ?

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 ?

Upvotes: 2

Views: 4435

Answers (2)

Rafiq
Rafiq

Reputation: 11565

  1. You can add any number of env vars into your deployment file
spec:
      containers:
        - name: auth
          image: lord/auth
          env:
            - name: MONGO_URI
              value: "mongodb://auth-mongo-srv:27017/auth"
 process.env.MONGO_URI

or you can add a secret first then use newly created secret into your countless deployment files to share same environment variable with value:

kubectl create secret generic jwt-secret --from-literal=JWT_KEY=my_awesome_jwt_secret_code
spec:
      containers:
        - name: auth
          image: lord/auth
          env:
            - name: MONGO_URI
              value: "mongodb://auth-mongo-srv:27017/auth"
            - name: JWT_KEY
              valueFrom:
                secretKeyRef:
                  name: jwt-secret
                  key: JWT_KEY
 process.env.MONGO_URI
 process.env.JWT_KEY

Upvotes: 2

Suresh Vishnoi
Suresh Vishnoi

Reputation: 18403

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

Upvotes: 4

Related Questions