-
Notifications
You must be signed in to change notification settings - Fork 0
/
1-create-cluster.sh
executable file
·228 lines (183 loc) · 5.79 KB
/
1-create-cluster.sh
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
#!/bin/bash
# Prerequisites (macOS):
# - aws cli => to create AWS resources
# => pip install --upgrade --user awscli
# => aws configure
# - jq => to parse JSON results returned by the AWS CLI
# => brew install jq
# - chronic => to suppress output unless there's a non-zero exit code
# => brew install moreutils
# - kops => to create the actual kubernetes cluster
# => brew install kops
export PREFIX="chapati"
export URL="example.com"
export AWS_REGION="eu-central-1"
####################
# SPECIFY CLUSTER #
###################
printf "1️⃣ Please specify a cluster name (e.g. 'canary', or 'dev'): "
read CLUSTER_NAME
printf "\n"
if [ "$CLUSTER_NAME" != "canary" ] && [ "$CLUSTER_NAME" != "dev" ]
then
echo "Sorry, but I can only help you with the 'dev' and 'canary' clusters right now"
exit 1
fi
printf "\n"
################################
# Generate SSH key for cluster #
################################
echo "2️⃣ Let's generate a new SSH key for this cluster"
ssh-keygen -t rsa -f ${PREFIX}-${CLUSTER_NAME}
export PUBLIC_SSH_KEY=./${PREFIX}-${CLUSTER_NAME}.pub
printf "\n"
echo " 🔑 Awesome, now please put the private key into our 1password team vault"
printf " Type 'done' to confirm that you safely stored the private key in the team vault: "
read CONFIRM
printf "\n"
if [ "$CONFIRM" != "done" ]
then
echo "❗️ Ok, one more chance: Type 'done' to confirm you've stored the private ssh key in the 1password team vault"
read CONFIRM
fi
if [ "$CONFIRM" != "done" ]
then
echo "❌ Aborting, you've had your chance…"
exit 1
fi
echo " Cool, now let's go create a cluster!"
printf "\n"
#####################
# Create S3 Buckets #
#####################
echo "3️⃣ Create S3 buckets for kops and kubernetes config"
printf " a) Creating S3 bucket for kops config…"
KOPS_CONFIG_BUCKET=${PREFIX}.kops-${CLUSTER_NAME}.config
aws s3 ls | grep $KOPS_CONFIG_BUCKET > /dev/null
if [ $? -eq 0 ]
then
printf " ✅ Bucket already exists\n"
else
chronic aws s3api create-bucket \
--bucket $KOPS_CONFIG_BUCKET \
--region $AWS_REGION \
--create-bucket-configuration LocationConstraint=${AWS_REGION}
chronic aws s3api put-bucket-versioning \
--bucket $KOPS_CONFIG_BUCKET \
--versioning-configuration Status=Enabled
printf " ✅\n"
fi
printf " b) Creating S3 bucket for kubernetes config…"
K8_CONFIG_BUCKET=${PREFIX}.k8-${CLUSTER_NAME}.config
aws s3 ls | grep $K8_CONFIG_BUCKET > /dev/null
if [ $? -eq 0 ]
then
printf " ✅ Bucket already exists\n"
else
chronic aws s3api create-bucket \
--bucket $K8_CONFIG_BUCKET \
--region $AWS_REGION \
--create-bucket-configuration LocationConstraint=$AWS_REGION
chronic aws s3api put-bucket-versioning \
--bucket $K8_CONFIG_BUCKET \
--versioning-configuration Status=Enabled
printf " ✅\n"
fi
printf "\n"
########################
# Create IAM Resources #
########################
echo "4️⃣ Create IAM user and group for kops"
printf " a) Creating IAM group for kops…"
aws iam list-groups | grep kops > /dev/null
if [ $? -eq 0 ]
then
printf " ✅ IAM group 'kops' already exisst\n"
else
chronic aws iam create-group --group-name kops
printf " ✅\n"
fi
printf " b) Attaching IAM policies to kops usergroup…"
export policies="
AmazonEC2FullAccess
AmazonRoute53FullAccess
AmazonS3FullAccess
IAMFullAccess
AmazonVPCFullAccess"
NEW_POLICY_CREATED=false
for policy in $policies; do
ARN_EXISTS=$(aws iam list-attached-group-policies --group-name kops | jq --arg policy $policy '.AttachedPolicies[] | select(.PolicyName == $policy) | .PolicyName' > /dev/null)
if [ "$ARN_EXISTS" = "null" ]
then
aws iam attach-group-policy --policy-arn "arn:aws:iam::aws:policy/$policy" --group-name kops;
$NEW_POLICY_CREATED=true
fi
done
if [ "$NEW_POLICY_CREATED" = true ]
then
printf " ✅\n"
else
printf " ✅ Policies already exist\n"
fi
printf " c) Creating IAM user for kops…"
aws iam list-users | grep kops > /dev/null
if [ $? -eq 0 ]
then
printf " ✅ IAM user 'kops' already exists\n"
else
aws iam create-user --user-name kops
aws iam add-user-to-group --user-name kops --group-name kops
aws iam create-access-key --user-name kops
printf " ✅\n"
fi
printf "\n"
#######################
# Create kops cluster #
#######################
echo "5️⃣ Create new kops cluster"
export CLUSTER_URL="k8-$CLUSTER_NAME.$URL"
kops create cluster \
--state s3://${KOPS_CONFIG_BUCKET} \
--ssh-public-key $PUBLIC_SSH_KEY \
--cloud aws \
--zones ${AWS_REGION}a \
--topology private \
--networking calico \
--network-cidr=10.0.0.0/16 \
--bastion \
--master-size m3.medium \
--node-size m3.medium \
--node-count 3 \
--yes \
$CLUSTER_URL
printf "\n"
echo " ✅ Successfully kicked off cluster creation"
printf "o\n"
#####################
# Export kubeconfig #
#####################
echo "6️⃣ Export kubeconfig from new cluster"
# To export the kubectl configuration to a specific file we need to
# set the KUBECONFIG environment variable.
# see `kops export kubecfg --help` for further information
export KUBECONFIG=./kubeconfig
chronic kops export kubecfg $CLUSTER_URL --state=s3://${KOPS_CONFIG_BUCKET}
printf "\n"
######################
# Encrypt kubeconfig #
######################
echo "7️⃣ Encrypt kubeconfig with OpenSSL"
openssl enc -aes-256-cbc -salt -in kubeconfig -out kubeconfig.enc
printf "\n"
#####################
# Upload kubeconfig #
#####################
echo "8️⃣ Upload encrypted kubeconfig to S3"
chronic aws s3 cp kubeconfig.enc s3://${K8_CONFIG_BUCKET}/kubeconfig.enc
printf "\n\n"
#########
# Done! #
#########
echo "🏁 Finished! 🏁"
echo " It will take 10-15mins until your cluster is fully functional"
echo " You can see if the cluster is ready by running 'kops validate cluster --state s3://${KOPS_CONFIG_BUCKET} --name ${CLUSTER_URL}'"