数据处理任务量级巨大?构建预置容器镜像的Amazon EKS解决方案了解一下!

概述

Amazon EKS是一项托管的Kubernetes服务,且经过认证与Kubernetes一致,因此上游Kubernetes上运行的现有应用程序可与Amazon EKS兼容。出于其安全性、可靠性和可扩展性,在各个行业和领域,Amazon EKS是运行Kubernetes的最佳平台

我们看到在医药行业和自动驾驶领域利用Amazon EKS+Spot的组合方案去做海量的数据处理计算任务,他们的特点就是利用Amazon EKS集群Spot计算节点做任务处理,并且Amazon EKS的工作节点不需要常驻,每次调度的计算任务量级巨大。本文就是针对类似场景,提供通过预置任务容器Image到Amazon EKS Node Custom AMI中,利用预置容器Image的AMI在每次构建海量任务时创建工作节点,避免数千或数万任务调度时,相同的Image被重复拉取,尤其Image的Size很大的情况下会消耗额外的计算时长。

???? 想要了解更多亚马逊云科技最新技术发布和实践创新,敬请关注2021亚马逊云科技中国峰会!点击图片报名吧~更多精彩内容,敬请期待8.19-20北京9.15深圳分会吧!

架构示意图

此方案基于Amazon EKS Custom AMI的开源项目,通过修改定制脚本,拉取保存在ECR镜像仓库中的Image,利用Packer工具构建Amazon EKS Worker Nodes AMI,使用eklctl工具在Amazon EKS集群中创建预置了Image的Node Groups。

环境搭建

部署Amazon EKS Custom AMI构建环境

 1# 安装 HashiCorp Packer 工具
 2sudo yum install -y yum-utils
 3sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo
 4sudo yum -y install packer
 5
 6packer --version
 71.6.6
 8# 安装 Git、Docker、更新 AWS Cli 和安装 eksctl
 9sudo yum install git -y
10sudo yum install docker -y
11sudo systemctl start docker
12
13curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
14unzip awscliv2.zip
15sudo ./aws/install
16. ~/.bash_profile
17
18aws –version
19
20curl --silent --location "https://github.com/weaveworks/eksctl/releases/latest/download/eksctl_$(uname -s)_amd64.tar.gz" | tar xz -C /tmp
21sudo mv -v /tmp/eksctl /usr/local/bin
22# Clone Amazon Custom AMI git
23cd /home/ec2-user/
24git clone https://github.com/awslabs/amazon-eks-ami

首先需要准备一台EC2实例,具体流程可以参考如何创建用于Amazon EKS的自定义Amazon Linux AMI,以下会描述简要流程

  • 如何创建用于Amazon EKS的自定义Amazon Linux AMI

    https://aws.amazon.com/cn/premiumsupport/knowledge-center/eks-custom-linux-ami/

 1# 准备 Dockerfile,构建 Demo Image
 2$ touch Dockerfile
 3$ vim Dockerfile
 4添加如下内容:
 5FROM ubuntu:18.04
 6
 7# Install dependencies
 8RUN apt-get update && \
 9 apt-get -y install apache2
10
11# Install apache and write hello world message
12RUN echo 'Hello World!' > /var/www/html/index.html
13
14# Configure apache
15RUN echo '. /etc/apache2/envvars' > /root/run_apache.sh && \
16 echo 'mkdir -p /var/run/apache2' >> /root/run_apache.sh && \
17 echo 'mkdir -p /var/lock/apache2' >> /root/run_apache.sh && \ 
18 echo '/usr/sbin/apache2 -D FOREGROUND' >> /root/run_apache.sh && \ 
19 chmod 755 /root/run_apache.sh
20
21EXPOSE 80
22
23CMD /root/run_apache.sh
24# Build docker image
25$ docker build -t hello-world .

准备任务Image

 1# 登陆 ECR,创建 ECR 存储库, Push 镜像
 2$ aws ecr get-login-password --region cn-northwest-1 | sudo docker login --username AWS --password-stdin account_id.dkr.ecr.cn-northwest-1.amazonaws.com.cn
 3
 4$ aws ecr create-repository \
 5    --repository-name hello-world \
 6    --image-scanning-configuration scanOnPush=true \
 7--region cn-northwest-1
 8
 9$ docker tag hello-world:latest account_id.dkr.ecr.cn-northwest-1.amazonaws.com/hello-world:latest
10$ docker push account_id.dkr.ecr. cn-northwest-1.amazonaws.com.cn/hello-world:latest

Push任务Image到ECR

1# 编辑构建脚本
2$ cd amazon-eks-ami && vim scripts/install-worker.sh

修改Amazon EKS Custom AMI构建脚本

 1export ACCOUNT_ID=$(aws sts get-caller-identity --output text --query Account)
 2export AWS_REGION=$(curl -s 169.254.169.254/latest/dynamic/instance-identity/document | jq -r '.region')
 3
 4curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
 5unzip awscliv2.zip
 6sudo ./aws/install
 7
 8/usr/local/bin/aws --version
 9aws sts get-caller-identity
