Giter Site home page Giter Site logo

msconf's Introduction

MSCONF project

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

Steps to make it work

Install minikube

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

Clone the project

You need to clone the project
git clone https://github.com/amiriry/msconf.git

Make minikube work with local docker images

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
    back to original docker deamon:

If you want to go back to original docker configuration do:
unset $(export | grep DOCKER | awk -F'[ =]' '{print $3}' | xargs)


Build the container image

Go into build/ dir, where all the application code is.
Run:
docker build -t confms .

    Notice the dot in the end, very important for context of the build

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

Install Helm Chart

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

Test the MS

Define pods as variables

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}')

-- Do the API test --

   test /healthy

kubectl exec $MS_TEST_POD -- curl -s http://microservice:5000/healthy

   test /get_variable
# 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
   test /set_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

-- Do the PYTEST tests --

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.

msconf's People

Contributors

kingbryant42 avatar

Watchers

Amir shkedy avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.