This repository is a sample python Flask app, that can be deployed on Kubernetes using CircleCI. You can use this project as a template to deploy your app on Teko Kubernetes cluster, CI/CD pipeline with CircleCI.
- Flask application
- CI/CD using CircleCI
- Celery
- Redis pubsub
In order to deploy your app on Kubernetes, some basic knowledge about Kubernetes and Docker is required. The template uses helm to simplify the deployment.
- Installing kubectl
- To access Kubernetes cluster, ask SA team to obtain your
config
file, then place it at $HOME/.kube
- To access Kubernetes cluster, ask SA team to obtain your
- Installing helm
- Initialize
helm
using command:helm init --client-only
- Initialize
- An account to access Docker registry and helm chart repository which is Harbor
- Ask SA team to create your project and account.
A helm chart is created to simplify deployment: https://github.com/teko-vn/helm-charts/tree/master/flaskapp. Just copy values.yaml then edit its values to your own values.
NOTE: In this repo, values-tpl.yaml is used with a placeholder
{{ branch }}
. This placeholder will be replaced to deploy your app in different environments. The file is placed at<project_root>/deployments/k8s/
directory to work with Makefile in CI configuration
-
nameOverride
,fullnameOverride
: Your app name in short and long description. You need to provide both. -
replicaCount
: The number of instances your app will launch. -
image
: Docker Image configregistry
: Docker image registry. Ex: gcr.io (Google Cloud Container Registry), docker.io (Docker Hub), hub.k8s.teko.vn (Teko Harbor). If you leave this empty, Docker Hub will be used.repository
: The path your Docker image. Ex:hello-flask/hell-flask
tag
: The image tag. Ex: 1.0.0, 1.2.0...port
: The container's port.pullPolicy
: Image pull policy. Ex:Always
,IfNotPresent
dockerConfig
: docker config json file. The file can be found at $HOME/.docker/config.json after you login to the registry by callingdocker login <registry>
-
healthcheck
: Health-checkenabled
:true
to enable heath-check.liveness
: HTTP URL to check your app's liveness.readiness
: HTTP URL to check your app's readiness.host
: HTTPhost
header when calling HTTP GET request to health-check URLs.
-
hosts
: An array of your app's hosts.host
: your app's host. Ex: myapp.k8s.teko.vnpaths
: an array of URI paths. Leave it""
, so all requests tohost
will be routed to your service.- Example
so all requests to myapp.k8s.teko.vn/api and myapp.k8s.teko.vn/api/v1 will be routed to your service.
- host: myapp.k8s.teko.vn paths: - "/api" - "/api/v1"
-
secrets
: your app's secretsIt is an array of key-value pair. Example:
secrets: - db-url: "mysql://user:passw@host:3306/mydb" - amqp-url: "amqp://user:[email protected]/teko-prod"
-
environmentVariables
: your app's environment variables.It can be a string or a value from your apps' secrets. Example:
environmentVariables: - name: FLASK_CONFIG value: production - name: DATABASE_URL valueFromSecret: db-url
Sample CircleCI configuration file at config.yml. A Makefile is created for some commands. You can edit these files to suit your needs.
NOTE: The values file (
values-tpl.yaml
) edited in previous step should be placed at<project_root>/deployments/k8s/
directory to work with Makefile.
-
Makefile
You need to provide your project' values, which are at top of the Makefile:
PROJECT_NAME := hello-flask IMAGE_REPO := hub.k8s.teko.vn/hello-flask/hello-flask HARBOR_SERVER := https://hub.k8s.teko.vn
-
.circleci/config.yml
There are two workflows configured:
-
feature-development
When a feature is developed at
feature/*
branch, it can be deployed at<your-app>-<short-branch-name>.k8s.teko.vn
.When feature is merged to
master
branch, it can be deployed to staging for UAT testing. -
deploy-production
When a git tag is created, following semver convention (Ex: v0.1.1, v1.2.0...), your app is ready to deploy on production environment.
-
-
Configure CirleCI environments
Some environment variables is required to configure at your project CircleCI settings.
DEV_KUBE_CONFIG
: kubectl config file for k8s development cluster. This value must be base64 encoded.PROD_KUBE_CONFIG
: kubectl config file for k8s production cluster. This value must be base64 encoded.HARBOR_USERNAME
: Harbor username, which is needed to use Teko Harbor for image registry andflaskapp
helm chart.HARBOR_PASSWORD
: Harbor password, which is needed to use Teko Harbor for image registry andflaskapp
helm chart.