View a markdown version of this page

容器镜像自定义 - AWS 上的分布式负载测试

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

容器镜像自定义

负载测试仪图片

此解决方案使用由 AWS 管理的公用 Amazon Elastic Container Registry (Amazon ECR) 图像存储库来存储用于运行已配置测试的映像。如果您想自定义容器镜像,可以重建镜像并将其推送到您自己的 AWS 账户中的 ECR 镜像存储库中。

如果要自定义此解决方案,则可以使用默认容器镜像,或者编辑此容器以满足您的需求。如果您自定义解决方案,请在构建自定义解决方案之前使用以下代码示例声明环境变量。

#!/bin/bash export REGION=aws-region-code # the AWS region to launch the solution (e.g. us-east-1) export BUCKET_PREFIX=my-bucket-name # prefix of the bucket name without the region code export BUCKET_NAME=$BUCKET_PREFIX-$REGION # full bucket name where the code will reside export SOLUTION_NAME=my-solution-name export VERSION=my-version # version number for the customized code export PUBLIC_ECR_REGISTRY=public.ecr.aws/awssolutions/distributed-load-testing-on-aws-load-tester # replace with the container registry and image if you want to use a different container image export PUBLIC_ECR_TAG=v3.1.0 # replace with the container image tag if you want to use a different container image

如果您选择自定义容器镜像,则可以将其托管在私有镜像存储库或您的 AWS 账户中的公共镜像存储库中。图像资源位于代码库中的deployment/ecr/distributed-load-testing-on-aws-load-tester目录中。

您可以构建映像并将其推送到主机目的地。

创建自己的映像后,可以在构建自定义解决方案之前声明以下环境变量。

#!/bin/bash export PUBLIC_ECR_REGISTRY=YOUR_ECR_REGISTRY_URI # e.g. YOUR_ACCOUNT_ID.dkr.ecr.us-east-1.amazonaws.com/YOUR_IMAGE_NAME export PUBLIC_ECR_TAG=YOUR_ECR_TAG # e.g. latest, v3.4.0

以下示例显示了容器文件。

FROM public.ecr.aws/amazonlinux/amazonlinux:2023-minimal RUN dnf update -y && \ dnf install -y python3.11 python3.11-pip java-21-amazon-corretto bc procps jq findutils unzip && \ dnf clean all ENV PIP_INSTALL="pip3.11 install --no-cache-dir" # install bzt RUN $PIP_INSTALL --upgrade bzt awscli setuptools==78.1.1 h11 urllib3==2.2.2 && \ $PIP_INSTALL --upgrade bzt COPY ./.bzt-rc /root/.bzt-rc RUN chmod 755 /root/.bzt-rc # install bzt tools RUN bzt -install-tools -o modules.install-checker.exclude=selenium,gatling,tsung,siege,ab,k6,external-results-loader,locust,junit,testng,rspec,mocha,nunit,xunit,wdio,robot,newman RUN rm -rf /root/.bzt/selenium-taurus RUN mkdir /bzt-configs /tmp/artifacts ADD ./load-test.sh /bzt-configs/ ADD ./*.jar /bzt-configs/ ADD ./*.py /bzt-configs/ RUN chmod 755 /bzt-configs/load-test.sh RUN chmod 755 /bzt-configs/ecslistener.py RUN chmod 755 /bzt-configs/ecscontroller.py RUN chmod 755 /bzt-configs/jar_updater.py RUN python3.11 /bzt-configs/jar_updater.py # Remove jar files from /tmp RUN rm -rf /tmp/jmeter-plugins-manager-1* && \ rm -rf /usr/local/lib/python3.11/site-packages/setuptools-65.5.0.dist-info && \ rm -rf /usr/local/lib/python3.11/site-packages/urllib3-1.26.17.dist-info # Add settings file to capture the output logs from bzt cli RUN mkdir -p /etc/bzt.d && echo '{"settings": {"artifacts-dir": "/tmp/artifacts"}}' > /etc/bzt.d/90-artifacts-dir.json WORKDIR /bzt-configs ENTRYPOINT ["./load-test.sh"]

