跳到主要内容

DatomsDBS AWS EC2 测试脚本使用示例

📝 使用场景示例

场景1: 开发团队快速验证部署

背景: 开发团队需要在云环境中快速验证DatomsDBS的部署和基础功能。

# 步骤1: 准备环境
aws configure # 配置AWS凭证
git clone https://github.com/yourusername/datomsDBS.git
cd datomsDBS

# 步骤2: 运行简化测试
./scripts/simple_aws_test.sh https://github.com/yourusername/datomsDBS.git

# 预期结果:
# - 15分钟内完成部署
# - 费用约$0.10
# - 获得访问地址和清理命令

场景2: 生产前全面测试验证

背景: 产品上线前需要进行全面的功能测试和性能验证。

# 步骤1: 运行完整测试套件
./scripts/aws_ec2_test_deployment.sh

# 交互式配置:
是否继续执行? (y/N): y
请输入DatomsDBS项目的Git URL: https://github.com/yourusername/datomsDBS.git
是否保持EC2实例运行以便进一步测试? (y/N): y # 选择保留进行手动验证
是否删除SSH密钥对? (y/N): N # 保留密钥用于后续连接

# 预期结果:
# - 35分钟完成全面测试
# - 费用约$0.25
# - 生成详细测试报告
# - 实例保持运行供进一步测试

场景3: CI/CD管道集成

背景: 将AWS测试集成到CI/CD管道中进行自动化验证。

# CI脚本示例 (.github/workflows/aws-test.yml)
name: AWS EC2 Deployment Test
on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
aws-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v2
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ap-southeast-1

- name: Run AWS deployment test
run: |
chmod +x scripts/simple_aws_test.sh
echo "y" | ./scripts/simple_aws_test.sh ${{ github.server_url }}/${{ github.repository }}.git

场景4: 多区域部署测试

背景: 测试DatomsDBS在不同AWS区域的部署表现。

# 美国东部测试
AWS_REGION=us-east-1 ./scripts/simple_aws_test.sh https://github.com/yourusername/datomsDBS.git

# 欧洲西部测试
AWS_REGION=eu-west-1 ./scripts/simple_aws_test.sh https://github.com/yourusername/datomsDBS.git

# 亚太东南部测试 (默认)
./scripts/simple_aws_test.sh https://github.com/yourusername/datomsDBS.git

🔄 实际操作流程

完整测试流程示例

# 1. 环境准备
ubuntu@local:~$ aws configure list
Name Value Type Location
---- ----- ---- --------
profile <not set> None None
access_key ****************ABCD env
secret_key ****************XYZ env
region ap-southeast-1 env

# 2. 克隆项目
ubuntu@local:~$ git clone https://github.com/username/datomsDBS.git
ubuntu@local:~$ cd datomsDBS

# 3. 运行完整测试
ubuntu@local:~/datomsDBS$ ./scripts/aws_ec2_test_deployment.sh

==========================================
DatomsDBS AWS EC2 自动化测试
==========================================
此脚本将在AWS EC2上自动部署和测试DatomsDBS

配置信息:
区域: ap-southeast-1
实例类型: t3.xlarge
AMI: ami-047126e50991d067b

⚠️ 注意: 此脚本会产生AWS费用!

是否继续执行? (y/N): y

请输入DatomsDBS项目的Git URL: https://github.com/username/datomsDBS.git

[STEP] 检查前置条件...
[SUCCESS] AWS CLI 已安装: aws-cli/2.15.0
[SUCCESS] AWS 认证检查通过
[SUCCESS] 前置条件检查完成

[STEP] 检查或创建SSH密钥对...
[INFO] 创建新的SSH密钥对...
[SUCCESS] SSH密钥对创建完成: datomsdbs-test-key.pem

[STEP] 创建安全组...
[INFO] 使用VPC: vpc-12345678
[INFO] 创建新安全组...
[INFO] 配置安全组规则...
[SUCCESS] 安全组创建完成: sg-abcdef123

[STEP] 启动EC2实例...
[SUCCESS] EC2实例启动成功: i-1234567890abcdef0
[INFO] 等待实例启动中...
[SUCCESS] 实例运行中,公网IP: 54.169.123.45
[INFO] 等待SSH服务启动...
[SUCCESS] SSH连接就绪

[STEP] 开始部署和测试DatomsDBS...
[INFO] 上传设置脚本到EC2实例...
[INFO] 在EC2实例上执行部署和测试...

# ... 远程执行日志 ...

[REMOTE-INFO] 等待系统初始化完成...
[REMOTE-SUCCESS] 系统初始化完成
[REMOTE-INFO] 验证Node.js安装...
v18.19.0
9.2.0
[REMOTE-INFO] 克隆DatomsDBS项目...
[REMOTE-INFO] 运行DatomsDBS设置脚本...
[REMOTE-INFO] 启动DatomsDBS服务...
[REMOTE-INFO] 运行健康检查...
[REMOTE-INFO] 运行测试套件...
[REMOTE-SUCCESS] 单元测试通过
[REMOTE-SUCCESS] 集成测试通过
[REMOTE-INFO] 运行Agent测试...
[REMOTE-SUCCESS] Agent测试通过
[REMOTE-INFO] 生成测试报告...

DatomsDBS AWS EC2 测试报告
============================
测试时间: 2024-12-28 14:30:00
实例类型: t3.xlarge
AWS区域: ap-southeast-1
Node.js版本: v18.19.0
服务状态: {"status":"ok","version":"0.1.0","timestamp":"2024-12-28T06:30:00.000Z"}

测试结果:
- 服务启动: ✓
- 健康检查: ✓
- 单元测试: ✓
- 集成测试: ✓
- Agent测试: ✓

访问地址:
- 主服务: http://54.169.123.45:9000
- 管理界面: http://54.169.123.45:9000/admin

默认管理员账户: admin/admin123

[REMOTE-SUCCESS] 测试完成!查看报告: ~/test_report.txt

[INFO] 下载测试报告...
[SUCCESS] 部署和测试完成!

==========================================
🎉 DatomsDBS AWS EC2 测试部署完成!
==========================================

🌐 访问地址:
主服务: http://54.169.123.45:9000
管理界面: http://54.169.123.45:9000/admin

👤 默认管理员账户:
用户名: admin
密码: admin123

🔧 SSH连接命令:
ssh -i datomsdbs-test-key.pem ubuntu@54.169.123.45

是否保持EC2实例运行以便进一步测试? (y/N): y

[WARNING] 实例将继续运行,记得手动删除以避免产生费用!
[INFO] 删除命令: aws ec2 terminate-instances --instance-ids i-1234567890abcdef0 --region ap-southeast-1

# 4. 手动验证功能
ubuntu@local:~/datomsDBS$ ssh -i datomsdbs-test-key.pem ubuntu@54.169.123.45

ubuntu@ip-172-31-xx-xx:~$ curl http://localhost:9000/api/health
{"status":"ok","version":"0.1.0","timestamp":"2024-12-28T06:30:00.000Z"}

ubuntu@ip-172-31-xx-xx:~$ curl http://localhost:9000/api/admin/status
{"authenticated":false,"message":"Token required"}

# 5. 手动清理资源
ubuntu@local:~/datomsDBS$ aws ec2 terminate-instances --instance-ids i-1234567890abcdef0 --region ap-southeast-1

🚨 异常处理示例

网络连接问题

# 症状: SSH连接超时
[ERROR] SSH连接超时

# 解决步骤:
1. 检查安全组配置
aws ec2 describe-security-groups --group-ids sg-abcdef123 --region ap-southeast-1

2. 检查实例状态
aws ec2 describe-instance-status --instance-ids i-1234567890abcdef0 --region ap-southeast-1

3. 手动SSH测试
ssh -i datomsdbs-test-key.pem -v ubuntu@54.169.123.45

AWS权限问题

# 症状: 权限被拒绝
An error occurred (UnauthorizedOperation) when calling the RunInstances operation

# 解决步骤:
1. 检查当前用户
aws sts get-caller-identity

2. 检查权限策略
aws iam list-attached-user-policies --user-name your-username

3. 测试最小权限
aws ec2 describe-instances --region ap-southeast-1

实例启动失败

# 症状: 实例启动失败
[ERROR] 实例启动失败

# 调试步骤:
1. 检查配额限制
aws service-quotas get-service-quota --service-code ec2 --quota-code L-1216C47A

2. 尝试不同可用区
aws ec2 describe-availability-zones --region ap-southeast-1

3. 检查AMI可用性
aws ec2 describe-images --image-ids ami-047126e50991d067b --region ap-southeast-1

💡 高级使用技巧

