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
kubectl create -f app_054.yml
then let's run it in old way not using service (because why not :p being lazy)
kubectl get pods
kubectl port-forward envtest-5864c59697-fkn4g 3000
why 3000? will because it's specified in our app inside secretapp.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 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
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
apiVersion: v1
kind: ConfigMap
metadata:
name: configs
data:
LANGUAGE: Polish
API_KEY: 333-444-555
let's create it now
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
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
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 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
kubectl describe cm configs
cm is shortcut for configmaps
kubectl get cm configs -o yaml