refactor: rename deploy/k8s to deploy/kubernetes; add SetEnvKeyReplacer for env var support in K8s

This commit is contained in:
attacker committed 2026-06-24 04:02:26 +00:00
1 parent d581441fa8
commit bbf06204cc
15 files changed
+226 -44

No files matched your search

+151
View File
@@ -0,0 +1,151 @@
# 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) |
| `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 k8s deploy
# 查看状态
./run.sh k8s status
# 查看日志
./run.sh k8s logs
# 更新(重新构建镜像 + 滚动更新)
./run.sh k8s upgrade
# 清理
./run.sh k8s 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
```
## 配置说明
| 项目 | 来源 | 值 |
|------|------|----|
| 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 k8s delete
# 或手动(持久卷需单独删)
kubectl delete -f deploy/kubernetes/
kubectl -n devops delete pvc --all
```