Recently, we auto deployed SUSI Server on Google Cloud Platform using Kubernetes and Docker Images after each commit in the GitHub repo with the help of Travis Continuous Integration. So, basically, whenever a new commit is added to the repo, during the Travis build, we build the docker image of the server and then use it to deploy the server on Google Cloud Platform. We use Kubernetes for deployment since it is very easy to scale up the Project when traffic on the server is increased and Docker because using it we can easily build docker images which then can be used to update the deployment. This schematic will make things more clear what exactly is the procedure.
- You must be signed in to your Google Cloud Console and have enabled billing and must have credits left in your account.
- You must have a docker account and a repo in it. If you don’t have one, make it now.
- You should have enabled Travis on your repo and have a Travis.yml file in your repo.
- You must already have a project in Google Cloud Console. Make a new one if you don’t have.
Pre Deployment Steps
You will be needed to do some work on Google Cloud Platform before actually starting the auto deployment process. Those are:
- Creating a new Cluster.
- Adding and Formatting Persistence Disk
- Adding a Persistent Volume CLaim (PVC)
- Labeling a node as primary.
Check out this documentation on how to do that. It may help.
Img src: https://cloud.google.com/solutions/continuous-delivery-with-travis-ci
1. The first step is simply to add this line in Travis.yml file and create an empty deploy.sh, file mentioned below.
- bash kubernetes/travis/deploy.sh
Now we’ll be moving line by line and adding commands in the empty deploy.sh file that we created in the previous step.
2. Next step is to remove obsolete Google Cloud files and install Google Cloud SDK and kubectl command. Use following lines to do that.
echo ">>> Removing obsolete gcoud files"
sudo rm -f /usr/bin/git-credential-gcloud.sh
sudo rm -f /usr/bin/bq
sudo rm -f /usr/bin/gsutil
sudo rm -f /usr/bin/gcloud
echo ">>> Installing new files"
curl https://sdk.cloud.google.com | bash;
gcloud components install kubectl
3. In this step you will be needed to download a JSON file which contains your Google Cloud Credentials, then copy that file to your repo and encrypt it using Travis encryption keys. Follow https://youtu.be/7U4jjRw_AJk this video to see how to do that.
4. So, now you have added your encrypted credentials.json files in your repo and now you need to use those credentials to login into your google cloud account. So, use below lines to do that.
echo ">>> Decrypting credentials and authenticating gcloud account"
# Decrypt the credentials we added to the repo using the key we added with the Travis command line tool
openssl aes-256-cbc -K $encrypted_YOUR_key -iv $encrypted_YOUR_iv -in ./kubernetes/travis/Credentials.json.enc -out Credentials.json -d
gcloud auth activate-service-account --key-file Credentials.json
#add gcoud project id
gcloud config set project YOUR_PROJECT_ID
gcloud container clusters get-credentials YOUR_CONTAINER
The above lines of code first decrypt your credentials, then login into your account and set the project you already created earlier.
5. Now, we have logged into Google Cloud, we need to build docker image from a dockerfile. Follow official docker docs to see how to write a dockerfile. Here is an example of dockerfile. You will need to add “$DOCKER_USERNAME” and “$DOCKER_PASSWORD” as environment variables in Travis settings of your repo.
echo ">>> Building Docker image"
docker build --no-cache -t YOUR_DOCKER_USERNAME/YOUR_DOCKER_REPO:$TRAVIS_COMMIT .
docker login -u="$DOCKER_USERNAME" -p="$DOCKER_PASSWORD"
docker tag YOUR_DOCKER_USERNAME/YOUR_DOCKER_REPO:$TRAVIS_COMMIT YOUR_DOCKER_USERNAME/YOUR_DOCKER_REPO:latest
6. Now, just push the docker image created in previous step and update the deployment.
echo ">>> Pushing docker image"
docker push YOUR_DOCKER_USERNAME/YOUR_DOCKER_REPO
echo ">>> Updating deployment"
kubectl set image deployment/YOUR_CONTAINER_NAME --namespace=default YOUR_CONTAINER_NAME=YOUR_DOCKER_USERNAME/YOUR_DOCKER_REPO:$TRAVIS_COMMIT
This blog was about how we have configured travis build and auto deployed SUSI Server on Google Cloud Platform using Kubernetes and Docker. You can do the same with your server too or if you are looking to contribute to SUSI Server, this may help you a little in understanding the code of the repo.
- The documentation for setting up your project on Google CLoud Console before starting auto deployment https://github.com/fossasia/susi_server/blob/afb00cd9c421876f5d640ce87941e502aa52e004/docs/installation/installation_kubernetes_gcloud.md
- The documentation for encrypting your google cloud credentials and adding them to your repo https://cloud.google.com/solutions/continuous-delivery-with-travis-ci
- Docs for Docker to get you started with Docker https://docs.docker.com/
- Travis Documentation on how to secure your credentials https://docs.travis-ci.com/user/encryption-keys/
- Travis Documentation on how to add environment variables in your repo settings https://docs.travis-ci.com/user/environment-variables/