Flux 企业 GitOps 与最小权限
验证:未记录
- 适用版本
- 未记录
- 验证范围
- 尚未登记验证记录。
本文缩写(英文全称与中文含义)
| 缩写 | 英文全称/名称 | 中文含义 |
|---|---|---|
| CD | Continuous Delivery / Continuous Deployment | 持续交付/持续部署;是否自动上线取决于流程 |
| CLI | Command-Line Interface | 命令行接口 |
| CRD | Custom Resource Definition | Kubernetes 自定义资源定义 |
| GitOps | GitOps | 以 Git 保存期望状态、由自动化持续协调实际状态的运维方式 |
| PAT | Personal Access Token | 个人访问令牌 |
| PR | Pull Request | 代码合并请求 |
| SA | Service Account | 服务账号,供程序或工作负载使用的身份 |
| SSH | Secure Shell | 安全远程登录协议 |
场景:平台控制 GitOps 根目录,业务只能更新自己的部署路径。前置:基线、受保护的私有 Git、已审核 Flux CLI。与 Argo CD 二选一;平台 Git 的写权限等同于高权限基础设施变更。
安装与职责拆分#
使用已有 Git 仓库与 SSH deploy key,bootstrap 会向仓库提交控制器清单;先评审分支保护与机器人权限,不使用个人永久 PAT。
flux check --pre
flux bootstrap git --url=ssh://git@git.example.com/platform/fleet.git --branch=main --path=clusters/staging --private-key-file=/secure/path/bootstrap-key
flux checkbootstrap 完成后按最小权限轮换凭据,业务 source 只需读。控制器版本随生成清单锁定,升级通过平台 PR,不在生产直接拉最新 install manifest。Bootstrap
按官方多租户方案,在生成的 controller Deployments overlay 中为 kustomize/helm controller 配置 --no-cross-namespace-refs=true、--default-service-account=default,为 kustomize-controller 配置 --no-remote-bases=true;确保默认 SA 无额外权限。参数是否受支持须按所选 release 校验。多租户模式
Hands-on:用租户 SA 发布真实业务#
平台先创建 orders namespace、运行时 SA、Secret、网络与入口。保存 orders-reconciler.yaml:
apiVersion: v1
kind: ServiceAccount
metadata: {name: orders-reconciler, namespace: orders}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata: {name: orders-reconciler, namespace: orders}
rules:
- apiGroups: [apps]
resources: [deployments]
verbs: [get, list, watch, create, update, patch, delete]
- apiGroups: [""]
resources: [services, configmaps]
verbs: [get, list, watch, create, update, patch, delete]
- apiGroups: [policy]
resources: [poddisruptionbudgets]
verbs: [get, list, watch, create, update, patch, delete]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata: {name: orders-reconciler, namespace: orders}
subjects:
- {kind: ServiceAccount, name: orders-reconciler, namespace: orders}
roleRef: {apiGroup: rbac.authorization.k8s.io, kind: Role, name: orders-reconciler}把只读 Git SSH 凭据作为 orders/orders-git-auth Secret 交付,包含 Flux 要求的 identity、identity.pub、known_hosts;host key 从企业可信渠道核验,不能禁用 SSH 校验。
保存 orders-source.yaml;仓库路径仅含 Role 允许的资源,不能再次创建 namespace/SA:
apiVersion: source.toolkit.fluxcd.io/v1
kind: GitRepository
metadata: {name: orders, namespace: orders}
spec:
interval: 1m
url: ssh://git@git.example.com/commerce/orders-deploy.git
ref: {branch: main}
secretRef: {name: orders-git-auth}
---
apiVersion: kustomize.toolkit.fluxcd.io/v1
kind: Kustomization
metadata: {name: orders, namespace: orders}
spec:
interval: 5m
sourceRef: {kind: GitRepository, name: orders}
path: ./environments/staging
targetNamespace: orders
serviceAccountName: orders-reconciler
prune: false
wait: true
timeout: 5mkubectl apply -f orders-reconciler.yaml -f orders-source.yaml
flux reconcile source git orders -n orders
flux reconcile kustomization orders -n orders --with-source
flux get all -n orders
kubectl -n orders rollout status deployment/orders-api --timeout=10m平台负责审批 Source/Kustomization 与其 SA 字段;只允许业务提交 Git,不开放任意改协调身份的权限。CRD/namespace/密钥同步应在上游 Kustomization 中,通过 dependsOn 编排,不能用 sleep 猜就绪。
验收、告警与恢复#
- 在业务分支加入 ClusterRole 或其他 namespace 资源:必须被拒绝;修正后协调恢复。
- 修改预生产的受控字段,验证自动纠偏与事件;启用 prune 前演练误删 diff,并对数据资源设置独立生命周期。
- Git 暂不可用时现有应用继续运行,但告警显示协调停滞;恢复后应用到期望 commit,不能静默跳过失败。
- 故障发布用 Git revert,必要时先暂停:
flux suspend kustomization orders -n orders
# 完成 Git revert 并确认不破坏数据库兼容性后恢复。
flux resume kustomization orders -n orders
flux reconcile kustomization orders -n orders --with-source检查 controller 的 Ready、reconcile 错误和最后成功 revision,并执行基线业务验证。Flux 不提供业务数据回滚;重建集群从受保护 Git bootstrap,云资源/数据/密钥另恢复。暂停组件后记得恢复,否则“绿色 Pod”也可能长期运行旧版本。