Created
April 6, 2019 15:56
-
-
Save thediveo/c20b91dca7996b7e8d6dad3ccd8a7c75 to your computer and use it in GitHub Desktop.
Operate a host-local Docker container registry for kubeadm-dind clusters.
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 | |
# TheDiveO | |
function log { | |
local OPTS="" | |
if [[ "$1" = "-n" ]]; then | |
shift; OPTS+="-n" | |
fi | |
MSG="$1" | |
shift | |
if [[ -t 2 ]]; then | |
# colorful output to terminal | |
echo -e ${OPTS} "\x1B[97m* \x1B[92m${MSG}\x1B[39m $*" 1>&2 | |
else | |
# output to pipe | |
echo ${OPTS} "* ${MSG} $*" 1>&2 | |
fi | |
} | |
function green { | |
local OPTS="" | |
if [[ "$1" = "-n" ]]; then | |
shift; OPTS+="-n" | |
fi | |
if [[ -t 2 ]]; then | |
echo -e ${OPTS} "\x1B[92m$1\x1B[39m" 1>&2 | |
else | |
echo ${OPTS} "$1" 1>&2 | |
fi | |
} | |
function red { | |
local OPTS="" | |
if [[ "$1" = "-n" ]]; then | |
shift; OPTS+="-n" | |
fi | |
if [[ -t 2 ]]; then | |
echo -e ${OPTS} "\x1B[91m$1\x1B[39m" 1>&2 | |
else | |
echo ${OPTS} "$1" 1>&2 | |
fi | |
} | |
CMD="${1:-}" | |
case ${CMD} in | |
up) | |
# Start local Docker registry, if it's not running already. Contrary | |
# what the above link suggests, we don't enable the registry to | |
# *automatically* restart. Instead, we check if the registry is not | |
# already up and running, and only then start it (again). And we make | |
# sure to remove a stopped (exited) registry container in case we find | |
# one, before (re)starting the registry. See also: | |
# https://stackoverflow.com/a/38576401 | |
log "starting local Docker image registry" | |
if [[ ! "$(docker ps -q -f name=registry)" ]]; then | |
# Remove in case the container still exists, but has exited. | |
if [[ "$(docker ps -aq -f status=exited -f name=registry)" ]]; then | |
docker rm registry | |
fi | |
docker run -d -p 5000:5000 --name registry registry:2 | |
fi | |
# Taken from | |
# https://github.com/kubernetes-sigs/kubeadm-dind-cluster/issues/56#issuecomment-387463386. | |
# Run registry proxies on all Docker "Kubernetes nodes", forwarding | |
# their local port 5000 to our host's port 5000. The forwarder used | |
# here is the "Stupid TCP proxy", which can be found here: | |
# https://hub.docker.com/r/tecnativa/tcp-proxy/ | |
log "starting registry forwarders on Kubernetes \"nodes\"..." | |
docker ps -a -q --filter=label=mirantis.kubeadm_dind_cluster | while read CID; do | |
NAME=$(docker inspect --format='{{.Name}}' $CID) | |
NAME=${NAME:1} | |
echo -n "Docker Kubernetes node $CID \"$NAME\": (re)starting registry forwarder " | |
docker exec ${CID} /bin/bash -c "docker rm -fv registry-proxy || true" | |
# run registry proxy: forward from localhost:5000 on each node to host:5000 | |
docker exec ${CID} /bin/bash -c \ | |
"docker run --name registry-proxy -d -e LISTEN=':5000'\ | |
-e TALK=\"\$(/sbin/ip route|awk '/default/ { print \$3 }'):5000\"\ | |
-p 5000:5000 tecnativa/tcp-proxy" | |
done | |
;; | |
down) | |
# Nuke the registry container | |
log "stopping local Docker image registry" | |
docker rm -fv registry | |
# Nuke all registry forwarders (TCP forwarders) inside the Kubernetes | |
# "node" docker containers. | |
log "stopping registry forwarders on Kubernetes \"nodes\"..." | |
docker ps -a -q --filter=label=mirantis.kubeadm_dind_cluster \ | |
| while read CID; do | |
NAME=$(docker inspect --format='{{.Name}}' $CID) | |
NAME=${NAME:1} | |
echo -n "Docker Kubernetes node $CID \"$NAME\": stopping registry forwarder " | |
docker exec ${CID} /bin/bash -c \ | |
"docker rm -fv registry-proxy || true" | |
done | |
;; | |
status) | |
# Try to discover the status of the local registry and the registry | |
# forwarders... | |
echo -n "local docker registry: " | |
if [[ "$(docker ps -q -f name=registry)" ]]; then | |
green "[running]" | |
else | |
red "[unavailable]" | |
fi | |
docker ps -a -q --filter=label=mirantis.kubeadm_dind_cluster \ | |
| while read CID; do | |
NAME=$(docker inspect --format='{{.Name}}' $CID) | |
NAME=${NAME:1} | |
echo -n "Docker Kubernetes node $CID \"$NAME\": registry forwarder " | |
if [[ $(docker exec ${CID} /bin/bash -c \ | |
"docker ps -q -f name=registry-proxy") ]]; then | |
green "[running]" | |
else | |
red "[unavailable]" | |
fi | |
done | |
;; | |
*) | |
PROG=$(basename "$0") | |
echo "usage:" >&2 | |
echo " $PROG up" >&2 | |
echo " $PROG down" >&2 | |
echo " $PROG status" >&2 | |
exit 1 | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment