Automation of a local dev environment to learn, self-document, save time and avoid to reinvent the wheel
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
debian-unattended/custom/install_kubectl.sh

77 lines
3.0 KiB

#!/bin/sh
if [ "$(id -u)" -ne 0 ]; then
echo "This script should be run as root. Aborting..." > /dev/stderr
exit 1
fi
#
# Following this guide: https://kubernetes.io/docs/tasks/tools/install-kubectl/
#
echo "Updating apt package index..."
apt-get update > /dev/null
echo "Installing prerequisites..."
apt-get -q -y -o Dpkg::Use-Pty=0 install apt-transport-https gnupg2 curl
echo "Adding Kubernetes' repo key..."
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
echo "Adding Kubernetes repo..."
echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | tee -a /etc/apt/sources.list.d/kubernetes.list
echo "Updating apt package index again..."
apt-get update > /dev/null
echo "Installing 'kubectl'..."
apt-get -q -y -o Dpkg::Use-Pty=0 install kubectl
# TODO: the command `kubectl cluster-info` gives an error and the script
# provided from the docs gives an error. In the docs they are make
# some sort of assumption that I cannot still figure out.
# Ffs this is an entry level doc and they already start to omit not
# obvious stuff... sorry for not owning a magic crystal ball. >:(
# THIS, is a quality doc:
# https://www.valent-blog.eu/2019/01/01/installare-kubernetes-in-debian-9/
# The lines below comes from this doc.
echo "Installing 'kubelet' and 'kubeadm'..."
apt-get -q -y -o Dpkg::Use-Pty=0 install kubelet kubeadm
echo "Marking in hold the packages 'kubectl', 'kubelet' and 'kubeadm' in order to prevent them to update in the future..."
apt-mark hold kubelet kubeadm kubectl
echo "Reloading systemd and restarting 'kubelet' service"
systemctl daemon-reload
systemctl restart kubelet
# I guess these are the base images of kubernetes (?)
echo "Pulling some base images.."
kubeadm config images pull
echo "Initializing kubernetes.."
kubeadm init
# This if statement evaulates if the script is launched using sudo AND not
# a `sudo su` logged user
if [ -n "$SUDO_USER" ] && [ "$SUDO_COMMAND" != "/usr/bin/su" ]; then
SUDO_HOME=$(su -c "echo \$HOME" "$SUDO_USER")
echo "Adding kubernetes config to this user home folder (useful for non-root users)..."
su -c "mkdir -p $SUDO_HOME/.kube" "$SUDO_USER"
cp -i /etc/kubernetes/admin.conf "$SUDO_HOME/.kube/config"
chown "$SUDO_UID":"$SUDO_GID" "$SUDO_HOME/.kube/config"
# This last one thanks to:
# https://stackoverflow.com/questions/53814150/how-to-setup-a-2-node-kubernetes-cluster-in-custom-environment
su -c "echo \"export KUBECONFIG=/etc/kubernetes/admin.conf\" | tee -a $SUDO_HOME/.bashrc" "$SUDO_USER"
else
echo "Adding kubernetes config to this user home folder (useful for non-root users)..."
mkdir -p "$HOME/.kube"
cp -i /etc/kubernetes/admin.conf "$HOME/.kube/config"
chown "$(id -u)":"$(id -g)" "$HOME/.kube/config"
# This last one thanks to:
# https://stackoverflow.com/questions/53814150/how-to-setup-a-2-node-kubernetes-cluster-in-custom-environment
echo "export KUBECONFIG=/etc/kubernetes/admin.conf" | tee -a ~/.bashrc
fi