Simple containerized application with Kubernetes. This is a BucketList application which has backend, frontend and database deployed using Kubernetes.
This example can be deployed in microk8s.
- MicroK8s version located in
k8s-microk8s
folder - Helm charts are located in
k8s-microk8s-chart
folder
This section provides information on the location and purpose of Kubernetes-related files.
The k8-microk8s/
folder contains artifacts for creating Kubernetes objects using the command kubectl apply -f k8-specs.
The k8app-charts/
folder contains artifacts to assist in the creation of Helm charts using the command helm install
k8app ./k8app-chart. These artifacts will help to streamline the deployment process for your Kubernetes application.
We defined the following roles:
- Developer
- Can fully manage configmaps, services, pods, and secrets in the default namepsace
- Test commands:
kubectl auth can-i list pod --namespace default --as user_developer
- Expected answer is
yes
- Expected answer is
kubectl auth can-i list deploy --namespace default --as user_developer
- Expected answer is
no
- Expected answer is
- Devops
- Can fully manage ingress, deployments, cert-manager, replicaset, and secrets in the default namepsace
- Test commands:
kubectl auth can-i list deploy --namespace default --as user_devops
- Expected answer is
yes
- Expected answer is
kubectl auth can-i list pod --namespace default --as user_devops
- Expected answer is
no
- Expected answer is
- Security
- Can fully manage configmaps and secrets in all namespaces
- Test commands:
kubectl auth can-i list secrets --namespace default --as user_security
- Expected answer is
yes
- Expected answer is
kubectl auth can-i list deploy --namespace default --as user_security
- Expected answer is
no
- Expected answer is
In order to ensure that the application works on local version it is necessary to add the following lines to your /etc/hosts file (or equivalent on a non Linux based OS):
127.0.0.1 k8app.com
127.0.0.1 backend.k8app.com
-
Enable dns:
microk8s enable dns
-
Enable cert-manager:
microk8s enable cert-manager
-
Enable ingress:
microk8s enable ingress
-
Enable ha-cluster:
microk8s enable ha-cluster
-
Enable observability:
microk8s enable observability
-
Enable rbac:
microk8s enable rbac
-
Push Frontend Image:
docker buildx build -f frontend.dockerfile --platform linux/amd64 -t caxolahop3/sc-frontend-v2 . --push
-
Push Backend Image:
docker buildx build -f backend.dockerfile --platform linux/amd64 -t caxolahop3/sc-backend-v2 . --push
-
You then have two choices for deploying the application, either using K8s directly or using helm
- Using K8s directly: Navigate to
k8s-microk8s
folder and run:microk8s kubectl apply -f .
to install the applicationmicrok8s kubectl delete -f .
to uninstall the applicationmicrok8s kubectl apply -f .
to upgrade the application, when there is a change that can take effect, it will.
- Using helm: Navigate to the root folder of this repository:
microk8s helm3 install k8app k8s-microk8s-chart/
to install the applicationmicrok8s helm3 uninstall k8app
to uninstall the applicationmicrok8s helm3 upgrade k8app k8s-microk8s-chart/
to upgrade the application, when there is a change that can take effect, it will.
- Using K8s directly: Navigate to
-
Follow https://backend.k8app.com for backend and https://k8app.com for frontend
- Make a change in your deployment file (Change the image tag or the limits)
- Create configmaps.
- Execute
kubectl apply -f [FILE_NAME]
- Validate rollout satus with
kubectl rollout status deployment/[DEPLOYMENT_NAME]
- Get rollout history with
kubectl rollout history deployment/[DEPLOYMENT_NAME]
- Go back to a specific revosion
kubectl rollout undo deployment/[DEPLOYMENT_NAME] --to-revision=1
- Execute
microk8s kubectl apply -f frontend-deployment-canary.yml
- Delete deployment old version
kubectl delete -f k8s-microk8s/frontend-deployment.yml
For convenience of performing builds and making very first test runs, we also created docker-compose file that make use of 2 custom images and a Postgres database.
To run our docker-compose implementation:
- Go inside k8app (root project) folder
- Run
docker-compose up
- Navigate to frontend http://localhost:5000/
- Navigate to backend http://localhost:8080/
- Check out main page and visit http://localhost:8080/api/docs to play with API
- If you wish you can connect to PostgresDB (find credentials in .env file) to investigate the items table and data stored on it.
To perform an image build run: docker-compose build
. Note that you can adjust image names in .env
file in root
project folder.
To get more details about implementation of each module consult backend/
and frontend/
folders
Backend is built using Python and FastAPI framework.
Frontend is built using React. For production on top of yarm
build a nginx image is used on top. For development,
it can be run with either yarm
or npm run
.
Postgres is used as primary data store. Credentials for Docker-compose version can be found in .env
file. Credentials
for Microk8s
version can be found in corresponding ConfigMaps
and Secrets
.