Files
aiops/deploy/kubernetes/README.md
T
Logan.Li fd60361d93 chore: 统一本地服务器与远程仓库源码
- 合并 main 与 master 历史并以服务器结构为基线
- 保留迁移、数据保留、AWS 账单与部署安全能力
- 建立 SDD 规格和 CI 验证闭环
2026-08-05 20:03:25 +08:00

177 lines
6.0 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 Deployment + Service + 10Gi 持久卷 |
| `redis.yaml` | Redis 7 Deployment + Service + 2Gi 持久卷 |
| `deployment.yaml` | AIOps Deployment、NodePort Service (30080) 和 MySQL/Redis NetworkPolicy |
| `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
(Deployment) (Deployment)
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 v1.5.0
# 构建并推送 Docker Hub
./run.sh kubernetes push v1.5.0
# 清理
./run.sh kubernetes delete
```
镜像仓库:`lghost/aiops:<tag>`(由 `./run.sh release` 或 `kubernetes build` 构建注入版本号)。
### 方式二:手动 kubectl
```bash
cd deploy/kubernetes
kubectl apply -f namespace.yaml
# 先生成随机密钥并替换 secret.yaml 的 REPLACE_WITH_* 占位符,勿提交真实值。 // Logan.Li
# openssl rand -hex 32
kubectl apply -f secret.yaml
kubectl apply -f configmap.yaml
kubectl apply -f pvc-aiops-data.yaml
kubectl apply -f mysql.yaml
kubectl apply -f redis.yaml
kubectl -n devops rollout status deployment/mysql
kubectl -n devops rollout status deployment/redis
# 构建镜像(Mac/Linux 均可,按 TARGETARCH 自动选架构)
cd ../..
./run.sh kubernetes build v1.5.0
kubectl apply -f deploy/kubernetes/deployment.yaml
kubectl -n devops rollout status deployment/aiops
kubectl apply -f deploy/kubernetes/ingress.yaml # 可选
```
## 访问
```bash
# NodePort 方式(推荐 /version,无 DB 强依赖)
curl http://<NODE_IP>:30080/version
# 或配置 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(密码从本地 Secret 读取,避免出现在 shell 历史中)
MYSQL_PASSWORD="$(kubectl -n devops get secret aiops-secret -o jsonpath='{.data.mysql-password}' | base64 -d)"
kubectl -n devops exec -it deployment/mysql -- mysql -uaiops -p"$MYSQL_PASSWORD" aiops
# 滚动更新(换镜像 tag 后)
kubectl -n devops set image deployment/aiops aiops=lghost/aiops:v1.5.0 --record
kubectl -n devops rollout status 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` / `mysql-password` 键 / 库 `aiops` |
| MySQL root | Secret `aiops-secret` | `mysql-root-password` 键 |
| JWT | Secret `aiops-secret` | `jwt-secret` 键 |
| Redis | 无密码 | db `0` |
> ⚠️ `secret.yaml` 只包含不可用的占位符。部署前须通过受控 Secret 管理系统或本地安全通道填充随机值;ConfigMap 不得保存密码和 JWT。
## 安全基线
- 应用容器以 UID/GID `10001` 运行,移除了 `hostNetwork` 和 Docker Socket 挂载。
- SQLite/redka 与 uploads 分别通过 PVC 持久化;不要以 `emptyDir` 替代 uploads PVC。
- `deployment.yaml` 中的 NetworkPolicy 保护 MySQL/Redis 的集群内入口。应用同时接收集群外 Agent 上报,待明确 Ingress 与 Agent 出口 CIDR 后,再增加应用 Pod 的严格入站策略。
- 使用 Ingress 时应创建 TLS Secret 并配置 HTTPS;NodePort 仅适合受防火墙保护的内网运维入口。
## 资源限额
| 组件 | 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
```