How to Implement GitOps with Kubernetes Using ArgoCD

Updated: January 30, 2024 By: Guest Contributor Post a comment

Introduction to GitOps with Argo CD

GitOps is a modern approach to automation focused on using Git as the single source of truth for defining the state of applications and infrastructure. Combined with Kubernetes—a powerful container orchestration tool—GitOps streamlines development, deployment, scaling, and management of containerized applications. Argo CD, built with this philosophy in mind, lends itself naturally to manage Kubernetes resources through Git. This tutorial demonstrates how Argo CD can facilitate a GitOps workflow in a Kubernetes environment.

Prerequisites

  • A running Kubernetes cluster
  • kubectl installed and configured
  • Basic understanding of Kubernetes objects like Deployments and Services
  • A Git repository ready for use

Setting up Argo CD in Kubernetes

First, you need to install Argo CD into your Kubernetes cluster. You can do this using a single kubectl command to apply the official manifest from the Argo CD GitHub repository:

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

After the installation, you can access Argo CD using port forwarding:

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

Navigate to http://localhost:8080 in your web browser and use the default login ‘admin’, and the password which is the argocd-server pod name. Obtain it by running:

kubectl get pods -n argocd -l app.kubernetes.io/name=argocd-server -o name

Connecting a Git Repository

Configure Argo CD to monitor your Git repository. Suppose your repository URL is https://github.com/your-username/your-repo.git:

argocd repo add https://github.com/your-username/your-repo.git

Argo CD will now start to track changes in this repository.

Defining Infrastructure as Code (IaC)

Create a Kubernetes deployment manifest in YAML format and push it to your Git repository:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: sample-app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: sample
  template:
    metadata:
      labels:
        app: sample
    spec:
      containers:
      - name: nginx-container
        image: nginx:latest

Commit and push this file to your repository. This file serves as the declarative state you want your Kubernetes cluster to achieve.

Creating an Application in Argo CD

Use Argo CD CLI or UI to create an application that tracks your deployment:

argocd app create sample-app \
  --repo https://github.com/your-username/your-repo.git \
  --path . \
  --dest-server https://kubernetes.default.svc \
  --dest-namespace default

Argo CD will now sync your Kubernetes cluster to the state defined in your Git repository.

Whenever you make changes to the Kubernetes manifests in your Git repository, Argo CD will detect these changes and apply them to the cluster. You can check the sync status by:

argocd app get sample-app

Advanced Operations

To ensure your applications meet various requirements you’ll need to understand advanced features and operations such as rollbacks, automated sync policies, and handling secrets confidentially. These aspects are covered in the following articles:

Monitoring and Alerts

Integrating monitoring and alerting systems is additional phase in developing a resilient GitOps pipeline. Tools like Prometheus, Grafana, or third-party services can be set up to track the performance and health of your applications managed by Argo CD. See the detailed guide here: How to Use Kubernetes with Prometheus and Grafana for Monitoring.

Conclusion

Argo CD simplifies your Kubernetes deployments through GitOps, leading to more deterministic and reliable CICD pipelines. The introduction of a Git-focused approach for cluster management not only centralizes control but also ensures consistency and accountability across development stages.