#!/bin/sh# Check if there is already kubeadm command here# if kubeadm exists, there may be already installed Kubernetesifcommand kubeadm &> /dev/null
thenecho"[WARN] Kubernetes may be already installed. Please check again..."exitfi#br_mod=$(lsmod | grep 'br_netfilter')echo"Current mod for br_netfilter: $br_mod"if[[ ! $br_mod=~ "br_netfilter"]];thenecho"[WARN] There is no br_netfilter mod on this machine!"
sudo modprobe br_netfilter
fiecho"[INFO] br_netfilter mod loaded successfully!"
lsmod | grep br_netfilter
#
sudo tee /etc/sysctl.d/kubernetes.conf<<EOFnet.bridge.bridge-nf-call-ip6tables = 1net.bridge.bridge-nf-call-iptables = 1net.ipv4.ip_forward = 1EOF
sudo sysctl --system
# Install docker if there is not docker installedif ! command -v docker &> /dev/null
then# you can refer this article of Docker Installation: https://docs.docker.com/engine/install/centos/echo"[WARN] Docker is not installed, now we are going to install docker for Kubernetes!"# Remove docker first just in case
sudo yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine
# Install yum-utilsecho"[INFO] Install yum-utils..."
sudo yum install -y yum-utils
echo"[INFO] Add repo for docker-ce..."
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
echo"[INFO] Starting installing Docker..."
sudo yum install -y docker-ce docker-ce-cli- containerd.io
echo"[INFO] Docker installed successfully. Starting docker..."
sudo systemctl start docker
sudo systemctl enable docker
echo"[INFO] Docker started..."
sudo docker version
elseecho"[INFO] Docker already exists...No need to install it."
sudo systemctl start docker
sudo docker version
fiif ! command -v kubeadm &> /dev/null
then# There's no kubernetes components installedecho"[INFO] Add Kubernetes repository into local yum repositories..."
cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo[kubernetes]name=Kubernetesbaseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-\$basearchenabled=1gpgcheck=1repo_gpgcheck=1gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpgexclude=kubelet kubeadm kubectlEOFecho"[INFO] Kubernetes repo successfully added..."else# Kubernetes related components may be already installedecho"[WARN] Kubernetes related application or components may be already installed."exitfiecho"[INFO] Start installing Kubernetes components: kubelet, kubeadm, kubectl"
sudo yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes
sudo systemctl enable --now kubelet
# After installing kubernetes, we have some steps to do# Set SELinux in permissive mode (effectively disabling it)echo"[INFO] Set SELinux in permissive mode..."
sudo setenforce 0
sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
# Disable swapecho"[INFO] Disable Swap..."
sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
sudo swapoff -a
# Enable necessary portecho"[INFO] Start enable some necessary ports for Kubernetes..."
sudo firewall-cmd --add-port={6443,2379-2380,30000-32767,10250,10251,10252,5473,179,5473}/tcp --permanent
sudo firewall-cmd --add-port={4789,8285,8472}/udp --permanent
sudo firewall-cmd --reload
# append new domain name to hosts fileecho"[INFO] Start writing new domain name for kubernetes, please ensure your input is accurate and correct."fileToAppend="/etc/hosts"echo"If you input a wrong IP or Domain Name, please update it in $fileToAppend manually"read -p "Please input ip address of your machine(e.g. 192.168.2.10): " ipAddr
read -p "Please input the domain name(e.g. 'k8s-master01 k8s-master01.torres.com'): " domainName
ipAddrDomain="$ipAddr$domainName"echo"You are adding \"$ipAddrDomain\" as a new line into $fileToAppend."echo$ipAddrDomain >> $fileToAppend# Verify the installation, pull images from gcr.ioecho"[INFO] Start pulling relevant images about Kubernetes from gcr.io!"
sudo kubeadm config images pull
echo"Kubernetes installed successfully!!!!!!!!!!!!!!!"