Argo CD
Installation
- Install ArgoCD
Install ArgoCD, you can refer to ArgoCD Installation.
Install ArgoCD, you can refer to ArgoCD Installation.
When your k8s resource files located in `mainfests` folder, you can use the following command to deploy your app.
you only need to set `spec.source.path: mainfests`
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: hugo-blog
spec:
project: default
source:
repoURL: 'git@github.com:AaronYang0628/home-site.git'
targetRevision: main
path: mainfests
syncPolicy:
automated:
prune: true
selfHeal: true
syncOptions:
- CreateNamespace=true
- ApplyOutOfSyncOnly=true
destination:
server: https://kubernetes.default.svc
namespace: applicationNot only you need files in `mainfests` folder, but also need files in root folder.
you have to create an extra file `kustomization.yaml`, and set `spec.source.path: .`
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: hugo-blog
spec:
project: default
source:
repoURL: 'git@github.com:AaronYang0628/home-site.git'
targetRevision: main
path: .
syncPolicy:
automated:
prune: true
selfHeal: true
syncOptions:
- CreateNamespace=true
- ApplyOutOfSyncOnly=true
destination:
server: https://kubernetes.default.svc
namespace: applicationresources:
- manifests/pvc.yaml
- manifests/job.yaml
- manifests/deployment.yaml
- ...ArgoCD 凭借其声明式的 GitOps 理念,能非常优雅地处理多 Kubernetes 集群的应用发布。它允许你从一个中心化的 Git 仓库管理多个集群的应用部署,确保状态一致并能快速回滚。
下面这张图概括了使用 ArgoCD 进行多集群发布的典型工作流,帮你先建立一个整体印象:
flowchart TD
A[Git仓库] --> B{ArgoCD Server}
B --> C[ApplicationSet<br>集群生成器]
B --> D[ApplicationSet<br>Git生成器]
B --> E[手动Application<br>资源]
C --> F[集群A<br>App1 & App2]
C --> G[集群B<br>App1 & App2]
D --> H[集群A<br>App1]
D --> I[集群A<br>App2]
E --> J[特定集群<br>特定应用]要让 ArgoCD 管理外部集群,你需要先将目标集群的访问凭证添加进来。
kubeconfig 文件。argocd cluster add <context-name> --name <cluster-name> --kubeconfig ~/.kube/config<context-name> 是你 kubeconfig 中的上下文名称。<cluster-name> 是你在 ArgoCD 中为这个集群起的别名。argocd cluster list连接集群后,核心是定义部署规则。ArgoCD 主要通过 Application 和 ApplicationSet 资源来描述部署。
Application 资源:定义一个应用在特定集群的部署。管理大量集群和应用时,手动创建每个 Application 会很繁琐。ApplicationSet 资源:这是实现多集群部署的推荐方式。它能根据生成器(Generators)自动为多个集群或多个应用创建 Application 资源。上面的流程图展示了 ApplicationSet 的两种主要生成器以及手动创建 Application 的方式。
| 生成器类型 | 工作原理 | 适用场景 |
|---|---|---|
| List Generator | 在 YAML 中静态列出集群和URL。 | 集群数量固定、变化少的场景。 |
| Cluster Generator | 动态使用 ArgoCD 中已注册的集群。 | 集群动态变化,需自动纳入新集群的场景。 |
| Git Generator | 根据 Git 仓库中的目录结构自动生成应用。 | 管理大量微服务,每个服务在独立目录。 |
这里以 Cluster Generator 为例,展示一个 ApplicationSet 的 YAML 配置:
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: my-app-multi-cluster
spec:
generators:
- clusters: {} # 自动发现ArgoCD中所有已注册集群
template:
metadata:
name: '{{name}}-my-app'
spec:
project: default
source:
repoURL: 'https://your-git-repo.com/your-app.git'
targetRevision: HEAD
path: k8s-manifests
destination:
server: '{{server}}' # 生成器提供的集群API地址
namespace: my-app-namespace
syncPolicy:
syncOptions:
- CreateNamespace=true # 自动创建命名空间
automated:
prune: true # 自动清理
selfHeal: true # 自动修复漂移在这个模板中:
generators 下的 clusters: {} 会让 ArgoCD 自动发现所有已注册的集群。template 中,{{name}} 和 {{server}} 是变量,Cluster Generator 会为每个已注册的集群填充它们。syncPolicy 下的配置实现了自动同步、自动创建命名空间和资源清理。automated 同步,实现 Git 变更自动部署。Manual)或通过 PR 审批流程,以增加控制力。argocd-repo-server 和 argocd-application-controller 的副本数和资源限制。利用 ArgoCD 管理多 Kubernetes 集群应用发布,核心是掌握 ApplicationSet 和 Generators 的用法。通过 Cluster Generator 或 Git Generator,你可以灵活地实现“一次定义,多处部署”。
希望这些信息能帮助你着手搭建多集群发布流程。如果你能分享更多关于你具体环境的信息(比如集群的大致数量和应用的组织结构),或许我可以给出更贴合你场景的建议。
kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -dARGOCD_PASS=$(kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -d)
MASTER_IP=$(kubectl get nodes --selector=node-role.kubernetes.io/control-plane -o jsonpath='{$.items[0].status.addresses[?(@.type=="InternalIP")].address}')
argocd login --insecure --username admin $MASTER_IP:30443 --password $ARGOCD_PASSargocd app terminate-op <$>