FluxCD

github: flux fluxctl, flux2

Flux 是一种工具,用于使 Kubernetes 集群与配置源(如 Git 存储库和 OCI 工件)保持同步,并在有新代码需要部署时自动更新配置。

Flux 版本 2(“v2”)从头开始构建,以使用 Kubernetes 的 API 扩展系统,并与 Prometheus 和 Kubernetes 生态系统的其他核心组件集成。在版本 2 中,Flux 支持多租户和同步任意数量的 Git 存储库,以及其他长期要求的功能。

组件

Source Controller

The main role of the source management component is to provide a common interface for artifacts acquisition. The source API defines a set of Kubernetes objects that cluster admins and various automated operators can interact with to offload the Git and Helm repositories operations to a dedicated controller.

源管理组件的主要作用是提供用于获取工件的通用接口。源 API 定义了一组 Kubernetes 对象,集群管理员和各种自动化操作员可以与这些对象交互,以将 Git 和 Helm 存储库操作卸载到专用控制器。

Kustomize Controller

The kustomize-controller is a Kubernetes operator, specialized in running continuous delivery pipelines for infrastructure and workloads defined with Kubernetes manifests and assembled with Kustomize.

kustomize-controller 是一个 Kubernetes 操作器,专门用于运行使用 Kubernetes 清单定义并使用 Kustomize 组装的基础设施和工作负载的持续交付管道。

Helm Controller

The Helm Controller is a Kubernetes operator, allowing one to declaratively manage Helm chart releases with Kubernetes manifests.

Helm Controller 是一个 Kubernetes 操作器,允许使用 Kubernetes 清单声明式地管理 Helm 图表发布。

安装

要遵循本指南,您需要满足以下条件:

# 安装 Flux CLI
#curl -s https://fluxcd.io/install.sh | sudo bash
#tar -zxvf flux_2.0.1_linux_amd64.tar.gz
tar -zxvf flux_2.1.2_linux_amd64.tar.gz
mv flux /usr/bin/

# 添加自动补全
source <(flux completion bash)
echo 'source <(flux completion bash)' >> /etc/profile

# 检查 Kubernetes 集群
flux check --pre

将 Flux 安装到集群上

# 运行 bootstrap 命令
flux bootstrap git \
  --url=ssh://git@<host>/<org>/<repository> \
  --branch=main \
  --private-key-file=<path/to/private.key> \
  --password=<key-passphrase> \
  --path=clusters/my-cluster

flux bootstrap git \
  --url=ssh://git@gitlab.gotodev.cn/root/fleet-infra.git \
  --branch=main \
  --private-key-file=id_rsa \
  --path=clusters/my-cluster

上面的引导命令执行以下操作:

部署示例应用程序

克隆仓库

git clone git@gitlab.gotodev.cn:root/fleet-infra.git
cd fleet-infra

将 podinfo 存储库添加到 Flux

创建指向 podinfo 存储库主分支的GitRepository清单

# ssh方式,需要创建ssh key
ssh-keyscan gitlab.gotodev.cn >> known_hosts

kubectl create secret generic flux-git-deploy-key \
  --from-file=identity=id_rsa \
  --from-file=known_hosts=known_hosts \
  -n flux-system

flux create source git podinfo \
  --url=ssh://git@gitlab.gotodev.cn/root/podinfo.git \
  --branch=master \
  --interval=1m \
  --secret-ref=flux-git-deploy-key \
  --export > ./clusters/my-cluster/podinfo-source.yaml

# https方式,只能是公开的仓库
flux create source git podinfo \
  --url=https://github.com/stefanprodan/podinfo \
  --branch=master \
  --interval=1m \
  --export > ./clusters/my-cluster/podinfo-source.yaml

# 提交并将podinfo-source.yaml文件推送到fleet-infra存储库
git add -A && git commit -m "Add podinfo GitRepository"
git push

部署 podinfo 应用程序

使用flux create命令创建应用 podinfo 部署的Kustomization

flux create kustomization podinfo \
  --target-namespace=default \
  --source=podinfo \
  --path="./kustomize" \
  --prune=true \
  --wait=true \
  --interval=30m \
  --retry-interval=2m \
  --health-check-timeout=3m \
  --export > ./clusters/my-cluster/podinfo-kustomization.yaml

# 提交清单并将其推送到Kustomization存储库
git add -A && git commit -m "Add podinfo Kustomization"
git push

fleet-infra存储库的结构应类似于:

fleet-infra
└── clusters/
    └── my-cluster/
        ├── flux-system/                        
        │   ├── gotk-components.yaml
        │   ├── gotk-sync.yaml
        │   └── kustomization.yaml
        ├── podinfo-kustomization.yaml
        └── podinfo-source.yaml

观察 Flux 同步应用程序

# 检查 Git 源状态,特别是 authentication 是否正确
flux get sources git podinfo -n flux-system

[root@master ~]# flux get sources git podinfo -n flux-system
NAME    REVISION                SUSPENDED       READY   MESSAGE                                             
podinfo master@sha1:ff906f22    False           True    stored artifact for revision 'master@sha1:ff906f22'

# 使用flux get命令观看podinfo应用程序
flux get kustomizations --watch

[root@master ~]# flux get kustomizations --watch
NAME            REVISION                SUSPENDED       READY   MESSAGE                              
flux-system     main@sha1:c130d2e2      False           True    Applied revision: main@sha1:c130d2e2
podinfo         master@sha1:ff906f22    False           True    Applied revision: master@sha1:ff906f22

# 检查 podinfo 是否已部署到你的集群上
kubectl -n default get deployments,services

[root@master ~]# kggrepo -A
NAMESPACE     NAME          URL                                                AGE     READY   STATUS
flux-system   flux-system   ssh://git@gitlab.gotodev.cn/root/fleet-infra.git   2d15h   False   xxx
flux-system   podinfo       ssh://git@gitlab.gotodev.cn/root/podinfo.git       2d15h   False   xxx
[root@master ~]# 
[root@master ~]# kgks -A
NAMESPACE     NAME          AGE     READY   STATUS
flux-system   flux-system   2d15h   True    Applied revision: main@sha1:xxx
flux-system   podinfo       2d15h   True    Applied revision: master@sha1:xxx
[root@master ~]#

Helm 示例应用程序

github, README_zh

flux bootstrap git \
  --url=ssh://git@gitlab.gotodev.cn/root/flux2-kustomize-helm-example.git \
  --branch=master \
  --private-key-file=id_rsa \
  --path=clusters/staging

kubectl create secret generic flux-git-deploy-key \
  --from-file=identity=id_rsa \
  --from-file=known_hosts=known_hosts \
  -n flux-system

常用命令

# 手动触发同步kustomization
flux reconcile kustomization podinfo

# 手动触发同步helmrelease
flux reconcile helmrelease <helmrelease-name> --with-source

版本

v2.1.0 版本与以下 Kubernetes 版本兼容:

Kubernetes 版本 最低要求
v1.25 >= 1.25.0
v1.26 >= 1.26.0
v1.27 >= 1.27.1
v1.28 >= 1.28.0

请注意,Flux 可能适用于旧版本的 Kubernetes,例如 1.21,但我们不建议在生产中运行终止的版本,也不建议这些版本提供支持。

v2.0.0 版本与以下 Kubernetes 版本兼容:

Kubernetes 版本 最低要求
v1.24 >= 1.24.0
v1.25 >= 1.25.0
v1.26 >= 1.26.0
v1.27 >= 1.27.1

请注意,Flux 可能适用于旧版本的 Kubernetes,例如 1.19,但我们不建议在生产中运行终止的版本,也不为已这些版本提供支持。