/
k8s_workers.py
executable file
·331 lines (314 loc) · 14.4 KB
/
k8s_workers.py
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
#!/usr/bin/env python
"""Module with k8s nodegroup."""
from __future__ import print_function
import json
import os
import sys
from troposphere import (
Base64, Equals, If, NoValue, Not, Output, Sub, autoscaling, ec2
)
from troposphere.policies import AutoScalingRollingUpdate, UpdatePolicy
import botocore
from runway.cfngin.blueprints.base import Blueprint
from runway.cfngin.blueprints.variables.types import (
CFNNumber, CFNString, EC2ImageId, EC2SecurityGroupId, EC2SubnetIdList,
EC2VPCId
)
def get_valid_instance_types():
"""Return list of instance types."""
ec2_service_file = os.path.join(os.path.dirname(botocore.__file__),
'data',
'ec2',
'2016-11-15',
'service-2.json')
# This encoding needs to be explicitly called out as utf-8 on Windows
# (or it will try cp1252 instead)
openkwargs = {}
if sys.version_info[0] > 2:
openkwargs['encoding'] = 'utf-8'
with open(ec2_service_file, 'r', **openkwargs) as stream:
return json.load(stream)['shapes']['InstanceType']['enum']
class NodeGroup(Blueprint):
"""Stacker blueprint for creating k8s nodegroup."""
VARIABLES = {
'KeyName': {'type': CFNString, # string to allow it to be unset
'description': '(Optional) EC2 Key Pair to allow SSH '
'access to the instances',
'default': ''},
'NodeImageId': {'type': EC2ImageId,
'description': 'AMI id for the node instances.'},
'NodeInstanceType': {'type': CFNString,
'description': 'EC2 instance type for the node '
'instances',
'default': 't2.medium',
'allowed_values': get_valid_instance_types(),
'constraint_description': 'Must be a valid EC2 '
'instance type'},
'NodeInstanceProfile': {'type': CFNString,
'description': 'Instance profile for the '
'nodes.'},
'NodeAutoScalingGroupMinSize': {'type': CFNNumber,
'description': 'Minimum size of Node '
'Group ASG.',
'default': 1},
'NodeAutoScalingGroupMaxSize': {'type': CFNNumber,
'description': 'Maximum size of Node '
'Group ASG.',
'default': 3},
'NodeVolumeSize': {'type': CFNNumber,
'description': 'Node volume size',
'default': 20},
'ClusterName': {'type': CFNString,
'description': 'The cluster name provided when the '
'cluster was created. If it is '
'incorrect, nodes will not be able to '
'join the cluster.'},
'BootstrapArguments': {'type': CFNString,
'description': 'Arguments to pass to the '
'bootstrap script. See '
'files/bootstrap.sh in '
'https://github.com/awslabs/amazon-eks-ami', # noqa
'default': ''},
'NodeGroupName': {'type': CFNString,
'description': 'Unique identifier for the Node '
'Group.'},
'ClusterControlPlaneSecurityGroup': {'type': EC2SecurityGroupId,
'description': 'The security '
'group of the '
'cluster control '
'plane.'},
'VpcId': {'type': EC2VPCId,
'description': 'The VPC of the worker instances'},
'Subnets': {'type': EC2SubnetIdList,
'description': 'The subnets where workers can be '
'created.'},
'UseDesiredInstanceCount': {'type': CFNString,
'description': 'Should the initial '
'bootstrap instance count '
'be used?'},
'UseSpotInstances': {'type': CFNString,
'default': 'no',
'allowed_values': ['yes', 'no'],
'description': 'Should the instances be '
'configured as spot instances?'},
'SpotBidPrice': {'type': CFNString,
'description': 'Bid price for Spot instance workers '
'(only relevant if UseSpotInstances '
'is set to "yes")',
'default': '0.68'},
}
def create_template(self):
"""Create template (main function called by Stacker)."""
template = self.template
variables = self.get_variables()
template.add_version('2010-09-09')
template.add_description('Kubernetes workers via EKS - V1.0.0 '
'- compatible with amazon-eks-node-v23+')
# Metadata
template.add_metadata({
'AWS::CloudFormation::Interface': {
'ParameterGroups': [
{'Label': {'default': 'EKS Cluster'},
'Parameters': [variables[i].name
for i
in ['ClusterName',
'ClusterControlPlaneSecurityGroup']]},
{'Label': {'default': 'Worker Node Configuration'},
'Parameters': [variables[i].name
for i
in ['NodeGroupName',
'NodeAutoScalingGroupMinSize',
'NodeAutoScalingGroupMaxSize',
'UseDesiredInstanceCount',
'NodeInstanceType',
'NodeInstanceProfile',
'NodeImageId',
'NodeVolumeSize',
'KeyName',
'UseSpotInstances',
'SpotBidPrice',
'BootstrapArguments']]},
{'Label': {'default': 'Worker Network Configuration'},
'Parameters': [variables[i].name
for i
in ['VpcId', 'Subnets']]}
]
}
})
# Conditions
template.add_condition(
'SetSpotPrice',
Equals(variables['UseSpotInstances'].ref, 'yes')
)
template.add_condition(
'DesiredInstanceCountSpecified',
Equals(variables['UseDesiredInstanceCount'].ref, 'true')
)
template.add_condition(
'KeyNameSpecified',
Not(Equals(variables['KeyName'].ref, ''))
)
# Resources
nodesecuritygroup = template.add_resource(
ec2.SecurityGroup(
'NodeSecurityGroup',
GroupDescription='Security group for all nodes in the cluster',
Tags=[
{'Key': Sub('kubernetes.io/cluster/${ClusterName}'),
'Value': 'owned'},
],
VpcId=variables['VpcId'].ref
)
)
template.add_output(
Output(
'NodeSecurityGroup',
Description='Security group for all nodes in the cluster',
Value=nodesecuritygroup.ref()
)
)
template.add_resource(
ec2.SecurityGroupIngress(
'NodeSecurityGroupIngress',
Description='Allow node to communicate with each other',
GroupId=nodesecuritygroup.ref(),
SourceSecurityGroupId=nodesecuritygroup.ref(),
IpProtocol='-1',
FromPort=0,
ToPort=65535
)
)
template.add_resource(
ec2.SecurityGroupIngress(
'NodeSecurityGroupFromControlPlaneIngress',
Description='Allow worker Kubelets and pods to receive '
'communication from the cluster control plane',
GroupId=nodesecuritygroup.ref(),
SourceSecurityGroupId=variables['ClusterControlPlaneSecurityGroup'].ref, # noqa
IpProtocol='tcp',
FromPort=1025,
ToPort=65535
)
)
template.add_resource(
ec2.SecurityGroupEgress(
'ControlPlaneEgressToNodeSecurityGroup',
Description='Allow the cluster control plane to communicate '
'with worker Kubelet and pods',
GroupId=variables['ClusterControlPlaneSecurityGroup'].ref,
DestinationSecurityGroupId=nodesecuritygroup.ref(),
IpProtocol='tcp',
FromPort=1025,
ToPort=65535
)
)
template.add_resource(
ec2.SecurityGroupIngress(
'NodeSecurityGroupFromControlPlaneOn443Ingress',
Description='Allow pods running extension API servers on port '
'443 to receive communication from cluster '
'control plane',
GroupId=nodesecuritygroup.ref(),
SourceSecurityGroupId=variables['ClusterControlPlaneSecurityGroup'].ref, # noqa
IpProtocol='tcp',
FromPort=443,
ToPort=443
)
)
template.add_resource(
ec2.SecurityGroupEgress(
'ControlPlaneEgressToNodeSecurityGroupOn443',
Description='Allow the cluster control plane to communicate '
'with pods running extension API servers on port '
'443',
GroupId=variables['ClusterControlPlaneSecurityGroup'].ref,
DestinationSecurityGroupId=nodesecuritygroup.ref(),
IpProtocol='tcp',
FromPort=443,
ToPort=443
)
)
template.add_resource(
ec2.SecurityGroupIngress(
'ClusterControlPlaneSecurityGroupIngress',
Description='Allow pods to communicate with the cluster API '
'Server',
GroupId=variables['ClusterControlPlaneSecurityGroup'].ref,
SourceSecurityGroupId=nodesecuritygroup.ref(),
IpProtocol='tcp',
FromPort=443,
ToPort=443
)
)
nodelaunchconfig = template.add_resource(
autoscaling.LaunchConfiguration(
'NodeLaunchConfig',
AssociatePublicIpAddress=True,
IamInstanceProfile=variables['NodeInstanceProfile'].ref,
ImageId=variables['NodeImageId'].ref,
InstanceType=variables['NodeInstanceType'].ref,
KeyName=If(
'KeyNameSpecified',
variables['KeyName'].ref,
NoValue
),
SecurityGroups=[nodesecuritygroup.ref()],
SpotPrice=If('SetSpotPrice',
variables['SpotBidPrice'].ref,
NoValue),
BlockDeviceMappings=[autoscaling.BlockDeviceMapping(
DeviceName='/dev/xvda',
Ebs=autoscaling.EBSBlockDevice(
VolumeSize=variables['NodeVolumeSize'].ref,
VolumeType='gp2',
DeleteOnTermination=True
)
)],
UserData=Base64(
Sub('\n'.join([
'#!/bin/bash',
'set -o xtrace',
'/etc/eks/bootstrap.sh ${ClusterName} ${BootstrapArguments}', # noqa
'/opt/aws/bin/cfn-signal --exit-code $? \\',
'--stack ${AWS::StackName} \\',
'--resource NodeGroup \\',
'--region ${AWS::Region}'
]))
)
)
)
template.add_resource(
autoscaling.AutoScalingGroup(
'NodeGroup',
DesiredCapacity=If(
'DesiredInstanceCountSpecified',
variables['NodeAutoScalingGroupMaxSize'].ref,
NoValue
),
LaunchConfigurationName=nodelaunchconfig.ref(),
MinSize=variables['NodeAutoScalingGroupMinSize'].ref,
MaxSize=variables['NodeAutoScalingGroupMaxSize'].ref,
VPCZoneIdentifier=variables['Subnets'].ref,
Tags=[
autoscaling.Tag(
'Name',
Sub('${ClusterName}-${NodeGroupName}-Node'),
True),
autoscaling.Tag(
Sub('kubernetes.io/cluster/${ClusterName}'),
'owned',
True)
],
UpdatePolicy=UpdatePolicy(
AutoScalingRollingUpdate=AutoScalingRollingUpdate(
MinInstancesInService='1',
MaxBatchSize='1'
)
)
)
)
# Helper section to enable easy blueprint -> template generation
# (just run `python <thisfile>` to output the json)
if __name__ == "__main__":
from runway.cfngin.context import Context
print(NodeGroup('test', Context({"namespace": "test"}), None).to_json())