Files
tankwar_proj/server/DEPLOYMENT_GUIDE.md
2026-05-02 13:50:52 +08:00

256 lines
5.0 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Tank War Server - 完整部署指南
## 概述
本指南详细说明如何将Tank War Server部署到由3台CVM组成的Kubernetes集群中。
## 集群信息
目标K8s集群由以下3台CVM组成:
- 43.139.80.61 (host_172.16.16.16)
- 43.138.255.42 (host_172.16.16.17)
- 159.75.104.221 (host_172.16.16.8)
SSH连接:`ssh root@host_172.16.16.16`
## 部署前准备
### 1. 环境检查
```bash
# 在server目录下运行测试脚本
cd /Users/hanchengxi/workspace/tankwar_proj/server
./test-deployment.sh
```
### 2. 配置Kubernetes访问
确保kubectl已配置连接到目标集群:
```bash
# 检查集群连接
kubectl cluster-info
# 查看当前上下文
kubectl config current-context
# 如果未配置,需要获取集群的kubeconfig文件
# 通常从集群管理员处获取或通过云平台控制台下载
```
## 部署步骤
### 步骤1:构建Docker镜像
```bash
# 在server目录下构建镜像
docker build -t tankwar-server:latest .
# 验证镜像构建成功
docker images | grep tankwar-server
```
### 步骤2:部署到Kubernetes
```bash
# 方法1:使用部署脚本(推荐)
chmod +x deploy.sh
./deploy.sh
# 方法2:手动部署
kubectl create namespace tankwar --dry-run=client -o yaml | kubectl apply -f -
kubectl apply -f k8s-deployment.yaml -n tankwar
```
### 步骤3:验证部署
```bash
# 运行验证脚本
chmod +x verify-deployment.sh
./verify-deployment.sh
# 或手动验证
kubectl get all -n tankwar
kubectl logs -l app=tankwar-server -n tankwar
```
## 配置文件说明
### Dockerfile
- 基于Node.js 18 Alpine镜像
- 暴露端口3000
- 生产环境配置
### k8s-deployment.yaml
包含以下Kubernetes资源:
1. **ConfigMap**: 环境变量配置
2. **Deployment**:
- 3个副本
- 资源限制:内存512MiCPU 500m
- 健康检查探针
3. **Service**:
- LoadBalancer类型
- 端口3000
## 网络配置
### 服务暴露
服务使用LoadBalancer类型,将通过云平台的负载均衡器暴露:
```bash
# 获取外部IP
kubectl get svc tankwar-server-service -n tankwar
# WebSocket连接地址
ws://<external-ip>:3000
```
### 端口映射
- 容器端口:3000
- 服务端口:3000
- 外部访问端口:3000
## 健康检查
服务器提供HTTP健康检查端点:
```bash
# 健康检查URL
http://<external-ip>:3000/health
# 返回JSON格式的健康状态
{
"status": "healthy",
"timestamp": "2024-01-01T00:00:00.000Z",
"activeConnections": 0,
"activeRooms": 0,
"activeTeamRooms": 0
}
```
## 监控和维护
### 查看状态
```bash
# 查看Pod状态
kubectl get pods -n tankwar -w
# 查看服务状态
kubectl get svc -n tankwar
# 查看日志
kubectl logs -l app=tankwar-server -n tankwar --tail=50
# 查看资源使用
kubectl top pods -n tankwar
```
### 扩展和伸缩
```bash
# 扩展副本数量
kubectl scale deployment/tankwar-server --replicas=5 -n tankwar
# 自动伸缩(如果配置了HPA
kubectl autoscale deployment/tankwar-server --min=3 --max=10 --cpu-percent=80 -n tankwar
```
### 更新部署
```bash
# 重新构建镜像
docker build -t tankwar-server:latest .
# 滚动更新
kubectl rollout restart deployment/tankwar-server -n tankwar
# 查看更新状态
kubectl rollout status deployment/tankwar-server -n tankwar
```
## 故障排除
### 常见问题
1. **镜像构建失败**
```bash
# 检查Docker守护进程
docker info
# 检查Dockerfile语法
docker build --no-cache -t tankwar-server:latest .
```
2. **Pod无法启动**
```bash
# 查看Pod详情
kubectl describe pod <pod-name> -n tankwar
# 查看事件
kubectl get events -n tankwar
```
3. **服务无法访问**
```bash
# 检查服务端点
kubectl get endpoints tankwar-server-service -n tankwar
# 检查网络策略
kubectl get networkpolicies -n tankwar
```
4. **健康检查失败**
```bash
# 检查Pod内部
kubectl exec -it <pod-name> -n tankwar -- wget -qO- http://localhost:3000/health
```
### 调试命令
```bash
# 进入Pod调试
kubectl exec -it <pod-name> -n tankwar -- /bin/sh
# 端口转发本地调试
kubectl port-forward svc/tankwar-server-service 3000:3000 -n tankwar
# 然后访问:http://localhost:3000/health
```
## 清理部署
```bash
# 删除部署
kubectl delete -f k8s-deployment.yaml -n tankwar
# 删除命名空间
kubectl delete namespace tankwar
# 清理镜像
docker rmi tankwar-server:latest
```
## 安全考虑
1. **网络策略**: 配置适当的网络策略限制访问
2. **资源限制**: 设置合理的资源限制防止资源耗尽
3. **镜像安全**: 定期更新基础镜像修复安全漏洞
4. **访问控制**: 配置RBAC权限控制
## 性能优化建议
1. **副本数量**: 根据负载调整副本数量
2. **资源分配**: 根据实际使用情况调整资源限制
3. **连接池**: 考虑使用连接池优化WebSocket连接
4. **监控告警**: 配置监控和告警系统
## 支持信息
如有问题,请检查:
- 服务器日志:`kubectl logs -l app=tankwar-server -n tankwar`
- 部署状态:`kubectl get all -n tankwar`
- 集群状态:`kubectl cluster-info`