Skip to content

Instantly share code, notes, and snippets.

@anapsix anapsix/rkind.sh
Last active Jun 20, 2019

Embed
What would you like to do?
Rancher in KIND (Rancher in Kubernetes-in-Docker)
#!/usr/bin/env bash
#
# RKIND is a naive helper script to start KIND and Rancher Management Server
#
set -e
set -u
set -o pipefail
RANCHER_CONTAINER_NAME="rancher-for-kind"
RANCHER_HTTP_HOST_PORT=$[$[RANDOM%9000]+30000]
RANCHER_HTTPS_HOST_PORT=$[$[RANDOM%9000]+30000]
KIND_CLUSTER_NAME="kind-for-rancher"
case $(uname -s) in
Darwin)
localip="$(ipconfig getifaddr en0)"
;;
Linux)
localip="$(hostname -i)"
;;
*)
echo >&2 "Unsupported OS, exiting.."
exit 1
;;
esac
# check docker binary availability
if ! which docker >/dev/null; then
echo >&2 "Docker binary cannot be found in PATH"
echo >&2 "Install Docker or check your PATH, exiting.."
exit 1
fi
# check KIND binary availability
if ! which kind >/dev/null; then
echo >&2 "KIND binary is missing"
echo >&2 "Install it with \"go get sigs.k8s.io/kind\""
echo >&2 "Or download appropriate binary from https://github.com/kubernetes-sigs/kind/releases"
echo >&2 "For more details see:"
echo >&2 " - https://kind.sigs.k8s.io"
echo >&2 " - https://github.com/kubernetes-sigs/kind"
echo >&2 "exiting.."
exit 1
fi
if [[ "${1:-}" == "cleanup" ]]; then
docker rm -f ${RANCHER_CONTAINER_NAME}
kind delete cluster --name ${KIND_CLUSTER_NAME}
exit 0
fi
# Launch Rancher server
if [[ $(docker ps -f name=${RANCHER_CONTAINER_NAME} -q | wc -l) -ne 0 ]]; then
echo >&2 "Rancher container already present, delete it before trying again, exiting.."
exit 1
fi
if docker run -d \
--restart=unless-stopped \
--name ${RANCHER_CONTAINER_NAME} \
-p ${RANCHER_HTTP_HOST_PORT}:80 \
-p ${RANCHER_HTTPS_HOST_PORT}:443 \
rancher/rancher; then
echo >&2 "Rancher UI will be available at https://${localip}:${RANCHER_HTTPS_HOST_PORT}"
echo >&2 "It might take few up to 60 seconds for Rancher UI to become available.."
echo >&2 "While it's coming up, going to start KIND cluster"
fi
# Start KIND cluster
if [[ $(kind get clusters | grep -c ${KIND_CLUSTER_NAME}) -ne 0 ]]; then
echo >&2 "KIND cluster is already running.."
echo >&2 -n "Use running KIND? [y/N] "
read use_running_kind
case $use_running_kind in
n|N)
echo >&2 "exiting.."
exit 1
;;
y|Y)
echo >&2 "ok, continuing.."
;;
*)
echo >&2 "unrecognized option, exiting.."
exit 1
;;
esac
else
kind create cluster --name ${KIND_CLUSTER_NAME}
fi
echo >&2
cat >&2 <<EOM
### Next steps ###
- Setup admin credentials in Rancher UI
- Set "Rancher Server URL" to "https://${localip}:${RANCHER_HTTPS_HOST_PORT}" (should already be selected)
you may change it at any time in "Settings"
- Import KIND cluster to Rancher (via https://${localip}:${RANCHER_HTTPS_HOST_PORT}/g/clusters/add?provider=import)
(select "Import Existing cluster" when adding a cluster)
> To work around "Unable to connect to the server: x509: certificate signed by unknown authority"
> use "curl --insecure" to get the manigest, piping it's output to
> KUBECONFIG="\$(kind get kubeconfig-path --name=${KIND_CLUSTER_NAME})" kubectl apply -f -
To shut everything down, use "$0 cleanup", or manually with
docker rm -f ${RANCHER_CONTAINER_NAME}; kind delete cluster ${KIND_CLUSTER_NAME}
EOM
# Open Rancher UI in browser
open https://${localip}:${RANCHER_HTTPS_HOST_PORT}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.