Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

🏠 Back to Blog

Lab 06: Multi-Cluster

Argo CD runs in one (management) cluster and deploys to others. Three separate minikube clusters (not nodes): management, staging, prod.

Clusters

minikube start -p management --network=bridge --memory=2048 --cpus=2
minikube start -p staging --network=bridge --memory=2048 --cpus=2
minikube start -p prod --network=bridge --memory=2048 --cpus=2
kubectl get nodes --context staging
  • Each profile is a docker container on the default bridge network, so containers reach each other by IP
  • Kube contexts are named after the profiles

Argo CD in management:

kubectl config use-context management
kubectl create namespace argocd
kubectl apply -n argocd --server-side -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
kubectl wait --for=condition=Ready pods --all -n argocd --timeout=300s
kubectl port-forward svc/argocd-server -n argocd 8080:443
argocd login localhost:8080 --username admin --insecure --password "$(argocd admin initial-password -n argocd | head -1)"
  • Port-forward in its own terminal

Register clusters

Normally argocd cluster add <context>. Its kubeconfig server address is 127.0.0.1:<port> for minikube, which the in-cluster Argo CD can’t reach, so register by hand using the node’s bridge IP.

What cluster add does: creates argocd-manager ServiceAccount + cluster role in the target, then stores a cluster Secret in argocd.

STAGING_IP=$(docker inspect staging -f '{{.NetworkSettings.Networks.bridge.IPAddress}}')
kubectl --context staging -n kube-system create serviceaccount argocd-manager
kubectl --context staging create clusterrolebinding argocd-manager \
  --clusterrole=cluster-admin --serviceaccount=kube-system:argocd-manager
kubectl --context staging apply -f - <<EOF
apiVersion: v1
kind: Secret
metadata:
  name: argocd-manager-token
  namespace: kube-system
  annotations:
    kubernetes.io/service-account.name: argocd-manager
type: kubernetes.io/service-account-token
EOF
TOKEN=$(kubectl --context staging -n kube-system get secret argocd-manager-token -o jsonpath='{.data.token}' | base64 -d)
CA=$(kubectl --context staging -n kube-system get secret argocd-manager-token -o jsonpath='{.data.ca\.crt}')

Cluster secret in management (label is what makes it a cluster):

kubectl --context management apply -f - <<EOF
apiVersion: v1
kind: Secret
metadata:
  name: cluster-staging
  namespace: argocd
  labels:
    argocd.argoproj.io/secret-type: cluster
    environment: staging
    tier: non-prod
stringData:
  name: staging
  server: https://${STAGING_IP}:8443
  config: |
    {"bearerToken": "${TOKEN}", "tlsClientConfig": {"caData": "${CA}"}}
EOF
argocd cluster list
  • Repeat for prod (secret cluster-prod, labels environment: prod, tier: prod)
  • minikube apiserver listens on 8443 inside the container and its cert includes the node IP, so TLS verifies
  • Labels on the secret are what the ApplicationSet cluster generator selects on (can also be set with argocd cluster set <name> --label k=v)
  • cluster-admin is lazy, scope it down for real, and note tokens from kubernetes.io/service-account-token secrets never expire
  • in-cluster can’t be edited via CLI, label its secret directly if needed
  • Status shows Unknown until an app uses the cluster

App per cluster

Repo multi-cluster-app with manifests/ (namespace, deployment, service for web-app, replicas: 2):

argocd app create web-app-staging \
  --repo https://github.com/<user>/multi-cluster-app.git \
  --path manifests \
  --dest-name staging \
  --dest-namespace web-app \
  --sync-policy automated --self-heal \
  --sync-option CreateNamespace=true
argocd app create web-app-prod \
  --repo https://github.com/<user>/multi-cluster-app.git \
  --path manifests \
  --dest-name prod \
  --dest-namespace web-app \
  --sync-policy automated --self-heal \
  --sync-option CreateNamespace=true
kubectl --context staging get all -n web-app
kubectl --context prod get all -n web-app
  • Destination is --dest-name (cluster name) or --dest-server (URL), not both
  • The deployment is created on the remote cluster, none on management

Per-cluster config with kustomize

base/            namespace.yaml deployment.yaml service.yaml kustomization.yaml
overlays/staging kustomization.yaml
overlays/prod    kustomization.yaml
# overlays/prod/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base
patches:
- target:
    kind: Deployment
    name: web-app
  patch: |-
    - op: replace
      path: /spec/replicas
      value: 5
argocd app set web-app-staging --path overlays/staging
argocd app set web-app-prod --path overlays/prod
  • overlays/staging is the same with replicas: 2 (or no patch)
  • Git push triggers the auto-sync

ApplicationSet

  • One ApplicationSet generates an Application per generated parameter set
  • Generators: clusters (registered clusters, filtered by labels), git (dirs/files), list, matrix/merge (combine)
  • The ApplicationSet controller is part of the install
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
  name: web-app
  namespace: argocd
spec:
  generators:
  - clusters:
      selector:
        matchExpressions:
        - key: environment
          operator: In
          values: [staging, prod]
  template:
    metadata:
      name: 'web-app-{{name}}'
    spec:
      project: default
      source:
        repoURL: https://github.com/<user>/multi-cluster-app.git
        targetRevision: HEAD
        path: 'overlays/{{metadata.labels.environment}}'
      destination:
        server: '{{server}}'
        namespace: web-app
      syncPolicy:
        automated:
          prune: true
          selfHeal: true
        syncOptions:
        - CreateNamespace=true
  • {{name}}, {{server}}, {{metadata.labels.<k>}} come from the cluster secret
  • The environment label value has to match an overlay dir name (staging, prod)
  • Delete the two hand-made apps first or the names would clash on web-app-staging / web-app-prod
  • Add a third cluster with the label and an app appears with no other change
  • Deleting the ApplicationSet deletes the apps it generated

Failover

Active primary, manual standby on staging:

apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: critical-backup
  namespace: argocd
spec:
  project: default
  source:
    repoURL: https://github.com/argoproj/argocd-example-apps.git
    targetRevision: HEAD
    path: guestbook
  destination:
    name: staging
    namespace: critical
  syncPolicy:
    syncOptions:
    - CreateNamespace=true

Simulate prod loss:

minikube stop -p prod
argocd cluster list
argocd app sync critical-backup
kubectl --context staging get all -n critical
minikube start -p prod
  • Apps for the stopped cluster show Unknown / comparison errors, Argo CD doesn’t fail over by itself
  • The standby is only ever synced by hand (or by flipping a label an ApplicationSet selects on)
  • The manifests stay in Git, so rebuilding a cluster = register it and sync (that’s the DR story, also back up Argo CD itself with argocd admin export)
  • Traffic failover (DNS/LB) is outside Argo CD
  • After minikube start the node IP may change, update the cluster secret server

Overview

argocd app list -o wide
argocd app list --dest-name prod
argocd app list -o json | jq -r '.[] | "\(.metadata.name) \(.status.sync.status) \(.status.health.status)"'

Cleanup

kubectl --context management delete applicationset --all -n argocd
argocd app delete web-app-staging web-app-prod critical-backup --yes
minikube delete -p staging
minikube delete -p prod
minikube delete -p management
  • Delete apps/appsets before the clusters, otherwise the finalizer can’t reach the cluster and deletion hangs
  • minikube delete also removes the kube contexts