Sling Academy
Home/DevOps/How to Implement GitOps with Kubernetes Using ArgoCD

How to Implement GitOps with Kubernetes Using ArgoCD

Last updated: January 30, 2024

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.

Next Article: How to Use Terraform with Kubernetes for Infrastructure as Code

Previous Article: How to Use Kubernetes with Prometheus and Grafana for Monitoring

Series: Kubernetes Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide