Deploying applications to Kubernetes with Gitlab CI/CD,Helm Charts and ArgoCD
Tags: gitops, argocd, gitlab, cicd
Published: 2024-07-01 10:59:26

In my last post I talked about Gitops and how it helps in automation you can read about it 👉[here](https://dev.to/pankaj892/gitops-streamlining-kubernetes-application-deployment-with-gitlab-cicd-helm-charts-and-argocd-685)

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


![Project Workflow Diagram](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tn7jomy7uw2j2y9qxr5f.jpg)

####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](https://minikube.sigs.k8s.io/docs/),[Rancher Desktop](https://rancherdesktop.io/) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mpcwwz9t8uss507iwa2r.png)


![AKS Deployment Page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2dc9jrexec2p66kf2q1g.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ksgu7wfgh1gnk4j7s884.png)

* Manifest repo
This repo hosts helm charts and yaml files for deployment of our app to kubernetes
![Manifest repo image from gitlab](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dygh5lkk9mab3tmqygau.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g51zkh4ji7amscweagb8.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ay58x0y9nnyimuchfdl.png)

![Container registry image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tg5t12qa8z0cqz6jufku.png) 

#####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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/acrpaly0ij8euod759rb.png)

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
```

Access the argoCD server

```
$ kubectl patch svc argocd-server -n argocd -p '{"spec": {"type": "LoadBalancer"}}'
```

ArgoCD provides documentation which seems easy to follow you can access it [here](https://argo-cd.readthedocs.io/en/stable/getting_started/)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1eqh6hifx2527upr69hd.png)

Now add our repo details and connect 
![Repo details](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/797cegu609y65on1obwq.png)

If connection is successful it will show a successful status
![Connection Status](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b9i5uc35ken2jpahq2u5.png)

Now click on add app to connect our app
![argoCD Login Page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e49mndt7ege5ilbz7527.png)

Add the name of your app you can give any name but it should be lowercase
![App name details](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xwnhbd1caa9kw5vq9t46.png)

Add source and destination for your app
![Source app details](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4fdxqppe2z34stnrkice.png)

Since my repo has a values.yaml file and it is a helm chart so details are automatically picked up by argoCD
![Helm Details](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uc04uqoms8810wwj2729.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iuv3td5sivlezrq491he.png)

####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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ofy4v6orn1foco90fr0t.png)

After going to the link we have our app now ready
![First instance of app](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s9aorqr1uwm5tro5rllc.png)

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

![Background change](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qm0c6n0ogp1ng68uc3ny.png)

After committing my change the pipeline starts
![Pipeline run](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5z46lbosw4p3s301ynyw.png)

And background image of our app has been changed successfully
![Final App](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rw42ixj3xjstmskihbin.png)


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