DEV Community

Cover image for #054 Kubernetes - Environment Variables ConfigMaps
Omar
Omar

Posted on

2 1

#054 Kubernetes - Environment Variables ConfigMaps

ConfigMaps

When we have a big software we need to store environment variables in a config file which controlled by config maps.

files

original files can be found on here , I will put an copy of them inside my GitHub also because I need to edit them little bit.
so just pull the folder from my Git or clone it in case your new -> here

Lab

first let's create our deployment
c

kubectl create -f app_054.yml
Enter fullscreen mode Exit fullscreen mode

then let's run it in old way not using service (because why not :p being lazy)
Alt Text

kubectl get pods
kubectl port-forward envtest-5864c59697-fkn4g 3000 
Enter fullscreen mode Exit fullscreen mode

why 3000? will because it's specified in our app inside secretapp.js.

server.listen(3000);
Enter fullscreen mode Exit fullscreen mode

also let's take a look at our dockerfile

FROM node:9.1.0-alpine
EXPOSE 3000
ENV LANGUAGE English
ENV API_KEY 123-456-789
COPY secretapp.js .
ENTRYPOINT node secretapp.js
Enter fullscreen mode Exit fullscreen mode

we can see that our envirements are LANGUAGE English and API_KEY 123-456-789.
But let's run our app now in browser (or curl) and type localhost:3000
Alt Text
it's saying

Language: Polish
API Key: 333-444-555
Enter fullscreen mode Exit fullscreen mode

because our Kubernetes will override those settings to those who are specified in our app_054.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: envtest
spec:
  selector:
    matchLabels:
      name: envtest
  replicas: 1
  template:
    metadata:
      labels:
        name: envtest
    spec:
      containers:
      - name: envtest
        image: praqma/secrets-demo
        imagePullPolicy: Always
        ports:
        - containerPort: 3000
        env:
        - name: LANGUAGE
          value: Polish
        - name: API_KEY
          value: 333-444-555
Enter fullscreen mode Exit fullscreen mode

let's now move them into a config map , I call it app_054-cfmap.yml

apiVersion: v1
kind: ConfigMap
metadata:
        name: configs
data:
        LANGUAGE: Polish
        API_KEY: 333-444-555
Enter fullscreen mode Exit fullscreen mode

let's create it now
Alt Text

kubectl create -f app_054-cfmap.yml
kubectl get cm
Enter fullscreen mode Exit fullscreen mode

After being created let's now back to edit our main file , I will make another version in file app_054-v2.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: envtest
spec:
  selector:
    matchLabels:
      name: envtest
  replicas: 1
  template:
    metadata:
      labels:
        name: envtest
    spec:
      containers:
      - name: envtest
        image: praqma/secrets-demo
        imagePullPolicy: Always
        ports:
        - containerPort: 3000
        env:
        - name: LANGUAGE
          valueFrom:
            configMapKeyRef:
              name: configs
              key: LANGUAGE
        - name: API_KEY
          valueFrom:
            configMapKeyRef:
              name: configs
              key: API_KEY
Enter fullscreen mode Exit fullscreen mode

first let's delete old deployment , and then create the newer one , and then port-forward it

kubectl delete deployment envtest
kubectl create -f app_054-v2.yml
kubectl port-forward envtest-7476d77786-wqntd 3000
Enter fullscreen mode Exit fullscreen mode

If we go to the browser again and type localhost:3000
Alt Text
We will get same results.

Extras

now let's say I need to know what is the env variables or yml file of this config map , i type
Alt Text

kubectl describe cm configs
Enter fullscreen mode Exit fullscreen mode

cm is shortcut for configmaps
Alt Text

kubectl get cm configs -o yaml
Enter fullscreen mode Exit fullscreen mode

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more