基于k3s搭建全闭环cicd环境
2026/1/13大约 4 分钟
基于k3s搭建全闭环gitops环境
前面基于搭建的podman环境可以满足一些小项目需求,如果要进一步要求全流程自动化且进一步扩容合集中管理的话,就有点捉襟见肘了,那么现在有一个新的方案,基于podman方案更新的全新cicd环境,全流程闭环且极大降低运维工作量的环境。并且在硬件条件受限的前提下完成部署。 整个架构有以下软件构成: flux k3s gitea registry woodpecker
痛点
我之前的部署是自维护一套CA和DNS,每次配置都要导入证书,而且配置中遇到的所有问题与此相关,所以如果采用自部署CA和独立DNS的话就容易很多。
k3s命令
k3s 服务本身
# 重启整个 k3s
sudo systemctl restart k3s
# 查看 k3s 日志(实时跟)
sudo journalctl -u k3s -fCoreDNS / kube-system 相关
# 查看 apiserver(你之前试过)
kubectl -n kube-system logs -l app=kube-apiserver
# 重启 CoreDNS
kubectl -n kube-system rollout restart deployment coredns
# 查看 CoreDNS Pod
kubectl -n kube-system get pods -l k8s-app=kube-dns
# 编辑 CoreDNS ConfigMap(如果需要改 Corefile)
kubectl -n kube-system edit configmap coredns业务命名空间 / 应用相关
# 查看 axum-test 命名空间下的 Deployment 镜像
kubectl -n axum-test get deploy axum-test -o yaml | grep image
# 重启业务 Deployment
kubectl -n axum-test rollout restart deployment axum-test
# 查看业务 Pod
kubectl -n axum-test get pods镜像仓库
# 查看 devops 命名空间下的 Service(找 registry)
kubectl -n devops get svc
# 创建 registry TLS Secret(举例)
kubectl -n devops create secret tls my-app-local-tls \
--cert=my-app.local.pem \
--key=my-app.local-key.pem
# 创建给 Flux 用的 CA Secret
kubectl -n flux-system create secret generic my-app-registry-ca \
--from-file=ca.crt=./ca.pem
# 删除并重建 ImageRepository(相当于重装这个资源)
kubectl -n flux-system delete imagerepository axum-test-repo
kubectl -n flux-system get imagerepository axum-test-repo
# Pod 里用 curl 测试 registry(举例)
kubectl -n devops run tmp --image=curlimages/curl -it --rm -- \
curl -vk https://my-app.local/v2/测试
# 测试应用某个 yaml(不真的 apply)
kubectl apply -f apps/axum-test-deployment.yaml --dry-run=clientflux
flux有以下三个主体:
- source-controller:更新 GitRepository 的 revision
- kustomize-controller:发现 Git 有新 revision → 再 render 一遍 → 把 Deployment 更新到
0.1.2 - image-automation-controller:自动更改和提交新配置
flux命令: 重新获取git并重启系统
flux reconcile source git flux-system -n flux-system
flux reconcile kustomization flux-system -n flux-system暂停更新:
flux suspend image update flux-system -n flux-system还有一种方法则是直接在配置文件改了之后提交至git,文件automation.yaml,注意,这里暂停的是ImageUpdateAutomation:
apiVersion: image.toolkit.fluxcd.io/v1beta2
kind: ImageUpdateAutomation
metadata:
name: flux-system
namespace: flux-system
spec:
suspend: true # ⭐ 加这一行(如果没有)
interval: 1m
...推送完成之后重新获取即可。 flux如果需要回滚业务程序版本,首先要停止更新,然后直接改动版本号提交更新即可。
flux主要命令:
- 让 Git 源立刻同步
flux reconcile source git flux-system -n flux-system- 让某个 Kustomization 立刻 apply
flux reconcile kustomization flux-system -n flux-system- 镜像自动化
# 重新扫描镜像仓库
flux reconcile image repository axum-test-repo -n flux-system
# 重新计算策略(哪个 tag 是最新)
flux reconcile image policy axum-test-policy -n flux-system
# 重新执行自动改 Git 的任务
flux reconcile image update flux-system -n flux-system- 暂停自动 bump 镜像版本
flux suspend image update flux-system -n flux-system- 恢复自动 bump 镜像版本
flux resume image update flux-system -n flux-system- 安装flux
flux bootstrap git \
--url=ssh://git@gitea-alias/adminer/flux-config.git \ # git版本
--branch=main \ # 监控分支
--path=./flux \ # 监控的文件夹
--private-key-file=$HOME/project/devops/flux-deploy-key # git提交的私钥- 查看整个结构和特定的部署
flux get kustomizations -A
flux get image repository -n flux-system
flux get image policy -n flux-system
flux get image update -n flux-systemk3s侧的flux
- 查看所有的flux的pod
sudo kubectl -n flux-system get pods
# 查看更多的信息:
sudo kubectl -n flux-system get pods -o wide- 具体某个pod的信息
sudo kubectl -n flux-system get pods | grep kustomize-controller
# or
sudo kubectl -n flux-system get imagerepository
# 假设名字是 kustomize-controller-7f9d9c9fcb-abcde
sudo kubectl -n flux-system describe pod kustomize-controller-7f9d9c9fcb-abcde- 具体某个pod的日志
# Git 源同步相关
sudo kubectl -n flux-system logs deployment/source-controller
# 应用 YAML(Kustomization)相关
sudo kubectl -n flux-system logs deployment/kustomize-controller
# 扫描镜像 / ImageRepository / ImagePolicy 相关
sudo kubectl -n flux-system logs deployment/image-reflector-controller
# 改 Git / ImageUpdateAutomation 相关
sudo kubectl -n flux-system logs deployment/image-automation-controller- 列出所有的deployment
sudo kubectl -n flux-system get deploy- 重启某个 Flux 容器
# 全部重启
sudo kubectl -n flux-system rollout restart deploy/source-controller
sudo kubectl -n flux-system rollout restart deploy/kustomize-controller
sudo kubectl -n flux-system rollout restart deploy/image-reflector-controller
sudo kubectl -n flux-system rollout restart deploy/image-automation-controller
# 看重启后的状态
sudo kubectl -n flux-system get pods