flux + kubernetes + gitops + Kustomization
# 1. 安装依赖yum install -y git# 2. 设置代理根据你的环境export http_proxyhttp://192.168.3.77:20171export https_proxyhttp://192.168.3.77:20171export NO_PROXY*.baidu.com,10.0.0.0/8,.cluster.local,192.168.3.0/24# 3. 安装 flux CLIcurl -s https://fluxcd.io/install.sh | bashsource ~/.bashrc# 4. 初始化 Git 仓库mkdir -p /root/gitops-repocd /root/gitops-repo/git initgit config --global user.name usergit config --global user.email userexample.com# 5. 安装 Flux 到 Kubernetesflux install# 6. 创建目录结构mkdir -p base overlays/dev# 7. 编写 base 资源cat base/deployment.yaml EOFapiVersion: apps/v1kind: Deploymentmetadata:name: myappspec:replicas: 1selector:matchLabels:app: myapptemplate:metadata:labels:app: myappspec:containers:- name: myappimage: nginx:alpineports:- containerPort: 80EOFcat base/service.yaml EOFapiVersion: v1kind: Servicemetadata:name: myappspec:type: ClusterIPports:- port: 80selector:app: myappEOFcat base/kustomization.yaml EOFapiVersion: kustomize.config.k8s.io/v1beta1kind: Kustomizationresources: //这里必须写resources ./overlays/dev 不用写是应为他是监听主目录是唯一的例外- deployment.yaml- service.yamlEOF# 8. 编写 overlays/devcat overlays/dev/kustomization.yaml EOFapiVersion: kustomize.config.k8s.io/v1beta1kind: Kustomizationresources:- ../../basereplicas: //只会修改被resources引入的yaml- name: myappcount: 2images:- name: nginxnewTag: 1.25-alpineEOF# 9. 提交到 Gitgit add .git commit -m init gitops# 10. 配置 SSH 密钥本地Git认证ssh-keygen -t rsa -N -f /root/.ssh/id_rsacat /root/.ssh/id_rsa.pub /root/.ssh/authorized_keyschmod 600 /root/.ssh/authorized_keysssh -o StrictHostKeyCheckingno root192.168.3.243 echo OK# 11. 创建 Git 源Flux 拉取 Gitflux create source git gitops-repo \--urlssh://root192.168.3.243/root/gitops-repo \--branchmaster \--interval30s \--private-key-file/root/.ssh/id_rsa# 12. 授予 Flux 集群权限kubectl create clusterrolebinding flux-cluster-admin \--clusterrolecluster-admin \--serviceaccountflux-system:flux-system# 13. 加入测试资源带 namespacecat overlays/dev/nginx-test.yaml EOFapiVersion: v1kind: Podmetadata:namespace: defaultname: nginx-testspec:containers:- name: nginximage: nginx:alpineEOFgit add .git commit -m fix: add namespace# 14. 创建 Kustomization自动部署flux create kustomization gitops-dev \--sourcegitops-repo \--path./overlays/dev \--prunetrue \--interval30s \--target-namespacedefault# 15. 查看最终状态flux get allkubectl get po