DEV Community

Cover image for Implemeting GitOps with Argocd
.·. Felipe Paz .·.
.·. Felipe Paz .·.

Posted on

Implemeting GitOps with Argocd

This project aims to demonstrate how to integrate GitOps concepts using GitHub Actions, ArgoCD, and Kubernetes.

Through this guide, you’ll be able to trigger changes automatically via GitHub Actions and ArgoCD. In this example, we will run Kubernetes and ArgoCD through Minikube.

Prerequisites

External setup

Navigate to your Dockerhub panel and create a personal token

Image description

Create a fork of this project and navigate to the project's settings page.

Image description

Click on Secrets and variables > Actions and create 3 variables:

  • DOCKER_USERNAME - Your Dockerhub username
  • DOCKER_REGISTRY - docker.io
  • DOCKER_PASSWORD - Personal token created in the previous step

Image description

Local setup

  • Initalize docker
  • Initialize minikube
minikube start
Enter fullscreen mode Exit fullscreen mode

Once minikube is ready and running, install two project's dependencies:

  • Argocd
  • Skater Reloader

Installing Argocd

Navigate to the folder infra/k8s and run the follwing:

kubectl apply -k kustomization.yaml
Enter fullscreen mode Exit fullscreen mode

Installing Skater Reloader

Stakater Reloader can be applied using its remote configuration. To do that, run the following command:

kubectl apply -f https://raw.githubusercontent.com/stakater/Reloader/master/deployments/kubernetes/reloader.yaml
Enter fullscreen mode Exit fullscreen mode

Configuring Argocd

First of all, you need to access the Argocd console. To do that, run the command:

minikube service argocd-server -n argocd --url
Enter fullscreen mode Exit fullscreen mode

The output will be something like this:

http://127.0.0.1:55643
Enter fullscreen mode Exit fullscreen mode

Now, you can access the Argocd console using the link above.

By default, Argocd has the admin user enabled and a default password. To visualize this password you need to:

  • Identify the pod responsible for argocd-server
kubectl get pods -n argocd
Enter fullscreen mode Exit fullscreen mode

The output will be something like this

NAME READY STATUS RESTARTS AGE
argocd-application-controller-0 1/1 Running 0 125m
argocd-applicationset-controller-86fc5c85-bgt82 1/1 Running 0 125m
argocd-dex-server-7f4689df5-p8fwx 1/1 Running 0 125m
argocd-notifications-controller-59f78959c8-m2fsh 1/1 Running 0 125m
argocd-redis-74cb89f466-t549k 1/1 Running 0 125m
argocd-repo-server-6578ccfc67-txds5 1/1 Running 0 125m
argocd-server-854c79df45-ff5zs 1/1 Running 0 125m

The pod we need is the pod argocd-server-854c79df45-ff5zs. Once you have identified the pod, run the following command to get the default password:

kubectl exec -it argocd-server-854c79df45-ff5zs -n argocd -- argocd admin initial-password
Enter fullscreen mode Exit fullscreen mode

The output will be something like:

Sqfjox8ceceIosuW
Enter fullscreen mode Exit fullscreen mode

or

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

The output will be something like:

Sqfjox8ceceIosuW%
Enter fullscreen mode Exit fullscreen mode

Ignore the final %

Given that, now you need to access the Argocd console. Go to the url created by minikube and enter admin as username and the password displayed by the command above.

Image description

As soon as you signin you will see a page like this

Image description

In your project, it will be blank as you are creating this project from scratch. In my screenshot, I have already created the application and because of that you can see it listed in the image.

Navigate to the Settings > Repositories

Image description

Click on Connect Repo. In this step, you will need to create a ssh key and connect Argocd to your Github repository via ssh.

Run the following command:

ssh-keygen -t ed25519 -C "<your_email_here>" -f gitops-argocd -P ""
Enter fullscreen mode Exit fullscreen mode

The output will be something like:

Generating public/private ed25519 key pair.
Your identification has been saved in gitops-argocd
Your public key has been saved in gitops-argocd.pub
The key fingerprint is:
SHA256:7FXbnyKfvckbD1eMhXvTDfyPqaS23sVCa0WGlBsK7UY <your_email_here>
The key's randomart image is:
+--[ED25519 256]--+
|         .  ..   |
|        . E.oo . |
|         + .oo* .|
|       .  +..= Bo|
|        S.. o =.B|
|       . . . + ==|
|        .  .=.*o+|
|          .=o+=o+|
|         o+.oo *+|
+----[SHA256]-----+
Enter fullscreen mode Exit fullscreen mode

Copy the key with .pub extension

cat gitops-argocd.pub
Enter fullscreen mode Exit fullscreen mode

Navigate to your Github repository and click on Settings > Deploy keys > Add deploy key

Image description

Name it and paste the copied key.

Now, go back to the Argocd console and click on Connect Repo. Paste the key you generated and click on Connect.

Image description

You will see the repository listed. Ensure you can see the status Successful

Image description

Click on the 3 dots to open the menu and then Create Application

Image description

You'll be redirected to the dashboard page where you'll be able to create the application.

Image description

For Project field you need to select default.

Scroll down until Source section

Image description

Fill the fields as follows:

  • Revision: the branch you wanna sync, in this example we are syncing main
  • Path: the folder location where Argocd can find the kubernetes manifests. In this project, we are pointing to infra/k8s

For Destination section, fill out the fields with the follwing values:

  • Repository URL: select in the combo box
  • Namespace: defautl

Image description

Scroll to the last section and make sure you have Directory selected.

In the exclude field, add kustomization.yaml. This is to avoid Argocd to recreate itself again.

Once it's done, click on Create at the top of the modal and voilà, your application is almost done.

Click on App Details, scroll until "SYNC POLICY" and in AUTOMATED click on SYNC. This will be enough to keep Argocd monitoring the repository / branch / path.

Finally, your application will look like this:

Image description

If you can't see this, just click on the SYNC button at the top of the page and Argocd will sync with your repository.

As you have enabled the auto sync in the previous step, Argocd will watch the repository every 3 minutes. You can disable this just hitting the button App Details and disabling it in the POLICY SYNC section.

Every time you want to sync your application, just click on SYNC and Argocd will update your manifests inside the kubernetes.

How to test it?

There are three ways to run this project:

  • Making any change in the src folder
  • Creating a new tag
  • Change kubernetes manifests in the infra/k8s

Making any change in the src folder

This is the easiest way to test it. Just make any change in the src folder and commit it. Open a PR and merge the changes into the main branch. This will trigger the pipeline and create the new docker image. It will push the image to the container registry, in this example we are using docker.io.

THe pipeline will push the image to the docker registry and update the file infra/k8s/deployment.yaml and the main branch with the new image name.

Argocd will detect the change and update the kubernetes deployment with the new image. The image tag name will be always latest

Creating a new tag

This is the second way to test it. Just create a new tag and push it. The process will follow the same previous flow, however, the image will take the tag as its tag's name.

Change kubernetes manifests in the infra/k8s

This is the third way to test it. Just make any change in the infra/k8s folder and push it to the repository. In this case, the Github Actions won't be triggered and Argocd will update whatever you changed.

For example, let's assume you've changed a value in the configmap. Argocd will identify this and update only the configmap, as this is the only manifest updated.

How to visualize the application?

This project has a NodeJs appliaction showing a simples index.html page. In the configmap, there's a variable COLOR. This variable reflects to the index.html

Image description

COLOR will afect the title "GitOps - ArgoCD" and the "Check my Github".

Minikube needs to provide a port and url to access the service from your local machine. To do that, run the command:

minikube service argocd-server -n argocd --url
Enter fullscreen mode Exit fullscreen mode

The output will be something like this:

http://127.0.0.1:55643
Enter fullscreen mode Exit fullscreen mode

Open this URL in your browser and check the application. To see all the flow working, try to change the color in the configmap and push the changes to the main branch. Wait for a moment until Argocd regocnizes the changes and start making the changes. You can follow this operation by accessing the Argocd console.

Once the update is done, just refresh the express application's page and that's it. If you entered a valid color and followed this guide correctly, you will see the changes.

You can see the full project here

Top comments (0)