Skip to content

Multi-Cluster Kubernetes: Federation & Cluster API

DodaTech 2 min read

In this tutorial, you'll learn about Multi. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

Multi-cluster Kubernetes enables workload distribution across multiple clusters for high availability, disaster recovery, and geographic latency optimization using Cluster API and federation tools.

What You'll Learn

This tutorial covers Cluster API for provisioning clusters, KubeFed for federated deployment, multi-cluster service discovery, and strategies for workload distribution across clusters.

Why It Matters

Single-cluster deployments create single points of failure. Multi-cluster architectures provide region-level availability, reduce latency for global users, and isolate failure domains.

Real-World Use

Spotify runs over 300 Kubernetes clusters managed by Cluster API. Bloomberg operates multi-cluster deployments across data centers with automated failover using KubeFed and Cluster API.

Cluster API: Declarative Cluster Management

Cluster API treats Kubernetes clusters as Kubernetes resources.

Installing Cluster API

# Install Cluster API CLI
curl -L https://github.com/kubernetes-sigs/cluster-api/releases/download/v1.6.0/clusterctl-linux-amd64 -o clusterctl
chmod +x clusterctl
sudo mv clusterctl /usr/local/bin/

# Initialize Cluster API on management cluster
clusterctl init --infrastructure aws

Defining a Cluster

apiVersion: cluster.x-k8s.io/v1beta1
kind: Cluster
metadata:
  name: production-cluster
  namespace: default
spec:
  clusterNetwork:
    pods:
      cidrBlocks:
      - 10.96.0.0/12
    services:
      cidrBlocks:
      - 10.224.0.0/16
  infrastructureRef:
    apiVersion: infrastructure.cluster.x-k8s.io/v1beta2
    kind: AWSCluster
    name: production-cluster
---
apiVersion: infrastructure.cluster.x-k8s.io/v1beta2
kind: AWSCluster
metadata:
  name: production-cluster
spec:
  region: us-east-1
  sshKeyName: default
# Provision the cluster
kubectl apply -f cluster.yaml

# Watch cluster creation
clusterctl describe cluster production-cluster

# Get kubeconfig for the new cluster
clusterctl get kubeconfig production-cluster > production.kubeconfig

MachineDeployments

apiVersion: cluster.x-k8s.io/v1beta1
kind: MachineDeployment
metadata:
  name: worker-pool
  namespace: default
spec:
  clusterName: production-cluster
  replicas: 5
  template:
    spec:
      clusterName: production-cluster
      version: v1.29.0
      bootstrap:
        configRef:
          name: worker-pool
          apiVersion: bootstrap.cluster.x-k8s.io/v1beta1
          kind: KubeadmConfigTemplate
      infrastructureRef:
        name: worker-pool
        apiVersion: infrastructure.cluster.x-k8s.io/v1beta2
        kind: AWSMachineTemplate

KubeFed: Federated Deployments

KubeFed synchronizes resources across multiple clusters.

# Install KubeFed
kubectl apply -k github.com/kubernetes-sigs/kubefed/config/enable

Federating a Deployment

apiVersion: types.kubefed.io/v1beta1
kind: FederatedDeployment
metadata:
  name: myapp
  namespace: test
spec:
  template:
    metadata:
      labels:
        app: myapp
    spec:
      replicas: 3
      template:
        spec:
          containers:
          - image: nginx
            name: nginx
  placement:
    clusters:
    - name: cluster-us
    - name: cluster-eu
    - name: cluster-asia
  overrides:
  - clusterName: cluster-asia
    clusterOverrides:
    - path: "/spec/replicas"
      value: 5

Multi-Cluster Service Discovery

Use ServiceExports and ServiceImports for cross-cluster service discovery.

apiVersion: multicluster.x-k8s.io/v1alpha1
kind: ServiceExport
metadata:
  name: api-service
  namespace: production
---
apiVersion: multicluster.x-k8s.io/v1alpha1
kind: ServiceImport
metadata:
  name: api-service
  namespace: production
spec:
  type: ClusterSetIP
  ports:
  - port: 8080

Backup and Restore with Velero

Velero supports migrating resources between clusters.

# Backup from source cluster
velero backup create migration-backup --include-namespaces production

# Restore to destination cluster
velero restore create --from-backup Migration-backup

Practice Questions

  1. What is the role of the management cluster in Cluster API? It runs Cluster API controllers and stores cluster definitions as custom resources, provisioning worker clusters declaratively.

  2. How does KubeFed propagate resources to member clusters? It watches FederatedDeployment resources and creates corresponding Deployment resources in each selected member cluster.

  3. What is the purpose of overrides in federation? Overrides customize resource configurations per cluster, such as different replica counts for different regions.

  4. How do you access a Cluster API provisioned cluster? Use clusterctl get kubeconfig to retrieve the kubeconfig for the provisioned cluster.

  5. What is a MachineDeployment in Cluster API? It defines a group of worker nodes with the same configuration, similar to a Deployment for nodes.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro