ArgoCD: Advanced Usage

Managing Multiple Clusters and Environments

ArgoCD makes it easy to manage deployments across multiple Kubernetes clusters and environments like dev, staging, and production.

Adding Multiple Kubernetes Clusters

To add a new cluster to ArgoCD:

argocd cluster add <context-name>

This registers an external Kubernetes cluster, allowing ArgoCD to deploy and manage applications remotely.

Using ApplicationSets for Multi-Cluster Deployments

ApplicationSets allow you to define applications dynamically across multiple clusters.

apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
  name: multi-cluster-app
spec:
  generators:
    - list:
        elements:
          - cluster: dev
          - cluster: staging
  template:
    metadata:
      name: '{{cluster}}-app'
    spec:
      project: default
      source:
        repoURL: https://github.com/myrepo.git
        path: k8s-manifests
      destination:
        server: '{{cluster}}'
        namespace: default

Apply it with:

kubectl apply -f applicationset.yaml

This will deploy the same application to multiple clusters automatically.

Customizing ArgoCD Behaviors with Sync Hooks

Sync Hooks allow pre- and post-deployment tasks, such as database migrations, to be executed as part of the deployment process.

Example: PreSync Hook for Database Migrations

apiVersion: batch/v1
kind: Job
metadata:
  annotations:
    argocd.argoproj.io/hook: PreSync
  name: db-migration
spec:
  template:
    spec:
      containers:
        - name: migrate
          image: myrepo/migrate:latest
      restartPolicy: Never

Apply the hook with:

kubectl apply -f sync-hooks.yaml

This ensures the migration job runs before the main deployment begins.

ArgoCD Plugins and Extension Mechanisms

ArgoCD supports Config Management Plugins (CMP), allowing customization of how manifests are processed before deployment.

Installing and Configuring a Custom Plugin

apiVersion: v1
kind: ConfigMap
metadata:
  name: argocd-cmp-plugin
  namespace: argocd
data:
  configManagementPlugins: |
    - name: my-plugin
      generate:
        command: ["/usr/local/bin/my-plugin"]

Apply the plugin configuration:

kubectl apply -f plugin-config.yaml

This custom plugin will be executed whenever ArgoCD processes manifests.

Resource Management (Resource Inclusion/Exclusion)

ArgoCD allows fine-grained control over which Kubernetes resources are managed and synchronized.

Excluding Certain Resource Types from Sync

To ignore specific fields (e.g., number of replicas in a Deployment):

spec:
  ignoreDifferences:
    - group: apps
      kind: Deployment
      jsonPointers:
        - /spec/replicas

This ensures that changes to replicas made outside Git do not trigger a sync.

Selecting Only Specific Resources

To apply ArgoCD sync to a specific set of resources:

spec:
  resourceSelector:
    matchLabels:
      env: production

This ensures only resources labeled as env: production are managed by ArgoCD.

Hands-On Exercise

  1. Deploy an ApplicationSet across multiple clusters
    • Create and apply an ApplicationSet YAML file.
  2. Configure PreSync and PostSync hooks
    • Add a hook for a database migration job.
  3. Implement and test a custom ArgoCD plugin
    • Configure a custom manifest generator plugin.
  4. Exclude specific resources from synchronization
    • Modify the ignoreDifferences field to prevent syncing unwanted changes.

By mastering these advanced features, you’ll unlock the full potential of ArgoCD for large-scale Kubernetes deployments!