ArgoCD

What is ArgoCD all about?

ArgoCD is a declarative GitOps tool that automates Kubernetes deployments by syncing our clusters with the desired state in your Git repositories.

Integrating it with our existing pipeline enhances our CI/CD process by separating continuous integration (handled by GitLab) from continuous deployment (managed by ArgoCD). So, while GitLab builds (and tests) our code, ArgoCD ensures automated, consistent, and traceable deployments.

Get access to the ArgoCD Dashboard

We already set up the ArgoCD basics needed in our cluster, but there are no configurations yet. So for starters, let’s connect to the ArgoCD dashboard running in our cluster. For this, we first need a port forwarding:

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

When visiting http://localhost:8080, we should now see:

ArgoCD Homescreen

Seeing it? Great!

In order to log in, we need to get the admin passwort ArgoCD automatically created for us. Let’s run:

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

Please copy (and keep for the duration of this workshop) the password from your terminal and login in on your browser with the username admin.

ArgoCD Dashboard

We can now click through the dashboard which is, not surprinsingly, pretty empty in the moment.

Adding our repository

As the very first step, let’s connect our Gitlab repository with ArgoCD. To ensure ArgoCD is not only connected to it, but also is allowed to read from it, we again need a Gitlab deploy token.

Hence visit the Gitlab UI, navigate to Settings -> Repository and look for Deploy tokens. Expand the section and click “Add token”.

Name it “argocd-deploy-token”, choose the “read repository” scope and create your token. Copy the password, as you won’t see it again :)

Having the token, we could now add the repository in the ArgoCD way. But thats not very DevOps-y, right? So let’s use our terminal instead.

To be able to communicate with ArgoCD, we first need to install the CLI by running

brew install argocd

Other installation options can be found here.

All set? Great, then lets login to the ArgoCD Server by running:

argocd login localhost:8080 \
  --insecure \
  --username admin \
  --password $(kubectl get secret \
  -n argocd \
  argocd-initial-admin-secret \
  -o jsonpath="{.data.password}" |
  base64 -d)

After we successfully logged in, let’s add our repository with the following command:

argocd repo add https://git.bnerd.com/workshop/todo-app-<your namespace>.git --username <deploy-token-name> --password <deploy-token-password>

Let’s check in the ArgoCD UI: Navigate to Settings, click on Repositories - and there we have it 🎉

Adding our application

Our repository is now linked with ArgoCD and ArgoCD is able to read from it thanks to the deploy token. Time to get our applications into ArgoCD.

In our infrastructure folder, create a new directory called argocd-apps. Within that directory, create the files project.yaml,frontend.yaml and backend.yaml and add the following:

project.yaml

apiVersion: argoproj.io/v1alpha1
kind: AppProject
metadata:
  name: todo-app-<your namespace> # Add your namespace
  namespace: argocd
spec:
  description: Frontend and backend services for my ToDo App
  sourceRepos:
    - https://git.bnerd.com/workshop/todo-app-<your namespace>.git # Add your namespace
  destinations:
    - namespace: <your namespace> # Add your namespace
      server: https://kubernetes.default.svc
  clusterResourceWhitelist:
    - group: "*"
      kind: "*"

frontend.yaml

apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: frontend-<your namespace> # Add your namespace
  namespace: argocd
spec:
  project: todo-app-<your namespace> # Add your namespace
  source:
    repoURL: https://git.bnerd.com/workshop/todo-app-<your namespace>.git # Add your namespace
    targetRevision: main
    path: infrastructure/charts/frontend
    helm:
      valueFiles:
        - values.yaml
      parameters:
        - name: ingress.hosts[0].host
          value: "bnerd-<your namespace>.todos" # Add your namespace
  destination:
    server: https://kubernetes.default.svc
    namespace: <your namespace> # Add your namespace
  syncPolicy:
    automated:
      prune: true
      selfHeal: true

backend.yaml

apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: backend-<your namespace> # Add your groupname
  namespace: argocd
spec:
  project: todo-app-<your namespace> # Add your namespace
  source:
    repoURL: https://git.bnerd.com/workshop/todo-app-<your namespace>.git # Add your groupname
    targetRevision: main
    path: infrastructure/charts/backend
    helm:
      valueFiles:
        - values.yaml
      parameters:
        - name: ingress.hosts[0].host
          value: "bnerd-<your namespace>.api" # New host for backend when multiple apps
  destination:
    server: https://kubernetes.default.svc
    namespace: <your namespace> # Add your namespace
  syncPolicy:
    automated:
      prune: true
      selfHeal: true

⚠️ Please do not forget to add your namespaces in both files.

Ready? Then let’s add these configurations to our cluster by running from the root of our project:

kubectl apply -f infrastructure/argocd-apps/project.yaml

to setup a project, then

kubectl apply -f infrastructure/argocd-apps/frontend.yaml

for adding the frontend and with

kubectl apply -f infrastructure/argocd-apps/backend.yaml

for adding the backend.

Lets have a look into the ArgoCD UI and there we have our applications :)