-
Notifications
You must be signed in to change notification settings - Fork 0
/
k8smaker_addworker_aws
executable file
·70 lines (58 loc) · 2.78 KB
/
k8smaker_addworker_aws
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
#!/bin/bash
if [[ $(whoami) = "root" ]]; then
echo "Must not run as root."
exit 1
fi
source ./k8smaker_config
if [[ $1 = '' ]]; then
echo "Pass IP or hostname of the new worker node as the first argument:"
echo "./k8smaker_addworker worker1.local"
exit 1
fi
mkdir -p ~/"$CLUSTERNAME"
ADDSCRIPT=~/"$CLUSTERNAME/k8smaker_addworker_$1"
KUBEADMSCRIPT=~/"$CLUSTERNAME/k8smaker_addworker_$1_join.yaml"
# this picks up your external facing IP address and hostname
echo "Worker is $SSHUSERNAME@$1"
echo "Script stored in $ADDSCRIPT"
# Delete an existing k8s install while leaving Docker alone. Doesn't get rid of all the extra things we did, but those aren't harmful to reinstalling.
echo "You have 5 seconds before this script remotely deletes all Kubernetes and Etcd data. Control-C To Abort!"
sleep 5
# configure us to be able to login remotely without a password
ssh-copy-id -f -i "$HOME/.ssh/$CLUSTERNAME.pub" "$SSHUSERNAME"@"$1"
# generate the script we want to run on the remote machine
echo "#!/bin/bash" >"$ADDSCRIPT"
cat ./k8smaker_config >>"$ADDSCRIPT"
echo '# AWS nodes must have $(hostname) == $(hostname -f) because somewhere in the middle of initialization, someone is using the short name, and it blows up.' >>"$ADDSCRIPT"
echo 'sudo hostnamectl set-hostname $(curl -s http://169.254.169.254/latest/meta-data/local-hostname)' >>"$ADDSCRIPT"
cat ./k8smaker_precondition >>"$ADDSCRIPT"
echo "sudo usermod -aG docker $SSHUSERNAME" >>"$ADDSCRIPT"
echo "mkdir -p ~/.kube" >>"$ADDSCRIPT"
DISCOVERYTOKEN=$(openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //')
echo "sudo -i kubeadm join --config=/home/$SSHUSERNAME/k8smaker_addworker_$1_join.yaml" >>"$ADDSCRIPT"
chmod a+x "$ADDSCRIPT"
# Generate the kubeadm join script and copy it to the remote machine
cat >"$KUBEADMSCRIPT" <<EOF
---
apiVersion: kubeadm.k8s.io/v1beta2
kind: JoinConfiguration
caCertPath: /etc/kubernetes/pki/ca.crt
discovery:
bootstrapToken:
token: ${BOOTSTRAPTOKEN}
apiServerEndpoint: "${CONTROLPLANEENDPOINT}:6443"
caCertHashes: ["sha256:${DISCOVERYTOKEN}"]
timeout: 5m0s
tlsBootstrapToken: ${BOOTSTRAPTOKEN}
nodeRegistration:
kubeletExtraArgs:
cloud-provider: aws
EOF
# copy the scripts to the new node and run it
scp -i "$HOME/.ssh/$CLUSTERNAME" "$ADDSCRIPT" "$SSHUSERNAME"@"$1":~/"k8smaker_addworker_$1"
scp -i "$HOME/.ssh/$CLUSTERNAME" "$KUBEADMSCRIPT" "$SSHUSERNAME"@"$1":~/"k8smaker_addworker_$1_join.yaml"
ssh -t -i "$HOME/.ssh/$CLUSTERNAME" "$SSHUSERNAME"@"$1" ~/"k8smaker_addworker_$1"
# Configure kubectl for each worker node
scp -i "$HOME/.ssh/$CLUSTERNAME" ~/.kube/config "$SSHUSERNAME"@"$1":~/.kube/config
# give the new node the role of 'worker'
kubectl label nodes "$1" --overwrite "node-role.kubernetes.io/worker="