除容器文件外,该目录还包含以下 bash 脚本,该脚本可在运行 Taurus/Blazemeter 程序之前从 Amazon S3 下载测试配置。

#!/bin/bash # set a uuid for the results xml file name in S3 UUID=$(cat /proc/sys/kernel/random/uuid) pypid=0 echo "S3_BUCKET:: ${S3_BUCKET}" echo "TEST_ID:: ${TEST_ID}" echo "TEST_TYPE:: ${TEST_TYPE}" echo "FILE_TYPE:: ${FILE_TYPE}" echo "PREFIX:: ${PREFIX}" echo "UUID:: ${UUID}" echo "LIVE_DATA_ENABLED:: ${LIVE_DATA_ENABLED}" echo "MAIN_STACK_REGION:: ${MAIN_STACK_REGION}" cat /proc/self/cgroup TASK_ID=$(grep -oE '[a-f0-9]{32}' /proc/self/cgroup | head -n 1) echo $TASK_ID sigterm_handler() { if [ $pypid -ne 0 ]; then echo "container received SIGTERM." kill -15 $pypid wait $pypid exit 143 #128 + 15 fi } trap 'sigterm_handler' SIGTERM echo "Download test scenario" aws s3 cp s3://$S3_BUCKET/test-scenarios/$TEST_ID-$AWS_REGION.json test.json --region $MAIN_STACK_REGION # Set the default log file values to jmeter LOG_FILE="jmeter.log" OUT_FILE="jmeter.out" ERR_FILE="jmeter.err" KPI_EXT="jtl" # download JMeter jmx file if [ "$TEST_TYPE" != "simple" ]; then # setting the log file values to the test type LOG_FILE="${TEST_TYPE}.log" OUT_FILE="${TEST_TYPE}.out" ERR_FILE="${TEST_TYPE}.err" # set variables based on TEST_TYPE if [ "$TEST_TYPE" == "jmeter" ]; then EXT="jmx" TYPE_NAME="JMeter" # Copy *.jar to JMeter library path. See the Taurus JMeter path: https://gettaurus.org/docs/JMeter/ JMETER_LIB_PATH=`find ~/.bzt/jmeter-taurus -type d -name "lib"` echo "cp $PWD/*.jar $JMETER_LIB_PATH" cp $PWD/*.jar $JMETER_LIB_PATH elif [ "$TEST_TYPE" == "k6" ]; then curl --output /tmp/artifacts/k6.rpm https://dl.k6.io/rpm/x86_64/k6-v0.58.0-amd64.rpm rpm -ivh /tmp/artifacts/k6.rpm dnf install -y k6 rm -rf /tmp/artifacts/k6.rpm EXT="js" KPI_EXT="csv" TYPE_NAME="K6" elif [ "$TEST_TYPE" == "locust" ]; then EXT="py" TYPE_NAME="Locust" fi if [ "$FILE_TYPE" != "zip" ]; then aws s3 cp s3://$S3_BUCKET/public/test-scenarios/$TEST_TYPE/$TEST_ID.$EXT ./ --region $MAIN_STACK_REGION else aws s3 cp s3://$S3_BUCKET/public/test-scenarios/$TEST_TYPE/$TEST_ID.zip ./ --region $MAIN_STACK_REGION unzip $TEST_ID.zip echo "UNZIPPED" ls -l # If zip and locust, make sure to pick locustfile if [ "$TEST_TYPE" != "locust" ]; then TEST_SCRIPT=$(find . -name "*.${EXT}" | head -n 1) else TEST_SCRIPT=$(find . -name "locustfile.py" | head -n 1) fi # only looks for the first test script file. TEST_SCRIPT=`find . -name "*.${EXT}" | head -n 1` echo $TEST_SCRIPT if [ -z "$TEST_SCRIPT" ]; then echo "There is no test script (.${EXT}) in the zip file." exit 1 fi sed -i -e "s|$TEST_ID.$EXT|$TEST_SCRIPT|g" test.json # copy bundled plugin jars to jmeter extension folder to make them available to jmeter BUNDLED_PLUGIN_DIR=`find $PWD -type d -name "plugins" | head -n 1` # attempt to copy only if a /plugins folder is present in upload if [ -z "$BUNDLED_PLUGIN_DIR" ]; then echo "skipping plugin installation (no /plugins folder in upload)" else # ensure the jmeter extensions folder exists JMETER_EXT_PATH=`find ~/.bzt/jmeter-taurus -type d -name "ext"` if [ -z "$JMETER_EXT_PATH" ]; then # fail fast - if plugins bundled they will be needed for the tests echo "jmeter extension path (~/.bzt/jmeter-taurus/**/ext) not found - cannot install bundled plugins" exit 1 fi cp -v $BUNDLED_PLUGIN_DIR/*.jar $JMETER_EXT_PATH fi fi fi #Download python script if [ -z "$IPNETWORK" ]; then python3.11 -u $SCRIPT $TIMEOUT & pypid=$! wait $pypid pypid=0 else aws s3 cp s3://$S3_BUCKET/Container_IPs/${TEST_ID}_IPHOSTS_${AWS_REGION}.txt ./ --region $MAIN_STACK_REGION export IPHOSTS=$(cat ${TEST_ID}_IPHOSTS_${AWS_REGION}.txt) python3.11 -u $SCRIPT $IPNETWORK $IPHOSTS fi echo "Running test" stdbuf -i0 -o0 -e0 bzt test.json -o modules.console.disable=true | stdbuf -i0 -o0 -e0 tee -a result.tmp | sed -u -e "s|^|$TEST_ID $LIVE_DATA_ENABLED |" CALCULATED_DURATION=`cat result.tmp | grep -m1 "Test duration" | awk -F ' ' '{ print $5 }' | awk -F ':' '{ print ($1 * 3600) + ($2 * 60) + $3 }'` # upload custom results to S3 if any # every file goes under $TEST_ID/$PREFIX/$UUID to distinguish the result correctly if [ "$TEST_TYPE" != "simple" ]; then if [ "$FILE_TYPE" != "zip" ]; then cat $TEST_ID.$EXT | grep filename > results.txt else cat $TEST_SCRIPT | grep filename > results.txt fi if [ -f results.txt ]; then sed -i -e 's/<stringProp name="filename">//g' results.txt sed -i -e 's/<\/stringProp>//g' results.txt sed -i -e 's/ //g' results.txt echo "Files to upload as results" cat results.txt files=(`cat results.txt`) extensions=() for f in "${files[@]}"; do ext="${f##*.}" if [[ ! " ${extensions[@]} " =~ " ${ext} " ]]; then extensions+=("$ext") fi done # Find all files in the current folder with the same extensions all_files=() for ext in "${extensions[@]}"; do for f in *."$ext"; do all_files+=("$f") done done for f in "${all_files[@]}"; do p="s3://$S3_BUCKET/results/$TEST_ID/${TYPE_NAME}_Result/$PREFIX/$UUID/$f" if [[ $f = /* ]]; then p="s3://$S3_BUCKET/results/$TEST_ID/${TYPE_NAME}_Result/$PREFIX/$UUID$f" fi echo "Uploading $p" aws s3 cp $f $p --region $MAIN_STACK_REGION done fi fi if [ -f /tmp/artifacts/results.xml ]; then # Insert the Task ID at the same level as <FinalStatus> curl -s $ECS_CONTAINER_METADATA_URI_V4/task Task_CPU=$(curl -s $ECS_CONTAINER_METADATA_URI_V4/task | jq '.Limits.CPU') Task_Memory=$(curl -s $ECS_CONTAINER_METADATA_URI_V4/task | jq '.Limits.Memory') START_TIME=$(curl -s "$ECS_CONTAINER_METADATA_URI_V4/task" | jq -r '.Containers[0].StartedAt') # Convert start time to seconds since epoch START_TIME_EPOCH=$(date -d "$START_TIME" +%s) # Calculate elapsed time in seconds CURRENT_TIME_EPOCH=$(date +%s) ECS_DURATION=$((CURRENT_TIME_EPOCH - START_TIME_EPOCH)) sed -i.bak 's/<\/FinalStatus>/<TaskId>'"$TASK_ID"'<\/TaskId><\/FinalStatus>/' /tmp/artifacts/results.xml sed -i 's/<\/FinalStatus>/<TaskCPU>'"$Task_CPU"'<\/TaskCPU><\/FinalStatus>/' /tmp/artifacts/results.xml sed -i 's/<\/FinalStatus>/<TaskMemory>'"$Task_Memory"'<\/TaskMemory><\/FinalStatus>/' /tmp/artifacts/results.xml sed -i 's/<\/FinalStatus>/<ECSDuration>'"$ECS_DURATION"'<\/ECSDuration><\/FinalStatus>/' /tmp/artifacts/results.xml echo "Validating Test Duration" TEST_DURATION=$(grep -E '<TestDuration>[0-9]+.[0-9]+</TestDuration>' /tmp/artifacts/results.xml | sed -e 's/<TestDuration>//' | sed -e 's/<\/TestDuration>//') if (( $(echo "$TEST_DURATION > $CALCULATED_DURATION" | bc -l) )); then echo "Updating test duration: $CALCULATED_DURATION s" sed -i.bak.td 's/<TestDuration>[0-9]*\.[0-9]*<\/TestDuration>/<TestDuration>'"$CALCULATED_DURATION"'<\/TestDuration>/' /tmp/artifacts/results.xml fi if [ "$TEST_TYPE" == "simple" ]; then TEST_TYPE="jmeter" fi echo "Uploading results, bzt log, and JMeter log, out, and err files" aws s3 cp /tmp/artifacts/results.xml s3://$S3_BUCKET/results/${TEST_ID}/${PREFIX}-${UUID}-${AWS_REGION}.xml --region $MAIN_STACK_REGION aws s3 cp /tmp/artifacts/bzt.log s3://$S3_BUCKET/results/${TEST_ID}/bzt-${PREFIX}-${UUID}-${AWS_REGION}.log --region $MAIN_STACK_REGION aws s3 cp /tmp/artifacts/$LOG_FILE s3://$S3_BUCKET/results/${TEST_ID}/${TEST_TYPE}-${PREFIX}-${UUID}-${AWS_REGION}.log --region $MAIN_STACK_REGION aws s3 cp /tmp/artifacts/$OUT_FILE s3://$S3_BUCKET/results/${TEST_ID}/${TEST_TYPE}-${PREFIX}-${UUID}-${AWS_REGION}.out --region $MAIN_STACK_REGION aws s3 cp /tmp/artifacts/$ERR_FILE s3://$S3_BUCKET/results/${TEST_ID}/${TEST_TYPE}-${PREFIX}-${UUID}-${AWS_REGION}.err --region $MAIN_STACK_REGION aws s3 cp /tmp/artifacts/kpi.${KPI_EXT} s3://$S3_BUCKET/results/${TEST_ID}/kpi-${PREFIX}-${UUID}-${AWS_REGION}.${KPI_EXT} --region $MAIN_STACK_REGION else echo "An error occurred while the test was running." fi

