333 lines
12 KiB
Markdown
333 lines
12 KiB
Markdown
# deploy-test — 测试服务器部署脚本集
|
||
|
||
> **适用场景**:部署在**有公网 IP 的测试服务器**上。所有服务(后端、前端、Docker 基础设施、LiveKit)均在本机运行。
|
||
>
|
||
> 如果你在本机 Mac 开发,请使用 `deploy-local/` 目录。
|
||
|
||
---
|
||
|
||
## 两套环境对比
|
||
|
||
| 项目 | deploy-test(本目录)| deploy-local/ |
|
||
|------|---------------------|----------------|
|
||
| 适用机器 | 测试服务器(有公网 IP) | 本机 Mac(无公网 IP) |
|
||
| 配置文件 | `.env.deploy-test` | `.env.deploy-local` |
|
||
| 运行时目录 | `.deploy-test/` | `.deploy-local/` |
|
||
| LiveKit | 本机 Docker 启动,使用公网 IP | 指向本目录服务器的 LiveKit |
|
||
| Redis/Kafka/Etcd | 本机 Docker | 本机 Docker |
|
||
| 后端服务 | 本机进程 | 本机进程 |
|
||
| 前端服务 | 本机进程(可选) | 本机进程 |
|
||
|
||
---
|
||
|
||
## 目录结构
|
||
|
||
```
|
||
deploy-test/
|
||
├── 00-init-tools.sh # 步骤0(可选):Linux 服务器安装 Go / Node / Docker、GOPROXY、GitHub HTTPS 重写
|
||
├── common.sh # 公共函数库(路径、日志函数)
|
||
├── 01-init-env.sh # 步骤1:生成 .env.deploy-test 配置模板
|
||
├── 02-patch-config.sh # 步骤2:将 .env.deploy-test 写入各服务 YAML
|
||
├── 03-start-infra.sh # 步骤3:启动 Docker 容器(Redis/Kafka/Etcd/LiveKit)
|
||
├── 04-build.sh # 步骤4:编译所有后端 Go 服务
|
||
├── 05-start.sh # 步骤5:启动所有后端服务
|
||
├── 06-install-frontend.sh # 步骤6:安装前端依赖(可选)
|
||
├── 07-start-frontend.sh # 步骤7:启动前端开发服务器(可选)
|
||
├── stop.sh # 停止后端服务
|
||
├── stop-infra.sh # 停止 Docker 容器(含 LiveKit)
|
||
├── stop-frontend.sh # 停止前端服务
|
||
├── remove-infra.sh # 删除 Docker 容器及数据(危险!)
|
||
├── restart.sh # 重启指定服务(支持 --build)
|
||
├── status.sh # 查看所有服务状态
|
||
├── logs.sh # 查看日志(统一入口)
|
||
├── check-conn.sh # 验证 MongoDB / S3 连接
|
||
└── setup.sh # 一键完整部署(首次使用)
|
||
```
|
||
|
||
运行时目录(`.deploy-test/`,已加入 `.gitignore`):
|
||
|
||
```
|
||
.deploy-test/
|
||
├── bin/ # Go 编译产物
|
||
├── pids/ # PID 文件
|
||
├── logs/ # 后端/前端服务日志
|
||
├── docker-data/ # Docker 数据卷
|
||
├── docker-logs/ # Docker 容器日志(按日期滚动)
|
||
└── script-logs/ # 脚本执行日志(带时间戳)
|
||
```
|
||
|
||
---
|
||
|
||
## 步骤 0:`00-init-tools.sh`(裸机 / 新服务器)
|
||
|
||
在**尚未安装 Go、Node、Docker** 的 Linux 测试服务器上,先执行本脚本再跑 `01-init-env.sh` 及后续步骤。`setup.sh` **不会**自动调用它,需手动执行。
|
||
|
||
| 项目 | 说明 |
|
||
|------|------|
|
||
| **作用** | 安装 Go(默认 1.22.5,可用 `GO_VERSION` 覆盖)、配置 GOPROXY(测速选节点)、安装 Node.js LTS、全局安装 pnpm/yarn、安装 Docker、将 GitHub SSH 克隆地址重写为 HTTPS(避免 `pc` 等项目 `yarn install` 因无 SSH 密钥失败) |
|
||
| **环境变量** | `GO_VERSION`(如 `1.22.5`)、`GO_ARCH`(`amd64` / `arm64`)、`NODE_VERSION`(Node 大版本,默认 `20`) |
|
||
| **权限** | 需要 **root** 或 **sudo**(写入 `/usr/local/go`、`/etc/profile.d/deploy-env.sh` 等) |
|
||
| **系统** | 面向 Ubuntu/Debian(`apt-get`);脚本内注释说明前置条件 |
|
||
|
||
```bash
|
||
# 安装全部(Go + GOPROXY + Node + Docker)
|
||
sudo ./deploy-test/00-init-tools.sh
|
||
|
||
# 或只执行其中一项
|
||
sudo ./deploy-test/00-init-tools.sh go # Go + GOPROXY
|
||
sudo ./deploy-test/00-init-tools.sh goproxy # 仅重配 GOPROXY
|
||
sudo ./deploy-test/00-init-tools.sh node # 仅 Node / npm / pnpm / yarn
|
||
sudo ./deploy-test/00-init-tools.sh docker # 仅 Docker
|
||
```
|
||
|
||
执行结束后脚本会提示:新开终端需 `source /etc/profile.d/deploy-env.sh` 或重新登录 SSH,再执行 `./deploy-test/01-init-env.sh`。
|
||
|
||
---
|
||
|
||
## 快速开始
|
||
|
||
### 首次使用
|
||
|
||
```bash
|
||
# 裸机请先装工具(可选,见上文「步骤 0」)
|
||
# sudo ./deploy-test/00-init-tools.sh
|
||
|
||
# 一键执行(推荐,不含 00-init-tools)
|
||
./deploy-test/setup.sh
|
||
```
|
||
|
||
### 分步执行
|
||
|
||
```bash
|
||
# 0. (可选)裸机安装 Go / Node / Docker,见上文「步骤 0」
|
||
# sudo ./deploy-test/00-init-tools.sh
|
||
|
||
# 1. 生成配置模板
|
||
./deploy-test/01-init-env.sh
|
||
|
||
# 2. 修改配置(重要:确认 DEPLOY_TEST_IP 等信息正确)
|
||
vim .env.deploy-test
|
||
|
||
# 3. 将配置写入各服务 YAML(包括 livekit/livekit.yaml)
|
||
./deploy-test/02-patch-config.sh
|
||
|
||
# 4. 启动 Docker 基础设施(Redis / Kafka / Etcd / LiveKit)
|
||
./deploy-test/03-start-infra.sh
|
||
|
||
# 5. 编译后端服务
|
||
./deploy-test/04-build.sh
|
||
|
||
# 6. 启动后端服务
|
||
./deploy-test/05-start.sh
|
||
|
||
# 7. 安装前端依赖(可选)
|
||
./deploy-test/06-install-frontend.sh
|
||
|
||
# 8. 启动前端开发服务器(可选)
|
||
./deploy-test/07-start-frontend.sh
|
||
```
|
||
|
||
> **pc(Electron)依赖安装**:在跑 `06-install-frontend.sh` 前,裸机请先执行 **`00-init-tools.sh`**(见上文「步骤 0」,含 GitHub SSH→HTTPS 重写)。仍报错见下文「pc 前端 yarn install 失败」。`pc` 建议仅用 **Yarn**;可删除 `package-lock.json` 消除混用锁文件警告。
|
||
|
||
---
|
||
|
||
## 配置文件(`.env.deploy-test`)
|
||
|
||
```bash
|
||
# ══ 测试服务器公网 IP ═══════════════════════════════════════════
|
||
DEPLOY_TEST_IP=54.116.29.247 # 本机公网 IP(LiveKit WebRTC 必需)
|
||
|
||
# ══ MongoDB ════════════════════════════════════════════════════
|
||
MONGO_HOST=47.237.103.4
|
||
MONGO_PORT=27017
|
||
MONGO_USERNAME=minio_pC5wMB
|
||
MONGO_PASSWORD=rI57PJsJhnz_qlRkfnTa0RPT
|
||
MONGO_AUTHSOURCE=openim_v3
|
||
MONGO_DATABASE=openim_v3
|
||
BUILD_MONGO_DATABASE=build
|
||
|
||
# ══ Amazon S3 ══════════════════════════════════════════════════
|
||
OPENIM_AWS_REGION=ap-southeast-1
|
||
OPENIM_AWS_BUCKET=im1688
|
||
OPENIM_AWS_ACCESS_KEY_ID=xxx
|
||
OPENIM_AWS_SECRET_ACCESS_KEY=xxx
|
||
|
||
BUILD_AWS_REGION=ap-east-1
|
||
BUILD_AWS_BUCKET=im-hk-apk
|
||
BUILD_AWS_ACCESS_KEY=xxx
|
||
BUILD_AWS_SECRET_KEY=xxx
|
||
|
||
# ══ Docker Redis / Kafka / Etcd ════════════════════════════════
|
||
REDIS_PORT=6379
|
||
REDIS_PASSWORD=openIM123
|
||
KAFKA_PORT=9092
|
||
ETCD_PORT=2379
|
||
|
||
# ══ LiveKit(本机 Docker,使用公网 IP)════════════════════════
|
||
LIVEKIT_NODE_IP=54.116.29.247 # 与 DEPLOY_TEST_IP 保持一致
|
||
LIVEKIT_URL=ws://127.0.0.1:7880
|
||
LIVEKIT_API_KEY=API8462dba2
|
||
LIVEKIT_API_SECRET=xxx
|
||
|
||
# ══ Cloudflare Stream / 腾讯云 RTC ════════════════════════════
|
||
CF_ACCOUNT_ID=
|
||
CF_API_TOKEN=
|
||
TENCENT_SDK_APP_ID=xxx
|
||
TENCENT_SDK_SECRET_KEY=xxx
|
||
```
|
||
|
||
---
|
||
|
||
## 服务地址
|
||
|
||
### 后端服务
|
||
|
||
| 服务 | 端口 |
|
||
|------|------|
|
||
| openim-server | :10002 (HTTP) / :10001 (WS) |
|
||
| chat-api | :10008 |
|
||
| admin-api | :10009 |
|
||
| meetingmsg | :8000 (WS) |
|
||
| livecloud | :8080 |
|
||
| livestream | :8081 |
|
||
| build-server | :8281 |
|
||
|
||
### 前端开发服务器(可选)
|
||
|
||
| 项目 | 端口 | 说明 |
|
||
|------|------|------|
|
||
| pc (Electron) | :7777 | Electron 桌面客户端 |
|
||
| meetingh5 | :5188 | 直播观看 H5(弹幕+视频) |
|
||
| h5 | :3003 | 移动端 H5 |
|
||
| cms | :8001 | 后台管理 |
|
||
| build-cms | :8002 | 构建管理后台 |
|
||
| build-down | :8003 | 下载页 |
|
||
|
||
> **meetingh5 访问方式**
|
||
>
|
||
> `02-patch-config.sh` 会自动生成 `meetingh5/.env.local`,设置默认后端地址:
|
||
>
|
||
> ```
|
||
> # 直接访问(使用 .env.local 中的默认后端)
|
||
> http://<DEPLOY_TEST_IP>:5188
|
||
>
|
||
> # 或显式传入 URL 参数(优先级最高)
|
||
> http://<DEPLOY_TEST_IP>:5188?ws=ws://<DEPLOY_TEST_IP>:8000&liveApi=http://<DEPLOY_TEST_IP>:8081
|
||
> ```
|
||
>
|
||
> - `ws` → meetingmsg 弹幕 WebSocket `:8000`
|
||
> - `liveApi` → livestream 直播间 API `:8081`
|
||
|
||
### Docker 基础设施
|
||
|
||
| 服务 | 端口 |
|
||
|------|------|
|
||
| Redis | :6379 |
|
||
| Kafka | :9092 |
|
||
| Etcd | :2379 |
|
||
| LiveKit | :7880 (API) / :7882 (TCP) / :50000-51000/udp (WebRTC) |
|
||
|
||
---
|
||
|
||
## LiveKit 说明
|
||
|
||
本测试服务器运行本地 LiveKit 容器,WebRTC 媒体流通过公网 IP 对外暴露。
|
||
|
||
```
|
||
测试服务器(DEPLOY_TEST_IP: 54.116.29.247)
|
||
│
|
||
├── dev-redis :6379 ←── dev-livekit 通过 host.docker.internal 访问
|
||
└── dev-livekit
|
||
:7880 → HTTP API(后端连接)
|
||
:7882/tcp+udp → WebRTC fallback
|
||
:50000-51000/udp → WebRTC 媒体流(客户端直连公网 IP)
|
||
```
|
||
|
||
**防火墙必须开放**:7880/tcp、7882/tcp+udp、50000-51000/udp
|
||
|
||
本机 Mac(deploy-local)的 LiveKit 连接地址为 `ws://54.116.29.247:7880`,与此保持一致。
|
||
|
||
---
|
||
|
||
## 日志体系
|
||
|
||
```
|
||
.deploy-test/
|
||
├── script-logs/ ← 每次脚本执行的完整输出(带时间戳,自动去除颜色码)
|
||
├── logs/ ← 后端/前端服务进程 stdout+stderr
|
||
└── docker-logs/ ← Docker 容器日志(每日一文件)
|
||
├── redis/
|
||
├── kafka/
|
||
├── etcd/
|
||
└── livekit/
|
||
```
|
||
|
||
```bash
|
||
# 查看所有日志概览
|
||
./deploy-test/logs.sh
|
||
|
||
# 实时跟踪某个服务
|
||
./deploy-test/logs.sh openim-server
|
||
./deploy-test/logs.sh livekit
|
||
./deploy-test/logs.sh cms
|
||
|
||
# 查看脚本执行历史
|
||
./deploy-test/logs.sh scripts
|
||
./deploy-test/logs.sh scripts --last # 最新一次完整输出
|
||
```
|
||
|
||
---
|
||
|
||
## 日常操作
|
||
|
||
```bash
|
||
# 早上开机
|
||
./deploy-test/03-start-infra.sh # Docker 容器(含 LiveKit)
|
||
./deploy-test/05-start.sh # 后端服务
|
||
|
||
# 查看状态
|
||
./deploy-test/status.sh
|
||
|
||
# 重启单个后端服务
|
||
./deploy-test/restart.sh chat-api
|
||
./deploy-test/restart.sh chat-api --build # 重编译 + 重启
|
||
|
||
# 下班关机
|
||
./deploy-test/stop.sh # 后端进程
|
||
./deploy-test/stop-infra.sh # Docker 容器(含 LiveKit,数据保留)
|
||
```
|
||
|
||
---
|
||
|
||
## 故障排查
|
||
|
||
### pc 前端 `yarn install` 失败(Git SSH / Host key verification failed)
|
||
|
||
现象示例:`git ls-remote ... ssh://git@github.com/electron/node-gyp.git` → `Host key verification failed` 或 `Could not read from remote repository`。
|
||
|
||
| 说明 | 处理 |
|
||
|------|------|
|
||
| **锁文件** | 仓库中 `pc/yarn.lock`、`pc/package-lock.json` 已将 `@electron/node-gyp` 的 `resolved` 固定为 **`git+https://github.com/electron/node-gyp.git#...`**(无需 SSH)。请拉取最新代码后再执行 `yarn install`。 |
|
||
| **环境** | 首次部署请执行 `./deploy-test/00-init-tools.sh`,脚本内会通过 `git config url.*.insteadOf` 将 `git+ssh://`、`ssh://git@github.com/`、`git@github.com:` 重写为 `https://github.com/`。 |
|
||
| **仍失败** | 手动执行(与 `00-init-tools.sh` 一致):<br>`git config --global url."https://github.com/".insteadOf "git+ssh://git@github.com/"`<br>`git config --global url."https://github.com/".insteadOf "ssh://git@github.com/"`<br>`git config --global url."https://github.com/".insteadOf "git@github.com:"` |
|
||
| **包管理器** | `pc` 使用 **Yarn**。若同时存在 `package-lock.json`,Yarn 会警告混用锁文件;可删除 `pc/package-lock.json`,仅保留 `yarn.lock`(与团队约定一致即可)。 |
|
||
|
||
```bash
|
||
cd pc && rm -rf node_modules && yarn install
|
||
```
|
||
|
||
### 其他
|
||
|
||
```bash
|
||
# 验证 MongoDB / S3 连接
|
||
./deploy-test/check-conn.sh
|
||
|
||
# 查看 LiveKit 日志(WebRTC 不通时)
|
||
./deploy-test/logs.sh livekit --last
|
||
|
||
# 重置 Docker 环境(删除所有数据)
|
||
./deploy-test/remove-infra.sh
|
||
./deploy-test/02-patch-config.sh
|
||
./deploy-test/03-start-infra.sh
|
||
```
|