|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Prerequisites (macOS): |
| 4 | +# - aws cli => to create AWS resources |
| 5 | +# => pip install --upgrade --user awscli |
| 6 | +# => aws configure |
| 7 | + |
| 8 | +# - jq => to parse JSON results returned by the AWS CLI |
| 9 | +# => brew install jq |
| 10 | + |
| 11 | +# - chronic => to suppress output unless there's a non-zero exit code |
| 12 | +# => brew install moreutils |
| 13 | + |
| 14 | +# - kops => to create the actual kubernetes cluster |
| 15 | +# => brew install kops |
| 16 | + |
| 17 | +export PREFIX="chapati" |
| 18 | +export URL="example.com" |
| 19 | +export AWS_REGION="eu-central-1" |
| 20 | + |
| 21 | + |
| 22 | +#################### |
| 23 | +# SPECIFY CLUSTER # |
| 24 | +################### |
| 25 | + |
| 26 | +printf "1️⃣ Please specify a cluster name (e.g. 'canary', or 'dev'): " |
| 27 | +read CLUSTER_NAME |
| 28 | +printf "\n" |
| 29 | + |
| 30 | +if [ "$CLUSTER_NAME" != "canary" ] && [ "$CLUSTER_NAME" != "dev" ] |
| 31 | +then |
| 32 | + echo "Sorry, but I can only help you with the 'dev' and 'canary' clusters right now" |
| 33 | + exit 1 |
| 34 | +fi |
| 35 | +printf "\n" |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | +################################ |
| 40 | +# Generate SSH key for cluster # |
| 41 | +################################ |
| 42 | + |
| 43 | +echo "2️⃣ Let's generate a new SSH key for this cluster" |
| 44 | +ssh-keygen -t rsa -f ${PREFIX}-${CLUSTER_NAME} |
| 45 | +export PUBLIC_SSH_KEY=./${PREFIX}-${CLUSTER_NAME}.pub |
| 46 | +printf "\n" |
| 47 | +echo " 🔑 Awesome, now please put the private key into our 1password team vault" |
| 48 | +printf " Type 'done' to confirm that you safely stored the private key in the team vault: " |
| 49 | +read CONFIRM |
| 50 | +printf "\n" |
| 51 | + |
| 52 | +if [ "$CONFIRM" != "done" ] |
| 53 | +then |
| 54 | + echo "❗️ Ok, one more chance: Type 'done' to confirm you've stored the private ssh key in the 1password team vault" |
| 55 | + read CONFIRM |
| 56 | +fi |
| 57 | + |
| 58 | +if [ "$CONFIRM" != "done" ] |
| 59 | +then |
| 60 | + echo "❌ Aborting, you've had your chance…" |
| 61 | + exit 1 |
| 62 | +fi |
| 63 | + |
| 64 | +echo " Cool, now let's go create a cluster!" |
| 65 | +printf "\n" |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | +##################### |
| 70 | +# Create S3 Buckets # |
| 71 | +##################### |
| 72 | + |
| 73 | +echo "3️⃣ Create S3 buckets for kops and kubernetes config" |
| 74 | +printf " a) Creating S3 bucket for kops config…" |
| 75 | +KOPS_CONFIG_BUCKET=${PREFIX}.kops-${CLUSTER_NAME}.config |
| 76 | +aws s3 ls | grep $KOPS_CONFIG_BUCKET > /dev/null |
| 77 | +if [ $? -eq 0 ] |
| 78 | +then |
| 79 | + printf " ✅ Bucket already exists\n" |
| 80 | +else |
| 81 | + chronic aws s3api create-bucket \ |
| 82 | + --bucket $KOPS_CONFIG_BUCKET \ |
| 83 | + --region $AWS_REGION \ |
| 84 | + --create-bucket-configuration LocationConstraint=${AWS_REGION} |
| 85 | + |
| 86 | + chronic aws s3api put-bucket-versioning \ |
| 87 | + --bucket $KOPS_CONFIG_BUCKET \ |
| 88 | + --versioning-configuration Status=Enabled |
| 89 | + printf " ✅\n" |
| 90 | +fi |
| 91 | + |
| 92 | +printf " b) Creating S3 bucket for kubernetes config…" |
| 93 | +K8_CONFIG_BUCKET=${PREFIX}.k8-${CLUSTER_NAME}.config |
| 94 | +aws s3 ls | grep $K8_CONFIG_BUCKET > /dev/null |
| 95 | +if [ $? -eq 0 ] |
| 96 | +then |
| 97 | + printf " ✅ Bucket already exists\n" |
| 98 | +else |
| 99 | + chronic aws s3api create-bucket \ |
| 100 | + --bucket $K8_CONFIG_BUCKET \ |
| 101 | + --region $AWS_REGION \ |
| 102 | + --create-bucket-configuration LocationConstraint=$AWS_REGION |
| 103 | + |
| 104 | + chronic aws s3api put-bucket-versioning \ |
| 105 | + --bucket $K8_CONFIG_BUCKET \ |
| 106 | + --versioning-configuration Status=Enabled |
| 107 | + printf " ✅\n" |
| 108 | +fi |
| 109 | +printf "\n" |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | +######################## |
| 114 | +# Create IAM Resources # |
| 115 | +######################## |
| 116 | +echo "4️⃣ Create IAM user and group for kops" |
| 117 | +printf " a) Creating IAM group for kops…" |
| 118 | +aws iam list-groups | grep kops > /dev/null |
| 119 | +if [ $? -eq 0 ] |
| 120 | +then |
| 121 | + printf " ✅ IAM group 'kops' already exisst\n" |
| 122 | +else |
| 123 | + chronic aws iam create-group --group-name kops |
| 124 | + printf " ✅\n" |
| 125 | +fi |
| 126 | + |
| 127 | +printf " b) Attaching IAM policies to kops usergroup…" |
| 128 | +export policies=" |
| 129 | +AmazonEC2FullAccess |
| 130 | +AmazonRoute53FullAccess |
| 131 | +AmazonS3FullAccess |
| 132 | +IAMFullAccess |
| 133 | +AmazonVPCFullAccess" |
| 134 | + |
| 135 | +NEW_POLICY_CREATED=false |
| 136 | +for policy in $policies; do |
| 137 | + ARN_EXISTS=$(aws iam list-attached-group-policies --group-name kops | jq --arg policy $policy '.AttachedPolicies[] | select(.PolicyName == $policy) | .PolicyName' > /dev/null) |
| 138 | + if [ "$ARN_EXISTS" = "null" ] |
| 139 | + then |
| 140 | + aws iam attach-group-policy --policy-arn "arn:aws:iam::aws:policy/$policy" --group-name kops; |
| 141 | + $NEW_POLICY_CREATED=true |
| 142 | + fi |
| 143 | +done |
| 144 | +if [ "$NEW_POLICY_CREATED" = true ] |
| 145 | +then |
| 146 | + printf " ✅\n" |
| 147 | +else |
| 148 | + printf " ✅ Policies already exist\n" |
| 149 | +fi |
| 150 | + |
| 151 | +printf " c) Creating IAM user for kops…" |
| 152 | +aws iam list-users | grep kops > /dev/null |
| 153 | +if [ $? -eq 0 ] |
| 154 | +then |
| 155 | + printf " ✅ IAM user 'kops' already exists\n" |
| 156 | +else |
| 157 | + aws iam create-user --user-name kops |
| 158 | + aws iam add-user-to-group --user-name kops --group-name kops |
| 159 | + aws iam create-access-key --user-name kops |
| 160 | + printf " ✅\n" |
| 161 | +fi |
| 162 | +printf "\n" |
| 163 | + |
| 164 | + |
| 165 | + |
| 166 | +####################### |
| 167 | +# Create kops cluster # |
| 168 | +####################### |
| 169 | +echo "5️⃣ Create new kops cluster" |
| 170 | +export CLUSTER_URL="k8-$CLUSTER_NAME.$URL" |
| 171 | +kops create cluster \ |
| 172 | + --state s3://${KOPS_CONFIG_BUCKET} \ |
| 173 | + --ssh-public-key $PUBLIC_SSH_KEY \ |
| 174 | + --cloud aws \ |
| 175 | + --zones ${AWS_REGION}a \ |
| 176 | + --topology private \ |
| 177 | + --networking calico \ |
| 178 | + --network-cidr=10.0.0.0/16 \ |
| 179 | + --bastion \ |
| 180 | + --master-size m3.medium \ |
| 181 | + --node-size m3.medium \ |
| 182 | + --node-count 3 \ |
| 183 | + --yes \ |
| 184 | + $CLUSTER_URL |
| 185 | +printf "\n" |
| 186 | +echo " ✅ Successfully kicked off cluster creation" |
| 187 | +printf "o\n" |
| 188 | + |
| 189 | + |
| 190 | + |
| 191 | +##################### |
| 192 | +# Export kubeconfig # |
| 193 | +##################### |
| 194 | +echo "6️⃣ Export kubeconfig from new cluster" |
| 195 | +# To export the kubectl configuration to a specific file we need to |
| 196 | +# set the KUBECONFIG environment variable. |
| 197 | +# see `kops export kubecfg --help` for further information |
| 198 | +export KUBECONFIG=./kubeconfig |
| 199 | +chronic kops export kubecfg $CLUSTER_URL --state=s3://${KOPS_CONFIG_BUCKET} |
| 200 | +printf "\n" |
| 201 | + |
| 202 | + |
| 203 | + |
| 204 | +###################### |
| 205 | +# Encrypt kubeconfig # |
| 206 | +###################### |
| 207 | +echo "7️⃣ Encrypt kubeconfig with OpenSSL" |
| 208 | +openssl enc -aes-256-cbc -salt -in kubeconfig -out kubeconfig.enc |
| 209 | +printf "\n" |
| 210 | + |
| 211 | + |
| 212 | + |
| 213 | +##################### |
| 214 | +# Upload kubeconfig # |
| 215 | +##################### |
| 216 | +echo "8️⃣ Upload encrypted kubeconfig to S3" |
| 217 | +chronic aws s3 cp kubeconfig.enc s3://${K8_CONFIG_BUCKET}/kubeconfig.enc |
| 218 | +printf "\n\n" |
| 219 | + |
| 220 | + |
| 221 | + |
| 222 | +######### |
| 223 | +# Done! # |
| 224 | +######### |
| 225 | +echo "🏁 Finished! 🏁" |
| 226 | +echo " It will take 10-15mins until your cluster is fully functional" |
| 227 | +echo " You can see if the cluster is ready by running 'kops validate cluster --state s3://${KOPS_CONFIG_BUCKET} --name ${CLUSTER_URL}'" |
| 228 | + |
0 commit comments