使用 GitRepository 与 Kustomization 部署你的第一个应用
通过 Kustomization 资源将 Flux 指向 Git 中的清单,并观察同步。
Flux 和集群中的其他组件一样,是通过 Kubernetes 资源来配置的。其中两个承担了大部分工作:source(例如 GitRepository)说明配置存放在哪里,而 Kustomization 则表示”取那个 source 中这个路径下的清单,并让它们持续应用到这个集群”。本指南部署一个小应用,然后修改 Git 来观察同步的发生。本指南假设 Flux 已针对 fleet-infra 仓库完成 bootstrap(参见上一篇指南)。
准备清单
在你的 fleet-infra 克隆中,于 bootstrap 创建的 clusters 文件夹旁边添加一个应用文件夹:
fleet-infra/
├── clusters/
│ └── dev/
│ ├── flux-system/ # created by bootstrap
│ └── webapp.yaml # the Flux Kustomization (next section)
└── apps/
└── webapp/
├── namespace.yaml
├── deployment.yaml
└── service.yaml
apps/webapp/namespace.yaml:
apiVersion: v1
kind: Namespace
metadata:
name: webapp
apps/webapp/deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: webapp
namespace: webapp
spec:
replicas: 2
selector:
matchLabels:
app: webapp
template:
metadata:
labels:
app: webapp
spec:
containers:
- name: webapp
image: nginx:1.25
ports:
- containerPort: 80
apps/webapp/service.yaml:
apiVersion: v1
kind: Service
metadata:
name: webapp
namespace: webapp
spec:
selector:
app: webapp
ports:
- port: 80
targetPort: 80
与某些工具不同,这里的清单显式写明了它们的命名空间,并且文件夹中包含 Namespace 对象,这样 Flux 会创建它。这里不需要 kustomization.yaml:当文件夹中没有它时,Flux 会构建出该文件夹中每个 YAML 文件的列表。
编写 Flux Kustomization
Bootstrap 已经创建了一个指向同一仓库、名为 flux-system 的 GitRepository,所以你可以复用它。clusters/dev/webapp.yaml:
apiVersion: kustomize.toolkit.fluxcd.io/v1
kind: Kustomization
metadata:
name: webapp
namespace: flux-system
spec:
interval: 10m
sourceRef:
kind: GitRepository
name: flux-system
path: ./apps/webapp
prune: true
wait: true
timeout: 2m
关键部分说明:
sourceRef是期望状态的来源。这里是 bootstrap 所设置的flux-system仓库。path是 source 内要应用的文件夹。interval是 Flux 多久重新应用一次清单并修正漂移,即使 Git 没有变化。prune: true表示从 Git 中移除的对象也会从集群中移除。除非你设置它,否则它是关闭的。wait和timeout让 Flux 在报告成功之前,等待所应用的资源变为就绪。
你可以用 CLI 生成这份 YAML,而不必手写:
flux create kustomization webapp \
--source=GitRepository/flux-system \
--path=./apps/webapp \
--prune=true --wait=true --timeout=2m --interval=10m \
--export > clusters/dev/webapp.yaml
提交并让 Flux 接手
git add -A
git commit -m "Add webapp"
git push
flux-system Kustomization 已经在监视 ./clusters/dev,所以它会在下一次运行时发现新的 webapp Kustomization。你永远不需要对这个应用运行 kubectl apply。若想跳过等待,可以让 Flux 立即检查 Git:
flux reconcile source git flux-system
flux get kustomizations --watch
确认部署
webapp Kustomization 会出现,并变为 Ready,附带一条指明所应用修订版本的消息。检查集群,并让 Flux 显示它所拥有的内容:
kubectl get all -n webapp
flux tree kustomization webapp
修改 Git,观察同步
编辑 apps/webapp/deployment.yaml,将镜像改为 nginx:1.26,然后提交并推送。Flux 按 GitRepository 的 interval 获取仓库(bootstrap 之后默认是一分钟),然后应用变更。一个新的 ReplicaSet 会滚动发布。验证:
flux reconcile kustomization webapp --with-source
kubectl get deploy webapp -n webapp \
-o jsonpath='{.spec.template.spec.containers[0].image}'; echo
这就是完整的循环:编辑 Git,Flux 让集群收敛。下一篇指南介绍如何控制这种行为。
使用另一个仓库
你不必把应用清单放在 bootstrap 仓库中。要从另一个仓库部署,请为它创建一个 GitRepository,并在 Kustomization 中引用它:
flux create source git webapp-config \
--url=https://github.com/your-org/webapp-config \
--branch=main --interval=1m \
--export > clusters/dev/webapp-source.yaml
对于私有仓库,使用 flux create secret git 添加凭据并用 --secret-ref 引用,或者使用 SSH 部署密钥。然后在 Kustomization 中设置 sourceRef.name: webapp-config。
热门指南
-
推送与拉取:Flux 如何向 Kubernetes 交付 为什么基于拉取、由 Git 驱动的交付会改变你部署、保障安全和恢复集群的方式。 -
控制同步行为:间隔、Prune、漂移修正与回滚 决定 Flux 何时应用变更、删除什么、如何暂停它,以及如何撤销一次有问题的部署。 -
使用 Flux 镜像自动化实现镜像更新自动化 让 Flux 检测新的容器镜像标签,并将其提交回 Git。 -
使用 Flux 配置多集群 从一个仓库管理多个集群,包含共享基础设施和每个集群各自的差异。 -
Flux CD 与 Argo CD:如何选择 GitOps 工具 比较架构、UI、Helm、密钥、租户和镜像自动化,以选出合适的工具。