Skip to content

第五部分 多集群与 GitOps

随着业务规模扩大,单集群往往无法满足需求。本部分讲解多集群管理方案、Cluster API 和 GitOps 交付模式。


第 19 章 多集群管理方案

19.1 为什么需要多集群

  • 高可用和灾备
  • 地域分布
  • 环境隔离(开发/测试/生产)
  • 避免单集群规模瓶颈
  • 多云策略

19.2 多集群管理工具

工具特点
Karmada华为开源,K8s 原生多集群编排
Federation v2K8s 社区方案
Cluster API声明式集群生命周期管理
Rancher企业级多集群管理平台
Open Cluster ManagementCNCF 项目

19.3 多集群网络

  • 各集群独立网络
  • 通过 VPN/专线/云联网互联
  • 使用 Submariner 等工具实现跨集群 Pod 通信

19.4 本章练习

  1. 列出多集群管理的典型场景。
  2. 比较 Karmada 和 Cluster API 的适用场景。

第 20 章 Karmada 多集群编排

20.1 Karmada 架构

┌─────────────────────────────────────┐
│           Karmada 控制平面           │
│  ┌─────────┐ ┌─────────┐ ┌────────┐ │
│  │ API Server│ │ Scheduler│ │ Controller│ │
│  └─────────┘ └─────────┘ └────────┘ │
└───────────┬─────────────────────────┘
            │ PropagationPolicy
    ┌───────┴───────┐
    v               v
┌─────────┐    ┌─────────┐
│ Member1 │    │ Member2 │
│ Cluster │    │ Cluster │
└─────────┘    └─────────┘

20.2 部署 Karmada

bash
# 使用 Helm 部署
git clone https://github.com/karmada-io/karmada.git
cd karmada
kubectl create ns karmada-system
helm install karmada ./charts/karmada -n karmada-system

20.3 PropagationPolicy

yaml
apiVersion: policy.karmada.io/v1alpha1
kind: PropagationPolicy
metadata:
  name: nginx-propagation
spec:
  resourceSelectors:
  - apiVersion: apps/v1
    kind: Deployment
    name: nginx
  placement:
    clusterAffinity:
      clusterNames:
      - member1
      - member2
    replicaScheduling:
      replicaDivisionPreference: Weighted
      replicaSchedulingType: Divided
      weightPreference:
        staticWeightList:
        - targetCluster:
            clusterNames:
            - member1
          weight: 1
        - targetCluster:
            clusterNames:
            - member2
          weight: 1

20.4 本章练习

  1. 部署 Karmada 控制平面。
  2. 接入两个 Member 集群。
  3. 使用 PropagationPolicy 将应用分发到多个集群。

第 21 章 Cluster API

21.1 什么是 Cluster API

Cluster API 是 Kubernetes 的一个子项目,提供声明式 API 来创建、配置和管理集群。

21.2 核心资源

资源说明
Cluster集群定义
Machine工作节点定义
MachineDeployment节点池
KubeadmControlPlane控制平面

21.3 本章练习

  1. 了解 Cluster API 的核心概念。
  2. 尝试使用 Cluster API 在云环境中创建一个集群。

第 22 章 GitOps 与 ArgoCD

22.1 什么是 GitOps

GitOps 是一种以 Git 为单一事实来源的持续交付模式:

  • 基础设施和应用配置都存储在 Git 中
  • 自动化工具监控 Git 变化并同步到集群
  • 任何变更都可追溯、可回滚

22.2 ArgoCD 架构

Git Repo


ArgoCD → K8s API Server → Cluster

22.3 部署 ArgoCD

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

# 获取初始密码
argocd admin initial-password -n argocd

22.4 创建 ArgoCD Application

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

22.5 ArgoCD 最佳实践

  • 一个应用一个 Application
  • 使用 ApplicationSet 管理多环境
  • 配置漂移自动修复
  • 启用 SSO 和 RBAC
  • 使用 Argo Rollouts 实现渐进式交付

22.6 Flux 简介

Flux 是另一个流行的 GitOps 工具,与 ArgoCD 类似,但更偏向声明式和 GitOps Toolkit。

22.7 本章练习

  1. 部署 ArgoCD。
  2. 创建一个 Application 同步 Git 仓库中的应用。
  3. 修改 Git 中的配置,观察 ArgoCD 自动同步。
  4. 了解 Argo Rollouts 的蓝绿/金丝雀发布。

第五部分总结

完成本阶段学习后,你应该能够:

  • 理解多集群管理的场景和工具
  • 使用 Karmada 分发应用到多集群
  • 了解 Cluster API 的声明式集群管理
  • 使用 ArgoCD 实施 GitOps 持续交付

进入第六部分:高级可观测性