10
11/usr/local/bin/aws configure set default.region ${AWS_REGION}
12/usr/local/bin/aws configure get default.region
13
14/usr/local/bin/aws ecr get-login-password --region cn-northwest-1 | sudo docker login --username AWS --password-stdin [account id].dkr.ecr.cn-northwest-1.amazonaws.com.cn
15echo "start docker"
16sudo systemctl start docker
17echo "pull image"
18sudo docker pull [account id].dkr.ecr.cn-northwest-1.amazonaws.com.cn/hello-world:latest
19echo "docker stop"
20sudo systemctl stop docker

导入环境变量

1# 导入环境变量,Access Key 和 Secret Access Key
2export AWS_ACCESS_KEY_ID=AKIA37FFOR2TESTTEST
3export AWS_SECRET_ACCESS_KEY=lh+Yc+vfFOLIq1FTESTTEST TESTTEST TEST
4
5export ACCOUNT_ID=$(aws sts get-caller-identity --output text --query Account)
6export AWS_REGION=$(curl -s 169.254.169.254/latest/dynamic/instance-identity/document | jq -r '.region')

构建定制 AMI

1$ cd /home/ec2-user/amazon-eks-ami
2$ make 1.18

等待构建过程结束,记录AMI ID

或者在EC2 AMI控制台界面,利用AMI ID搜索可以查看详情

创建Amazon EKS的

Node Group

生成Node Group的yaml文件

 1# cluster-nodegroup.yaml 文件
 2$ vim cluster-nodegroup.yaml
 3apiVersion: eksctl.io/v1alpha5
 4kind: ClusterConfig
 5metadata:
 6    name: eks-test
 7    region: cn-northwest-1
 8nodeGroups:
 9    - name: test-ng-1
10      ami: ami-063b63a893ab19ebb # 自定义EKS AMI ID
11      minSize: 1
12      maxSize: 5
13      desiredCapacity: 1
14      instancesDistribution:
15        instanceTypes: ["m5.large", "m5d.large", "m4.large"]
16        onDemandBaseCapacity: 0
17        onDemandPercentageAboveBaseCapacity: 0
18        spotAllocationStrategy: capacity-optimized
19      ssh:
20        allow: true
21        publicKeyName: ec2_key_pair
22      labels:
23        lifecycle: Ec2Spot
24        intent: apps
25        aws.amazon.com/spot: "true"
26      taints:
27        spotInstance: "true:PreferNoSchedule"
28      tags:
29        k8s.io/cluster-autoscaler/node-template/label/lifecycle: Ec2Spot
30        k8s.io/cluster-autoscaler/node-template/label/intent: apps
31        k8s.io/cluster-autoscaler/node-template/label/aws.amazon.com/spot: "true"
32        k8s.io/cluster-autoscaler/node-template/taint/spotInstance: "true:PreferNoSchedule"
33      iam:
34        withAddonPolicies:
35          autoScaler: true
36          cloudWatch: true
37          albIngress: true

使用eksctl创建Node Group

1$ eksctl create nodegroup -f cluster-nodegroup.yaml

登陆Worker Node检查Image

我们可以通过ssh登陆到Worker Node上,检查预置的Image已经存在。

总结

利用Amazon EKS Custom AMI的能力,您可以通过预置自定义Image到Amazon EKS的Worker Node,实现在部署海量计算任务Pod的场景中,不需要重复并行的去ECR上拉取任务Image,达到快速启动Pod的目的。参考本文,您也可以按照自己的使用场景,通过定制install-worker.sh,实现各种各样的自定义能力,方便您更好的使用Amazon EKS服务去支持多样的工作负载。

参考资料

  • Amazon Elastic Kubernetes Service

    https://www.amazonaws.cn/eks/

  • 如何创建用于Amazon EKS的自定义Amazon Linux AMI?

    https://aws.amazon.com/cn/premiumsupport/knowledge-center/eks-custom-linux-ami/

  • 构建Amazon ECR镜像库

    https://docs.aws.amazon.com/zh_cn/AmazonECR/latest/userguide/getting-started-cli.html

  • 使用EKSCTL构建Amazon EKS集群

    https://eksctl.io/introduction/

  • Introducing launch template and custom AMI support in Amazon EKS Managed Node Groups

    https://aws.amazon.com/cn/blogs/containers/introducing-launch-template-and-custom-ami-support-in-amazon-eks-managed-node-groups/


本篇作者

唐健

亚马逊云科技解决方案架构师

负责基于的云计算方案的架构设计,同时致力于亚马逊云服务在移动应用与互联网行业的应用和推广。拥有多年移动互联网研发及技术团队管理经验,丰富的互联网应用架构项目经历。

听说,点完下面4个按钮

就不会碰到bug了!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值