ArgoCD is a powerful GitOps tool designed to automate Kubernetes deployments. It provides a declarative approach to continuous deployment, ensuring your Kubernetes clusters are always in sync with your Git repository.

In this guide, we’ll walk through setting up ArgoCD in a Kubernetes cluster and deploying a sample application.


Prerequisites

Before getting started, ensure you have:

  • A running Kubernetes cluster.
  • kubectl installed and configured to access your cluster.
  • Administrative access to the cluster (for namespace creation and permissions).
  • A Git repository containing Kubernetes manifests or Helm charts.

Step 1: Install ArgoCD

  1. Add the argocd namespace to your Kubernetes cluster:
kubectl create namespace argocd
  1. Install ArgoCD using the official manifests:
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml 
  1. Verify that the ArgoCD pods are running:
kubectl get pods -n argocd

You should see pods like argocd-server, argocd-repo-server, argocd-application-controller, and argocd-dex-server in a Running state.

Step 2: Expose the ArgoCD Server

ArgoCD’s server needs to be accessible for managing your deployments. Choose one of the following methods to expose it:

Option 1: Port Forwarding (For Local Development) Run the following command to access the ArgoCD server locally:

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

Visit https://localhost:8080 in your browser.

Option 2: LoadBalancer or Ingress (For Production) Edit the argocd-server service to use a LoadBalancer:

kubectl edit svc argocd-server -n argocd

Change the type to LoadBalancer and save. The external IP will be assigned by your cloud provider:

kubectl get svc -n argocd

Step 3: Log in to ArgoCD

  1. Retrieve the initial admin password:
kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -d
  1. Log in to the ArgoCD UI using the admin username and password:

Step 4: Configure a Git Repository

  1. Add your Git repository to ArgoCD:
argocd repo add https://github.com/your-repo/kubernetes-manifests.git \
    --username <your-username> \
    --password <your-password>
  1. Verify the repository was added successfully:
argocd repo list

Step 5: Deploy an Application

  1. Create an ArgoCD Application that syncs your Git repository to a Kubernetes namespace:
kubectl apply -f - <<EOF
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: sample-app
  namespace: argocd
spec:
  project: default
  source:
    repoURL: https://github.com/your-repo/kubernetes-manifests.git
    targetRevision: HEAD
    path: manifests/sample-app
  destination:
    server: https://kubernetes.default.svc
    namespace: sample-app
  syncPolicy:
    automated:
      prune: true
      selfHeal: true
EOF
  1. Verify the application status:
argocd app get sample-app
  1. Check if the application is synced and running:
kubectl get all -n sample-app

Step 6: Automating Syncing

Enable automated syncing to ensure that the cluster always matches the desired state in the Git repository. This can be done by setting syncPolicy.automated in the application manifest.

syncPolicy:
  automated:
    prune: true
    selfHeal: true

Prune: Automatically delete resources no longer defined in Git. SelfHeal: Reconcile cluster state if drift occurs.