mohamedbouziane/vegeta-loadtest-deploy
GitHub: mohamedbouziane/vegeta-loadtest-deploy
一个基于 AWS Fargate 与 Jenkins 的自动化负载测试部署方案,实现基础设施即代码与安全可控的性能压测。
Stars: 1 | Forks: 0
# vegeta-loadtest-deploy
包含 **配置** AWS 基础设施并以容器化负载测试方式 **运行** vegeta-loadtest 所需的一切内容——Terraform、Docker 镜像、配置覆盖层以及 Jenkins 流水线。
## 目录
- [概述](#overview)
- [仓库结构](#repository-structure)
- [先决条件](#prerequisites)
- [快速开始](#quick-start)
- [配置](#configuration)
- [全局配置](#global-config)
- [端点](#endpoints)
- [ID 文件](#ids-file)
- [证书与密钥](#certificates-and-keys)
- [Docker 镜像](#docker-images)
- [Terraform](#terraform)
- [在 ECS 上运行](#running-on-ecs)
- [Jenkins 流水线](#jenkins-pipeline)
- [本地开发](#local-development)
- [许可证](#license)
## 概述
```
Jenkins / CI
│
├─ 1. docker build ──► Deploy image (Terraform + AWS CLI)
│
├─ 2. TF Plan / Apply ──► ECS cluster + Fargate task definition
│
└─ 3. run_ecs.sh ──► aws ecs run-task
│
▼
┌────────────────────────┐
│ vegeta-loadtest │
│ /config/config.toml │
│ /config/endpoints.json│
│ /config/ids.txt │
└────────────────────────┘
```
## 仓库结构
```
.
├── Dockerfile # Deploy tooling image (Terraform, AWS CLI, yq)
├── Dockerfile.run # Runtime image — vegeta-loadtest binary + config
├── deploy.sh # Deployment orchestrator (TF_PLAN, TF_APPLY, ...)
├── bootstrap.sh # Local tooling bootstrap
├── run_ecs.sh # Trigger ECS Fargate task
├── ecs_overrides.json # ECS run-task command overrides
├── Makefile # Convenience targets
├── Jenkinsfile # CI/CD pipeline definition
├── config/
│ ├── overlays/
│ │ └── dev/
│ │ └── eu-central-1/
│ │ ├── config.toml # Global load test config
│ │ ├── automated-loadtests.json # Endpoint definitions
│ │ └── body/ # Optional POST body files
│ │ ├── body-mta.json
│ │ ├── body-changes.json
│ │ ├── body-changes-xl.json
│ │ └── body-changes-xxl.json
│ └── keys/
│ ├── body-create-license.json # License enrollment body (template)
│ ├── body-create-token.json # Token request body (template)
│ └── privateKey.example # Private key placeholder
├── checkov/
│ └── config.yaml # Checkov IaC security scan config
└── terraform/
├── ecs.tf # ECS cluster and Fargate task definition
├── iam.tf # IAM roles and policies
├── secrets.tf # Secrets Manager for TLS certs and keys
├── security_group.tf # VPC security groups
├── logs.tf # CloudWatch log group
├── variables.tf # Input variables
├── locals.tf # Local values and tags
├── outputs.tf # Output values (cluster ARN, task ARN, ...)
├── data.tf # Data sources
├── provider.tf # AWS provider configuration
└── backend.tf # Remote state backend (S3)
```
## 先决条件
| 工具 | 版本 |
|------|------|
| Docker | 20+ |
| AWS CLI | v2 |
| Terraform | 1.4+ |
| Make | 任意 |
| jq | 任意 |
对于在不使用 Docker 的情况下本地运行 Terraform,请同时安装:
- [Checkov](https://www.checkov.io/) — IaC 安全扫描器
- [yq](https://github.com/mikefarah/yq) — YAML 处理器
## 快速开始
```
# 1. 引导本地工具
./bootstrap.sh
# 2. 构建部署镜像
make docker.build
# 3. 对 Terraform 进行 Lint
make tf.lint
# 4. 部署 ECS 基础设施
docker run --rm \
-e ACTION=deploy \
-e ENVIRONMENT=dev \
-e REGION=eu-central-1 \
-e IMAGE_TAG=latest \
-v $PWD:/usr/local/src \
vegeta-loadtest-deploy TF_APPLY
# 5. 运行负载测试
VEGETA_DURATION=5m VEGETA_VEHICLES=50 VEGETA_RAMPUP=30s ECS_TASKS=1 \
./run_ecs.sh
```
## 配置
### 全局配置
位于 `config/overlays/dev/eu-central-1/config.toml`。编译到运行时镜像的 `/config/config.toml` 中。
关键设置:
| 键 | 默认值 | 描述 |
|----|--------|------|
| `Global.baseURL` | `https://api.eu-central-1.example.com` | 目标 API 基础 URL |
| `Global.logLevel` | `INFO` | 日志详细程度(`DEBUG`、`INFO`、`WARN`、`ERROR`) |
| `Global.workers` | `10` | 初始并发工作数 |
| `Global.maxWorkers` | `100` | 最大并发工作数 |
| `Global.statusIntervalDuration` | `5s` | 状态打印间隔 |
| `Global.HTTPClient.timeoutSec` | `20` | 每个请求的超时秒数 |
| `Global.Params.idsFile` | `./config/ids.txt` | 用于 `[[id]]` 占位符的 ID 文件路径 |
| `Global.Params.idsToRead` | `-1` | 要加载的 ID 数量(`-1` = 全部) |
### 端点
位于 `config/overlays/dev/eu-central-1/automated-loadtests.json`。每个条目定义一个要攻击的目标:
```
[
{
"name": "list-items",
"urlPath": "/api/{{version}}/items",
"method": "GET",
"ratePerVehPerSec": 0.5,
"headers": { "accept": "application/json" },
"expected": { "success": 0.999, "P50ms": 100 }
},
{
"name": "get-item",
"urlPath": "/api/{{version}}/items/[[id]]",
"method": "GET",
"ratePerVehPerSec": 1.0,
"headers": { "accept": "application/json" },
"expected": { "success": 0.999, "P50ms": 50 }
}
]
```
| 占位符 | 替换为 |
|--------|--------|
| `{{version}}` | 来自 `Global.Params.defaults` 的值 |
| `[[id]]` | 从 `Global.Params.idsFile` 读取的行 |
### ID 文件
每行一个 ID 的纯文本文件。将其放置在 `config/ids.txt` 中——它会被复制到运行时镜像的 `/config/ids.txt`。
```
item-001
item-002
item-003
```
### 证书与密钥
TLS 证书和许可私钥存储在 AWS Secrets Manager 中,并在运行时作为环境变量注入到容器(参见 `terraform/secrets.tf`)。
| 容器路径 | 描述 |
|----------|------|
| `/config/certs/certificate.pem` | mTLS 客户端证书 |
| `/config/certs/privkey.key` | mTLS 客户端私钥 |
| `/config/certs/ca.pem` | 根 CA 证书 |
| `/config/keys/privateKey` | 许可注册私钥 |
| `/config/keys/body-create-license.json` | 许可注册请求体 |
## Docker 镜像
### 部署镜像(`Dockerfile`)
用于 CI 以运行 Terraform。包含 Terraform `1.4.6`、AWS CLI v2、yq `4.34.1`、jq 以及 bash。
```
docker build -t vegeta-loadtest-deploy .
```
### 运行时镜像(`Dockerfile.run`)
在带有目标环境配置和区域覆盖的 `vegeta-loadtest` 二进制镜像基础上构建:
```
COPY config/overlays/${ENVIRONMENT_SHORT_NAME}/${REGION}/ /config
COPY config/ids.txt /config/ids.txt
COPY config/keys /config/keys
```
构建示例:
```
docker build \
--build-arg REGISTRY_HOSTNAME=ghcr.io/mohamedbouziane/ \
--build-arg IMAGE_TAG=latest \
--build-arg ENVIRONMENT_SHORT_NAME=dev \
--build-arg REGION=eu-central-1 \
-f Dockerfile.run \
-t vegeta-loadtest-run:latest .
```
## Terraform
`terraform/` 中的基础设施提供以下功能:
- **ECS 集群**——用于负载测试任务的 Fargate 集群
- **任务定义**——带有密钥注入的单个 vegeta-loadtest 容器
- **IAM 角色**——用于拉取镜像、读取密钥和写入日志的执行角色与任务角色
- **Secrets Manager**——mTLS 证书和私钥以密钥形式存储
- **CloudWatch 日志**——容器输出的日志组
- **安全组**——Fargate 任务的仅出站访问规则
```
# 计划(在部署容器内)
./deploy.sh TF_PLAN
# 应用
./deploy.sh TF_APPLY
# 销毁
ACTION=destroy ./deploy.sh TF_APPLY
```
## 在 ECS 上运行
在 `TF_APPLY` 成功后触发负载测试:
```
export VEGETA_DURATION=5m
export VEGETA_VEHICLES=100
export VEGETA_RAMPUP=30s
export VEGETA_EXTRA_PARAMS=""
export ECS_TASKS=1
./run_ecs.sh
```
`run_ecs.sh` 从 Terraform 输出读取集群 ARN、任务定义、子网和安全组,然后调用 `aws ecs run-task`。
## Jenkins 流水线
| 阶段 | 描述 |
|------|------|
| **构建部署镜像** | 构建 `Dockerfile` 部署工具镜像 |
| **初始化** | 运行 `bootstrap.sh` |
| **Terraform 计划** | 计划 Terraform 变更 |
| **Terraform 应用** | 应用 Terraform 变更(仅主分支 + ECS 平台) |
| **在 ECS 上运行负载测试** | 通过 `run_ecs.sh` 触发 Fargate 任务 |
**参数:**
| 参数 | 默认值 | 描述 |
|------|--------|------|
| `IMAGE_TAG` | `latest` | 要部署的 vegeta-loadtest 镜像标签 |
| `ENVIRONMENT` | `dev` | 目标环境 |
| `REGION` | `eu-central-1` | AWS 区域 |
| `PLATFORM` | `jenkins` | `jenkins`(仅计划)或 `ecs`(计划 + 应用 + 运行) |
| `ECS_TASKS` | `1` | 并行 Fargate 任务数量 |
| `ACTION` | `deploy` | `deploy` 或 `destroy` |
| `DURATION` | `5s` | 负载测试持续时间(例如 `5m`、`1h`) |
| `VEHICLES` | `1` | 虚拟用户数量 |
| `RAMPUP` | `1s` | 预热时间 |
| `EXTRA_PARAMS` | | 传递给容器的额外 `-e KEY=VALUE` 环境变量 |
## 本地开发
```
make help # List all available targets
make infra.lint # Lint Terraform (Checkov)
make tf.lint # Lint Terraform only
make tf.lock # Regenerate Terraform provider lock file
make docker.build # Build the deploy image locally
```
## 许可证
[Apache 2.0](LICENSE)
## 目录
- [概述](#overview)
- [仓库结构](#repository-structure)
- [先决条件](#prerequisites)
- [快速开始](#quick-start)
- [配置](#configuration)
- [全局配置(`config.toml`)](#global-config-configtoml)
- [端点(`automated-loadtests.json`)](#endpoints-automated-loadtestsjson)
- [ID 文件(`ids.txt`)](#ids-file-idstxt)
- [证书与密钥](#certificates--keys)
- [Docker 镜像](#docker-images)
- [Terraform](#terraform)
- [在 ECS 上运行](#running-on-ecs)
- [Jenkins 流水线](#jenkins-pipeline)
- [本地开发#local-development)
- [许可证](#license)
## 概述
```
┌─────────────────────────────────────────────────────────────┐
│ Jenkins / CI │
│ │
│ 1. Build deploy image ──► docker build (Dockerfile) │
│ 2. TF Plan / Apply ──► ECS cluster + task definition │
│ 3. Run load test ──► ECS Fargate task │
│ │ │ │
│ │ ▼ │
│ │ ┌─────────────────────┐ │
│ │ │ vegeta-loadtest │ │
│ │ │ (Dockerfile.run) │ │
│ │ │ /config/config.toml │ │
│ └────────────►│ /config/endpoints │ │
│ └─────────────────────┘ │
└─────────────────────────────────────────────────────────────┘
```
## 仓库结构
```
.
├── Dockerfile # Deploy tooling image (Terraform, AWS CLI, yq)
├── Dockerfile.run # Runtime image with vegeta-loadtest + config baked in
├── deploy.sh # Deployment orchestrator (TF_PLAN, TF_APPLY, ...)
├── bootstrap.sh # Local tooling bootstrap
├── run_ecs.sh # Trigger ECS task execution
├── ecs_overrides.json # ECS task override template
├── Makefile # Convenience targets (lint, plan, build)
├── Jenkinsfile # CI/CD pipeline definition
├── config/
│ ├── overlays/
│ │ └── dev/
│ │ └── eu-central-1/
│ │ ├── config.toml # Global load test configuration
│ │ ├── automated-loadtests.json # Endpoint definitions
│ │ └── body/ # Request body files
│ │ ├── body-mta.json
│ │ ├── body-changes.json
│ │ ├── body-changes-xl.json
│ │ └── body-changes-xxl.json
│ └── keys/
│ ├── body-create-license.json # License enrollment body (template)
│ ├── body-create-token.json # Token request body (template)
│ └── privateKey.example # Private key placeholder
├── checkov/
│ └── config.yaml # Checkov IaC security scan config
└── terraform/
├── ecs.tf # ECS cluster, task definition, service
├── iam.tf # IAM roles and policies
├── secrets.tf # Secrets Manager resources
├── security_group.tf # VPC security groups
├── logs.tf # CloudWatch log group
├── variables.tf # Input variables
├── locals.tf # Local values and tags
├── outputs.tf # Output values
├── data.tf # Data sources
├── provider.tf # AWS provider configuration
└── backend.tf # Remote state backend
```
## 先决条件
| 工具 | 版本 |
|------|------|
| Docker | 20+ |
| AWS CLI | v2 |
| Terraform | 1.4+ |
| Make | 任意 |
| jq | 任意 |
对于在不使用 Docker 的情况下本地运行 Terraform,请同时安装:
- [Checkov](https://www.checkov.io/) — IaC 安全扫描器
- [yq](https://github.com/mikefarah/yq) — YAML 处理器
## 快速开始
```
# 1. 引导本地工具
./bootstrap.sh
# 2. 构建部署镜像
make docker.build
# 3. 对 Terraform 和 Kubernetes 配置进行 Lint
make infra.lint
# 4. 部署基础设施(ECS 集群 + 任务定义)
ENVIRONMENT=dev REGION=eu-central-1 IMAGE_TAG=latest \
docker run --rm \
-e ACTION=deploy \
-e ENVIRONMENT=dev \
-e REGION=eu-central-1 \
-e IMAGE_TAG=latest \
-v $PWD:/usr/local/src \
vegeta-loadtest-deploy TF_APPLY
# 5. 在 ECS 上运行负载测试
VEGETA_DURATION=5m VEGETA_VEHICLES=50 VEGETA_RAMPUP=30s ECS_TASKS=1 \
./run_ecs.sh
```
## 配置
### 全局配置(`config.toml`)
位于 `config/overlays/dev/eu-central-1/config.toml`。编译到运行时镜像的 `/config/config.toml` 中。
关键设置:
| 键 | 默认值 | 描述 |
|----|--------|------|
| `Global.baseURL` | `https://api.eu-central-1.example.com` | 目标 API 基础 URL |
| `Global.logLevel` | `INFO` | 日志详细程度(`DEBUG`、`INFO`、`WARN`、`ERROR`) |
| `Global.workers` | `10` | 初始并发工作数 |
| `Global.maxWorkers` | `100` | 最大并发工作数 |
| `Global.statusIntervalDuration` | `5s` | 状态打印间隔 |
| `Global.HTTPClient.timeoutSec` | `20` | 每个请求的超时秒数 |
| `Global.HTTPClient.tlsSessionResume` | `true` | 启用 TLS 会话恢复 |
| `Global.Params.defaults` | `["version=v1",...]` | 默认占位符值 |
| `Global.Params.idsFile` | `./config/ids.txt` | 用于 `[[id]]` 占位符的 ID 文件路径 |
| `Global.Params.idsToRead` | `-1` | 要加载的 ID 数量(`-1` = 全部) |
### 端点(`automated-loadtests.json`)
定义要攻击的目标端点。位置:`config/overlays/dev/eu-central-1/automated-loadtests.json`。
```
[
{
"name": "list-items",
"urlPath": "/api/{{version}}/items",
"method": "GET",
"ratePerVehPerSec": 0.5,
"headers": { "accept": "application/json" },
"expected": { "success": 0.999, "P50ms": 100 }
},
{
"name": "get-item",
"urlPath": "/api/{{version}}/items/[[id]]",
"method": "GET",
"ratePerVehPerSec": 1.0,
"headers": { "accept": "application/json" },
"expected": { "success": 0.999, "P50ms": 50 }
}
]
```
**占位符:**
- `{{version}}` — 替换为来自 `Global.Params.defaults` 的值
- `[[id]]` — 替换为从 `Global.Params.idsFile` 读取的值
### ID 文件(`ids.txt`)
每行一个 ID 的纯文本文件。挂载到容器中的 `/config/ids.txt`。
```
item-001
item-002
item-003
```
### 证书与密钥
| 容器路径 | 描述 |
|----------|------|
| `/config/certs/certificate.pem` | mTLS 客户端证书 |
| `/config/certs/privkey.key` | mTLS 客户端私钥 |
| `/config/certs/ca.pem` | 根 CA 证书 |
| `/config/keys/privateKey` | 许可注册私钥 |
| `/config/keys/body-create-license.json` | 许可注册请求体 |
## Docker 镜像
### 部署镜像(`Dockerfile`)
用于 CI 运行 Terraform 命令。包含:
- Terraform `1.4.6`
- AWS CLI v2
- yq `4.34.1`
- bash、jq、curl、gnupg
```
docker build -t vegeta-loadtest-deploy .
```
### 运行时镜像(`Dockerfile.run`)
基于 `vegeta-loadtest` 二进制镜像构建,并注入目标环境与区域的覆盖配置:
```
COPY config/overlays/${ENVIRONMENT_SHORT_NAME}/${REGION}/ /config
COPY config/ids*.txt /config
COPY config/keys /config/keys
COPY config/endpoints*.json /config
```
构建示例:
```
docker build \
--build-arg REGISTRY_HOSTNAME=ghcr.io/mohamedbouziane/ \
--build-arg IMAGE_TAG=latest \
--build-arg ENVIRONMENT_SHORT_NAME=dev \
--build-arg REGION=eu-central-1 \
-f Dockerfile.run \
-t vegeta-loadtest-run:latest .
```
## Terraform
`terraform/` 中的基础设施管理如下内容:
- **ECS 集群**——用于运行负载测试任务的 Fargate 集群
- **任务定义**——带有注入密钥的 vegeta-loadtest 容器
- **IAM 角色**——具备最小权限策略的任务执行角色与任务角色
- **Secrets Manager**——以密钥形式存储 mTLS 证书和私钥
- **CloudWatch 日志**——任务输出的日志组
- **安全组**——Fargate 任务的仅出站规则
```
# 初始化(在部署容器内运行)
./deploy.sh INIT
# 计划
./deploy.sh TF_PLAN
# 应用
./deploy.sh TF_APPLY
# 销毁
ACTION=destroy ./deploy.sh TF_APPLY
```
## 在 ECS 上运行
基础设施配置完成后,运行负载测试:
```
export VEGETA_DURATION=5m
export VEGETA_VEHICLES=100
export VEGETA_RAMPUP=30s
export VEGETA_EXTRA_PARAMS=""
export ECS_TASKS=1
./run_ecs.sh
```
该脚本读取 Terraform 输出(集群 ARN、任务定义、子网、安全组)并调用 `aws ecs run-task`。
## Jenkins 流水线
`Jenkinsfile` 定义完整的 CI/CD 流水线:
| 阶段 | 描述 |
|------|------|
| **构建部署镜像** | 构建 `Dockerfile` 部署工具镜像 |
| **初始化** | 运行 `bootstrap.sh` |
| **Terraform 计划** | 计划 Terraform 变更 |
| **Terraform 应用** | 应用 Terraform 变更(仅主分支 + ECS 平台) |
| **在 ECS 上运行负载测试** | 通过 `run_ecs.sh` 触发 ECS 任务 |
**参数:**
| 参数 | 默认值 | 描述 |
|------|--------|------|
| `IMAGE_TAG` | `latest` | vegeta-loadtest 镜像标签 |
| `ENVIRONMENT` | `dev` | 目标环境 |
| `REGION` | `eu-central-1` | AWS 区域 |
| `PLATFORM` | `jenkins` | `jenkins` 或 `ecs` |
| `ECS_TASKS` | `1` | 并行 ECS 任务数量 |
| `ACTION` | `deploy` | `deploy` 或 `destroy` |
| `DURATION` | `5s` | 负载测试持续时间(例如 `5m`、`1h`) |
| `VEHICLES` | `1` | 虚拟用户数量 |
| `RAMPUP` | `1s` | 预热时间 |
| `EXTRA_PARAMS` | | 传递给容器的额外 `-e KEY=VALUE` 环境变量 |
## 本地开发
```
# 对所有基础设施进行 Lint
make infra.lint
# 仅对 Terraform 进行 Lint
make tf.lint
# 仅对 Kubernetes 清单进行 Lint
make k8s.lint
# 更新 Terraform 提供者锁定文件
make tf.lock
# 查看所有可用目标
make help
```
## 许可证
[Apache 2.0](LICENSE)
标签:AWS, AWS CLI, Docker, DPI, ECS, ECS, Fargate, IaC安全, Jenkins, Makefile, NIDS, SIEM, Terraform, Terraform, vegeta, YAML配置, 云端负载测试, 人体姿态估计, 安全扫描, 安全防御评估, 容器化, 开源框架, 性能测试, 持续交付, 持续集成, 时序注入, 特权提升, 脚本编排, 自动化部署, 请求拦截, 负载测试