说明
之前写过一篇利用 Amazon CloudWatch 监控 GPU文章,主要是使用 CloudWatch 监控 GPU 资源。但是部署起来较为麻烦,GPU机器量较少时可以手动部署下,量较多时也可以使用 ansible 类的自动化工具进行部署。
这里为了后期部署扩展方便采用 Docker 容器化方式进行部署。
方案架构
方案说明:这里将 Role 、Instances和gpu-mon.py 打包成镜像,运行容器镜像将指标推送至 AWS CloudWatch,设置报警规则和阈值,最后触发阈值使用飞书进行报警。
添加IAM权限
在AWS管理控制台,创建IAM使其具有实例推送数据到 CloudWatch 的权限,创建好IAM后,获取key_id
和secret_key
,这里使用AWS CLI
定义,如下:
在 AWS IAM 控制台创建gpu-mon-strategy
策略,内容如下:
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"cloudwatch:PutMetricData"
],
"Effect": "Allow",
"Resource": "*"
}
]
}
将上面创建的策略绑定到gpu-mon
这个账户【无则创建】,获取该账户key_id
和secret_key
,后面配置文件中将使用。
创建两个配置文件:
# cat config
[default]
region=eu-west-1
# cat credentials
# 即上面创建 gpu-mon 账户的key_id和secret_key
[default]
aws_access_key_id = AKIAWU76Xqwertyu
aws_secret_access_key = KPbQGyblMOaodasdfghjklmnbvcxz
下载 AWS CLI
详见在 Linux 上安装 AWS CLI 版本 2
这里我们只需要执行如下步骤就好:
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
容器化 Docker
Dockerfile文件
内容如下:
FROM nvidia/cuda:11.4.0-devel-ubuntu18.04
MAINTAINER wutengfei "tengfei.wu@xxxxx.com"
WORKDIR /opt
RUN apt-get update \
&& apt-get install python-pip -y \
&& apt-get install curl -y \
&& pip install nvidia-ml-py \
&& pip install jmespath -i http://pypi.douban.com/simple --trusted-host pypi.douban.com \
&& pip install python-dateutil -i http://pypi.douban.com/simple --trusted-host pypi.douban.com \
&& pip install s3transfer -i http://pypi.douban.com/simple --trusted-host pypi.douban.com \
&& pip install jmespath -i http://pypi.douban.com/simple --trusted-host pypi.douban.com \
&& pip install boto3 -i http://pypi.douban.com/simple --trusted-host pypi.douban.com \
&& DEBIAN_FRONTEND=noninteractive apt-get install tzdata -y \
&& apt-get clean \
&& apt-get -y autoremove \
&& rm -rf /var/lib/apt/lists/*
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime \
&& echo $TZ > /etc/timezone \
&& dpkg-reconfigure -f noninteractive tzdata \
&& mkdir -p /root/.aws \
&& mkdir -p /opt/aws \
&& mkdir -p /usr/local/aws-cli
COPY ["gpu-mon.py", "/opt/gpu-mon.py"]
COPY ["config", "/root/.aws/config"]
COPY ["credentials", "/root/.aws/credentials"]
COPY ["aws", "/opt/aws"]
RUN ./aws/install -i /usr/local/aws-cli -b /usr/local/bin
CMD nohup python gpu-mon.py
镜像打包和上传
# 最新镜像
docker build -t registry.cn-shanghai.aliyuncs.com/test/cloudwatch-gpu:202201261626 .
# 推送至阿里云
docker push registry.cn-shanghai.aliyuncs.com/test/cloudwatch-gpu:202201261626
镜像使用
# 使用主机网络
docker run -itd --name gpu-cloudwatch --net=host registry.cn-shanghai.aliyuncs.com/test/cloudwatch-gpu:202201261626
设置CloudWatch监控指标
登录 AWS 控制台,cloudwatch里即可看到新建的gpu监控指标: