Simple Nodejs application connecting to MariaDB and Redis. App is packaged using Docker and can be deployed to Kubernetes. This application is part of the 'Introduction to K8s' presentation at the Central Wisconsin IT Conference 2020.
If you see something that could be improved either in the code or this document please feel free to open a pull request.
In order to run the project in its entirety, you will need to have :
- A git account
- A docker repository (I used Docker hub)
- Docker installed in your machine ( I used Docker Desktop on my Mac)
- Minikube installed in your machine.
Also, this document refers to the image in docker hub as bolbeck/cwit2020. You should change this to your own image name so that it can run under your own docker hub account (otherwise you will not be able to push the image out).
The app has three components, which run in 3 separate containers:
-
MariaDB database: based on the official MariaDB image. When the image is initialized, it automatically creates a test database and a test table. Sample data is loaded on image container initialization via the script in the ./MariaDB/init directory. This is run only once and only if the data volume (./MariaDB/Data) is empty.
-
Redis: Based on the official Redis image and used to cache data to be displayed in the application.
-
Nodejs: app packaged via the
Dockerfile
in the nodeApp directory. The app has three entry points:- Root ("/") just pulls writes hello world and the hostname
- /mariadb pulls data from the test DB in MariaDB and posts the data on the browser
- /redis gets data from Redis if the data has been cached. Otherwise, it pulls the data from MariaDb and caches it in Redis. Data is the posted on the browser.
Note that the application sends back pre-rendered page back to the client and uses pug as the rendering engine.
If this is the first time you are starting up the application, you will first need to create the node_modules
folder since that is not checked into source control.
If you have npm installed in your machine:
cd ./nodeApp
npm install
If you do not have npm installed in your machine, from the root folder of our repo (where we have the docker-compose file):
docker-compose run --rm nodewithdb bash
npm install
exit
The above commands will:
- Start the node app and log you into the container console
- In the container, run
npm install
to create the node_modules folder. - Exit the container and return to our host machine
Use docker-compose up
in the same directory where you have the docker-compose file to bring the application up .
Use docker-compose down
in the same directory where you have the docker-compose file to bring the application down.
To push this the node image to docker hub, we will first need to tag it properly, based in the docker hub account id. we can also give it a proper tag so that we can keep a history.
- login to docker hub, tag image and push to docker hub:
docker login --username <dockerUserId>
docker tag cwit_2020_nodewithdb <dockerUserId>/nodewithdb
docker push <dockerUserId>/nodewithdb:latest
Note that you will need to change the name of the image to match your own docker hub account
There are 4 folders containing Kubernetes manifests. The folders build on each other as we proceed through the demo:
- KubernetesWRedis: Manifests for the node app, MariaDB and Redis setup as 3 separate deployments.
- KubernetesSideCar Manifests for the node app, MariaDB and Redis but uses Redis as a sidecar for the node app. In other words, Redis runs in the same pod as the node app.
- KubernetesSecret Moves several environment variables for the node app from the configMap to a K* secret.
- KubernetesKustomize Uses Kustomize to build variant of the node app deployment manifest so that it may be deployed to different environments
The initial manifest where created using Kompose, which converted our docker-compose files to K* manifests.
Kompose out of the box may not create exactly what you need, but gets you 80% - 90% there. The final modified files are in the Kubernetes folders already, but you could recreate the original output from Kompose:
mkdir KubernetesOrig
cd KubernetesOrig
kompose --file docker-compose.yml convert
For individual manifests execute
kubectl apply -f <path/filename.yaml>
For all the manifests at once:
kubectl apply -f <Foldername>
Similarly, to delete resources created by the manifests:
kubectl delete -f <path/filename.yaml>
or
kubectl delete -f <Foldername>
To see how the resources spin or down, use the dashboard
minikube dashboard
To find the url where the application is running:
minikube service list