Kubernetes equivalent of env-file in Docker

Johan :

Background:

Currently we're using Docker and Docker Compose for our services. We have externalized the configuration for different environments into files that define environment variables read by the application. For example a prod.env file:

ENV_VAR_ONE=Something Prod
ENV_VAR_TWO=Something else Prod

and a test.env file:

ENV_VAR_ONE=Something Test
ENV_VAR_TWO=Something else Test

Thus we can simply use the prod.env or test.env file when starting the container:

docker run --env-file prod.env <image>

Our application then picks up its configuration based on the environment variables defined in prod.env.

Questions:

  1. Is there a way to provide environment variables from a file in Kubernetes (for example when defining a pod) instead of hardcoding them like this:
apiVersion: v1
kind: Pod
metadata: 
  labels: 
    context: docker-k8s-lab
    name: mysql-pod
  name: mysql-pod
spec: 
  containers: 
    - 
      env: 
        - 
          name: MYSQL_USER
          value: mysql
        - 
          name: MYSQL_PASSWORD
          value: mysql
        - 
          name: MYSQL_DATABASE
          value: sample
        - 
          name: MYSQL_ROOT_PASSWORD
          value: supersecret
      image: "mysql:latest"
      name: mysql
      ports: 
        - 
          containerPort: 3306
  1. If this is not possible, what is the suggested approach?
Pixel Elephant :

You can populate a container's environment variables through the use of Secrets or ConfigMaps. Use Secrets when the data you are working with is sensitive (e.g. passwords), and ConfigMaps when it is not.

In your Pod definition specify that the container should pull values from a Secret:

apiVersion: v1
kind: Pod
metadata: 
  labels: 
    context: docker-k8s-lab
    name: mysql-pod
  name: mysql-pod
spec: 
  containers:
  - image: "mysql:latest"
    name: mysql
    ports: 
    - containerPort: 3306
    envFrom:
      - secretRef:
         name: mysql-secret

Note that this syntax is only available in Kubernetes 1.6 or later. On an earlier version of Kubernetes you will have to specify each value manually, e.g.:

env: 
- name: MYSQL_USER
  valueFrom:
    secretKeyRef:
      name: mysql-secret
      key: MYSQL_USER

(Note that env take an array as value)

And repeating for every value.

Whichever approach you use, you can now define two different Secrets, one for production and one for dev.

dev-secret.yaml:

apiVersion: v1
kind: Secret
metadata:
  name: mysql-secret
type: Opaque
data:
  MYSQL_USER: bXlzcWwK
  MYSQL_PASSWORD: bXlzcWwK
  MYSQL_DATABASE: c2FtcGxlCg==
  MYSQL_ROOT_PASSWORD: c3VwZXJzZWNyZXQK

prod-secret.yaml:

apiVersion: v1
kind: Secret
metadata:
  name: mysql-secret
type: Opaque
data:
  MYSQL_USER: am9obgo=
  MYSQL_PASSWORD: c2VjdXJlCg==
  MYSQL_DATABASE: cHJvZC1kYgo=
  MYSQL_ROOT_PASSWORD: cm9vdHkK

And deploy the correct secret to the correct Kubernetes cluster:

kubectl config use-context dev
kubectl create -f dev-secret.yaml

kubectl config use-context prod
kubectl create -f prod-secret.yaml

Now whenever a Pod starts it will populate its environment variables from the values specified in the Secret.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Kubernetes env-file

Equivalent of --env-file for build-arg?

env variables not substituted in docker file

How to build Docker with env file

Kubernetes yaml file - Same property in env and envFrom

Is it possible to source a `.env` file to create Kubernetes secrets?

What's the kubectl equivalent of docker exec bash in Kubernetes?

Docker image ENV cant find a file?

How to hide env variables from docker file

Docker Swarm with image versions externalized to .env file

Hidden file .env not copied using Docker COPY

.env file not getting sourced in Alpine Linux Docker

Django CSRF failing with .env file for Docker

Regarding passing file as env variable in docker container

Specify the env file docker compose uses

Azure DevOps copying .env file to docker image

GitHub CI - Docker .env file not found

Docker Compose: .env: no such file or directory / NodeJS

Is there an equivalent to "minikube docker-env" for using Docker in K8s?

kubernetes - creating secrets from env file configured to a certain namespace

rendering env-var inside kubernetes kubeconfig yaml file

equivalent of --net=host in docker compose file when creating docker container

Kubernetes cronjob no file or directory but it's in the docker image

Issue in converting a Docker image to a Kubernetes Deployment File

Pass file to docker container in a kubernetes Pod

Docker --env-file - executable file not found in $PATH

How to update configuration file (.env file) while running docker container

docker compose env_file not working inside my compose file

Docker compose obtain env variables from .env file and pass to docker build