forked from aws-samples/aws-plugin-for-slurm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.yaml
463 lines (423 loc) · 16.5 KB
/
template.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
AWSTemplateFormatVersion: "2010-09-09"
Description: Deploy an example Slurm headnode with the AWS plugin for Slurm (version 2)
Parameters:
SlurmPackageUrl:
Type: String
Default: https://download.schedmd.com/slurm/slurm-21.08.6.tar.bz2
Description: URL to the Slurm installation package. The filename must be like slurm-*.tar.bz2
PluginPrefixUrl:
Type: String
Default: https://github.com/aws-samples/aws-plugin-for-slurm/raw/plugin-v2/
Description: Path to the plugin files
VpcId:
Type: AWS::EC2::VPC::Id
Description: VPC where the headnode and the compute nodes will be launched
Subnet1Id:
Type: AWS::EC2::Subnet::Id
Description: Subnet 1 in the VPC where the headnode and the compute nodes will be launched. Must be a public subnet or a private subnet with a default route to NAT
Subnet2Id:
Type: AWS::EC2::Subnet::Id
Description: Subnet 2 in the VPC where the compute nodes will be launched. Must be a public subnet or a private subnet with a default route to NAT
KeyPair:
Type: AWS::EC2::KeyPair::KeyName
Description: Key pair that will be used to launch instances
LatestAmiId:
Type: AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>
Default: /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2
HeadNodeInstanceType:
Type: String
Default: c5.large
Description: Instance type to use to launch the head node
ComputeNodeInstanceType:
Type: String
Default: c5.large
Description: Instance type to use to launch the compute node
ComputeNodeCPUs:
Type: Number
Default: 2
Description: Number of vCPUs for the compute node instance type
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
- Label:
default: Network
Parameters:
- VpcId
- Subnet1Id
- Subnet2Id
- Label:
default: Instances
Parameters:
- HeadNodeInstanceType
- ComputeNodeInstanceType
- ComputeNodeCPUs
- KeyPair
- LatestAmiId
- Label:
default: Packages
Parameters:
- SlurmPackageUrl
- PluginPrefixUrl
ParameterLabels:
VpcId:
default: VPC ID
Subnet1Id:
default: Subnet 1 ID
Subnet2Id:
default: Subnet 2 ID
HeadNodeInstanceType:
default: Headnode Instance Type
ComputeNodeInstanceType:
default: Compute Node Instance Type
ComputeNodeCPUs:
default: Compute Node vCPUs
KeyPair:
default: Key Pair
LatestAmiId:
default: Latest Amazon Linux 2 AMI ID
SlurmPackageUrl:
default: Slurm Package URL
PluginPrefixUrl:
default: Plugin URL Prefix
Resources:
SecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Allow SSH traffic from Internet and traffic between Slurm nodes
VpcId: !Ref VpcId
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: 0.0.0.0/0
SecurityGroupInbound:
Type: AWS::EC2::SecurityGroupIngress
Properties:
IpProtocol: -1
SourceSecurityGroupId: !GetAtt [ SecurityGroup, GroupId ]
GroupId: !GetAtt [ SecurityGroup, GroupId ]
ComputeNodeRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service: ec2.amazonaws.com
Action: sts:AssumeRole
Policies:
- PolicyName: root
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action: ec2:DescribeTags
Resource: '*'
ComputeNodeInstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Roles:
- !Ref ComputeNodeRole
HeadNodeRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service: ec2.amazonaws.com
Action: sts:AssumeRole
Policies:
- PolicyName: root
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- ec2:CreateFleet
- ec2:RunInstances
- ec2:TerminateInstances
- ec2:CreateTags
- ec2:DescribeInstances
- iam:CreateServiceLinkedRole
Resource: '*'
- Effect: Allow
Action: iam:PassRole
Resource: !GetAtt [ ComputeNodeRole, Arn ]
HeadNodeInstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Roles:
- !Ref HeadNodeRole
HeadNodeNetworkInterface:
Type: AWS::EC2::NetworkInterface
Properties:
GroupSet:
- !GetAtt [ SecurityGroup, GroupId ]
SubnetId: !Ref Subnet1Id
LaunchTemplate:
Type: AWS::EC2::LaunchTemplate
Properties:
LaunchTemplateData:
KeyName: !Ref KeyPair
ImageId: !Ref LatestAmiId
IamInstanceProfile:
Arn: !GetAtt [ ComputeNodeInstanceProfile, Arn ]
SecurityGroupIds:
- !GetAtt [ SecurityGroup, GroupId ]
UserData:
Fn::Base64:
!Sub |
#!/bin/bash -x
yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum install munge munge-libs munge-devel -y
echo "welcometoslurmamazonuserwelcometoslurmamazonuserwelcometoslurmamazonuser" | tee /etc/munge/munge.key
chown munge:munge /etc/munge/munge.key
chmod 600 /etc/munge/munge.key
chown -R munge /etc/munge/ /var/log/munge/
chmod 0700 /etc/munge/ /var/log/munge/
systemctl enable munge
systemctl start munge
sleep 15
yum install openssl openssl-devel pam-devel numactl numactl-devel hwloc hwloc-devel lua lua-devel readline-devel rrdtool-devel ncurses-devel man2html libibmad libibumad rpm-build -y
mkdir -p /nfs
mount -t nfs ${HeadNodeNetworkInterface.PrimaryPrivateIpAddress}:/nfs /nfs
export SLURM_HOME=/nfs/slurm
mkdir -p /var/spool/slurm
'cp' $SLURM_HOME/etc/slurm/slurmd.service /lib/systemd/system
systemctl enable slurmd.service
systemctl start slurmd.service
HeadNodeInstance:
Type: AWS::EC2::Instance
Properties:
ImageId: !Ref LatestAmiId
InstanceType: !Ref HeadNodeInstanceType
IamInstanceProfile: !Ref HeadNodeInstanceProfile
KeyName: !Ref KeyPair
NetworkInterfaces:
- NetworkInterfaceId: !Ref HeadNodeNetworkInterface
DeviceIndex: 0
Tags:
- Key: Name
Value: headnode
UserData:
Fn::Base64:
!Sub |
#!/bin/bash -x
# Install packages
yum update -y
yum install nfs-utils python2 python2-pip python3 python3-pip -y
yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum install munge munge-libs munge-devel openssl openssl-devel pam-devel numactl numactl-devel hwloc hwloc-devel lua lua-devel readline-devel rrdtool-devel ncurses-devel man2html libibmad libibumad rpm-build libyaml http-parser-devel json-c-devel perl-devel -y
yum groupinstall "Development Tools" -y
pip3 install boto3
pip3 install awscli
# Configure NFS share
mkdir -p /nfs
echo "/nfs *(rw,async,no_subtree_check,no_root_squash)" | tee /etc/exports
systemctl enable nfs
systemctl start nfs
exportfs -av
# Configure Munge
echo "welcometoslurmamazonuserwelcometoslurmamazonuserwelcometoslurmamazonuser" | tee /etc/munge/munge.key
chown munge:munge /etc/munge/munge.key
chmod 600 /etc/munge/munge.key
chown -R munge /etc/munge/ /var/log/munge/
chmod 0700 /etc/munge/ /var/log/munge/
systemctl enable munge
systemctl start munge
sleep 5
# Install Slurm
cd /home/ec2-user/
wget -q ${SlurmPackageUrl}
tar -xvf /home/ec2-user/slurm-*.tar.bz2 -C /home/ec2-user
cd /home/ec2-user/slurm-*
/home/ec2-user/slurm-*/configure --prefix=/nfs/slurm
make -j 4
make install
make install-contrib
sleep 5
export SLURM_HOME=/nfs/slurm
mkdir -p $SLURM_HOME/etc/slurm
'cp' /home/ec2-user/slurm-*/etc/* $SLURM_HOME/etc/slurm
# Install plugin
mkdir -p $SLURM_HOME/etc/aws
cd $SLURM_HOME/etc/aws
wget -q ${PluginPrefixUrl}common.py
wget -q ${PluginPrefixUrl}resume.py
wget -q ${PluginPrefixUrl}suspend.py
wget -q ${PluginPrefixUrl}generate_conf.py
wget -q ${PluginPrefixUrl}change_state.py
chmod +x *.py
cat > $SLURM_HOME/etc/aws/config.json <<EOF
{
"LogLevel": "INFO",
"LogFileName": "/var/log/slurm_plugin.log",
"SlurmBinPath": "$SLURM_HOME/bin",
"SlurmConf": {
"PrivateData": "CLOUD",
"ResumeProgram": "$SLURM_HOME/etc/aws/resume.py",
"SuspendProgram": "$SLURM_HOME/etc/aws/suspend.py",
"ResumeRate": 100,
"SuspendRate": 100,
"ResumeTimeout": 300,
"SuspendTime": 350,
"TreeWidth": 60000,
"GresTypes": "gpu"
}
}
EOF
cat > $SLURM_HOME/etc/aws/partitions.json <<'EOF'
{
"Partitions": [
{
"PartitionName": "aws",
"NodeGroups": [
{
"NodeGroupName": "node",
"MaxNodes": 100,
"Region": "${AWS::Region}",
"SlurmSpecifications": {
"CPUs": "${ComputeNodeCPUs}"
},
"PurchasingOption": "on-demand",
"OnDemandOptions": {
"AllocationStrategy": "lowest-price"
},
"LaunchTemplateSpecification": {
"LaunchTemplateId": "${LaunchTemplate}",
"Version": "$Latest"
},
"LaunchTemplateOverrides": [
{
"InstanceType": "${ComputeNodeInstanceType}"
}
],
"SubnetIds": [
"${Subnet1Id}",
"${Subnet2Id}"
]
}
],
"PartitionOptions": {
"Default": "Yes"
}
}
]
}
EOF
cat > $SLURM_HOME/etc/slurm.conf <<'EOF'
ClusterName=amazon
ControlMachine=@HEADNODE@
ControlAddr=${HeadNodeNetworkInterface.PrimaryPrivateIpAddress}
SlurmdUser=root
SlurmctldPort=6817
SlurmdPort=6818
AuthType=auth/munge
StateSaveLocation=/var/spool/slurm/ctld
SlurmdSpoolDir=/var/spool/slurm/d
SwitchType=switch/none
MpiDefault=none
SlurmctldPidFile=/var/run/slurmctld.pid
SlurmdPidFile=/var/run/slurmd.pid
ProctrackType=proctrack/pgid
ReturnToService=2
# TIMERS
SlurmctldTimeout=300
SlurmdTimeout=60
InactiveLimit=0
MinJobAge=300
KillWait=30
Waittime=0
# SCHEDULING
SchedulerType=sched/backfill
SelectType=select/cons_res
SelectTypeParameters=CR_Core
# LOGGING
SlurmctldDebug=3
SlurmctldLogFile=/var/log/slurmctld.log
SlurmdDebug=3
SlurmdLogFile=/var/log/slurmd.log
DebugFlags=NO_CONF_HASH
JobCompType=jobcomp/none
EOF
sed -i "s|@HEADNODE@|$HOSTNAME|g" $SLURM_HOME/etc/slurm.conf
# Configure the plugin
$SLURM_HOME/etc/aws/generate_conf.py
cat $SLURM_HOME/etc/aws/slurm.conf.aws >> $SLURM_HOME/etc/slurm.conf
cp $SLURM_HOME/etc/aws/gres.conf.aws $SLURM_HOME/etc/gres.conf
crontab -l > mycron
cat > mycron <<EOF
* * * * * $SLURM_HOME/etc/aws/change_state.py &>/dev/null
EOF
crontab mycron
rm mycron
# Get dynamically the node name when launching slurmd
cat > $SLURM_HOME/etc/aws/get_nodename <<'EOF'
instanceid=`/usr/bin/curl --fail -m 2 -s 169.254.169.254/latest/meta-data/instance-id`
if [[ ! -z "$instanceid" ]]; then
region=`/usr/bin/curl -s 169.254.169.254/latest/meta-data/placement/availability-zone`
region=${!region::-1}
hostname=`/usr/bin/aws ec2 describe-tags --filters "Name=resource-id,Values=$instanceid" "Name=key,Values=Name" --region $region --query "Tags[0].Value" --output=text`
fi
if [ ! -z "$hostname" -a "$hostname" != "None" ]; then
echo $hostname
else
echo `hostname`
fi
EOF
chmod +x $SLURM_HOME/etc/aws/get_nodename
cat > $SLURM_HOME/etc/slurm/slurmd.service <<EOF
[Unit]
Description=Slurm node daemon
After=munge.service network.target remote-fs.target
[Service]
Type=forking
EnvironmentFile=-/etc/sysconfig/slurmd
ExecStartPre=/bin/bash -c "/bin/systemctl set-environment SLURM_NODENAME=\$($SLURM_HOME/etc/aws/get_nodename)"
ExecStart=/nfs/slurm/sbin/slurmd -N \$SLURM_NODENAME \$SLURMD_OPTIONS
ExecReload=/bin/kill -HUP \$MAINPID
PIDFile=/var/run/slurmd.pid
KillMode=process
LimitNOFILE=131072
LimitMEMLOCK=infinity
LimitSTACK=infinity
Delegate=yes
[Install]
WantedBy=multi-user.target
EOF
cat > $SLURM_HOME/etc/slurm/slurmctld.service <<EOF
[Unit]
Description=Slurm controller daemon
After=network.target munge.service
ConditionPathExists=/nfs/slurm/etc/slurm.conf
[Service]
Type=forking
EnvironmentFile=-/etc/sysconfig/slurmctld
ExecStart=/nfs/slurm/sbin/slurmctld \$SLURMCTLD_OPTIONS
ExecReload=/bin/kill -HUP \$MAINPID
PIDFile=/var/run/slurmctld.pid
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
# Set environment variables
echo 'export SLURM_HOME=/nfs/slurm' | tee /etc/profile.d/slurm.sh
echo 'export SLURM_CONF=$SLURM_HOME/etc/slurm.conf' | tee -a /etc/profile.d/slurm.sh
echo 'export PATH=/nfs/slurm/bin:$PATH' | tee -a /etc/profile.d/slurm.sh
# Launch Slurmctld
mkdir -p /var/spool/slurm
'cp' /nfs/slurm/etc/slurm/slurmd.service /lib/systemd/system
'cp' /nfs/slurm/etc/slurm/slurmctld.service /lib/systemd/system
systemctl enable slurmctld
systemctl start slurmctld
/opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} --resource HeadNodeInstance --region ${AWS::Region}
CreationPolicy:
ResourceSignal:
Timeout: PT15M
Outputs:
HeadNodeId:
Description: Head node instance ID
Value: !Ref HeadNodeInstance