开发环境搭建
Nantian Gateway 的控制面用 Go 编写,数据面用 Rust 编写,管理界面用 Next.js 编写。本页带你从零搭建本地开发环境,完成编译、运行和调试的全流程。
| 工具 | 版本要求 | 用途 |
|---|---|---|
| Go | ≥ 1.21 | 控制面编译 |
| Rust | ≥ 1.85 | 数据面编译 |
| Docker | ≥ 24.0 | 本地集群和容器化运行 |
| kubectl | ≥ 1.28 | Kubernetes 集群操作 |
| Helm | ≥ 3.12 | Chart 打包和部署 |
| protoc | ≥ 3.21 | Protobuf 编译 |
| Node.js | ≥ 18 | 管理界面和文档站 |
| 工具 | 用途 |
|---|---|
| Kind | 本地 Kubernetes 集群 |
| just | 命令运行器(CI 和任务自动化) |
| golangci-lint | Go 代码 Lint |
| cargo-watch | Rust 热重载开发 |
快速验证环境
Section titled “快速验证环境”# 验证 Gogo version# 预期: go version go1.21.x ...
# 验证 Rustrustc --version# 预期: rustc 1.88.x ...
# 验证 Dockerdocker version
# 验证 kubectlkubectl version --client
# 验证 Helmhelm version
# 验证 protocprotoc --version
# 验证 Node.jsnode --version# Fork 主仓库,然后克隆你的 Forkgit clone https://github.com/<your-username>/gateway.gitcd gateway
# 添加主仓库为 upstreamgit remote add upstream https://github.com/nantian-gw/gateway.git搭建本地 Kubernetes 集群
Section titled “搭建本地 Kubernetes 集群”开发环境推荐使用 Kind 搭建本地集群:
# 创建 Kind 集群kind create cluster --name nantian-dev
# 验证集群状态kubectl cluster-infokubectl get nodes安装 Gateway API CRD
Section titled “安装 Gateway API CRD”# 安装 Gateway API v1.5.1 CRDkubectl apply -f https://github.com/kubernetes-sigs/gateway-api/releases/download/v1.5.1/standard-install.yaml编译和运行控制面
Section titled “编译和运行控制面”控制面使用 Go 编写,入口在 gateway/cmd/manager/main.go。
# 进入控制面目录cd gateway
# 安装依赖go mod download
# 编译控制面go build -o bin/manager ./cmd/manager
# 本地运行控制面(需要先安装 Nantian Gateway CRD)kubectl apply -f deploy/crds/./bin/manager --config configs/controlplane/config.yamlmanager 二进制只接受一个参数 --config(YAML 配置文件路径)。所有运行时行为——监听地址、主从选举、认领的 GatewayClass——都在该配置文件里设置,而非命令行参数。
控制面关键配置项
Section titled “控制面关键配置项”| 配置项 | 默认值 | 说明 |
|---|---|---|
grpcAddr | :18080 | gRPC xDS 服务器地址 |
adminAddr | :18081 | Admin API / HTTP 地址 |
metricsAddr | :18082 | Prometheus 指标地址 |
healthProbeAddr | :18083 | 健康与就绪探针地址 |
完整 schema(含主从选举与 GatewayClass 名称)见 控制面配置 参考文档。
编译和运行数据面
Section titled “编译和运行数据面”数据面使用 Rust 编写,位于 dataplane/ 目录。
# 进入数据面目录cd dataplane
# 编译数据面(debug 模式)cargo build
# 编译数据面(release 模式,性能优化)cargo build --release
# 运行数据面cargo run -p ntgw-app -- --config configs/dataplane/config.yamlntgw-app 二进制同样只接受一个 --config 参数。节点标识、控制面地址、admin/代理监听地址都来自配置文件。节点标识也可以通过 NANTIAN_GW_NODE_ID 环境变量注入,Helm chart 会用 Pod 名称设置它。
数据面关键配置项
Section titled “数据面关键配置项”| 配置项 | 默认值 | 说明 |
|---|---|---|
nodeId | dp-local | 节点标识 |
xds.serverAddress | http://127.0.0.1:18080 | 控制面 gRPC 地址 |
runtime.adminAddress | 0.0.0.0:19080 | Admin API 和指标地址 |
runtime.listenAddress | 0.0.0.0:8080 | HTTP 代理监听地址 |
完整 schema 见 数据面配置 参考文档。
编译 Protobuf
Section titled “编译 Protobuf”修改 proto/ 目录下的 .proto 文件后,需要重新生成 Go 和 Rust 代码:
# 生成 Go 代码cd protobuf generate
# 生成 Rust 代码(在 dataplane 目录下)cd ../dataplane# 代码生成已集成在 dataplane 的 build.rs 中cargo build运行管理界面
Section titled “运行管理界面”管理界面是基于 Next.js 的 Web 应用:
cd dashboard
# 安装依赖npm install
# 开发模式运行npm run dev
# 浏览器访问 http://localhost:3000文档站使用 Astro + Starlight:
cd website
# 安装依赖npm install
# 开发模式运行npm run dev
# 浏览器访问 http://localhost:4321使用 Docker Compose 一键启动
Section titled “使用 Docker Compose 一键启动”项目提供了 docker-compose.yml 用于快速启动完整的开发环境:
docker compose up -d这会启动:
- 控制面实例
- 数据面实例
- 管理界面
Go 控制面调试
Section titled “Go 控制面调试”使用 Delve 调试 Go 代码:
# 安装 Delvego install github.com/go-delve/delve/cmd/dlv@latest
# 调试模式运行控制面dlv debug ./cmd/manager -- --config configs/controlplane/config.yamlVS Code 用户可以安装 Go 扩展,在 .vscode/launch.json 中配置调试目标。
Rust 数据面调试
Section titled “Rust 数据面调试”Rust 数据面支持 RUST_LOG 环境变量控制日志级别:
# 启用详细日志RUST_LOG=debug cargo run -p ntgw-app -- --config configs/dataplane/config.yaml
# 仅查看特定模块日志RUST_LOG=ntgw_http=trace cargo run -p ntgw-app -- --config configs/dataplane/config.yaml常用的日志级别:error、warn、info、debug、trace。
查看 xDS 通信
Section titled “查看 xDS 通信”在开发过程中,可以查看控制面和数据面之间的 xDS 通信来验证配置是否正确传递:
# 查看控制面 Admin API 中已连接的数据面节点curl http://localhost:18081/v1/nodes | jq
# 查看数据面 Admin API 中的节点状态(含 xDS 流状态)curl http://localhost:19080/v1/node | jq