How to perform a Continuous Deployment (CD) on Google Cloud Kubernetes using Jenkins?
In the previous article, I discussed setting up a CI environment with Docker. In this article, I will try to explain how to deploy an application that has gone through the CI process to a cluster on Google Kubernetes (k8s) Engine.
Automating tasks is one of the most important issues, especially in projects with many services. You might be able to manually build and deploy a monolithic application, but when application modules are separated into different services, as in a microservices architecture, we may have many projects. Considering that we are constantly developing on these projects, building and deploying for each project becomes very cumbersome.
When we set up a continuous delivery environment, we only need to commit the code change and wait a while for the change to be reflected in the relevant environment. The automation environment builds, tests, analyzes, and delivers our project to the relevant environment (e.g., test) for us.
The purpose of this article is to illustrate the process from the commit operation to the delivery of the application to the GCP (Google Cloud Platform) k8s cluster.
Before starting, you need to have an account on GCP. Google provides free trials for these types of tests. GKE (Google Kubernetes Engine) runs on virtual machines automatically created on Google Compute Services. However, since the free tier only provides a micro instance for free, this will not be sufficient for a K8S cluster. Therefore, you may need to make a payment.
After creating your account, GCP will require you to create a Project. The name of the created project will be needed in later steps.
Creating a Kubernetes Cluster on GCP
After completing the account creation process, we now need to create a K8S cluster. We need to connect to the GCP console at https://console.cloud.google.com/ and go to the "Kubernetes Engine" section. Then, we need to create a new cluster from the "Cluster" tab.
In the next step, we can easily create a Kubernetes cluster using the default settings with the "Your first cluster" option.
The cluster name and zone information you specify in this step will be needed in subsequent steps. After the cluster is created, we need to create a GCP service account, which is necessary for the deployment process.
Defining a Jenkins service account on GCP
To do this, go to the GCP console and navigate to the IAM tab.
Clicking the Create Service Account button will open the following screen:
We can use "jenkins-cd" as the account name. After creating the account, we need to go to the account details and generate a key using the "CREATE KEY" button. We need to download this key. With this key, we will access the GCP Docker registry and the K8S cluster via Jenkins.
We need to authorize the jenkins-cd account we created via the AM tab. Press the "ADD" button, type "jenkins-cd" in the "New Members" section, and assign the following roles:
- Kubernetes Engine Developer
- Storage Admin (for Docker registry)
We need to assign them roles.
Up to this point, we have made the necessary adjustments on GCP. These are:
- We created a GKE cluster.
- We created a service account for external cluster management and deploying Docker images.
Getting Jenkins ready:
In the previous article, I created a Dockerfile to run Jenkins on Docker. For this scenario, we need to modify this Dockerfile slightly. The new Dockerfile is as follows:
FROM jenkins/jenkins:2.176.4
USER root
ARG ssh_prv_key
ARG ssh_pub_key
# Install kubectl for GKE integration
RUN curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.16.0/bin/linux/amd64/kubectl
RUN chmod +x ./kubectl
RUN mv ./kubectl /usr/local/bin/kubectl
RUN mkdir -p /root/.ssh && \
chmod 0700 /root/.ssh && \
ssh-keyscan -p 2022 [host-name] > /root/.ssh/known_hosts
RUN echo "$ssh_prv_key" > /root/.ssh/id_rsa && \
echo "$ssh_pub_key" > /root/.ssh/id_rsa.pub && \
chmod 600 /root/.ssh/id_rsa && \
chmod 600 /root/.ssh/id_rsa.pub
USER jenkins

Dilaver DEMİREL