CI/CD pipeline to deploy a web application on Google Kubernetes Engine (GKE) from GitHub
Google Kubernetes Engine (GKE), a management and orchestration service for container clusters within Google Cloud. When we use GKE to run our containerized workload, Google cloud provides us the advanced cluster management features like Node pools, automatic scaling, Node auto-repair, logging/monitoring, and Google Cloud’s load balancer. You can interact with GKE using the gcloud command-line interface or the Google Cloud Platform Console.
To learn about Google Kubernetes Engine, click here
This article is just to show you how to deploy a containerized (docker) simple Django web application on GKE and setup CI/CD pipeline in GKE using cloud build to automatically deploy new changes from GitHub. A high-level flow diagram:-
1. Google cloud account with free tier/credit
2. GitHub account
3. Knowledge of Django (python)
To follow this exercise, you can download the code from GitHub. This is a single-page Django (Python) web application with a dockerfile to build as a container(docker) image. Create your own repository on GitHub and upload the application code that you can deploy in GKE later.
Deploying application on GKE
Log into Google Cloud Console and select a project that you want to use for this exercise. Open the Google Kubernetes Engine pages either using the top left menu option (three bars) or typing in the search box.
- Create Cluster: You need to first create the cluster using the cluster option, give a name to the cluster like ‘django-web-1’. Select Location Type as Zone and chose a Zone as ‘us-central1-c’. Click create button to create a cluster, Google Cloud will create a cluster with the default configuration
2. Create workload (Deploy): Once the cluster has been created successfully and ready to use. You can create workload either using the Deploy option in the Cluster screen or open the workloads page to create workload/container.
In the Edit container screen, select ‘New Container Image’, choose GitHub in Repository Provider and authenticate your GitHub repository to use your Django web application code. Give a proper name to your workload/application like ‘Django-web’. Keep other configuration as default
Click the create/deploy button then GKE will start building container image and configuring Kubernetes components like clusters, Pods
3. Create Services & Ingress: Now you need to create a service & ingress object to expose to outside using an HTTP(S) load balancer. In workloads page, go to action and select Expose or open services & ingress tab, select Service Type as ‘Load Balancer’, give Port and Target Port values as ‘8000’, press Expose button to create the service object. This will implement Ingress resources as Google Cloud load balancers for HTTP(S) workload.
4. Run & Test application: To test the running application, go to the service & ingress tab and open the service detail page. Click on the external endpoints IP address link to open the application.
The application will be opened in a new tab and will render the index.html page.
Setup a CI/CD Pipeline
Now the application is running on the GKE cluster. You can check and verify the Google Kubernetes Engine components configuration in different tabs like clusters, workloads, applications and monitor their activities, usage, and errors.
You can now set up an automatic deployment (CI/CD) pipeline to apply your code changes from GitHub to your workload running in GKE clusters.
Open the workloads tab, go to the action option, and select automate deployment. The console window will show a configuration page on the right corner for providing configuration. Provide your GitHub repository details in the Source repository section and click continue
Now provide the dockerfile name in the Build configuration section. Your dockerfile should be present in the root directory of your code repository. Docker File will be used by Google cloud build to build a docker image from your GitHub code and will store in Google Container Registry. Click continue
Important: make sure Google Cloud Build and Google Container Registry API are enabled for the projects
Automated deployment configuration expects a YAML configuration file in your docker image that instructs the GKE to deploy it on the cluster. You can create your own YAML configuration file or download it by clicking the ‘View Google Recommended YAML’ button and save & commit in folder name ‘Kubernetes in your repository’s root directory. Hit update to create or update the configuration for automatically deploy the application.
You can now verify the automated deployment setup. Open the index.html file from the below location, make a change in the text that will be displayed when this page will be rendered. Commit your change in the GitHub repository.
Once the changes are committed in the GitHub repository, Google cloud build detects changes and will trigger the process to build a new image and deploy it on the GKE cluster from the container registry. You can track the GKE update progress in the below Applications screen.
Refresh or reopen the application and verify your newly committed changes
To avoid incurring charges to your Google Cloud account, you can delete the project or delete the individual resources used in this tutorial
This article was just to show you how you can deploy a simple web application on the GKE cluster and setup a CI/CD pipeline to automatically deploy the application. If you want to learn more about Google Kubernetes Engine & Kubernetes architecture, components, and working, follow the below links