K8S deployments with shared environment variables
Asked Answered
W

2

10

We have a set of deployments (sets of pods) that are all using same docker image. Examples:

  • web api
  • web admin
  • web tasks worker nodes
  • data tasks worker nodes
  • ...

They all require a set of environment variables that are common, for example location of the database host, secret keys to external services, etc. They also have a set of environment variables that are not common.

Is there anyway where one could either:

  1. Reuse a template where environment variables are defined
  2. Load environment variables from file and set them on the pods

The optimal solution would be one that is namespace aware, as we separate the test, stage and prod environment using kubernetes namespaces.

Something similar to dockers env_file would be nice. But I cannot find any examples or reference related to this. The only thing I can find is setting env via secrets, but that is not clean, way to verbose, as I still need to write all environment variables for each deployment.

Wichman answered 15/4, 2018 at 19:27 Comment(1)
Could possibly use a PodPreset, see kubernetes.io/docs/tasks/inject-data-application/podpresetTakamatsu
E
19

You can create a ConfigMap with all the common key:value pairs of env variables.

Then you can reuse the configmap to declare all the values of configMap as environment in Deployment.

Here is an example taken from kubernetes official docs.

Create a ConfigMap containing multiple key-value pairs.

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

Use envFrom to define all of the ConfigMap’s data as Pod environment variables. The key from the ConfigMap becomes the environment variable name in the Pod.

apiVersion: v1
kind: Pod
metadata:
  name: test-pod
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ "/bin/sh", "-c", "env" ]
      envFrom:
      - configMapRef:
          name: special-config # All the key-value pair will be taken as environment key-value pair
      env:
      - name: uncommon
        value: "uncommon value"
  restartPolicy: Never

You can specify uncommon env variables in env field.

Now, to verify if the environment variables are actually available, see the logs.

$ kubectl logs -f test-pod 
KUBERNETES_PORT=tcp://10.96.0.1:443
SPECIAL_LEVEL=very
uncommon=uncommon value
SPECIAL_TYPE=charm
...

Here, it is visible that all the provided environments are available.

Ens answered 15/4, 2018 at 21:17 Comment(1)
Thank you so much, I'd double up if I couldFellini
O
0

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
apiVersion: apps/v1
kind: Deployment
metadata:
  name: auth-depl
spec:
  replicas: 1
  selector:
    matchLabels:
      app: auth
  template:
    metadata:
      labels:
        app: auth
    spec:
      containers:
        - name: auth
          image: lord/auth
          resources:
            requests:
              memory: "128Mi"
              cpu: "250m"
            limits:
              memory: "256Mi"
              cpu: "500m"
          env:
            - name: JWT_KEY
              valueFrom:
                secretKeyRef:
                  name: jwt-secret
                  key: JWT_KEY
 process.env.JWT_KEY
apiVersion: apps/v1
kind: Deployment
metadata:
  name: tickets-depl
spec:
  replicas: 1
  selector:
    matchLabels:
      app: tickets
  template:
    metadata:
      labels:
        app: tickets
    spec:
      containers:
        - name: tickets
          image: lord/tickets
          resources:
            requests:
              memory: "128Mi"
              cpu: "250m"
            limits:
              memory: "256Mi"
              cpu: "500m"
          env:
            - name: JWT_KEY
              valueFrom:
                secretKeyRef:
                  name: jwt-secret
                  key: JWT_KEY
 process.env.JWT_KEY
Opportune answered 3/5, 2021 at 12:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.