feat: 三种部署方式 — embed单容器 + 二进制部署 + K8s

新增三种部署方式(原有 docker-compose 不受影响):

1. Docker embed 单容器 (docker-compose.embed.yml):
   - go:embed 前端嵌入后端二进制
   - 不需要 nginx 容器,3容器→2容器
   - 镜像仅 54.8MB (vs 原来 1.16GB)
   - 用法: make docker-embed

2. 二进制部署 (deploy/binary/):
   - make build-binary 生成单二进制
   - install.sh 自动创建 systemd 服务
   - 适合无 Docker 环境
   - 用法: scp aiops + install.sh, bash install.sh install

3. Kubernetes 部署 (deploy/k8s/):
   - devops 命名空间全套 yaml
   - MySQL StatefulSet + Redis StatefulSet + Backend Deployment
   - ConfigMap + Secret + Ingress
   - 用法: kubectl apply -f deploy/k8s/

核心改动:
- backend/cmd/embed.go: go:embed 前端 + SPA fallback 路由
- backend/Dockerfile.embed: 四阶段构建 (前端→后端embed→agent→runtime)
- Makefile: build-binary + docker-embed 目标
- deploy/README.md: 四种部署方式对比文档
This commit is contained in:
Logan authored and attacker committed 2026-06-21 14:20:29 +08:00
1 parent 24f955832e
commit 4631b02f13
15 files changed
+1218 -3

No files matched your search

+165
View File
@@ -0,0 +1,165 @@
#!/bin/bash
# ============================================================
# AIOps 二进制部署脚本(embed 模式)
# 一个二进制 = 前端 + 后端,无需 Docker/Nginx
# 署名 Logan.Li
# ============================================================
set -e
# 颜色
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
info() { echo -e "${GREEN}[INFO]${NC} $1"; }
warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
error() { echo -e "${RED}[ERROR]${NC} $1"; }
# 默认路径
INSTALL_DIR="/opt/aiops"
CONFIG_FILE="$INSTALL_DIR/config.yaml"
SERVICE_FILE="/etc/systemd/system/aiops.service"
BINARY_NAME="aiops"
PID_FILE="/var/run/aiops.pid"
# ============================================================
# 命令处理
# ============================================================
case "$1" in
install)
info "AIOps 二进制安装"
echo ""
if [ "$(id -u)" -ne 0 ]; then
error "请使用 root 或 sudo 执行"
exit 1
fi
# 检查二进制
if [ ! -f "./${BINARY_NAME}" ]; then
error "当前目录找不到 ./${BINARY_NAME} 二进制文件"
echo " 请先在开发机执行: make build-binary"
exit 1
fi
# 创建安装目录
mkdir -p "$INSTALL_DIR/logs" "$INSTALL_DIR/uploads"
# 复制二进制
cp "./${BINARY_NAME}" "$INSTALL_DIR/"
chmod +x "$INSTALL_DIR/${BINARY_NAME}"
info "二进制已安装到 $INSTALL_DIR/${BINARY_NAME}"
# 生成默认配置(如果不存在)
if [ ! -f "$CONFIG_FILE" ]; then
cat > "$CONFIG_FILE" << 'EOF'
mode: release
port: "8080"
log_level: info
database:
driver: mysql
host: 127.0.0.1
port: 3306
user: aiops
password: aiops_pass_2026
dbname: aiops
charset: utf8mb4
max_conn: 100
max_idle: 10
redis:
driver: redis
host: 127.0.0.1
port: 6379
password: ""
db: 0
jwt:
secret: ezops-aiops-jwt-secret-2026
expire_hour: 24
EOF
info "默认配置已生成: $CONFIG_FILE"
warn "请根据实际环境修改数据库/Redis 配置"
fi
# 创建 systemd 服务
cat > "$SERVICE_FILE" << EOF
[Unit]
Description=AIOps Platform (Embed Mode)
After=network-online.target mysql.service redis.service
Wants=network-online.target
[Service]
Type=simple
WorkingDirectory=${INSTALL_DIR}
ExecStart=${INSTALL_DIR}/${BINARY_NAME}
Restart=always
RestartSec=5
LimitNOFILE=65536
Environment=GIN_MODE=release
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable aiops
info "systemd 服务已创建并启用开机自启"
echo ""
info "安装完成!"
echo ""
echo " 启动: systemctl start aiops"
echo " 状态: systemctl status aiops"
echo " 日志: journalctl -u aiops -f"
echo " 配置: vim $CONFIG_FILE"
echo " 访问: http://<IP>:8080"
echo ""
;;
start)
systemctl start aiops
info "AIOps 已启动"
;;
stop)
systemctl stop aiops
info "AIOps 已停止"
;;
restart)
systemctl restart aiops
info "AIOps 已重启"
;;
status)
systemctl status aiops --no-pager
;;
uninstall)
warn "即将卸载 AIOps(保留数据/配置)"
systemctl stop aiops 2>/dev/null
systemctl disable aiops
rm -f "$SERVICE_FILE"
rm -f "$INSTALL_DIR/${BINARY_NAME}"
systemctl daemon-reload
info "已卸载(配置/日志/数据保留在 $INSTALL_DIR)"
;;
*)
echo "AIOps 二进制部署工具"
echo ""
echo "用法: $0 {install|start|stop|restart|status|uninstall}"
echo ""
echo " install - 安装二进制 + 生成配置 + 创建 systemd 服务"
echo " start - 启动服务"
echo " stop - 停止服务"
echo " restart - 重启服务"
echo " status - 查看服务状态"
echo " uninstall - 卸载二进制(保留数据)"
echo ""
exit 1
;;
esac