Django project skeleton with a preconfigured CI/CD pipeline for k8s and GCP.
django-k8s-gcp-skeleton is a django project skeleton with a preconfigured CI/CD pipeline that deploys onto a Kubernetes cluster running on GCP. It uses CircleCI for running tests, uploading code to GCP's cloud build and applying the Kubernetes deployments.
It comes with bootstrapping scripts for setting up a GCP Cloud SQL instance, configuring a sql proxy, creating Kubernetes secrets & enabling GCP's Stackdriver tracing.
- Continous integration with CircleCI.
- Build containers with GCP's cloud build and saving to GCP's container registry.
- Deploy to a Kubernetes cluster running on GCP's Kubernetes Engine.
- Tracing with Stackdriver
- Create a new repository on GitHub.
- CircleCI also works with BitBucket but the instructions assume that GitHub will be used.
- Clone the skeleton into a local directory.
- Delete the
.git
folder, rungit init
, commit and push into your new repository.
Setup a GCP account, create a project and install the gcloud command line tool. The gcloud tool needs to be configured for your project. Install kubectl. GCP has up to date instructions for how to setup the cli tooling and creating a project.
Configure the following environment variables, I recommend direnv for managing environment variables but use whatever you're using already.
# The name you want to have on your k8s cluster
export CLUSTER_NAME=my-example-cluster
# The GCP region you want to use
export CLUSTER_REGION=europe-west3
# Name of your database instance, note: this is not the database name.
export DB_INSTANCE_NAME=my-psql
# The name of your database
export DB_NAME=my-example-db
# The name of your database user
export DB_USER=exampledbuser
# The password of your database user
export DB_PASSWORD=verysecret
# The id of the GCP project you created in the first step
export GCP_PROJECT=example-foobar-12232
# The name of your project
export PROJECT_NAME=myexample
# Your django secret key
export SECRET_KEY=somethingsecret
When the previous steps have been completed, run the following in your shell with the environment variables from the above step sourced.
# Login to GCP
$ gcloud auth login
# Enable the required GCP services
$ ./bootstrap/enable.sh
# Initialize the k8s application templates
$ ./bootstrap/init.sh
# Create a k8s cluster
$ ./bootstrap/create-cluster.sh
# Create a DB
$ ./bootstrap/setup-database.sh
# Create & configure service accounts
$ ./bootstrap/create-service-accounts.sh
You should now have a k8s cluster with a sql-proxy workload & service running.
The scripts should have created k8s folders from the templates. Delete the k8s-templates folders and commit the change. Add the k8s folders the init script created and commit them.
Copy the CI key:
cat ./bootstrap/keys/cibuild.json|base64
You can now delete the ./bootstrap/keys
folder.
All steps need to be configured inside CircleCI's interface.
- Select your organization and add a project.
- Select your repository, and set Linux as the operating system and Python as the language.
- Press the "Start building" button.
- The first build will fail the preflight check because the environment variables have not been configured.
- In the build interface, press the settings configuration button
- Add the following environment variables:
GCP_AUTH
- This is the value of the base64 encodedcibuild.json
key.GCP_PROJECT
- The id of the GCP projectCLUSTER_NAME
- Name of the kubernetes clusterCLUSTER_REGION
- Name of the kubernetes cluster regionPROJECT_NAME
- Name of the project, used for containers & k8s.
- Build the workflow again.
- Navigate to the ip of the service listed in the kubernetes interface. It should return a HTTP response with the text body "ok".
- Add a DATABASE_URL pointing to a local database and start working on your project.
- You can delete the bootstrap folder, it's not needed anymore.
- 0.0.1
- Initial release
Distributed under the MIT license. See LICENSE
for more information.
- Fork it (https://github.com/yourname/yourproject/fork)
- Create your feature branch (
git checkout -b feature/fooBar
) - Commit your changes (
git commit -am 'Add some fooBar'
) - Push to the branch (
git push origin feature/fooBar
) - Create a new Pull Request