Skip to content

Instantly share code, notes, and snippets.

@johnlane
Last active December 22, 2021 22:40
Show Gist options
  • Save johnlane/7d0617702f41c9805618eea87d72618f to your computer and use it in GitHub Desktop.
Save johnlane/7d0617702f41c9805618eea87d72618f to your computer and use it in GitHub Desktop.
A wrapper for k3d for use with ZFS file systems
#!/bin/bash
# wrapper for k3d v3 usage on ZFS using Docker volumes
# JL20200603
# Usage: pass 'create' as the first argument to create a cluster
# pass 'destroy' (or anything else) to destroy a cluster
#
# The following optional arguments may be provided (in this order):
# second argument is the cluster name (k3d is prefixed to this)
# following arguments apply to create: masters workers
# (default values may be seen/adjusted below)
[ "$1" == create ] && create=true || create=false
CLUSTER_NAME="${2:-default}"
MASTERS="${3:-1}"
WORKERS="${4:-2}"
K3S_TAG=latest
#DRYRUN=echo
if $create; then
PLUGIN_LS_OUT=`docker plugin ls --format '{{.Name}},{{.Enabled}}' | grep -E '^ashald/docker-volume-loopback'`
[ -z "${PLUGIN_LS_OUT}" ] && docker plugin install ashald/docker-volume-loopback DATA_DIR=/tmp/docker-loop/data
[ "${PLUGIN_LS_OUT##*,}" != "true" ] && docker plugin enable ashald/docker-volume-loopback
else
$DRYRUN k3d delete cluster ${CLUSTER_NAME}
fi
K3D_MOUNTS=()
for n in MASTERS WORKERS; do
for ((i=0;i<${!n};i++)); do
VOLUME_NAME="k3d-${CLUSTER_NAME}-${n::-1}-$i"
K3D_VOLUMES+=("${VOLUME_NAME,,}")
MOUNT_NAME="k3s@${n::-1}[$i]"
$create && $DRYRUN docker volume create -d ashald/docker-volume-loopback ${VOLUME_NAME,,} -o sparse=true -o fs=ext4 -o size=10GiB
K3D_MOUNTS+=('-v' "${VOLUME_NAME,,}:/var/lib/rancher/${MOUNT_NAME,,}")
done
done
if $create; then
$DRYRUN k3d create cluster ${CLUSTER_NAME} -i rancher/k3s:latest --switch \
--masters $MASTERS --workers $WORKERS ${K3D_MOUNTS[@]}
else
$DRYRUN docker volume rm -f ${K3D_VOLUMES[@]}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment