K8s: Multi-Cluster Management

Saurabh AdhauSaurabh Adhau
4 min read

🎯 Learning Objective

Understand how to manage multiple Kubernetes clusters using tools and strategies that provide centralized control, monitoring, and deployment.

πŸ“– Scenario

It would help if you managed multiple Kubernetes clusters across different environments (e.g., development, staging, production) to ensure consistency, security, and ease of administration.

Explanation

Managing multiple Kubernetes clusters can be complex without proper tools and strategies. Centralized management tools like Rancher, Kubernetes Federation (KubeFed), and GitOps provide a unified way to control, monitor, and deploy across multiple clusters.

πŸ”‘ Key Concepts:

  1. Multi-Cluster Management:

    • Managing multiple Kubernetes clusters from a single control plane.
  2. Rancher:

    • An open-source multi-cluster Kubernetes management platform.
  3. Kubernetes Federation (KubeFed):

    • A tool for coordinating the configuration of multiple Kubernetes clusters.
  4. GitOps:

    • A model where Git is the single source of truth for the system's desired state and changes are automatically applied to the clusters.

Rancher Installation

Rancher is an open-source platform for managing multiple Kubernetes clusters.

πŸ“‘ Install Rancher:

Deploy Rancher using Docker:

sudo docker run -d --restart=unless-stopped \
-p 80:80 -p 443:443 \
rancher/rancher:latest

Access the Rancher UI:

  • Open a web browser and navigate to https://<your-server-ip>

Kubernetes Federation (KubeFed) Setup

KubeFed allows you to coordinate configuration across multiple Kubernetes clusters.

πŸ“‘ Install KubeFed:

Install KubeFed CLI (kubefedctl):

wget https://github.com/kubernetes-sigs/kubefed/releases/download/v0.7.0/kubefedctl-$(uname | tr '[:upper:]' '[:lower:]')-amd64
chmod +x kubefedctl-$(uname | tr '[:upper:]' '[:lower:]')-amd64
sudo mv kubefedctl-$(uname | tr '[:upper:]' '[:lower:]')-amd64 /usr/local/bin/kubefedctl

Deploy KubeFed to the Host Cluster:

kubectl create ns kube-federation-system
kubefedctl join <HOST_CLUSTER> --cluster-context <HOST_CONTEXT> --host-cluster-context <HOST_CONTEXT> --v=2

Join Member Clusters:

kubefedctl join <MEMBER_CLUSTER> --cluster-context <MEMBER_CONTEXT> --host-cluster-context <HOST_CONTEXT> --v=2

GitOps Setup

GitOps uses Git as the single source of truth for your clusters’ desired state, with tools like Argo CD or Flux to apply changes.

πŸ“‘ Install Argo CD:

Install Argo CD in your Kubernetes cluster:

kubectl create namespace argocd
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml

Access the Argo CD UI:

  • Forward the Argo CD server port to your local machine:
kubectl port-forward svc/argocd-server -n argocd 8080:443

Steps to Implement Multi-Cluster Management

  1. Install and Access Rancher:

    • Deploy Rancher using Docker.

    • Access the Rancher UI via the web browser.

  2. Set Up Kubernetes Federation (KubeFed):

    • Install the KubeFed CLI.

    • Deploy KubeFed to the host cluster.

    • Join member clusters to the federation.

  3. Implement GitOps with Argo CD:

    • Install Argo CD in your Kubernetes cluster.

    • Access the Argo CD UI via port forwarding.

πŸ” Detailed Example Explanation:

Rancher:

  • Provides a web-based UI for managing multiple Kubernetes clusters, including provisioning, upgrades, monitoring, and security management.

KubeFed:

  • Enables you to manage multiple clusters as a single entity, allowing for consistent configuration and resource management across clusters.

GitOps with Argo CD:

  • Argo CD continuously monitors Git repositories and applies changes to Kubernetes clusters, ensuring the desired state is always maintained.

πŸ’‘ Benefits for Enterprise Applications:

  1. Centralized Management: Simplifies the management of multiple clusters from a single control plane.

  2. Consistency: Ensures consistent configuration and policies across clusters.

  3. Scalability: Easily manage and scale applications across multiple clusters.

  4. Disaster Recovery: Provides high availability and disaster recovery by distributing workloads across clusters.

Additional Concepts and Examples

Rancher Cluster Management:

Use Rancher to create, import, and manage Kubernetes clusters.

Example:

Creating a new cluster in Rancher:

  • Use the Rancher UI to create a new cluster

Federated Resources with KubeFed:

Use KubeFed to manage federated resources across clusters.

Example:

Creating a federated deployment:

apiVersion: types.kubefed.io/v1beta1
kind: FederatedDeployment
metadata:
  name: nginx
  namespace: default
spec:
  template:
    metadata:
      labels:
        app: nginx
    spec:
      replicas: 3
      template:
        metadata:
          labels:
            app: nginx
        spec:
          containers:
          - name: nginx
            image: nginx:1.14.2
            ports:
            - containerPort: 80

GitOps Workflow with Argo CD:

Use Argo CD to deploy applications and manage configuration across clusters.

Example:

Creating an Argo CD application:

apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: guestbook
  namespace: argocd
spec:
  project: default
  source:
    repoURL: https://github.com/argoproj/argocd-example-apps.git
    targetRevision: HEAD
    path: guestbook
  destination:
    server: https://kubernetes.default.svc
    namespace: default
  syncPolicy:
    automated:
      prune: true
      selfHeal: true

Hands-on Activity:

  1. Install and Access Rancher:

    • Deploy Rancher and access its UI via a web browser.
  2. Set Up Kubernetes Federation (KubeFed):

    • Install the KubeFed CLI, deploy KubeFed, and join member clusters.
  3. Implement GitOps with Argo CD:

    • Install Argo CD, access its UI, and create an application for deployment.
  4. Manage Clusters with Rancher:

    • Use Rancher to create, import, and manage clusters.
  5. Create Federated Resources with KubeFed:

    • Define and apply federated resources across clusters.
  6. Deploy Applications with Argo CD:

    • Use Argo CD to deploy and manage applications across clusters.
  7. Verify and Inspect:

    • Use Rancher, KubeFed, and Argo CD UIs and commands to verify and inspect multi-cluster management.

Following these steps, you can effectively manage multiple Kubernetes clusters, ensuring consistency, security, and ease of administration across different environments.

0
Subscribe to my newsletter

Read articles from Saurabh Adhau directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Saurabh Adhau
Saurabh Adhau

As a DevOps Engineer, I thrive in the cloud and command a vast arsenal of tools and technologies: ☁️ AWS and Azure Cloud: Where the sky is the limit, I ensure applications soar. πŸ”¨ DevOps Toolbelt: Git, GitHub, GitLab – I master them all for smooth development workflows. 🧱 Infrastructure as Code: Terraform and Ansible sculpt infrastructure like a masterpiece. 🐳 Containerization: With Docker, I package applications for effortless deployment. πŸš€ Orchestration: Kubernetes conducts my application symphonies. 🌐 Web Servers: Nginx and Apache, my trusted gatekeepers of the web.