|
#!/bin/bash |
|
|
|
# Source: http://kubernetes.io/docs/getting-started-guides/kubeadm |
|
|
|
set -e |
|
|
|
print_usage () { |
|
echo "" |
|
echo "Usage $0 -v 1.23.6 -M" |
|
echo " -v set kubernetes version" |
|
echo " -M if set, installs control plane components. " |
|
echo " use flag to install Master node" |
|
} |
|
|
|
|
|
configure_master () { |
|
### init k8s |
|
rm /root/.kube/config || true |
|
kubeadm init --kubernetes-version=${KUBE_VERSION} --ignore-preflight-errors=NumCPU --skip-token-print --pod-network-cidr 192.168.0.0/16 |
|
|
|
mkdir -p /root/.kube |
|
sudo cp -i /etc/kubernetes/admin.conf /root/.kube/config |
|
|
|
### CNI |
|
kubectl apply -f https://raw.githubusercontent.com/killer-sh/cks-course-environment/master/cluster-setup/calico.yaml |
|
|
|
# etcdctl |
|
ETCDCTL_VERSION=v3.5.1 |
|
ETCDCTL_VERSION_FULL=etcd-${ETCDCTL_VERSION}-linux-amd64 |
|
wget https://github.com/etcd-io/etcd/releases/download/${ETCDCTL_VERSION}/${ETCDCTL_VERSION_FULL}.tar.gz |
|
tar xzf ${ETCDCTL_VERSION_FULL}.tar.gz |
|
mv ${ETCDCTL_VERSION_FULL}/etcdctl /usr/bin/ |
|
rm -rf ${ETCDCTL_VERSION_FULL} ${ETCDCTL_VERSION_FULL}.tar.gz |
|
|
|
echo |
|
echo "### PASTE THE FOLLOWING COMMAND ON WORKER NODE's TO ADD THE CLUSTER ###" |
|
kubeadm token create --print-join-command --ttl 0 |
|
} |
|
|
|
|
|
configure_worker () { |
|
### init k8s |
|
kubeadm reset -f |
|
systemctl daemon-reload |
|
service kubelet start |
|
|
|
echo |
|
echo "EXECUTE ON MASTER: kubeadm token create --print-join-command --ttl 0" |
|
echo "THEN RUN THE OUTPUT AS COMMAND HERE TO ADD AS WORKER" |
|
echo |
|
} |
|
|
|
# Parse CLI arguments |
|
while getopts 'v:M' flag; do |
|
case "${flag}" in |
|
v) KUBE_VERSION="${OPTARG}" ;; |
|
M) INSTALL_MASTER="TRUE" ;; |
|
*) print_usage |
|
exit 1 ;; |
|
esac |
|
done |
|
|
|
|
|
if [[ ! -n "$KUBE_VERSION" ]] ; then |
|
echo "Missing required argument: Kubernetes Version" |
|
echo "Use -v flag to set Kubernetes version" |
|
exit 1 |
|
fi |
|
|
|
|
|
source /etc/lsb-release |
|
if [ "$DISTRIB_RELEASE" != "20.04" ]; then |
|
echo "################################# " |
|
echo "############ WARNING ############ " |
|
echo "################################# " |
|
echo |
|
echo "This script only works on Ubuntu 20.04!" |
|
echo "You're using: ${DISTRIB_DESCRIPTION}" |
|
echo "Better ABORT with Ctrl+C. Or press any key to continue the install" |
|
read |
|
fi |
|
|
|
|
|
if [[ ! -z $NSTALL_MASTER ]] ; then |
|
echo "Are sure you want to setup this machine as Master Node with Kubernetes v${KUBE_VERSION} ?" |
|
else |
|
echo "Are sure you want to setup this machine as Worker Node with Kubernetes v${KUBE_VERSION} ?" |
|
fi |
|
read -p "Continue installation [Y|n]: " CONTINUE_INSTALL |
|
|
|
if [[ $CONTINUE_INSTALL == 'n' ]] ; then |
|
echo "Installation cancelled" |
|
exit 1 |
|
fi |
|
|
|
### setup terminal |
|
apt-get update |
|
apt-get install -y bash-completion binutils |
|
echo 'colorscheme ron' >> ~/.vimrc |
|
echo 'set tabstop=2' >> ~/.vimrc |
|
echo 'set shiftwidth=2' >> ~/.vimrc |
|
echo 'set expandtab' >> ~/.vimrc |
|
echo 'source <(kubectl completion bash)' >> ~/.bashrc |
|
echo 'alias k=kubectl' >> ~/.bashrc |
|
echo 'alias c=clear' >> ~/.bashrc |
|
echo 'complete -F __start_kubectl k' >> ~/.bashrc |
|
sed -i '1s/^/force_color_prompt=yes\n/' ~/.bashrc |
|
|
|
|
|
### Turn all swap off and disable swap permanently |
|
swapoff -a |
|
sed -i '/\sswap\s/ s/^\(.*\)$/#\1/g' /etc/fstab |
|
|
|
|
|
### remove packages |
|
kubeadm reset -f || true |
|
crictl rm --force $(crictl ps -a -q) || true |
|
apt-mark unhold kubelet kubeadm kubectl kubernetes-cni || true |
|
apt-get remove -y docker.io containerd kubelet kubeadm kubectl kubernetes-cni || true |
|
apt-get autoremove -y |
|
systemctl daemon-reload |
|
|
|
|
|
### install podman |
|
. /etc/os-release |
|
echo "deb https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/testing/xUbuntu_${VERSION_ID}/ /" | sudo tee /etc/apt/sources.list.d/devel:kubic:libcontainers:testing.list |
|
curl -L "https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/testing/xUbuntu_${VERSION_ID}/Release.key" | sudo apt-key add - |
|
apt-get update -qq |
|
apt-get -qq -y install podman cri-tools containers-common |
|
rm /etc/apt/sources.list.d/devel:kubic:libcontainers:testing.list |
|
cat <<EOF | sudo tee /etc/containers/registries.conf |
|
[registries.search] |
|
registries = ['docker.io'] |
|
EOF |
|
|
|
|
|
### install packages |
|
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add - |
|
cat <<EOF > /etc/apt/sources.list.d/kubernetes.list |
|
deb http://apt.kubernetes.io/ kubernetes-xenial main |
|
EOF |
|
apt-get update |
|
apt-get install -y docker.io containerd kubelet=${KUBE_VERSION}-00 kubeadm=${KUBE_VERSION}-00 kubectl=${KUBE_VERSION}-00 kubernetes-cni |
|
apt-mark hold kubelet kubeadm kubectl kubernetes-cni |
|
|
|
|
|
### containerd |
|
cat <<EOF | sudo tee /etc/modules-load.d/containerd.conf |
|
overlay |
|
br_netfilter |
|
EOF |
|
sudo modprobe overlay |
|
sudo modprobe br_netfilter |
|
cat <<EOF | sudo tee /etc/sysctl.d/99-kubernetes-cri.conf |
|
net.bridge.bridge-nf-call-iptables = 1 |
|
net.ipv4.ip_forward = 1 |
|
net.bridge.bridge-nf-call-ip6tables = 1 |
|
EOF |
|
sudo sysctl --system |
|
sudo mkdir -p /etc/containerd |
|
|
|
|
|
### containerd config |
|
cat > /etc/containerd/config.toml <<EOF |
|
disabled_plugins = [] |
|
imports = [] |
|
oom_score = 0 |
|
plugin_dir = "" |
|
required_plugins = [] |
|
root = "/var/lib/containerd" |
|
state = "/run/containerd" |
|
version = 2 |
|
|
|
[plugins] |
|
|
|
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes] |
|
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc] |
|
base_runtime_spec = "" |
|
container_annotations = [] |
|
pod_annotations = [] |
|
privileged_without_host_devices = false |
|
runtime_engine = "" |
|
runtime_root = "" |
|
runtime_type = "io.containerd.runc.v2" |
|
|
|
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options] |
|
BinaryName = "" |
|
CriuImagePath = "" |
|
CriuPath = "" |
|
CriuWorkPath = "" |
|
IoGid = 0 |
|
IoUid = 0 |
|
NoNewKeyring = false |
|
NoPivotRoot = false |
|
Root = "" |
|
ShimCgroup = "" |
|
SystemdCgroup = true |
|
EOF |
|
|
|
|
|
### crictl uses containerd as default |
|
{ |
|
cat <<EOF | sudo tee /etc/crictl.yaml |
|
runtime-endpoint: unix:///run/containerd/containerd.sock |
|
EOF |
|
} |
|
|
|
|
|
### kubelet should use containerd |
|
{ |
|
cat <<EOF | sudo tee /etc/default/kubelet |
|
KUBELET_EXTRA_ARGS="--container-runtime remote --container-runtime-endpoint unix:///run/containerd/containerd.sock" |
|
EOF |
|
} |
|
|
|
|
|
### start services |
|
systemctl daemon-reload |
|
systemctl enable containerd |
|
systemctl restart containerd |
|
systemctl enable kubelet && systemctl start kubelet |
|
|
|
if [[ ! -z $INSTALL_MASTER ]] ; then |
|
configure_master |
|
else |
|
configure_worker |
|
fi |