I have used minikube as an example of a cloud-premise. This application has been packaged into a helmchart for sake of automation in deployment. Helming also makes our job easier to upgrade and track applications.
This is a modified version of (https://github.com/RikKraanVantage/kubernetes-flask-mysql) with Ingress
- Deploys a Flask API to perform CRUD operations in the MySQL database
- Deploys a MySQL server on a Kubernetes cluster
- Deploys an ingress that acts as kind of a reverse proxy to the Flask Webserver
-
Clone the repository
-
Configure
Docker
to use theDocker daemon
in your kubernetes cluster via your terminal:eval $(minikube docker-env)
-
Build a flask-api image with the Dockerfile in this repo:
docker build . -t flask-api
-
helm repo add <repo-name> https://raw.githubusercontent.com/Nibamot/sample_lnmp_stack/master
-
helm install <custom-chart-name> <repo/chart-name>
to install the helm chart. Once this is done you can check the status of all the resources we introduced, including deployments, services,config-maps, secrets,HPAs etc. -
This chart can be removed simply by doing a
helm uninstall <custom-chart-name>
-
Connect to your
MySQL database
by setting up a temporary pod as amysql-client
:kubectl run -it --rm --image=mysql --restart=Never mysql-client -- mysql --host mysql --password=<super-secret-password>
make sure to enter the (decoded) password specified in theflaskapi-secrets.yml
-
Create the database and table
CREATE DATABASE flaskapi;
USE flaskapi;
CREATE TABLE users(user_id INT PRIMARY KEY AUTO_INCREMENT, user_name VARCHAR(255), user_email VARCHAR(255), user_password VARCHAR(255));
The API can be accessed by exposing it using minikube: minikube service flask-service
. This will return a URL
. If you paste this to your browser you will see the hello world
message. You can use this service_URL
to make requests to the API
Now you can use the API
to CRUD
your database
- add a user:
curl -H "Content-Type: application/json" -d '{"name": "<user_name>", "email": "<user_email>", "pwd": "<user_password>"}' <service_URL>/create
- get all users:
curl <service_URL>/users
- get information of a specific user:
curl <service_URL>/user/<user_id>
- delete a user by user_id:
curl -H "Content-Type: application/json" <service_URL>/delete/<user_id>
- update a user's information:
curl -H "Content-Type: application/json" -d {"name": "<user_name>", "email": "<user_email>", "pwd": "<user_password>", "user_id": <user_id>} <service_URL>/update
Ingress basically provides external access to services within the cluster You can read more about ingress here:(https://kubernetes.io/docs/concepts/services-networking/ingress/). Ingress resources only work with Ingress Controllers. These are not started by default as part of a cluster. There is a list of available ingress controllers on this page(https://kubernetes.io/docs/concepts/services-networking/ingress-controllers/). In this example we install the nginx ingress controller within minikube.