Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jtyr
Last active June 2, 2019 22:43
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 jtyr/de0a93adf1ae7fccf7c4fcad1956f093 to your computer and use it in GitHub Desktop.
Save jtyr/de0a93adf1ae7fccf7c4fcad1956f093 to your computer and use it in GitHub Desktop.
Shell script used to bootstrap Kubernetes on the play-with-k8s.com service
###
# Shell script used to bootstrap Kubernetes on the play-with-k8s.com service
###
#
# Inspired by https://gist.github.com/jjo/78f60702fbfa1cbec7dd865f67a3728a
#
# Usage:
# source <(curl -sL bit.do/jtyr-pwk-setup)
#
# Edit:
# https://gist.github.com/jtyr/de0a93adf1ae7fccf7c4fcad1956f093/edit
function msg() {
echo "$@" >&2
}
# Check if we are root
if [ "$UID" -ne 0 ]; then
msg 'ERROR: You are not root!'
return 1
fi
# Initialize the cluster
if [ ! -f /etc/kubernetes/pki/ca.key ]; then
echo '### Set up the Kubernetes master'
kubeadm init --apiserver-advertise-address $(hostname -i) | tee ~/kubeadm-init.log
[ $? -ne 0 ] && msg 'ERROR: kubeadm init failed' && return 1
mkdir -p ~/.kube
ln -fs /etc/kubernetes/admin.conf ~/.kube/config
echo -n '### Waiting for Kube services to be up and running'
end=0
while [ $end -eq 0 ]; do
echo -n '.'
KUBE_CONTROLLER_RUNNING=$(kubectl -n kube-system get pods 2>/dev/null | grep ^kube-controller-manager | grep Running | wc -l)
KUBE_PROXY_RUNNING=$(kubectl -n kube-system get pods 2>/dev/null | grep ^kube-proxy | grep Running | wc -l)
KUBE_SCHEDULER_RUNNING=$(kubectl -n kube-system get pods 2>/dev/null | grep ^kube-scheduler | grep Running | wc -l)
KUBE_ETCD_RUNNING=$(kubectl -n kube-system get pods 2>/dev/null | grep ^etcd | grep Running | wc -l)
if [ "$KUBE_CONTROLLER_RUNNING$KUBE_PROXY_RUNNING$KUBE_SCHEDULER_RUNNING$KUBE_ETCD_RUNNING" == '1111' ]; then
echo -e '\n### Set up the Weave networking'
kubectl apply -n kube-system -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 -w0)"
[ $? -ne 0 ] && msg 'ERROR: kubeadm apply failed' && return 1
end=1
fi
sleep 1
done
fi
# Install some packages
if ( ! rpm -qa | grep bash-completion >/dev/null ); then
echo '### Installing additional packages'
yum -q -y install bash-completion vim mc git >/dev/null
fi
# Bash improvements
echo '### Configuring Bash completion'
source /usr/share/bash-completion/bash_completion
source <(kubectl completion bash)
source <(kubeadm completion bash)
# Show the kubeadm join command
if [ -f ~/kubeadm-init.log ]; then
echo '### Join nodes with:'
grep -A1 'kubeadm join' ~/kubeadm-init.log | sed -r -e 's/ \\//' -e 's/^\s+/ /' | xargs
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment