GitlabKubernetes

Setup Gitlab CI/CD pipeline for Kubernetes using Helm

In this blog, we will setup a Gitlab CI/CD pipeline for Kubernetes application using Helm chart.

Checkout my Youtube video on this article

We will be deploying a sample Drupal application using Helm chart and deploy in GKE

Agenda:

  1. Git repo in gitlab
  2. Create a .gitlab-ci.yaml and values.yaml file
  3. Create Kubernetes cluster from Gitlab
  4. Connect to Kubernetes cluster using cloud shell
  5. Create ingress controller on the Kubernetes Cluster
  6. Validate the application on the browser

Git Repo

Setup a GKE using gitlab

Go to your project in Gitlab and then 

  1. Click on Operations -> Kubernetes

Click on Connect Cluster with certificate

Create a new cluster -> Select GKE option -> This will ask you to login to your google account

Provide all the details

  • You need to create the project in Google cloud first and then in the dropdown it will show all the projects which exist in your Google account

This will take around 5–10 minutes. You can validate in your GKE also to check if your cluster is ready or not

Connect to cluster

Login to your Google cloud account -> Kubernetes Cluster -> Select your cluster -> Click on Connect

gi

You can copy the below command and run click on Activate cloud shell and paste this. If you will not run this command then your helm install will failed

gcloud container clusters get-credentials gke-2 --zone us-central1-a --project kubernetes-cluster-298116

Create ingress controller on the Kubernetes Cluster

For more details, click here

helm repo add nginx-stable https://helm.nginx.com/stable 
helm repo update
helm install nginx-ingress nginx-stable/nginx-ingress
# Check that the ingress controller is installed successfully kubectl get service nginx-ingress-nginx-ingress

Validate it on GKE — You can check that it has created a load balancer for you as shown below:

Create a Gitlab Pipeline

Create a .gitlab-ci.yaml with the below content

NOTE: 

environment: This tag is important and the value of this is what you have configured while creating a Kubernetes cluster. If you will not use this tag and the pipeline job will fail while making a connection to Kubernetes cluster as shown below

We are using the shared runners which gitlab provides by default so we need to use other docker image where helm software is also installed. If you will not use the docker image then the pipeline will fail with the error helm command not found

stages:
- test

format:
stage: test
image: devth/helm:latest
environment: production
script:

- helm repo add bitnami https://charts.bitnami.com/bitnami
- helm repo update
- helm install druapltest3 bitnami/drupal -f values.yaml

Create a values.yaml file if you want to customize it. I have customize it for the ingress to be enabled

ingress:
## Set to true to enable ingress record generation
##
enabled: true
hostname: drupal.local
path: /

Trigger the pipeline

If you are doing the local testing then update the /etc/hosts file with the below content. You can get the ingressIp using `kubectl get ingress’

Now access the url on the browser

Please follow and like us: