How to Set Up GitOps with ArgoCD on Ubuntu 22.04 LTS

In today’s DevOps landscape, managing infrastructure and application deployments in a reliable and consistent way is crucial. GitOps is a popular approach that uses Git as the single source of truth for defining the desired state of your infrastructure and applications. ArgoCD, an open-source continuous delivery tool, makes it easy to implement GitOps practices for Kubernetes environments. This guide walks you through the process of installing and configuring ArgoCD for GitOps on Ubuntu 22.04 LTS.


Prerequisites

Before starting, ensure you have:

  • Administrative access to your Ubuntu server.
  • The following tools installed:
  • Docker (for containerization)
  • Git (for version control)
  • Kubernetes (for orchestration)
  • Basic understanding of containerization, version control, and Kubernetes concepts.

Technical Implementation

Step 1: Install Docker, Kubernetes, and Git

  1. Install Docker:
   sudo apt update && sudo apt install docker.io -y
   sudo systemctl start docker
   sudo systemctl enable docker
   docker --version

Verify that Docker is running:

   docker run hello-world
  1. Install Kubernetes:
    Add the Kubernetes repository and install kubeadm:
   curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
   echo "deb http://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
   sudo apt update && sudo apt install -y kubeadm kubelet kubectl

Initialize the Kubernetes cluster:

   sudo kubeadm init --pod-network-cidr=10.244.0.0/16

Follow the instructions to set up kubectl for your user:

   mkdir -p $HOME/.kube
   sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
   sudo chown $(id -u):$(id -g) $HOME/.kube/config

Step 2: Install ArgoCD

Install ArgoCD in the Kubernetes cluster:

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

Verify the ArgoCD installation:

kubectl get pods -n argocd

Step 3: Access the ArgoCD Web UI

Expose ArgoCD Server:
Create a LoadBalancer service or port-forward the ArgoCD server for local access:

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

Log in to ArgoCD:
Retrieve the initial admin password:

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

Access the ArgoCD web interface at https://localhost:8080 and log in using admin and the decoded password.

Step 4: Configure GitOps with ArgoCD

Create a Git repository for your application configuration:

mkdir my-gitops-project
cd my-gitops-project
git init

Create an ArgoCD Application Manifest:
Create a file named app.yaml:

apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: my-gitops-app
  namespace: argocd
spec:
  project: default
  source:
    repoURL: 'https://github.com/yourusername/your-repo.git'
    targetRevision: HEAD
    path: my-gitops-project
  destination:
    server: 'https://kubernetes.default.svc'
    namespace: default
  syncPolicy:
    automated:
      prune: true
      selfHeal: true

Apply the Application manifest:

kubectl apply -f app.yaml

Sync the application:

argocd app sync my-gitops-app

Best Practices

  • Use Role-Based Access Control (RBAC) to restrict access to ArgoCD.
  • Implement Git commit signing to ensure the integrity of your configuration.
  • Monitor and log deployments using tools like Prometheus and Grafana for better visibility and performance tracking.

Troubleshooting

Common issues you might encounter:

  • ArgoCD server not starting: Check logs using kubectl logs -n argocd -l app.kubernetes.io/name=argocd-server.
  • Application sync errors: Verify the path, repo URL, and permissions of the Git repository.

Refer to the official ArgoCD documentation for more detailed troubleshooting tips.


Conclusion

In this guide, we walked through installing and configuring ArgoCD on Ubuntu 22.04 LTS to implement GitOps for your Kubernetes clusters. By following these steps, you now have a robust, automated pipeline for managing and deploying your infrastructure and applications. Remember to keep your ArgoCD version updated and implement security best practices for optimal performance.

Next Steps:

  • Explore more advanced ArgoCD features, like ArgoCD Notifications.
  • Integrate ArgoCD with a CI/CD tool like Jenkins or GitLab CI.
  • Scale your Kubernetes cluster for enhanced resilience and workload management.

Embrace the power of GitOps to simplify and automate your infrastructure management!