Deploying applications to Kubernetes by using ArgoCD

Initially, you usually just use kubectl to deploy applications as well as learn how to use the kubectl command. Or if you are studying for the CKAD/CKA exam you will find mastering kubectl really helpful. However, in my personal experience, using kubectl to deploy the application to Kubernetes manually does not usually happen, if the project only has one or two services, which is okay, but in most of cases, using Kubernetes to deploy applications will often follow the microservice model, and it is particular to take control of moreover 30 services in a project. Besides, sometimes we also need to change the deployment directly on the Kubernetes cluster with the kubectl edit command, leading to the configs in the local yaml file being different from the running yaml file.

Therefore, a tool for automating these processes as well as ensuring that the applications deployed on Kubernetes match the local yaml files is really necessary. In this article, I’m going to share about how to use ArgoCD to solve the above problems.

image

Note:
Kubernetes version 1.24.2
ArgoCD version 2.6.7

Prerequisites:
Kubernetes cluster
Git repositories contains manifest file

To install ArgoCD as well as deploy the application to k8s, you need to do the following steps:

  1. Creating a Git repository for containing manifest files.
  2. Deploying ArgoCD to Kubernetes cluster.
  3. Setting up an application on ArgoCD UI and enjoying the result.

Creating a Git repository for containing manifest files.

Suppose a web server Nginx needs to be deployed with version 1.23, so I created a git repository Github containing a deployment yaml.

You can fork this repository or create one on your own. That’s it for this step.

Deploying ArgoCD to Kubernetes cluster.

Actually, installing ArgoCD is kind of easy, we just need to run 2 commands and everything will be set up automatically:

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

The following output as

Then, verify it by the command:

$ kubectl get all -n argocd

The following output as

At this point, accessing to ArgoCD UI with the command:

$ kubectl port-forward svc/argocd-server -n argocd 8080:443

Then you can browse to http://localhost:8080

The default Username is admin and you get the Password by typing:

$ kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -d

Everything is looking good, now we can move to the next step to deploy the application.

Setting up an application on ArgoCD UI.

In this step, we create a new application with the Github repository which was created above and then ArgoCD creates the deployment Nginx to Kubernetes cluster automatically.

From the Applications page, click on NEW APP and input the information such as Application Name, Project Name, SYNC POLICY is Automatic.

Under SOURCE section, from the Repository URL input the Git repository, and then add k8s to Path input.

Finally, at the DESTINATION section, choose https://kubernetes.default.svc for Cluster URL and input any namespace you want. Eventually, press the CREATE button.

The following output as.

Verifying it with kubectl.

At this point, whenever the Nginx application needs to be updated such as changing image, updating tag, add livenessprobe,… We need to change the yaml file and push this change to Git repository then ArgoCD syncs an application when it detects differences between the desired manifests in Git, and the live state in the cluster.

Assume that there is a requirement about updating the new version of this Nginx deployment to new version 1.24.

Moving to k8s/nginx.yaml and changing the image tag from 1.23 to 1.24.

Then commit and push this change.

Back to ArgoCD UI and wait for 3 minutes because the default automatic sync interval is 180s. Otherwise, you can click on the REFRESH button to do it manually.

Verifying it again and seeing the new image.

Hoping now you have known how to implement ArgoCD to Kubernetes cluster and deploy the application with GitOps. From now on, the DevOps team has to push the changing yaml file to the git repository, so everything changing on the yaml files are always stored on git and they can check the git commit as needed. In the next post, I will share more about CI/CD with GitOps which is really helpful.