第四章:多集群部署¶
多集群架构¶
架构模式¶
┌─────────────────────────────────────────────────────────────────────┐
│ ArgoCD 多集群架构 │
│ │
│ ┌─────────────────────────────────────────────────────────────────┐│
│ │ ArgoCD (管理集群) ││
│ │ ││
│ │ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ ││
│ │ │ Application │ │ Application │ │ Application │ ││
│ │ │ (Dev) │ │ (Staging) │ │ (Production)│ ││
│ │ └─────────────┘ └─────────────┘ └─────────────┘ ││
│ └──────────────────────────┬──────────────────────────────────────┘│
│ │ │
│ ┌─────────────────┼─────────────────┐ │
│ │ │ │ │
│ ▼ ▼ ▼ │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ Dev Cluster │ │Staging Cluster│ │Prod Cluster │ │
│ │ │ │ │ │ │ │
│ │ ┌─────────┐ │ │ ┌─────────┐ │ │ ┌─────────┐ │ │
│ │ │ App │ │ │ │ App │ │ │ │ App │ │ │
│ │ └─────────┘ │ │ └─────────┘ │ │ └─────────┘ │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ │
└─────────────────────────────────────────────────────────────────────┘
部署模式¶
1. 集中式部署
- ArgoCD 部署在一个管理集群
- 管理多个目标集群
2. 分布式部署
- 每个集群部署 ArgoCD
- 独立管理
3. 混合模式
- 核心集群使用集中式
- 边缘集群使用分布式
添加集群¶
准备工作¶
# 确保 kubeconfig 包含目标集群
kubectl config get-contexts
# 切换到目标集群
kubectl config use-context target-cluster
添加集群¶
# 添加集群
argocd cluster add target-cluster
# 指定命名空间
argocd cluster add target-cluster --namespace argocd
# 设置标签
argocd cluster add target-cluster --label env=production
# 设置名称
argocd cluster add target-cluster --name production
通过 YAML 添加¶
apiVersion: v1
kind: Secret
metadata:
name: cluster-production
namespace: argocd
labels:
argocd.argoproj.io/secret-type: cluster
env: production
stringData:
name: production
server: https://production.example.com
config: |
{
"bearerToken": "<service-account-token>",
"tlsClientConfig": {
"insecure": false,
"caData": "<base64-encoded-ca>"
}
}
创建 Service Account¶
# 在目标集群创建
apiVersion: v1
kind: ServiceAccount
metadata:
name: argocd-manager
namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: argocd-manager-role
rules:
- apiGroups: ["*"]
resources: ["*"]
verbs: ["*"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: argocd-manager-role-binding
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: argocd-manager-role
subjects:
- kind: ServiceAccount
name: argocd-manager
namespace: kube-system
集群管理¶
查看集群¶
# 列出集群
argocd cluster list
# 查看集群详情
argocd cluster get <cluster-name>
# 查看集群资源
kubectl get secrets -n argocd -l argocd.argoproj.io/secret-type=cluster
更新集群¶
# 更新集群标签
argocd cluster set <cluster-name> --label env=staging
# 更新集群名称
argocd cluster set <cluster-name> --name new-name
删除集群¶
多集群 Application¶
指定目标集群¶
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: myapp-production
namespace: argocd
spec:
project: default
source:
repoURL: https://github.com/org/repo.git
targetRevision: main
path: apps/myapp
destination:
server: https://production.example.com # 目标集群
namespace: myapp
使用集群名称¶
ApplicationSet 多集群¶
Cluster Generator¶
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: myapp
namespace: argocd
spec:
generators:
- clusters:
selector:
matchLabels:
env: production
template:
metadata:
name: '{{name}}-myapp'
spec:
project: default
source:
repoURL: https://github.com/org/repo.git
targetRevision: main
path: apps/myapp
destination:
server: '{{server}}'
namespace: myapp
Matrix Generator¶
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: myapp
namespace: argocd
spec:
generators:
- matrix:
generators:
# 集群列表
- clusters:
selector:
matchLabels:
type: workload
# 应用列表
- list:
elements:
- appName: frontend
path: apps/frontend
- appName: backend
path: apps/backend
template:
metadata:
name: '{{name}}-{{appName}}'
spec:
project: default
source:
repoURL: https://github.com/org/repo.git
targetRevision: main
path: '{{path}}'
destination:
server: '{{server}}'
namespace: '{{appName}}'
多环境配置¶
目录结构¶
Git 仓库:
├── apps/
│ ├── base/
│ │ ├── deployment.yaml
│ │ ├── service.yaml
│ │ └── kustomization.yaml
│ ├── overlays/
│ │ ├── dev/
│ │ │ ├── kustomization.yaml
│ │ │ └── patches/
│ │ ├── staging/
│ │ │ ├── kustomization.yaml
│ │ │ └── patches/
│ │ └── production/
│ │ ├── kustomization.yaml
│ │ └── patches/
Application 配置¶
# Dev 环境
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: myapp-dev
namespace: argocd
spec:
project: default
source:
repoURL: https://github.com/org/repo.git
targetRevision: main
path: apps/overlays/dev
destination:
server: https://dev.example.com
namespace: myapp
---
# Production 环境
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: myapp-production
namespace: argocd
spec:
project: default
source:
repoURL: https://github.com/org/repo.git
targetRevision: main
path: apps/overlays/production
destination:
server: https://production.example.com
namespace: myapp
集群标签¶
设置标签¶
# 添加集群时设置标签
argocd cluster add target-cluster --label env=production --label region=us-west
# 更新标签
argocd cluster set target-cluster --label env=staging
使用标签选择器¶
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: myapp
namespace: argocd
spec:
generators:
- clusters:
selector:
matchLabels:
env: production
matchExpressions:
- key: region
operator: In
values:
- us-west
- us-east
小结¶
本章学习了:
- ✅ 多集群架构
- ✅ 添加集群
- ✅ 集群管理
- ✅ 多集群 Application
- ✅ ApplicationSet 多集群
- ✅ 多环境配置
- ✅ 集群标签
下一章¶
第五章:Helm 与 Kustomize - 学习 Helm 和 Kustomize 集成。