#054 Kubernetes - Environment Variables ConfigMaps
Tags: kubernetes, devops, tutorial
Published: 2020-08-25 17:10:38

# 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](https://github.com/eficode-academy/kubernetes-katas/tree/master/secrets) , 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](https://github.com/OmarElKhatibCS/DevOpsJourney)

# Lab
first let's create our deployment
![c](https://dev-to-uploads.s3.amazonaws.com/i/8z3g21grtkfiahsp1mn5.png)
```bash
kubectl create -f app_054.yml
```
then let's run it in old way not using service (because why not :p being lazy)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/vpm3prfhg8reh7clxbkl.png)
```bash
kubectl get pods
kubectl port-forward envtest-5864c59697-fkn4g 3000 
```
why 3000? will because it's specified in our app inside secretapp.js.
```js
server.listen(3000);
```
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
```
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](http://127.0.0.1:3000/)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/nctvtu2pwu9fm651zymy.png)
it's saying 
```
Language: Polish
API Key: 333-444-555
```
because our Kubernetes will override those settings to those who are specified in our app_054.yml
```yaml
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
```
let's now move them into a config map , I call it app_054-cfmap.yml
```yml
apiVersion: v1
kind: ConfigMap
metadata:
        name: configs
data:
        LANGUAGE: Polish
        API_KEY: 333-444-555
```
let's create it now
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/8jylrlwvgvwagzghydbx.png)
```bash
kubectl create -f app_054-cfmap.yml
kubectl get cm
```
After being created let's now back to edit our main file , I will make another version in file app_054-v2.yml
```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
```
first let's delete old deployment , and then create the newer one , and then port-forward it
```bash
kubectl delete deployment envtest
kubectl create -f app_054-v2.yml
kubectl port-forward envtest-7476d77786-wqntd 3000
```
If we go to the browser again and type [localhost:3000](http://127.0.0.1:3000/)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/nctvtu2pwu9fm651zymy.png)
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](https://dev-to-uploads.s3.amazonaws.com/i/ll3157y4rzckru0j6hz1.png)
```bash
kubectl describe cm configs
```
cm is shortcut for configmaps
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/2hcdbgnh5i288qftdnw0.png)
```bash
kubectl get cm configs -o yaml
```