Last active
December 17, 2021 07:02
-
-
Save superseb/922f3be6eacc89fcc31a45353dc5aaf5 to your computer and use it in GitHub Desktop.
Roll nodes in a Rancher 2 custom cluster using DigitalOcean droplets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Prereqs: | |
# * Rancher CLI configured to access the Rancher setup and an existing custom cluster with 3 nodes | |
# * doctl to create and delete droplets (https://github.com/digitalocean/doctl/releases) | |
# API token for Digital Ocean access | |
DIGITALOCEAN_TOKEN="" | |
# Prefix for newly created nodes | |
PREFIX="yourname" | |
# Existing custom cluster name used to roll nodes | |
CLUSTERNAME="custom" | |
# Change Docker version here if needed | |
IFS='' read -r -d '' TEMPLATE <<"EOF" | |
#!/bin/bash | |
curl https://releases.rancher.com/install-docker/20.10.sh | sh | |
EOF | |
# Check for doctl | |
if ! $(command -v doctl >/dev/null 2>&1); then | |
echo "doctl is not installed. Exiting..." | |
exit 1 | |
fi | |
## Check cluster state | |
# Should have 3 nodes | |
echo "Waiting for node count 3 in cluster" | |
until [ $(rancher node ls --format='{{.Name}}' | wc -l) -eq 3 ]; do | |
echo -n "." | |
done | |
echo "Found 3 nodes" | |
# Should be active | |
echo "Waiting for cluster $CLUSTERNAME to be active" | |
until rancher cluster ls | grep $CLUSTERNAME | grep -q active >/dev/null 2>&1; do | |
echo -n "." | |
sleep 10 | |
done | |
echo "Cluster $CLUSTERNAME is active" | |
## Add a new node first | |
# Create add command | |
JOINCOMMAND=$(rancher cluster add-node ${CLUSTERNAME} --etcd --controlplane --worker | tail -1) | |
# Create userdata | |
echo "${TEMPLATE}" > "userdata_${CLUSTERNAME}.txt" | |
echo $JOINCOMMAND >> "userdata_${CLUSTERNAME}.txt" | |
RSTRING=$(tr -dc 'a-zA-Z0-9' < /dev/urandom | fold -w 10 | head -n 1 | tr '[:upper:]' '[:lower:]') | |
echo "Creating node ${PREFIX}-etcd-rotate-${RSTRING}" | |
doctl --access-token $DIGITALOCEAN_TOKEN compute droplet create "${PREFIX}-etcd-rotate-${RSTRING}" --size s-2vcpu-4gb --image ubuntu-16-04-x64 --region ams3 --user-data-file "userdata_${CLUSTERNAME}.txt" --wait >/dev/null 2>&1 | |
echo "Waiting for node ${PREFIX}-etcd-rotate-${RSTRING} to register to cluster ${CLUSTERNAME}" | |
until rancher nodes | grep "${PREFIX}-etcd-rotate-${RSTRING}" >/dev/null 2>&1; do | |
echo -n "." | |
sleep 10 | |
done | |
echo "Node ${PREFIX}-etcd-rotate-${RSTRING} is registered" | |
echo "Waiting for cluster $CLUSTERNAME to be active" | |
until rancher cluster ls | grep $CLUSTERNAME | grep -q active >/dev/null 2>&1; do | |
echo -n "." | |
sleep 10 | |
done | |
echo "Cluster $CLUSTERNAME is active" | |
## Delete a node | |
# Select a node to delete | |
NODETODELETE=$(rancher node ls --format='{{.Name}}' | head -1) | |
echo "Selected ${NODETODELETE} to delete from cluster" | |
# Delete the node from Rancher | |
echo "Deleting ${NODETODELETE} from cluster" | |
rancher nodes delete $NODETODELETE | |
# Wait for cluster to be active | |
echo "Waiting for cluster $CLUSTERNAME to be active" | |
until rancher cluster ls | grep $CLUSTERNAME | grep -q active >/dev/null 2>&1; do | |
echo -n "." | |
sleep 20 | |
done | |
echo "Cluster $CLUSTERNAME is active" | |
echo "Deleting ${NODETODELETE} from DigitalOcean" | |
doctl --access-token $DIGITALOCEAN_TOKEN compute droplet delete $NODETODELETE --force >/dev/null 2>&1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment