-
Notifications
You must be signed in to change notification settings - Fork 9
/
kubernetes.yaml
1408 lines (1312 loc) · 46.3 KB
/
kubernetes.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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
AWSTemplateFormatVersion: '2010-09-09'
Parameters:
ClusterState:
Type: String
Description: |
etcd cluster state: If this option is set to existing, etcd
will attempt to join the existing cluster. If the wrong value is set, etcd
will attempt to start but fail safely.
Default: existing
KeyName:
Description: Existing EC2 KeyPair for SSH access.
Type: AWS::EC2::KeyPair::KeyName
ConstraintDescription: must be the name of an existing EC2 KeyPair.
Default: cfn-kubernetes
DomainName:
Type: String
ParentZoneID:
Type: String
ControllerSubdomain:
Description: Subdomain for controller ELB, relative to DomainName
Type: String
ControllerInstanceType:
Description: EC2 instance type for controller nodes.
Type: String
Default: m3.medium
ControllerPoolSize:
Type: Number
Default: 3
ControllerVolumeSize:
Type: Number
Default: 50
ControllerVolumeType:
Type: String
Default: gp2
KubeVersion:
Type: String
Default: v1.11.2
KubeadmFeatureGates:
Type: CommaDelimitedList
Default: ""
WorkerFeatureGates:
Type: String
Default: ""
WorkerCPUManagerPolicy:
Type: String
Default: "none"
KubeadmVersion:
Type: String
Default: v1.11.2
KubeadmURLRoot:
Type: String
Description: No trailing /
Default: https://storage.googleapis.com/kubernetes-release/release
KubeadmURLPath:
Type: String
Description: No leading /
Default: bin/linux/amd64/kubeadm
CriCtlVersion:
Type: String
Description: crictl version
Default: v1.11.1
VPCID:
Description: Existing VPC with attached internet gateway to use for this cluster.
Type: AWS::EC2::VPC::Id
InternetGateway:
Description: The InternetGateway attached to the VPC
Type: String
PublicSubnetCidrPrefix:
Type: String
PublicSubnetCidrASuffix:
Type: String
Default: 0.0/23
PublicSubnetCidrBSuffix:
Type: String
Default: 2.0/23
PublicSubnetCidrCSuffix:
Type: String
Default: 4.0/23
PrivateSubnetCidr:
Description: Range for all subnets in CIDR notation
Type: String
PrivateSubnetCount:
Description: Number of subnets available in SubnetCidr
Type: Number
Default: 8
PrivateSubnetBits:
Description: Subnet mask for private subnets in Fn::Cidr notation
Type: Number
Default: 11
SubnetAZA:
Type: String
Default: us-east-1b
SubnetAZB:
Type: String
Default: us-east-1c
SubnetAZC:
Type: String
Default: us-east-1d
assetBucket:
Type: String
Mappings:
Assets:
etcd:
# FIXME ExecStartPre=/bin/chown might not be needed since we bumped
# ignition version.
unit: |
[Unit]
Requires=coreos-metadata.service
After=coreos-metadata.service
[Service]
EnvironmentFile=/run/metadata/coreos
EnvironmentFile=/etc/etcd.env
Environment="RKT_RUN_ARGS=--volume etcd-ssl,kind=host,source=/etc/ssl/etcd \
--mount volume=etcd-ssl,target=/etc/ssl/etcd"
# member-add fails if its run again before etcd comes up, so we ignore
# the error for now. A failure to add a peer will result in etcd
# failing and we can monitor that.
ExecStartPre=-/etc/etcd-member-add "${COREOS_EC2_HOSTNAME}"
ExecStart=
ExecStart=/usr/lib/coreos/etcd-wrapper $ETCD_OPTS \
--name="${COREOS_EC2_HOSTNAME}" \
--listen-peer-urls="https://${COREOS_EC2_IPV4_LOCAL}:2380" \
--listen-client-urls="https://0.0.0.0:2379" \
--initial-advertise-peer-urls="https://${COREOS_EC2_HOSTNAME}:2380" \
--advertise-client-urls="https://${COREOS_EC2_HOSTNAME}:2379" \
--trusted-ca-file=/etc/ssl/etcd/ca.crt \
--cert-file=/etc/ssl/etcd/server.crt \
--key-file=/etc/ssl/etcd/server.key \
--peer-cert-file=/etc/ssl/etcd/peer.crt \
--peer-key-file=/etc/ssl/etcd/peer.key \
--peer-trusted-ca-file=/etc/ssl/etcd/ca.crt \
--client-cert-auth=true \
--peer-client-cert-auth=true
ExecStop=
ExecStop=/etc/etcd-member-remove "${COREOS_EC2_HOSTNAME}"
ExecStop=-/usr/bin/rkt stop --uuid-file=/var/lib/coreos/etcd-member-wrapper.uuid
etcdctlWrapper: |
#!/bin/bash
. /etc/etcd.env
etcdctl -D ${ETCD_DISCOVERY_SRV} \
--ca-file=/etc/ssl/etcd/ca.crt \
--cert-file=/etc/ssl/etcd/peer.crt \
--key-file=/etc/ssl/etcd/peer.key \
--no-sync "$@"
etcdMemberAdd: |
#!/bin/bash
set -euo pipefail
. /run/metadata/coreos
if [[ "$ETCD_INITIAL_CLUSTER_STATE" == "new" ]]; then
echo "New cluster, exiting"
exit 0
else
echo "Adding ourself to cluster"
/etc/etcdctl-wrapper member add "$COREOS_EC2_HOSTNAME" "https://$COREOS_EC2_HOSTNAME:2380"
fi
etcdMemberRemove: |
#!/bin/bash
set -euo pipefail
hostname=$1
ID=$(/etc/etcdctl-wrapper member list | awk -F: "/name=$hostname / { print \$1 }")
/etc/etcdctl-wrapper member remove "$ID"
kubelet:
unit: |
[Unit]
Description=Kubernetes Kubelet Server
Documentation=https://github.com/kubernetes/kubernetes
Requires=coreos-metadata.service
After=coreos-metadata.service
[Service]
EnvironmentFile=/run/metadata/coreos
EnvironmentFile=/etc/kubernetes.env
Environment="RKT_RUN_ARGS=--uuid-file-save=/var/run/kubelet-pod.uuid \
--volume dns,kind=host,source=/etc/resolv.conf \
--volume cni-opt,kind=host,source=/opt/cni \
--volume cni-etc,kind=host,source=/etc/cni \
--mount volume=dns,target=/etc/resolv.conf \
--mount volume=cni-opt,target=/opt/cni \
--mount volume=cni-etc,target=/etc/cni"
ExecStartPre=/bin/mkdir -p /opt/cni /etc/cni
ExecStartPre=-/usr/bin/rkt rm --uuid-file=/var/run/kubelet-pod.uui
ExecStart=/usr/lib/coreos/kubelet-wrapper \
--kubeconfig=/etc/kubernetes/admin.conf \
--pod-manifest-path=/etc/kubernetes/manifests \
--cloud-provider=aws \
--cloud-config=/etc/kubernetes/cloud-config \
--network-plugin=cni \
--pod-cidr=10.244.0.0/16 \
--cluster-dns=10.96.0.10 \
--node-ip=${COREOS_EC2_IPV4_LOCAL} \
--cluster-domain=${KUBELET_CLUSTER_DOMAIN} \
--allow-privileged
ExecStop=-/usr/bin/rkt stop --uuid-file=/var/run/kubelet-pod.uuid
[Install]
WantedBy=multi-user.target
kubeadm:
unit: |
[Unit]
After=etcd-member.service network-online.target
Description=Kubeadm init
Documentation=https://github.com/kubernetes/kubernetes
[Service]
Type=oneshot
EnvironmentFile=/run/metadata/coreos
EnvironmentFile=/etc/kubernetes.env
Environment="PATH=/bin:/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/opt/bin"
ExecStartPre=/bin/sh -c 'grep nodeName: /etc/kubernetes/controller.yaml \
&& exit 0; echo "nodeName: $COREOS_EC2_HOSTNAME" \
>> /etc/kubernetes/controller.yaml'
ExecStartPre=/bin/sh -c 'test -f /opt/bin/crictl && exit 0; \
curl -sfL "https://github.com/kubernetes-incubator/cri-tools/releases/download/${CRICTL_VERSION}/crictl-${CRICTL_VERSION}-linux-amd64.tar.gz" \
| tar -C /opt/bin/ -xzvf -'
ExecStart=/bin/sh -c 'while ! /opt/bin/kubeadm init --config /etc/kubernetes/controller.yaml \
--ignore-preflight-errors=KubeletVersion,Port-10250; do sleep 1; done'
ExecStartPost=/etc/etcd-signal-health
[Install]
WantedBy=multi-user.target
# Generate with:
# curl -L https://coreos.com/dist/aws/aws-stable.json \
# | jq 'to_entries|map(select(.key != "release_info"))|from_entries' \
# | json2yaml | sed 's/^/ /'
RegionToImageMap:
ap-northeast-1:
hvm: ami-55e41e2a
pv: ami-69e51f16
ap-northeast-2:
hvm: ami-c09338ae
pv: ami-aa903bc4
ap-south-1:
hvm: ami-84406eeb
pv: ami-ae5e70c1
ap-southeast-1:
hvm: ami-86bf81fa
pv: ami-19c1ff65
ap-southeast-2:
hvm: ami-f6e53794
pv: ami-b7e537d5
ca-central-1:
hvm: ami-6df57609
pv: ami-06fa7962
cn-north-1:
hvm: ami-555a8438
pv: ami-fc5c8291
cn-northwest-1:
hvm: ami-06a0b464
eu-central-1:
hvm: ami-4a83b6a1
pv: ami-8680b56d
eu-west-1:
hvm: ami-c70005be
pv: ami-2572775c
eu-west-2:
hvm: ami-177a9670
pv: ami-db7a96bc
eu-west-3:
hvm: ami-d240f1af
sa-east-1:
hvm: ami-a82079c4
pv: ami-9e267ff2
us-east-1:
hvm: ami-a32d46dc
pv: ami-6b3e5514
us-east-2:
hvm: ami-36497653
pv: ami-e4487781
us-gov-west-1:
hvm: ami-8ccc5ded
pv: ami-54cf5e35
us-west-1:
hvm: ami-6e647e0e
pv: ami-161a0076
us-west-2:
hvm: ami-4296ec3a
pv: ami-0e90ea76
Conditions:
isExisting: !Equals [ {"Ref": "ClusterState"}, "existing" ]
hasNoParentZone: !Equals [ {"Ref": "ParentZoneID"}, "" ]
hasParentZone: !Not [ { "Condition": "hasNoParentZone" } ]
Resources:
### Private Subnets ###
PrivateSubnetA:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPCID
CidrBlock: !Select [ 0, !Cidr [ !Ref PrivateSubnetCidr, !Ref PrivateSubnetCount, !Ref PrivateSubnetBits ] ]
AvailabilityZone: !Ref SubnetAZA
Tags:
- Key: KubernetesCluster
Value: !Ref DomainName
- Key: kubernetes.io/role/internal-elb
Value: 1
- Key: !Join [ "/", [ "kubernetes.io/cluster", !Ref DomainName ] ]
Value: owned
PrivateSubnetB:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPCID
CidrBlock: !Select [ 1, !Cidr [ !Ref PrivateSubnetCidr, !Ref PrivateSubnetCount, !Ref PrivateSubnetBits ] ]
AvailabilityZone: !Ref SubnetAZB
Tags:
- Key: KubernetesCluster
Value: !Ref DomainName
- Key: kubernetes.io/role/internal-elb
Value: 1
- Key: !Join [ "/", [ "kubernetes.io/cluster", !Ref DomainName ] ]
Value: owned
PrivateSubnetC:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPCID
CidrBlock: !Select [ 2, !Cidr [ !Ref PrivateSubnetCidr, !Ref PrivateSubnetCount, !Ref PrivateSubnetBits ] ]
AvailabilityZone: !Ref SubnetAZC
Tags:
- Key: KubernetesCluster
Value: !Ref DomainName
- Key: kubernetes.io/role/internal-elb
Value: 1
- Key: !Join [ "/", [ "kubernetes.io/cluster", !Ref DomainName ] ]
Value: owned
PrivateSubnetRouteTableA:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPCID
Tags:
- Key: KubernetesCluster
Value: !Ref DomainName
- Key: !Join [ "/", [ "kubernetes.io/cluster", !Ref DomainName ] ]
Value: owned
PrivateSubnetRouteTableB:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPCID
Tags:
- Key: KubernetesCluster
Value: !Ref DomainName
- Key: !Join [ "/", [ "kubernetes.io/cluster", !Ref DomainName ] ]
Value: owned
PrivateSubnetRouteTableC:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPCID
Tags:
- Key: KubernetesCluster
Value: !Ref DomainName
- Key: !Join [ "/", [ "kubernetes.io/cluster", !Ref DomainName ] ]
Value: owned
PrivateSubnetRouteA:
Type: AWS::EC2::Route
Properties:
RouteTableId: !Ref PrivateSubnetRouteTableA
DestinationCidrBlock: 0.0.0.0/0
NatGatewayId: !Ref NATGatewayA
PrivateSubnetRouteB:
Type: AWS::EC2::Route
Properties:
RouteTableId: !Ref PrivateSubnetRouteTableB
DestinationCidrBlock: 0.0.0.0/0
NatGatewayId: !Ref NATGatewayB
PrivateSubnetRouteC:
Type: AWS::EC2::Route
Properties:
RouteTableId: !Ref PrivateSubnetRouteTableC
DestinationCidrBlock: 0.0.0.0/0
NatGatewayId: !Ref NATGatewayC
PrivateSubnetRouteTableAssociationA:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref PrivateSubnetA
RouteTableId: !Ref PrivateSubnetRouteTableA
PrivateSubnetRouteTableAssociationB:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref PrivateSubnetB
RouteTableId: !Ref PrivateSubnetRouteTableB
PrivateSubnetRouteTableAssociationC:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref PrivateSubnetC
RouteTableId: !Ref PrivateSubnetRouteTableC
NATEIPA:
Type: AWS::EC2::EIP
Properties:
Domain: vpc
NATEIPB:
Type: AWS::EC2::EIP
Properties:
Domain: vpc
NATEIPC:
Type: AWS::EC2::EIP
Properties:
Domain: vpc
NATGatewayA:
Type: AWS::EC2::NatGateway
Properties:
AllocationId: !GetAtt NATEIPA.AllocationId
SubnetId: !Ref PublicSubnetA
NATGatewayB:
Type: AWS::EC2::NatGateway
Properties:
AllocationId: !GetAtt NATEIPB.AllocationId
SubnetId: !Ref PublicSubnetB
NATGatewayC:
Type: AWS::EC2::NatGateway
Properties:
AllocationId: !GetAtt NATEIPC.AllocationId
SubnetId: !Ref PublicSubnetA
### Public Subnets ###
PublicSubnetA:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPCID
CidrBlock: !Join [ "", [ !Ref PublicSubnetCidrPrefix, !Ref PublicSubnetCidrASuffix ] ]
AvailabilityZone: !Ref SubnetAZA
Tags:
- Key: KubernetesCluster
Value: !Ref DomainName
- Key: kubernetes.io/role/internal-elb
Value: 1
- Key: !Join [ "/", [ "kubernetes.io/cluster", !Ref DomainName ] ]
Value: owned
MapPublicIpOnLaunch: true
PublicSubnetB:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPCID
CidrBlock: !Join [ "", [ !Ref PublicSubnetCidrPrefix, !Ref PublicSubnetCidrBSuffix ] ]
AvailabilityZone: !Ref SubnetAZB
Tags:
- Key: KubernetesCluster
Value: !Ref DomainName
- Key: kubernetes.io/role/internal-elb
Value: 1
- Key: !Join [ "/", [ "kubernetes.io/cluster", !Ref DomainName ] ]
Value: owned
MapPublicIpOnLaunch: true
PublicSubnetC:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPCID
CidrBlock: !Join [ "", [ !Ref PublicSubnetCidrPrefix, !Ref PublicSubnetCidrCSuffix ] ]
AvailabilityZone: !Ref SubnetAZC
Tags:
- Key: KubernetesCluster
Value: !Ref DomainName
- Key: kubernetes.io/role/internal-elb
Value: 1
- Key: !Join [ "/", [ "kubernetes.io/cluster", !Ref DomainName ] ]
Value: owned
MapPublicIpOnLaunch: true
PublicSubnetRouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPCID
Tags:
- Key: KubernetesCluster
Value: !Ref DomainName
- Key: !Join [ "/", [ "kubernetes.io/cluster", !Ref DomainName ] ]
Value: owned
PublicSubnetRoute:
Type: AWS::EC2::Route
Properties:
RouteTableId: !Ref PublicSubnetRouteTable
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref InternetGateway
PublicSubnetRouteTableAssociationA:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref PublicSubnetA
RouteTableId: !Ref PublicSubnetRouteTable
PublicSubnetRouteTableAssociationB:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref PublicSubnetB
RouteTableId: !Ref PublicSubnetRouteTable
PublicSubnetRouteTableAssociationC:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref PublicSubnetC
RouteTableId: !Ref PublicSubnetRouteTable
BastionSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Enable SSH access via port 22
VpcId: !Ref VPCID
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: '22'
ToPort: '22'
CidrIp: '0.0.0.0/0'
BastionHost:
Type: AWS::EC2::Instance
Properties:
ImageId: !FindInMap [ RegionToImageMap, !Ref "AWS::Region", hvm ]
InstanceType: t2.micro
SubnetId: !Ref PublicSubnetA
SecurityGroupIds: [ !Ref BastionSecurityGroup ]
KeyName: !Ref KeyName
BastionRecordSet:
Type: "AWS::Route53::RecordSet"
Properties:
HostedZoneId: !Ref HostedZone
Name: !Join [ ".", [ "bastion", !Ref DomainName, "" ] ]
Type: A
TTL: 60
ResourceRecords:
- !GetAtt [ "BastionHost", "PublicIp" ]
AutoScalingNotificationTopic:
Type: "AWS::SNS::Topic"
Properties:
DisplayName: "Controller AutoScaling Events"
Subscription:
- Protocol: lambda
Endpoint: !GetAtt [ AutoScalingDNSUpdateLambda, Arn ]
ControllerAutoScalingGroup:
Type: AWS::AutoScaling::AutoScalingGroup
Properties:
VPCZoneIdentifier:
- !Ref PrivateSubnetA
- !Ref PrivateSubnetB
- !Ref PrivateSubnetC
LaunchConfigurationName:
Ref: ControllerLaunchConfiguration
DesiredCapacity: !Ref ControllerPoolSize
MaxSize: !Ref ControllerPoolSize
MinSize: !Ref ControllerPoolSize
LoadBalancerNames:
- !Ref ControllerELB
NotificationConfigurations:
- NotificationTypes:
- "autoscaling:EC2_INSTANCE_LAUNCH"
- "autoscaling:EC2_INSTANCE_TERMINATE"
TopicARN: !Ref AutoScalingNotificationTopic
Tags:
- Key: StackName
PropagateAtLaunch: true
Value: !Ref AWS::StackName
- Key: KubernetesCluster
PropagateAtLaunch: true
Value: !Ref DomainName
UpdatePolicy:
AutoScalingRollingUpdate:
MaxBatchSize: 1
MinInstancesInService: 2
WaitOnResourceSignals: true
PauseTime: PT1H
AutoScalingDNSUpdateLambda:
Type: "AWS::Lambda::Function"
Properties:
Role: !GetAtt [ AutoScalingDNSUpdateLambdaRole, Arn ]
Runtime: python3.6
Handler: index.lambda_handler
Code:
ZipFile: !Sub |
import boto3
import json
domain = "${DomainName}"
hostedZoneID= "${HostedZone}"
prefix = 'controller-'
ec2 = boto3.client('ec2')
route53 = boto3.client('route53')
autoscaling = boto3.client('autoscaling')
def lambda_handler(event, context):
print('Processing event: {}'.format(event))
record = event['Records'][0]
message = json.loads(record['Sns']['Message'])
asgis = autoscaling.describe_auto_scaling_groups(
AutoScalingGroupNames = [ message['AutoScalingGroupName'] ],
MaxRecords = 1
)['AutoScalingGroups'][0]['Instances']
print('Found instance ids: {}'.format(asgis))
reservations = ec2.describe_instances(
InstanceIds = list(map(lambda i: i['InstanceId'], asgis))
)['Reservations']
instances = [ y for x in list(map(lambda r: r['Instances'],
reservations)) for y in x ]
print('Found instances: {}'.format(instances))
changes = [{
'Action': 'UPSERT',
'ResourceRecordSet': {
'Type': 'SRV',
'Name': '_etcd-server-ssl._tcp.' + domain,
'TTL': 60,
'ResourceRecords': list(map(
lambda i: { 'Value': '0 0 2380 ' + i['PrivateDnsName'] },
instances
))
}
}, {
'Action': 'UPSERT',
'ResourceRecordSet': {
'Type': 'SRV',
'Name': '_etcd-client-ssl._tcp.' + domain,
'TTL': 60,
'ResourceRecords': list(map(
lambda i: { 'Value': '0 0 2379 ' + i['PrivateDnsName'] },
instances
))
}
}]
print('Applying these Changes: {}'.format(changes))
route53.change_resource_record_sets(HostedZoneId=hostedZoneID,
ChangeBatch={ 'Changes': changes }
)
LambdaInvokePermission:
Type: "AWS::Lambda::Permission"
Properties:
Action: "lambda:InvokeFunction"
Principal: sns.amazonaws.com
SourceArn: !Ref AutoScalingNotificationTopic
FunctionName: !GetAtt [ AutoScalingDNSUpdateLambda, Arn ]
AutoScalingDNSUpdateLambdaRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: "sts:AssumeRole"
Policies:
- PolicyName: "root"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- "ec2:DescribeNetworkInterfaces"
- "ec2:CreateNetworkInterface"
- "ec2:DeleteNetworkInterface"
- "ec2:DescribeInstances"
- "autoscaling:DescribeAutoScalingGroups"
Resource: "*"
- Effect: Allow
Action:
- "logs:CreateLogGroup"
- "logs:CreateLogStream"
- "logs:PutLogEvents"
Resource: "arn:aws:logs:*:*:*"
- Effect: Allow
Action:
- "route53:GetChange"
Resource:
- "*"
- Effect: Allow
Action:
- "route53:ChangeResourceRecordSets"
- "route53:ListResourceRecordSets"
Resource: !Sub
- "arn:aws:route53:::hostedzone/${HostedZone}"
- { HostedZone: !Ref HostedZone }
PolicyController:
Type: AWS::IAM::ManagedPolicy
Properties:
Description: "k8s-controller"
Path: /
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- "sts:AssumeRole"
Resource: "*"
- Effect: Allow
Action:
- "ec2:Describe*"
- "elasticloadbalancing:*"
- "autoscaling:DescribeAutoScalingGroups"
- "autoscaling:DescribeLaunchConfigurations"
- "autoscaling:DescribeTags"
- "ec2:CreateRoute"
- "ec2:CreateSecurityGroup"
- "ec2:CreateVolume"
- "ec2:ModifyVolume"
- "ec2:AttachVolume"
- "ec2:DetachVolume"
- "ec2:DeleteVolume" # FIXME: Once k8s tags the volumes,
- "ec2:CreateTags" # these should be scoped.
# Allow pulling from ECR (still requires permissive repo policy)
- "ecr:GetAuthorizationToken"
- "ecr:BatchCheckLayerAvailability"
- "ecr:GetDownloadUrlForLayer"
- "ecr:GetRepositoryPolicy"
- "ecr:DescribeRepositories"
- "ecr:ListImages"
- "ecr:BatchGetImage"
# For amazon-vpc-cni-k8s
- "ec2:CreateNetworkInterface"
- "ec2:AttachNetworkInterface"
- "ec2:DeleteNetworkInterface"
- "ec2:DetachNetworkInterface"
- "ec2:ModifyNetworkInterfaceAttribute"
- "ec2:AssignPrivateIpAddresses"
- "tag:TagResources"
Resource:
- "*"
- Effect: Allow
Action:
- "ec2:RevokeSecurityGroup*"
- "ec2:ModifyInstanceAttribute"
- "ec2:AuthorizeSecurityGroup*" # k8s needs to run this on the k8s managed and stack SG below
Resource:
- "*"
Condition:
StringEquals:
"ec2:ResourceTag/aws:cloudformation:stack-id": !Ref AWS::StackId
- Effect: Allow
Action:
- "ec2:RevokeSecurityGroup*"
- "ec2:ModifyInstanceAttribute"
- "ec2:AuthorizeSecurityGroup*" # ...for the workers too
Resource:
- "*"
Condition:
StringEquals:
"ec2:ResourceTag/aws:cloudformation:stack-id": !Ref WorkerPoolDefaultA
- Effect: Allow
Action:
- "ec2:RevokeSecurityGroup*"
- "ec2:ModifyInstanceAttribute"
- "ec2:AuthorizeSecurityGroup*" # ...for the workers too
Resource:
- "*"
Condition:
StringEquals:
"ec2:ResourceTag/aws:cloudformation:stack-id": !Ref WorkerPoolDefaultB
- Effect: Allow
Action:
- "ec2:RevokeSecurityGroup*"
- "ec2:ModifyInstanceAttribute"
- "ec2:AuthorizeSecurityGroup*" # ...for the workers too
Resource:
- "*"
Condition:
StringEquals:
"ec2:ResourceTag/aws:cloudformation:stack-id": !Ref WorkerPoolDefaultC
- Effect: Allow
Action:
- "ec2:AuthorizeSecurityGroup*"
- "ec2:DeleteSecurityGroup"
- "ec2:DeleteRoute"
- "ec2:RevokeSecurityGroup*"
Resource:
- "*"
Condition:
StringEquals:
"ec2:ResourceTag/KubernetesCluster": !Ref DomainName
- Effect: Allow
Action:
- "autoscaling:SetDesiredCapacity"
- "autoscaling:TerminateInstanceInAutoScalingGroup"
Resource:
- !Sub "arn:aws:autoscaling:${AWS::Region}:${AWS::AccountId}:autoScalingGroup:*:autoScalingGroupName/*"
- Effect: Allow
Action:
- "s3:Get*"
- "s3:List*"
- "s3:Head*"
Resource:
- !Sub
- "arn:aws:s3:::${assetBucket}/${domain}/*"
- assetBucket: !Ref assetBucket
domain: !Ref DomainName
- !Sub
- "arn:aws:s3:::${assetBucket}/${domain}"
- assetBucket: !Ref assetBucket
domain: !Ref DomainName
ControllerRole:
Type: AWS::IAM::Role
Properties:
ManagedPolicyArns:
- !Ref PolicyController
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service: ec2.amazonaws.com
Action: "sts:AssumeRole"
ControllerInstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Path: /
Roles:
- !Ref ControllerRole
ControllerLaunchConfiguration:
Type: AWS::AutoScaling::LaunchConfiguration
Properties:
KeyName: !Ref KeyName
ImageId: !FindInMap [ RegionToImageMap, !Ref "AWS::Region", hvm ]
InstanceType: !Ref ControllerInstanceType
IamInstanceProfile: !GetAtt [ ControllerInstanceProfile, Arn ]
SecurityGroups:
- !Ref ControllerSecurityGroup
BlockDeviceMappings:
- DeviceName: /dev/xvda
Ebs:
VolumeSize: !Ref ControllerVolumeSize
VolumeType: !Ref ControllerVolumeType
UserData:
Fn::Base64:
Fn::Sub:
- |
{
"ignition": {
"version": "2.1.0",
"config": {}
},
"storage": {
"files": [{
"filesystem": "root",
"path": "/etc/ssl/etcd/server.crt",
"user": { "name": "etcd" },
"mode": 420,
"contents": { "source": "s3://${assetBucket}/${domain}/etcd/server.pem" }
}, {
"filesystem": "root",
"path": "/etc/ssl/etcd/server.key",
"user": { "name": "etcd" },
"mode": 384,
"contents": { "source": "s3://${assetBucket}/${domain}/etcd/server-key.pem" }
}, {
"filesystem": "root",
"path": "/etc/ssl/etcd/peer.crt",
"user": { "name": "etcd" },
"mode": 420,
"contents": { "source": "s3://${assetBucket}/${domain}/etcd/peer.pem" }
}, {
"filesystem": "root",
"path": "/etc/ssl/etcd/peer.key",
"user": { "name": "etcd" },
"mode": 384,
"contents": { "source": "s3://${assetBucket}/${domain}/etcd/peer-key.pem" }
}, {
"filesystem": "root",
"path": "/etc/ssl/etcd/ca.crt",
"user": { "name": "etcd" },
"mode": 420,
"contents": { "source": "s3://${assetBucket}/${domain}/etcd/ca.pem" }
}, {
"filesystem": "root",
"path": "/etc/kubernetes/pki/ca.crt",
"mode": 420,
"contents": { "source": "s3://${assetBucket}/${domain}/kubeadm/ca.crt" }
}, {
"filesystem": "root",
"path": "/etc/kubernetes/pki/ca.key",
"mode": 384,
"contents": { "source": "s3://${assetBucket}/${domain}/kubeadm/ca.key" }
}, {
"filesystem": "root",
"path": "/etc/kubernetes/pki/sa.pub",
"mode": 420,
"contents": { "source": "s3://${assetBucket}/${domain}/kubeadm/sa.pub" }
}, {
"filesystem": "root",
"path": "/etc/kubernetes/pki/sa.key",
"mode": 384,
"contents": { "source": "s3://${assetBucket}/${domain}/kubeadm/sa.key" }
}, {
"filesystem": "root",
"path": "/etc/kubernetes/pki/front-proxy-ca.crt",
"mode": 420,
"contents": { "source": "s3://${assetBucket}/${domain}/kubeadm/front-proxy-ca.crt" }
}, {
"filesystem": "root",
"path": "/etc/kubernetes/pki/front-proxy-ca.key",
"mode": 384,
"contents": { "source": "s3://${assetBucket}/${domain}/kubeadm/front-proxy-ca.key" }
}, {
"filesystem": "root",
"path": "/etc/kubernetes/cloud-config",
"mode": 420,
"contents": { "source": "data:;base64,${cloudProviderConfig}" }
},
{
"filesystem": "root",
"path": "/etc/kubernetes/controller.yaml",
"mode": 420,
"contents": { "source": "data:;base64,${controllerConfig}" }
},
{
"filesystem": "root",
"path": "/etc/etcd.env",
"mode": 420,
"contents": { "source": "data:;base64,${etcdEnv}" }
}, {
"filesystem": "root",
"path": "/etc/etcdctl-wrapper",
"mode": 493,
"contents": { "source": "data:;base64,${etcdctlWrapper}" }
}, {
"filesystem": "root",
"path": "/etc/etcd-member-remove",
"mode": 493,
"contents": { "source": "data:;base64,${etcdMemberRemove}" }
}, {
"filesystem": "root",
"path": "/etc/etcd-member-add",
"mode": 493,
"contents": { "source": "data:;base64,${etcdMemberAdd}" }
}, {
"filesystem": "root",
"path": "/etc/etcd-signal-health",
"mode": 493,
"contents": { "source": "data:;base64,${etcdSignalHealth}" }
}, {
"filesystem": "root",
"path": "/opt/bin/kubeadm",
"mode": 493,
"contents": { "source": "${kubeadmURL}" }
}, {
"filesystem": "root",
"path": "/etc/kubernetes.env",
"mode": 420,