How can you pass environment variables in a Kubernetes deployment using kubectl?

To pass environment variables in a Kubernetes deployment with kubectl, we can use a few methods. These methods include the kubectl set env command, YAML files, and using ConfigMaps and Secrets. They help us define important environment variables that our applications need to run well in Kubernetes. When we learn these methods, we make our applications flexible. They can easily adjust to different environments.

In this article, we will look at different ways to pass environment variables in a Kubernetes deployment using kubectl. We will talk about these solutions:

  • How to pass environment variables using kubectl set env
  • How to pass environment variables with a YAML file
  • How to pass environment variables from ConfigMaps
  • How to pass environment variables from Secrets
  • How to set environment variables for different environments

By the end of this guide, we will understand how to manage environment variables well in our Kubernetes deployments.

How can we pass environment variables in a Kubernetes deployment using kubectl with kubectl set env?

To pass environment variables in a Kubernetes deployment using kubectl, we can use the kubectl set env command. This command helps us change existing deployments or create new ones while adding environment variables.

Example

Here is how we can set an environment variable in an existing deployment:

kubectl set env deployment/my-deployment MY_ENV_VAR=my_value

This command updates the my-deployment deployment and adds or changes the environment variable MY_ENV_VAR to my_value.

Multiple Variables

We can also set multiple environment variables at the same time:

kubectl set env deployment/my-deployment VAR1=value1 VAR2=value2

From a File

If we have a file with environment variable definitions, we can use the --from-env-file option:

kubectl set env deployment/my-deployment --from-env-file=path/to/env-file

Verify the Changes

To check that our environment variables are set correctly, we can describe the deployment:

kubectl describe deployment my-deployment

We should look for the Environment section in the output to make sure our variables are there.

This way, we can easily manage environment variables in Kubernetes deployments directly from the command line using kubectl.

How can you pass environment variables in a Kubernetes deployment using kubectl with a YAML file?

To pass environment variables in a Kubernetes deployment using a YAML file, we can define the env section in the container part of our deployment manifest. Here is a simple example of a YAML setup for a deployment that has environment variables.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app-container
        image: my-app-image:latest
        env:
        - name: ENV_VAR1
          value: "value1"
        - name: ENV_VAR2
          value: "value2"

In this example, we see that:

  • The env section lists the environment variable details.
  • Each variable has a name and a value.
  • We can also use ConfigMaps or Secrets for sensitive data.

To apply this setup, we should save the YAML file (like deployment.yaml) and run this command:

kubectl apply -f deployment.yaml

This command will create or update the deployment. It also adds the environment variables we defined into the container. For more complex setups, we can look into using ConfigMaps and Secrets to manage environment variables better.

How can we pass environment variables in a Kubernetes deployment using kubectl from ConfigMaps?

We can pass environment variables in a Kubernetes deployment from ConfigMaps using the envFrom or env fields in our deployment specification. This helps us to keep the configuration separate from our application image.

Using envFrom

To use envFrom, we first need to create a ConfigMap. Here is how we can create one:

kubectl create configmap my-config --from-literal=DATABASE_URL=mysql://user:password@hostname:port/dbname

After that, we can reference this ConfigMap in our deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-container
        image: my-image:latest
        envFrom:
          - configMapRef:
              name: my-config

Using env

Instead, we can also specify individual variables from the ConfigMap using env:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-container
        image: my-image:latest
        env:
          - name: DATABASE_URL
            valueFrom:
              configMapKeyRef:
                name: my-config
                key: DATABASE_URL

Applying the Deployment

To apply our deployment configuration, we save our YAML definition to a file. For example, we can name it deployment.yaml. Then we run:

kubectl apply -f deployment.yaml

Verify Environment Variables

We can check if the environment variables are set correctly in our pods:

kubectl exec -it <pod-name> -- printenv

This way, we can manage our application’s configuration in a dynamic way. For more detailed information on using ConfigMaps, we can check Kubernetes ConfigMaps.

How can we pass environment variables in a Kubernetes deployment using kubectl from Secrets?

In Kubernetes, we can pass environment variables from Secrets to our deployment. This helps us manage sensitive information safely. To do this, we first create a Secret. Then we reference it in our deployment configuration. Let’s see how to do this with kubectl.

Step 1: Create a Secret

We can create a Secret using kubectl. We can do this by giving literal values or by using a file. Here is an example using literal values:

kubectl create secret generic my-secret \
  --from-literal=DB_PASSWORD=mysecretpassword \
  --from-literal=API_KEY=myapikey

Step 2: Change Our Deployment to Use the Secret

Next, we can reference the Secret in our deployment YAML file. We can put it under env or envFrom. Here is an example of a deployment that uses the Secret to set environment variables:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app-container
        image: my-app-image:latest
        env:
        - name: DB_PASSWORD
          valueFrom:
            secretKeyRef:
              name: my-secret
              key: DB_PASSWORD
        - name: API_KEY
          valueFrom:
            secretKeyRef:
              name: my-secret
              key: API_KEY

Step 3: Apply the Deployment

After we define the deployment, we apply it using kubectl:

kubectl apply -f deployment.yaml

Step 4: Check Environment Variables

We can check that the environment variables are set correctly in the pod. We do this by running this command:

kubectl exec -it <pod-name> -- printenv

We need to replace <pod-name> with the name of our running pod.

This method makes sure that we handle sensitive information safely in our Kubernetes deployments. For more details on managing Secrets in Kubernetes, we can look at this guide.

How can we pass environment variables in a Kubernetes deployment using kubectl for different environments?

Passing environment variables in a Kubernetes deployment for different environments is easy with a few methods. One simple way is to use different YAML files for each environment like development, testing, and production. This helps us set up the environment variables based on what each environment needs.

Using Multiple YAML Files

We can make separate YAML files for each environment. In these files, we define the environment variables for that specific case. For example:

dev-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app-dev
spec:
  replicas: 1
  template:
    spec:
      containers:
      - name: my-app
        image: my-app:dev
        env:
        - name: ENVIRONMENT
          value: "development"
        - name: DATABASE_URL
          value: "dev-db-url"

prod-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app-prod
spec:
  replicas: 2
  template:
    spec:
      containers:
      - name: my-app
        image: my-app:prod
        env:
        - name: ENVIRONMENT
          value: "production"
        - name: DATABASE_URL
          value: "prod-db-url"

We can apply the right YAML file using kubectl:

kubectl apply -f dev-deployment.yaml # for development
kubectl apply -f prod-deployment.yaml # for production

Using ConfigMaps and Secrets

For more flexible settings, we can use ConfigMaps and Secrets. We create a ConfigMap or Secret for each environment. This stores the needed environment variables.

Creating a ConfigMap for Development

kubectl create configmap app-config --from-literal=ENVIRONMENT=development --from-literal=DATABASE_URL=dev-db-url

Creating a ConfigMap for Production

kubectl create configmap app-config --from-literal=ENVIRONMENT=production --from-literal=DATABASE_URL=prod-db-url

In our deployment YAML, we can refer to these ConfigMaps:

env:
- name: ENVIRONMENT
  valueFrom:
    configMapKeyRef:
      name: app-config
      key: ENVIRONMENT
- name: DATABASE_URL
  valueFrom:
    configMapKeyRef:
      name: app-config
      key: DATABASE_URL

Using Overlays with Kustomize

Kustomize helps us manage different settings for different environments with overlays. We can set up a base configuration and then make overlays for each environment.

Base Configuration (base/deployment.yaml)

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 1
  template:
    spec:
      containers:
      - name: my-app
        image: my-app:latest
        env:
        - name: ENVIRONMENT
          value: "default"

Overlay for Development (overlays/dev/kustomization.yaml)

resources:
- ../../base
images:
- name: my-app
  newTag: dev
patchesStrategicMerge:
- patch.yaml

Patch File (overlays/dev/patch.yaml)

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  template:
    spec:
      containers:
      - name: my-app
        env:
        - name: ENVIRONMENT
          value: "development"
        - name: DATABASE_URL
          value: "dev-db-url"

Applying the Overlay

kubectl apply -k overlays/dev

Conclusion

By using separate YAML files, ConfigMaps, Secrets, or Kustomize overlays, we can manage environment variables in our Kubernetes deployments across different environments. This way, we make sure our application is set up right based on where it runs.

Frequently Asked Questions

1. How do we set environment variables in a Kubernetes deployment using kubectl?

To set environment variables in a Kubernetes deployment with kubectl, we use the kubectl set env command. This command helps us update environment variables for an existing deployment. For example, we can run kubectl set env deployment/my-deployment MY_VAR=my_value. This sets the environment variable MY_VAR to my_value in the deployment we want.

2. What is the difference between using ConfigMaps and Secrets for environment variables in Kubernetes?

In Kubernetes, we use ConfigMaps to store non-sensitive data. Secrets are for sensitive information like passwords and API keys. When we want to pass environment variables in a deployment, we can use ConfigMaps for regular settings. For sensitive data, we should use Secrets. This way, we keep our application more secure.

3. Can we pass environment variables from a YAML file in a Kubernetes deployment?

Yes, we can define environment variables right in a YAML file when we create or update a Kubernetes deployment. We just need to put the environment variables under the env section of the container. Here is an example:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-deployment
spec:
  replicas: 1
  template:
    spec:
      containers:
      - name: my-container
        image: my-image
        env:
        - name: MY_VAR
          value: my_value

4. How can we manage environment variables for different environments in Kubernetes?

To manage environment variables for different environments, we can create separate ConfigMaps or Secrets for each environment like development, staging, and production. When we deploy, we can reference the right ConfigMap or Secret based on the environment. We can do this using the kubectl command or our CI/CD pipeline. This way, we keep things consistent and secure across environments.

5. What are the best practices for managing sensitive environment variables in Kubernetes?

When we manage sensitive environment variables in Kubernetes, we should always use Secrets. We should not use plain text in our deployment settings. Also, we need to control access to Secrets using Role-Based Access Control (RBAC). It is good to check our Secrets regularly. We can also think about using external tools for secret management to make security better. For more details on how to manage Kubernetes secrets securely, check out how do I manage secrets in Kubernetes securely.