Skip to content

Instantly share code, notes, and snippets.

@drnic
Last active April 16, 2023 09:22
Show Gist options
  • Save drnic/9c5f2d58865c8595fe3aa77672f3ebc8 to your computer and use it in GitHub Desktop.
Save drnic/9c5f2d58865c8595fe3aa77672f3ebc8 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -u
up() {
INSTANCE_TYPE=${INSTANCE_TYPE:-n1-standard-1}
(
set -x
gcloud compute instances create k3s-1 \
--machine-type "$INSTANCE_TYPE" \
--tags k3s,k3s-master
gcloud compute instances create k3s-2 k3s-3 \
--machine-type "$INSTANCE_TYPE" \
--tags k3s,k3s-worker
gcloud compute config-ssh
)
primary_server_ip=$(gcloud compute instances list \
--filter=tags.items=k3s-master \
--format="get(networkInterfaces[0].accessConfigs.natIP)")
(
set -x
k3sup install --ip "${primary_server_ip}" --context k3s --ssh-key ~/.ssh/google_compute_engine --user $(whoami)
gcloud compute firewall-rules create k3s --allow=tcp:6443 --target-tags=k3s
gcloud compute instances list \
--filter=tags.items=k3s-worker \
--format="get(networkInterfaces[0].accessConfigs.natIP)" | \
xargs -L1 k3sup join \
--server-ip $primary_server_ip \
--ssh-key ~/.ssh/google_compute_engine \
--user $(whoami) \
--ip
)
export KUBECONFIG=`pwd`/kubeconfig
kubectl get nodes
}
down() {
set -x
gcloud compute instances list \
--filter=tags.items=k3s --format="get(name)" | \
xargs gcloud compute instances delete -q
gcloud compute firewall-rules delete k3s
}
usage() {
echo "Bootstrap or tear down a k3s cluster on GCE"
echo " up"
echo " down"
}
case "${1:-usage}" in
up)
shift
up "$@"
;;
down)
shift
down "$@"
;;
*)
usage
exit 1
;;
esac
@sjmach
Copy link

sjmach commented Feb 6, 2022

In the latest gcloud version, you need to specify the zone. Thanks for the gist, it is pretty handy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment