DEV Community

Cover image for Deploying applications to Kubernetes with Gitlab CI/CD,Helm Charts and ArgoCD
pankaj892
pankaj892

Posted on

Deploying applications to Kubernetes with Gitlab CI/CD,Helm Charts and ArgoCD

In my last post I talked about Gitops and how it helps in automation you can read about it ๐Ÿ‘‰here

In this post we'll be doing a project using GitOps methodology and tools like ArgoCD

Project Workflow Diagram

Workflow

Let me take you through the project flow-

  1. Developers commit code to git repository
  2. After code is commited a CI pipeline is triggered in gitlab which has the job of building an image and tagging it and updating the image in the repository which hosts the manifest files.
  3. After the job is completed the pipeline pushes the images to a container registry like dockerhub,gitlab etc.
  4. ArgoCD syncs the manifest repo which contains helm charts and values
  5. ArgoCD deploys the changes to Kubernetes cluster and changes reflect in the application

Before we begin we need a kubernetes cluster up and running you can deploy a cluster in one of the many cloud environments or you can run it locally using tools like Minikube,Rancher Desktop to name a few

I prefer running my cluster on cloud since it takes away a lot of stress for setting it up locally.

1.Setting up the cluster

I have used Azure Kubernetes Service (AKS) for deploying kubernetes in Azure. It is a managed Kubernetes solution provided by Azure where we have access to nodes and Azure takes care of control plane.

Azure Kubernetes Service

AKS Deployment Page

As you can see in above images we have our cluster set up and ready we can we now move to our next step which is setting up the gitlab pipeline

2. Setting up CI pipeline in Gitlab

I have used gitlab for CI/CD since I believe it has a lot of options and support for CI/CD. You can choose any other CI tool like CircleCI, Jenkins to name a few
I have created 2 repos-

  • code-repo
    This repo hosts the Dockerfile and the source code of our app
    Code-repo image from gitlab

  • Manifest repo
    This repo hosts helm charts and yaml files for deployment of our app to kubernetes
    Manifest repo image from gitlab

In gitlab to setup a pipeline we need to define it in a yaml file called .gitlab-ci.yml

Make sure your pipeline looks like below
Gitlab CI file

The pipeline consisits of 2 stages - build and helm-chart-updation

Build Stage

In this stage the pipeline logs into the container registry based on the credentials defined in the environment variables and builds the docker image and pushes it to container registry which in our project is the gitlab container registry. Also we login to the repo through SSH Keys which I have provided as an variable for CI/CD

CI/CD Variable SSH

Container registry image

Helm chart updation

In this stage the pipeline after logging in by the SSH Key updates the values.yaml file in manifest repo to the current image from container registry with the latest tag so I don't have to manually change the tag after every run.

Now let's run the pipeline

Pipeline

My pipeline ran successfully. Now lets move on to setting up argoCD

3. ArgoCD

ArgoCD automates the deployment of applications to multiple Kubernetes clusters while maintaining desired state configuration stored in Git repositories. ArgoCD continuously monitors these repositories for changes, ensuring applications are always synchronized with the declared configuration. Its robust capabilities include automated rollbacks, extensive deployment auditing, and a user-friendly UI for visualizing and managing application deployments across environments. It is a core component in GitOps practices.

Enough said lets start

ArgoCD can be installed on your cluster by

$ kubectl create namespace argocd
$ kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
Enter fullscreen mode Exit fullscreen mode

Access the argoCD server

$ kubectl patch svc argocd-server -n argocd -p '{"spec": {"type": "LoadBalancer"}}'
Enter fullscreen mode Exit fullscreen mode

ArgoCD provides documentation which seems easy to follow you can access it here

Now lets log in to argoCD server
Before we can add our app we need to connect our repo.
Go to settings and go to repositories
Settings Page

Now add our repo details and connect
Repo details

If connection is successful it will show a successful status
Connection Status

Now click on add app to connect our app
argoCD Login Page

Add the name of your app you can give any name but it should be lowercase
App name details

Add source and destination for your app
Source app details

Since my repo has a values.yaml file and it is a helm chart so details are automatically picked up by argoCD
Helm Details

After adding details and creating your app you will see a screen like this which shows the appname and if you click on it you will get more details
Argo Progress

4. Testing the app

We have completed our app setup and it is synchronizing with our repo but how do we find the address where it will be hosted

Service command

After going to the link we have our app now ready
First instance of app

Now lets test it I will try to change the backgroud image of our app

Background change

After committing my change the pipeline starts
Pipeline run

And background image of our app has been changed successfully
Final App

Congratulations on completing this project and making it till here.

If you have any questions comment them down I would be happy to help you

Top comments (0)