除了 Dockerfile 和 bash 脚本外,该目录中还包含两个 Python 脚本。每个任务都在 bash 脚本中运行一个 Python 脚本。工作任务运行ecslistener.py脚本,而领导任务将运行ecscontroller.py脚本。该ecslistener.py脚本在端口 50000 上创建一个套接字并等待消息。该ecscontroller.py脚本连接到套接字并将启动测试消息发送给工作器任务,这样它们就可以同时启动。

Web 控制台镜像(仅限 ALB + ECS Fargate 模板)

ALB + ECS Fargate 模板在 ECS Fargate 上将 Web 控制台作为容器运行。默认情况下,该解决方案会从 AWS 管理的公共 Amazon ECR 存储库中提取 Web 控制台映像。

在限制访问公共容器注册表的环境中(例如,无法访问互联网的 VPC 或拥有 ECR 公共访问策略的账户),您必须将公共映像镜像到私有 Amazon ECR 存储库,并在 Web 控制台映像 URI 参数中提供私有映像 URI。 CloudFormation 您也可以使用这种方法自定义 Web 控制台映像以满足您的需求。

将公共镜像镜像到私有 ECR 存储库

要将 Web 控制台镜像镜像到私有 ECR 存储库,请执行以下操作:

  1. 向公共 ECR 注册表进行身份验证:

    $ aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin public.ecr.aws
  2. 提取公共 Web 控制台镜像:

    $ docker pull public.ecr.aws/aws-solutions/distributed-load-testing-on-aws-web-console:<version>
  3. 在您的账户中创建私有 ECR 存储库(如果尚不存在):

    $ aws ecr create-repository --repository-name <your-repo-name> --region <region> 2>/dev/null || true
  4. 向您的私有 ECR 注册表进行身份验证:

    $ aws ecr get-login-password --region <region> | docker login --username AWS --password-stdin <account-id>.dkr.ecr.<region>.amazonaws.com
  5. 标记图像并将其推送到您的私有存储库:

    $ docker tag public.ecr.aws/aws-solutions/distributed-load-testing-on-aws-web-console:<version> \ <account-id>.dkr.ecr.<region>.amazonaws.com/<your-repo-name>:<version> $ docker push <account-id>.dkr.ecr.<region>.amazonaws.com/<your-repo-name>:<version>
  6. 启动 ALB + ECS Fargate 堆栈时,在 Web 控制台镜像 URI 参数中输入私有镜像 URI:

    <account-id>.dkr.ecr.<region>.amazonaws.com/<your-repo-name>:<version>

