Files
aiops/deploy/kubernetes/README.md
T
Logan.Li 8df0acb338 fix(k8s): 同步 local 模式数据路径为 /app/sqlite3.db 与二进制同级
- deployment/quick-deploy: PVC 挂载 sqlite3.db/redka.db 到 /app/,含旧 data/ 迁移
- configmap 与 run.sh 状态输出对齐新路径
- README 补充 local 模式说明
2026-06-26 16:19:03 +08:00

164 lines
4.8 KiB
Markdown
Raw 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.
# AIOps Kubernetes 部署
AIOps 平台在 Kubernetes 上的部署清单(Go/Gin 后端 + go:embed 前端 + MySQL 8.0 + Redis 7)。
**单容器架构**:前端通过 `go:embed` 嵌入后端二进制,一个容器同时服务 API + WebSocket + SPA 静态资源,端口统一 8080。
所有资源部署在 **`devops`** 命名空间。
## 目录结构
| 文件 | 说明 |
|------|------|
| `namespace.yaml` | `devops` 命名空间 |
| `secret.yaml` | MySQL/JWT 密码(Secret) |
| `configmap.yaml` | 后端 `config.yaml`(ConfigMap) |
| `mysql.yaml` | MySQL 8.0 StatefulSet + Headless Service + 10Gi 持久卷 |
| `redis.yaml` | Redis 7 StatefulSet + Headless Service + 2Gi 持久卷 |
| `deployment.yaml` | AIOps Deployment(embed 单端口 8080)+ NodePort Service (30080) |
| `pvc-aiops-data.yaml` | local 模式 PVC:`sqlite3.db` / `redka.db` 与 `/app/aiops` 同级 |
| `ingress.yaml` | Ingress:所有流量 → aiops:8080(含 WebSocket 支持) |
## 架构
```
NodePort:30080 / Ingress
│
▼
┌─── aiops (Deployment) ───┐
│ 单端口 8080: │
│ • API (/api/*) │
│ • WebSocket (/ws/*) │
│ • 前端 SPA (go:embed) │
└───────────┬───────────────┘
│
┌──────────────┼──────────────┐
▼ ▼
MySQL 8.0 Redis 7
(StatefulSet) (StatefulSet)
mysql:3306 redis:6379
10Gi PV 2Gi PV
```
## 快速部署
### 方式一:run.sh 一键部署(推荐)
```bash
# 构建镜像 + 部署全部资源
./run.sh kubernetes deploy
# 查看状态
./run.sh kubernetes status
# 查看日志
./run.sh kubernetes logs
# 更新(重新构建镜像 + 滚动更新)
./run.sh kubernetes upgrade
# 清理
./run.sh kubernetes delete
```
### 方式二:手动 kubectl
```bash
cd deploy/kubernetes
# 1. 全量部署(文件按序: namespace → secret → configmap → mysql → redis → deployment)
kubectl apply -f namespace.yaml
kubectl apply -f secret.yaml
kubectl apply -f configmap.yaml
kubectl apply -f mysql.yaml
kubectl apply -f redis.yaml
# 2. 等待数据库就绪
kubectl -n devops rollout status statefulset/mysql
kubectl -n devops rollout status statefulset/redis
# 3. 构建镜像(在本地)
cd ../..
docker build -f Dockerfile.embed -t aiops:embed .
# 4. 部署应用
kubectl apply -f deployment.yaml
kubectl -n devops rollout status deployment/aiops
# 5. Ingress(按需修改 host)
kubectl apply -f ingress.yaml
```
## 访问
```bash
# NodePort 方式
curl http://<NODE_IP>:30080/health
# 或配置 Ingress host
echo "<INGRESS_IP> aiops.local" | sudo tee -a /etc/hosts
# 浏览器: http://aiops.local
```
## 常用运维命令
```bash
# 查看所有资源
kubectl -n devops get all,pvc,configmap,secret
# 查看应用日志
kubectl -n devops logs -f deployment/aiops
# 进入应用容器
kubectl -n devops exec -it deployment/aiops -- bash
# 进入 MySQL
kubectl -n devops exec -it mysql-0 -- mysql -uaiops -paiops_pass_2026 aiops
# 滚动更新(重新构建镜像后)
kubectl -n devops rollout restart deployment/aiops
# 查看 PVC
kubectl -n devops get pvc
```
## 配置说明
### local 模式(默认 `MODE=local`)
- 容器内工作目录 `/app`,数据文件与二进制同级:
- `/app/aiops` — 应用二进制(镜像内)
- `/app/sqlite3.db` — SQLite(PVC 持久化)
- `/app/redka.db` — 本地缓存(PVC 持久化)
- `replicas` 必须为 **1**(SQLite 不支持多副本共享);`./run.sh kubernetes scale` 扩到 >1 时会自动切 `MODE=release` + MySQL/Redis
- 旧版 PVC 若存有 `data/sqlite3.db`,init 容器会自动迁移到根路径
### 集群凭据
| 项目 | 来源 | 值 |
|------|------|----|
| MySQL 业务账号 | Secret `aiops-secret` | 用户 `aiops` / 密码 `aiops_pass_2026` / 库 `aiops` |
| MySQL root | Secret `aiops-secret` | `aiops_root_2026` |
| JWT | Secret `aiops-secret` | `aiops-jwt-secret-key-2026` |
| Redis | 无密码 | db `0` |
> ⚠️ 生产环境请修改 `secret.yaml` 和 `configmap.yaml` 中的默认密码。
## 资源限额
| 组件 | CPU 请求/上限 | 内存请求/上限 |
|------|---------------|---------------|
| aiops | 100m / 1 | 256Mi / 1Gi |
| mysql | 250m / 1 | 512Mi / 2Gi |
| redis | 50m / 250m | 128Mi / 256Mi |
## 清理
```bash
./run.sh kubernetes delete
# 或手动(持久卷需单独删)
kubectl delete -f deploy/kubernetes/
kubectl -n devops delete pvc --all
```