for more general environment variables go here.
This is an example project for very simple microservice that environment variables.
It also defines one, which makes the tests much easier to understand. Its name is MY_ENVIRONMENT_VARIABLE.
It is first defined with configmap named myconfigfile.yaml
with the value: vvvvv
You can run this project on any kubernetes infratructure you want, I used minikube
because its very easy and its on your local machine.
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube
You need to clone the project
git clone https://github.com/amiriry/msconf.git
eval $(minikube -p minikube docker-env)
This command will define all the variabes needed for kubectl
to point to the docker engine
inside minikube which is on port 2376 on the node that minikube starts.
You can see this ip with the command:
kubectl get nodes -owide
The environment variables that should be defined:
export | grep DOCKER
You should see all of these with values:
DOCKER_CERT_PATH
DOCKER_HOST
DOCKER_TLS_VERIFY
MINIKUBE_ACTIVE_DOCKERD
If you want to go back to original docker configuration do:
unset $(export | grep DOCKER | awk -F'[ =]' '{print $3}' | xargs)
Go into build/
dir, where all the application code is.
Run:
docker build -t confms .
We need to check that the image does exist in minikube registry:
minikube image ls | grep confms
The output should contain something like this:
docker.io/library/confms:latest
Go to the base folder msconf
cd ../
Do the command:
helm install myconfapp myconfapp/ --values myconfapp/values.yaml
Now you should see all charts resources up:
kubectl get all --show-labels | egrep -v "NAME|^$" | awk '{print $1}'
output:
pod/ms-demo-5c6fffd499-pp6rm
pod/ms-test-6b5c8996b-z7xmp
service/kubernetes
service/microservice
deployment.apps/ms-demo
deployment.apps/ms-test
replicaset.apps/ms-demo-5c6fffd499
replicaset.apps/ms-test-6b5c8996b
Notice:
There are 2 deployments
ms-demo - Where the actual code is running
ms-test - Tests to the ms api are going to be done from the pod in this deployment
For you to tests this ms more easily, you need to define env vars for the pods.
Use these commands:
MS_TEST_POD=$(kubectl get pod | grep "ms-test" | awk '{print $1}')
MS_DEMO_POD=$(kubectl get pod | grep "ms-demo" | awk '{print $1}')
kubectl exec $MS_TEST_POD -- curl -s http://microservice:5000/healthy
# Get value from configmap
kubectl get configmap myconfig -o jsonpath='{.data}{"\n"}'
# Get the environment variable from the api, using os.environ['MY_ENVIRONMENT_VARIABLE']
kubectl exec $MS_TEST_POD -- curl -s http://microservice:5000/get_variable
# First set the variable
kubectl exec $MS_TEST_POD -- curl -s -X POST http://microservice:5000/set_variable?new=aaaaa
# Check that it is different from before (what was defined as 'new' in set_variable request)
kubectl exec $MS_TEST_POD -- curl -s http://microservice:5000/get_variable
You can look in the file myconfapp/test_api.py
, it defines all the tests.
The way to run it is from is from where the code is, meaning from the ms pod.
That's the reason we defined MS_DEMO_POD variable.
To see the results:
kubectl exec $MS_DEMO_POD -- pytest -rA
The result should be:
==================================== PASSES ====================================
=========================== short test summary info ============================
PASSED test_api.py::test_healthy
PASSED test_api.py::test_get_var
PASSED test_api.py::test_set_var
============================== 3 passed in 0.07s ===============================
The tests names are self explanatory.