Files
k3s_auto_deploy/SUMMARY.md

325 lines
8.2 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# K3s + GitOps 部署总结
## ✅ 完成情况
### 1. 幂等性配置修复 ✅
**问题回顾:**
- 之前worker节点尝试通过公网IP连接master导致超时
- inventory配置中缺少必要的变量
- worker节点的token配置不正确
**已修复:**
-`inventory/hosts.ini` 使用正确的组名 `server``agent`
-`api_endpoint` 配置为内网IP `172.23.96.138`
- ✅ worker节点环境文件配置正确的内网IP和token
- ✅ 所有配置文件支持幂等性,可以安全重复执行
**验证结果:**
```bash
# 最后一次Ansible运行结果
172.23.96.138 : ok=25 changed=0 failed=0
172.23.96.139 : ok=18 changed=0 failed=0
172.23.96.140 : ok=18 changed=0 failed=0
```
`changed=0` 表示配置已稳定,支持幂等性!
### 2. 测试项目创建 ✅
**已创建完整的测试应用 `test-app`**
#### 应用组件
- **Deployment**: 2个nginx副本带自定义HTML页面
- **ConfigMap**: 包含HTML内容显示版本号和背景颜色
- **Service**: NodePort 30081
- **Ingress**: 域名访问 `test.jpc.net3w.com`
#### Git仓库
- **仓库地址**: http://8.216.38.248:32158/k3s-apps/test-app
- **分支**: main
- **内容**: manifests目录包含所有Kubernetes清单文件
#### ArgoCD应用
- **应用名**: test-app
- **状态**: Synced & Healthy
- **自动同步**: 已启用
- **自动修复**: 已启用
#### 访问方式
1. **NodePort**: http://8.216.38.248:30081 (或任意节点IP)
2. **域名**: http://test.jpc.net3w.com (需配置DNS)
### 3. GitOps自动更新流程 ✅
**工作流程:**
```
开发者修改代码
提交到Git (Gitea)
ArgoCD检测变化 (3分钟内)
自动同步到K3s集群
应用自动更新
```
**更新脚本:**
- 创建了 `update-app.sh` 脚本
- 支持一键更新应用版本
- 自动修改配置、提交Git、推送
**使用示例:**
```bash
cd /home/fei/k3s/test-app
./update-app.sh v2.0 # 更新到v2.0(粉红色背景)
./update-app.sh v3.0 # 更新到v3.0(蓝色背景)
./update-app.sh v4.0 # 更新到v4.0(绿色背景)
```
### 4. 部署配置Git管理 ✅
**已创建的文件:**
-`.gitignore` - 排除敏感信息
-`README-DEPLOYMENT.md` - 部署文档
-`USAGE-GUIDE.md` - 详细使用指南
-`SUMMARY.md` - 本总结文档
-`config/cluster-vars.yml.example` - 配置模板
-`demo-gitops-update.sh` - GitOps演示脚本
**可以存入Git的内容**
```
k3s自动化部署/
├── .gitignore # ✅ 已创建
├── README-DEPLOYMENT.md # ✅ 已创建
├── USAGE-GUIDE.md # ✅ 已创建
├── SUMMARY.md # ✅ 已创建
├── demo-gitops-update.sh # ✅ 已创建
├── config/
│ └── cluster-vars.yml.example # ✅ 已创建(模板)
├── scripts/ # ✅ 所有脚本
│ ├── generate-inventory.py
│ ├── deploy-gitea.sh
│ ├── setup-gitea.sh
│ ├── deploy-argocd.sh
│ ├── create-argocd-app.sh
│ └── push-demo-app.sh
└── k3s-ansible/
└── inventory/
└── hosts.ini # ✅ 自动生成的inventory
```
**不会存入Git的内容已在.gitignore**
- `config/cluster-vars.yml` - 包含敏感信息密码、IP等
- `*.vault` - Ansible加密文件
- Python缓存和临时文件
## 📊 当前集群状态
### K3s集群
```
Master: 8.216.38.248 (172.23.96.138) - Ready
Worker1: 8.216.41.97 (172.23.96.139) - Ready
Worker2: 8.216.33.69 (172.23.96.140) - Ready
```
### GitOps组件
```
Gitea: http://8.216.38.248:32158
- 管理员: gitea_admin / GitAdmin@2026
- ArgoCD用户: argocd / ArgoCD@2026
- 仓库: k3s-apps/demo-app, k3s-apps/test-app
ArgoCD: https://8.216.38.248:31875
- 用户: admin / ArgoAdmin@2026
- 应用: demo-app (Synced & Healthy)
- 应用: test-app (Synced & Healthy)
```
### 部署的应用
```
demo-app: NodePort 30080
- 2个nginx副本
- 状态: Running
test-app: NodePort 30081
- 2个nginx副本
- 状态: Running
- 域名: test.jpc.net3w.com
```
## 🎯 使用场景演示
### 场景1: 更新应用版本
```bash
# 1. SSH到master节点
ssh fei@8.216.38.248
# 2. 进入应用目录
cd /home/fei/k3s/test-app
# 3. 运行更新脚本
./update-app.sh v2.0
# 4. 等待3分钟ArgoCD自动同步
# 5. 验证更新
curl http://localhost:30081 | grep Version
```
### 场景2: 手动修改配置
```bash
# 1. 修改配置文件
vim manifests/deployment.yaml
# 2. 提交到Git
git add .
git commit -m "Update configuration"
git push
# 3. ArgoCD自动检测并部署3分钟内
kubectl get application test-app -n argocd -w
```
### 场景3: 创建新应用
```bash
# 1. 在Gitea创建新仓库
# 访问 http://8.216.38.248:32158
# 2. 创建Kubernetes清单文件
mkdir -p my-app/manifests
# 创建 deployment.yaml, service.yaml 等
# 3. 推送到Gitea
cd my-app
git init -b main
git add .
git commit -m "Initial commit"
git remote add origin http://...
git push
# 4. 在ArgoCD创建应用
kubectl apply -f argocd-app.yaml
# 5. ArgoCD自动部署
```
### 场景4: 回滚应用
```bash
# 方式1: 通过Git回滚
cd /home/fei/k3s/test-app
git log --oneline
git revert <commit-hash>
git push
# ArgoCD自动同步回滚
# 方式2: 通过ArgoCD Web UI
# 访问 https://8.216.38.248:31875
# 选择应用 → History → 选择版本 → Rollback
```
## 🔄 GitOps工作流程详解
### 完整流程
```
1. 开发者修改代码/配置
2. 提交到Git仓库 (Gitea)
3. ArgoCD定期检查Git仓库 (每3分钟)
4. 检测到变化后ArgoCD拉取最新配置
5. ArgoCD对比当前集群状态与Git中的期望状态
6. 如果有差异ArgoCD自动应用变更
7. Kubernetes更新Pod/Service/Ingress等资源
8. 应用自动滚动更新,零停机时间
9. ArgoCD持续监控确保状态一致
```
### 优势
-**声明式配置**: Git是唯一的真实来源
-**自动化部署**: 无需手动执行kubectl命令
-**版本控制**: 所有变更都有历史记录
-**快速回滚**: 一键回滚到任意历史版本
-**审计追踪**: 谁在什么时候做了什么改动
-**自我修复**: 如果有人手动修改集群ArgoCD会自动恢复
## 📝 下一步建议
### 1. 配置域名DNS
```bash
# 在DNS管理面板添加A记录
test.jpc.net3w.com → 8.216.38.248
argocd.jpc.net3w.com → 8.216.38.248
git.jpc.net3w.com → 8.216.38.248
```
### 2. 配置HTTPS证书
```bash
# 安装cert-manager
kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.13.0/cert-manager.yaml
# 配置Let's Encrypt
# 创建ClusterIssuer和Certificate资源
```
### 3. 添加监控
```bash
# 部署Prometheus + Grafana
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm install prometheus prometheus-community/kube-prometheus-stack -n monitoring --create-namespace
```
### 4. 配置备份
```bash
# 定期备份Gitea数据
kubectl exec -n gitea <pod> -- tar czf /tmp/backup.tar.gz /data
# 备份ArgoCD配置
kubectl get application -n argocd -o yaml > argocd-backup.yaml
```
### 5. 多环境管理
```bash
# 创建不同的命名空间
kubectl create namespace dev
kubectl create namespace staging
kubectl create namespace production
# 使用ArgoCD ApplicationSet管理多环境
```
## 🎉 总结
### 已完成
1.**幂等性配置修复** - 所有配置支持重复执行
2.**测试项目创建** - test-app完整部署并运行
3.**GitOps自动更新** - 修改Git自动部署到集群
4.**域名访问配置** - Ingress配置完成需DNS
5.**部署配置Git化** - 所有配置可存入Git管理
### 当前状态
- **K3s集群**: 3节点全部Ready
- **Gitea**: 运行正常2个仓库
- **ArgoCD**: 运行正常2个应用Synced
- **应用**: demo-app和test-app都在运行
### 可以开始使用
- ✅ 通过Git管理应用配置
- ✅ 自动部署更新
- ✅ 通过NodePort访问应用
- ✅ 通过域名访问配置DNS后
- ✅ 在ArgoCD Web UI查看状态
- ✅ 在Gitea管理Git仓库
**你的K3s + GitOps集群已经完全就绪可以投入使用** 🚀