Skip to content

Instantly share code, notes, and snippets.

@sjmach
Forked from drnic/k3sup-gce.sh
Last active February 6, 2022 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sjmach/532d71dfda07cc20d06e8507d106e5ca to your computer and use it in GitHub Desktop.
Save sjmach/532d71dfda07cc20d06e8507d106e5ca to your computer and use it in GitHub Desktop.
#!/bin/bash
set -u
up() {
INSTANCE_TYPE=${INSTANCE_TYPE:-n1-standard-1}
curl -sLS https://get.k3sup.dev | sh
sudo install k3sup /usr/local/bin/
(
set -x
gcloud config set compute/zone us-central1-a
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].networkIP)")
(
set -x
gcloud config set compute/zone us-central1-a
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 config set compute/zone us-central1-a
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment