主题
k8s-feishu-alarm 部署文档
环境信息
- 目标机器:
192.168.122.138(worker-vm1) - 登录凭据:
root/ai-ear.cn - 代码目录:
/root/work/k8s-feishu-alarm - 代码仓库:https://ai-ear.cn:9002/yaoguang2800/k8s-feishu-alarm.git
- 服务端口:
8000(后端同时托管前端静态资源) - 访问地址:http://192.168.122.138:8000
- 默认管理员:\1xxx\2
1. 安装依赖
bash
ssh root@192.168.122.138
# 安装 Python 3.11、Git、GCC、SELinux 管理工具
dnf install -y python3.11 python3.11-pip git gcc openssl-devel policycoreutils-python-utils
# 安装 Node 20(Vite 5 需要 Node 18+)
curl -fsSL https://rpm.nodesource.com/setup_20.x | bash -
dnf install -y nodejs --allowerasing
node --version # v20.x
npm --version # 10.x2. 代码目录
代码已放置在:
bash
/root/work/k8s-feishu-alarm如果未下载,可手动克隆:
bash
cd /root/work
git clone https://yaoguang2800:a15111917482@ai-ear.cn:9002/yaoguang2800/k8s-feishu-alarm.git3. 构建前端
bash
cd /root/work/k8s-feishu-alarm/frontend
rm -rf node_modules dist
npm install
npm run build构建产物位于 frontend/dist/,后端会自动托管该目录。
4. 配置后端虚拟环境
bash
cd /root/work/k8s-feishu-alarm/backend
rm -rf .venv
python3.11 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt --index-url https://mirrors.aliyun.com/pypi/simple/注意:项目使用
|联合类型注解,需要 Python 3.10+,因此必须使用python3.11。
5. SELinux 配置
由于代码位于 /root/work,systemd 默认无法执行该目录下的脚本,需要修复 SELinux 标签:
bash
semanage fcontext -a -t usr_t "/root/work(/.*)?" 2>/dev/null || \
semanage fcontext -m -t usr_t "/root/work(/.*)?"
restorecon -R /root/work6. 配置 systemd 服务
创建服务文件 /etc/systemd/system/k8s-feishu-alarm.service:
ini
[Unit]
Description=K8s Feishu Alarm
After=network.target
[Service]
Type=simple
User=root
WorkingDirectory=/root/work/k8s-feishu-alarm/backend
Environment=PATH=/root/work/k8s-feishu-alarm/backend/.venv/bin:/usr/local/bin:/usr/bin:/bin
ExecStart=/root/work/k8s-feishu-alarm/backend/.venv/bin/uvicorn main:app --host 0.0.0.0 --port 8000
Restart=always
[Install]
WantedBy=multi-user.target启动并启用开机自启:
bash
systemctl daemon-reload
systemctl enable k8s-feishu-alarm
systemctl start k8s-feishu-alarm7. 验证
bash
# 健康检查
curl http://localhost:8000/health
# {"status":"ok"}
# 首页
curl -s -o /dev/null -w "%{http_code}\n" http://localhost:8000/
# 200
# 登录测试
curl -s -X POST http://localhost:8000/api/auth/login \
-H "Content-Type: application/json" \
-d '{"username":"admin","password":"admin"}'8. 访问应用
在浏览器打开:
http://192.168.122.138:8000使用默认管理员账号登录:
- 用户名:xxx
- 密码:xxx
首次登录后,建议立即修改默认密码。
9. 宿主机端口转发
在宿主机(10.20.24.225)上配置 Nginx,将本机 18000 端口转发到虚拟机服务:
nginx
server {
listen 18000;
server_name _;
access_log /var/log/nginx/k8s-feishu-alarm.access.log;
error_log /var/log/nginx/k8s-feishu-alarm.error.log;
location / {
proxy_pass http://192.168.122.138:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}配置文件:/etc/nginx/conf.d/k8s-feishu-alarm.conf
bash
sudo nginx -t
sudo systemctl reload nginx验证:
bash
curl http://localhost:18000/health
# {"status":"ok"}访问地址:
- 宿主机内网:
http://10.20.24.225:18000 - 虚拟机本地:
http://192.168.122.138:8000
10. 主要后端 API
| 路径 | 说明 |
|---|---|
POST /api/auth/login | 登录获取 JWT |
GET /api/owners | 负责人列表 |
GET /api/applications | 应用列表 |
GET /api/templates | 告警模板 |
GET /api/bots | 飞书机器人 |
GET /api/tasks | 告警任务 |
常用命令
bash
# 查看服务状态
systemctl status k8s-feishu-alarm
# 查看日志
journalctl -u k8s-feishu-alarm -f
# 重启服务
systemctl restart k8s-feishu-alarm
# 停止服务
systemctl stop k8s-feishu-alarm