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: defaultApply it with:
kubectl apply -f applicationset.yamlThis 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: NeverApply the hook with:
kubectl apply -f sync-hooks.yamlThis 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.yamlThis 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/replicasThis 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: productionThis ensures only resources labeled as env: production are managed by ArgoCD.
Hands-On Exercise
- Deploy an ApplicationSet across multiple clusters
- Create and apply an
ApplicationSetYAML file.
- Create and apply an
- Configure PreSync and PostSync hooks
- Add a hook for a database migration job.
- Implement and test a custom ArgoCD plugin
- Configure a custom manifest generator plugin.
- Exclude specific resources from synchronization
- Modify the
ignoreDifferencesfield to prevent syncing unwanted changes.
- Modify the
By mastering these advanced features, you’ll unlock the full potential of ArgoCD for large-scale Kubernetes deployments!