批量测试多个配置

# 创建配置文件
cat > test-configs.txt << EOF
t3.medium,us-east-1
t3.large,eu-west-1
t3.xlarge,ap-southeast-1
EOF

# 批量执行测试
while IFS=',' read -r instance_type region; do
echo "Testing $instance_type in $region..."
sed -i "s/INSTANCE_TYPE=.*/INSTANCE_TYPE=\"$instance_type\"/" scripts/simple_aws_test.sh
sed -i "s/AWS_REGION=.*/AWS_REGION=\"$region\"/" scripts/simple_aws_test.sh
./scripts/simple_aws_test.sh https://github.com/username/datomsDBS.git
sleep 60 # 避免API限制
done < test-configs.txt

自定义测试脚本

# 创建自定义远程测试脚本
cat > custom-tests.sh << 'EOF'
#!/bin/bash
cd ~/datomsDBS

# 自定义健康检查
echo "运行扩展健康检查..."
curl -f http://localhost:9000/api/health
curl -f http://localhost:9000/api/version
curl -f http://localhost:9000/api/status

# 性能测试
echo "运行性能测试..."
ab -n 100 -c 10 http://localhost:9000/api/health

# 数据库连接测试
echo "测试数据库功能..."
curl -X POST http://localhost:9000/api/databases \
-H "Content-Type: application/json" \
-d '{"name":"test-db","type":"memory"}'
EOF

# 上传并执行自定义测试
scp -i datomsdbs-test-key.pem custom-tests.sh ubuntu@54.169.123.45:~/
ssh -i datomsdbs-test-key.pem ubuntu@54.169.123.45 "chmod +x ~/custom-tests.sh && ~/custom-tests.sh"

监控和日志收集

# 实时监控实例状态
watch -n 30 'aws ec2 describe-instances --instance-ids i-1234567890abcdef0 --region ap-southeast-1 --query "Reservations[0].Instances[0].[State.Name,PublicIpAddress]" --output table'

# 收集详细日志
ssh -i datomsdbs-test-key.pem ubuntu@54.169.123.45 << 'EOF'
# 系统日志
sudo journalctl -u cloud-init > ~/cloud-init.log

# 应用日志
cd ~/datomsDBS
npm run logs > ~/app.log 2>&1

# 系统状态
top -b -n 1 > ~/system-status.log
df -h >> ~/system-status.log
free -h >> ~/system-status.log
EOF

# 下载日志
scp -i datomsdbs-test-key.pem ubuntu@54.169.123.45:~/*.log ./logs/

📊 性能基准测试

负载测试示例

# 在EC2实例上安装Apache Bench
ssh -i datomsdbs-test-key.pem ubuntu@54.169.123.45 << 'EOF'
sudo apt-get update
sudo apt-get install -y apache2-utils

# 运行负载测试
cd ~/datomsDBS
echo "启动负载测试..."

# API健康检查负载测试
ab -n 1000 -c 50 http://localhost:9000/api/health

# 数据查询负载测试
ab -n 500 -c 25 -p data.json -T application/json http://localhost:9000/api/query
EOF

内存和CPU监控

# 持续监控资源使用
ssh -i datomsdbs-test-key.pem ubuntu@54.169.123.45 << 'EOF'
# 安装监控工具
sudo apt-get install -y htop iotop

# 运行监控脚本
cat > monitor.sh << 'MONITOR'
#!/bin/bash
while true; do
echo "=== $(date) ==="
echo "CPU使用率:"
top -b -n 1 | head -3
echo "内存使用:"
free -h
echo "磁盘IO:"
iostat 1 1
echo "网络连接:"
ss -tuln | grep 9000
echo "DatomsDBS进程:"
ps aux | grep node
echo "==================="
sleep 30
done
MONITOR

chmod +x monitor.sh
nohup ./monitor.sh > monitoring.log 2>&1 &
EOF

这个使用示例文档提供了完整的实际操作指南,包括:

  1. 真实场景示例 - 不同使用场景的具体操作
  2. 完整流程演示 - 从开始到结束的完整操作流程
  3. 异常处理 - 常见问题的诊断和解决方法
  4. 高级技巧 - 批量测试、自定义脚本、监控等进阶用法
  5. 性能测试 - 负载测试和资源监控示例

用户可以根据自己的需求选择合适的使用方式,从简单的快速验证到复杂的生产级测试都有详细的指导。