Last active
December 25, 2020 02:29
-
-
Save mansurali901/0633364cb9a67f1d9613070a4e6786e5 to your computer and use it in GitHub Desktop.
This script is meant to setup Kubernetes Environment on Ubuntu / Debian supported paltforms
This file contains hidden or 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 | |
# Maintainer : Mansur Ul Hasan | |
# EMail : [email protected] | |
# LinkedIn : https://www.linkedin.com/in/mansurulhasan/ | |
# Youtube : https://www.youtube.com/user/mansur7820/ | |
# This script is meant to setup Kubernetes Environment on Ubuntu / Debian supported paltforms | |
# We have try to cover all the required tools modules which needed by modern kubernetes | |
# Disclaimer : | |
# This script is well tested on ubuntu 18 and 16 | |
# before using this script you must ensure what you are doing this script will change some kernel | |
# parameters as well please double check that part to match your environment requirements | |
# How to use this script | |
# Download or clone script your server and run with given options | |
# Script has various options to invidiual functions accroding to requirements | |
# Here are some options with their definations | |
# MacBook-Pro-de-user:~ mulhasan$ sh KubernetesSetupUbuntu.sh --youroption | |
# --master Use this option when Kubenrnetes master need to setup on machine | |
# --node Use this option when slave of kubenrnetes need to install | |
# --docker This option can be used when only docker=ce | |
# --crio CRI-O (Container Runtime Interface) is optional tool for kubernetes | |
# --kubebin This option enables you to install kubelet kubeadm kubectl kube-cni | |
# --containerd This Option enables to install containerd daemon | |
InstallDocker () { | |
# Install Docker CE | |
## Set up the repository: | |
### Install packages to allow apt to use a repository over HTTPS | |
apt-get update && apt-get install apt-transport-https ca-certificates curl software-properties-common | |
### Add Docker’s official GPG key | |
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add - | |
### Add Docker apt repository. | |
add-apt-repository \ | |
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \ | |
$(lsb_release -cs) \ | |
stable" | |
## Install Docker CE. | |
apt-get update && apt-get install docker-ce=18.06.2~ce~3-0~ubuntu | |
# Setup daemon. | |
cat > /etc/docker/daemon.json <<EOF | |
{ | |
"exec-opts": ["native.cgroupdriver=systemd"], | |
"log-driver": "json-file", | |
"log-opts": { | |
"max-size": "100m" | |
}, | |
"storage-driver": "overlay2" | |
} | |
EOF | |
mkdir -p /etc/systemd/system/docker.service.d | |
# Restart docker. | |
systemctl daemon-reload | |
systemctl restart docker | |
} | |
InstallCRIO () { | |
#CRI-O is meant to provide an integration path between OCI conformant runtimes and the kubelet. Specifically, it implements the Kubelet Container Runtime Interface (CRI) using OCI conformant runtimes. The scope of CRI-O is tied to the scope of the CRI. | |
#At a high level, we expect the scope of CRI-O to be restricted to the following functionalities: | |
# Support multiple image formats including the existing Docker image format | |
# Support for multiple means to download images including trust & image verification | |
# Container image management (managing image layers, overlay filesystems, etc) | |
# Container process lifecycle management | |
# Monitoring and logging required to satisfy the CRI | |
# Resource isolation as required by the CRI | |
# Probing required modules | |
modprobe overlay | |
modprobe br_netfilter | |
# Setup required sysctl params, these persist across reboots. | |
cat > /etc/sysctl.d/99-kubernetes-cri.conf <<EOF | |
net.bridge.bridge-nf-call-iptables = 1 | |
net.ipv4.ip_forward = 1 | |
net.bridge.bridge-nf-call-ip6tables = 1 | |
EOF | |
# Reloading Kernel parameters | |
sysctl --system | |
# Install prerequisites | |
apt-get update | |
apt-get install software-properties-common | |
add-apt-repository ppa:projectatomic/ppa | |
apt-get update | |
# Install CRI-O | |
apt-get install cri-o-1.11 | |
# Restart CRI-O | |
systemctl start crio | |
# Refer to the CRI-O installation guide for more information. | |
# https://github.com/kubernetes-sigs/cri-o#getting-started | |
} | |
InstallContainerd () { | |
#This section contains the necessary steps to use containerd as CRI runtime. | |
#Use the following commands to install Containerd on your system | |
modprobe overlay | |
modprobe br_netfilter | |
# Setup required sysctl params, these persist across reboots. | |
#cat > /etc/sysctl.d/99-kubernetes-cri.conf <<EOF | |
#net.bridge.bridge-nf-call-iptables = 1 | |
#net.ipv4.ip_forward = 1 | |
#net.bridge.bridge-nf-call-ip6tables = 1 | |
#EOF | |
#sysctl --system | |
# Install containerd | |
## Set up the repository | |
### Install packages to allow apt to use a repository over HTTPS | |
apt-get update && apt-get install -y apt-transport-https ca-certificates curl software-properties-common | |
### Add Docker’s official GPG key | |
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add - | |
### Add Docker apt repository. | |
add-apt-repository \ | |
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \ | |
$(lsb_release -cs) \ | |
stable" | |
## Install containerd | |
apt-get update && apt-get install -y containerd.io | |
# Configure containerd | |
mkdir -p /etc/containerd | |
containerd config default > /etc/containerd/config.toml | |
# Restart containerd | |
systemctl restart containerd | |
# To use the systemd cgroup driver, set plugins.cri.systemd_cgroup = true in /etc/containerd/config.toml. When using kubeadm, manually configure | |
# the cgroup driver for kubelet as well. | |
sed -i 's/plugins.cri.systemd_cgroup = false/plugins.cri.systemd_cgroup = true/g' /etc/containerd/config.toml | |
# Restart containerd service | |
systemctl restart containerd | |
} | |
SetupKubernetesBin () { | |
# This function will install kubectl kubelet kubeadm kubernetes-cni binaries | |
# This function may need to execute on all nodes across cluster | |
# Add Kubernetes repository | |
apt-get update; apt-get install -y apt-transport-https; | |
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add - | |
echo "deb http://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee -a /etc/apt/sources.list.d/kubernetes.list | |
apt-get update | |
# Install Kubernetes components | |
sudo apt-get update | |
sudo apt-get install -y kubelet kubeadm kubernetes-cni | |
} | |
InitMaster () { | |
# This Function will setup master and this function only need to run on master | |
# At first we will aquire required infromation to setup master | |
# like IP Address Interface name etc | |
echo "Enter interface name which need to use to connect node's network | |
In our case this is our local network and we named it as eth0 " | |
sleep 10 | |
echo " " | |
echo " " | |
read -p "Enter Interface Name : " MasterInterface | |
MasterIP=`ifconfig $MasterInterface |grep "inet addr" |cut -d : -f2|awk '{print $1}'` | |
# Now we have all the required information to setup master | |
kubeadm init --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=$MasterIP --cri-socket=/var/run/dockershim.sock | |
} | |
SettingEnv () { | |
# Setting up Environment to access kubernetes cluster from CLI | |
echo "\e[1;31mWe are about to setup User Environment\e[0m" | |
sleep 30 | |
cd $HOME | |
whoami | |
cp /etc/kubernetes/admin.conf $HOME/ | |
chown $(id -u):$(id -g) $HOME/admin.conf | |
export KUBECONFIG=$HOME/admin.conf | |
echo "export KUBECONFIG=/etc/kubernetes/admin.conf" | tee -a ~/.bashrc | |
source ~/.bashrc | |
} | |
SettingNetwork () { | |
# Our cluster is setup now next thing we need to setup networking module | |
echo "\e[1;31mNow Sit tight networking module is setting up\e[0m" | |
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml | |
# Tainting Nodes | |
kubectl taint nodes --all dedicated- | |
} | |
SetupNode () { | |
# If Nodes need to setup execut this function this will install all required packages for nodes | |
InstallDocker | |
InstallCRIO | |
InstallContainerd | |
SetupKubernetesBin | |
} | |
SetupMaster () { | |
# If master need to setup this function will setup master | |
InstallDocker | |
InstallCRIO | |
InstallContainerd | |
SetupKubernetesBin | |
InitMaster | |
SettingEnv | |
SettingNetwork | |
} | |
case $1 in | |
--master) | |
SetupMaster | |
;; | |
--node) | |
SetupNode | |
;; | |
--docker) | |
InstallDocker | |
;; | |
--crio) | |
InstallCRIO | |
;; | |
--kubebin) | |
SetupKubernetesBin | |
;; | |
--containerd) | |
InstallContainerd | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment