Home > other >  Kubernetes: Set environment variable in all pods
Kubernetes: Set environment variable in all pods

Time:01-28

Is it possible to provide environment variables which will be set in all pods instead of configuring in each pods spec?

If not natively possible in Kubernetes, what would be an efficient method to accomplish it? We have Helm, but that still requires a lot of duplication.

This old answer suggested "PodPreset" which is no longer part of Kubernetes: Kubernetes - Shared environment variables for all Pods

CodePudding user response:

You could do this using a mutating admission webhook to inject the environment variable into the pod manifest.

There are more details on implementing webhooks here.

CodePudding user response:

I am not sure if you can do that for EVERY single pod in the cluster (if that is what you meant), but you CAN do it for every single pod within an application or service.

For example, via a Deployment, you can set a variable within the pod template, and all replicas will carry that value.

apiVersion: apps/v1
kind: Deployment
metadata:
  ...
spec:
  replicas: 5
  template:
    metadata:
      ...
    spec:
      containers:
      - image: nginx
        name: nginx
        ...
        env:
        - name: VAR_NAME      # <---
          value: "var_value"  # <---
        ...

In this (edited) example, all 5 replicas of the nginx will have the environment variable VAR_NAME set to the value var_value.

You could also use a configmap (https://kubernetes.io/docs/concepts/configuration/configmap/) or secrets (https://kubernetes.io/docs/concepts/configuration/secret/#using-secrets-as-environment-variables) to set environments variables from a shared location, depending on your requirements.

  •  Tags:  
  • Related