自定义 Web 控制台镜像

您也可以自定义 Web 控制台镜像。图像资源位于GitHub 存储库中的deployment/ecr/distributed-load-testing-on-aws-web-console目录中。编辑容器文件以满足您的需求,在本地构建映像,将其推送到您的私有 ECR 存储库,然后在 Web 控制台映像 URI 参数中提供 URI

Web 控制台容器使用 Nginx 为静态 Web 应用程序提供服务。启动时,入口点脚本会从 Amazon S3 下载 Web 控制台资产,并将其提取到 Nginx 文档根目录中。

以下示例显示了容器文件。

FROM public.ecr.aws/nginx/nginx:alpine # Install AWS CLI for S3 operations RUN apk upgrade --no-cache zlib libpng && \ apk add --no-cache aws-cli # Copy nginx configuration COPY nginx.conf /etc/nginx/nginx.conf # Copy entrypoint script COPY entrypoint.sh /usr/local/bin/entrypoint.sh RUN chmod +x /usr/local/bin/entrypoint.sh EXPOSE 80 ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]

入口点脚本从 S3 下载 Web 控制台资产并启动 Nginx。

#!/bin/sh set -e S3_URI="s3://${S3_BUCKET}/${S3_KEY}" echo "[$(date -Iseconds)] Downloading from $S3_URI" # AWS CLI has built-in retry with exponential backoff (standard mode) AWS_MAX_ATTEMPTS=5 aws s3 cp "$S3_URI" /tmp/web-app.zip echo "[$(date -Iseconds)] Download successful" # Extract and cleanup unzip -o /tmp/web-app.zip -d /usr/share/nginx/html/ rm -f /tmp/web-app.zip # Start Nginx exec nginx -g 'daemon off;'

Nginx 配置为单页应用程序 (SPA) 提供对 ALB 的运行状况检查支持、gzip 压缩、静态资产缓存和安全标头。

events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; gzip on; gzip_types text/plain text/css application/json application/javascript text/xml application/xml; server { listen 80; root /usr/share/nginx/html; index index.html; # Health check endpoint for ALB location /healthz { return 200 'OK'; } # SPA routing location / { try_files $uri $uri/ /index.html; } # Cache static assets for 1 year location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2)$ { expires 1y; add_header Cache-Control "public, immutable"; } # No cache for index.html (SPA entry point) location = /index.html { add_header Cache-Control "no-cache, no-store, must-revalidate"; } # No cache for runtime config location = /aws-exports.json { add_header Cache-Control "no-store, no-cache, must-revalidate"; } # Security headers add_header X-Frame-Options "SAMEORIGIN" always; add_header X-Content-Type-Options "nosniff" always; add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always; } }

有关更多信息,请参阅《亚马逊 ECR 用户指南》中的 “将 Docker 镜像推送到亚马逊 ECR 私有存储库”。