#!/bin/bash 

#check md5sum
md5_clmg=ab25fc92d9156e8c28119b0d66d44f3a
md5_cy=88180a7a7abcef317a8e5a99ad4962d6
md5_cordns=53834f3808a1a90c8895c15ded5fcdcd
md5_metimg=55e1a83dcf124da59b68365b262e051f
md5_meser=b9d8cde6633877205a204f664407a5ec

check(){
  if [ $UID != 0 ];then
     echo -e "\e[1;31m Current user is not root, please use root to execute\e[0m" && exit
  fi
  ping -c 2 qq.com &>/dev/null
  if [ $? != 0 ];then
     echo -e "\e31m [ERROR] Please connect to the network and try again.\e[0m" && exit
  fi
  echo -e "\e[5;34m Initializing environment...\e[0m"
}

init(){
  swapoff -a ; sed -i '/swap/d' /etc/fstab
  setenforce  0
  firewall-cmd --set-default-zone=trusted &>/dev/null
  command -v wget &> /dev/null || yum -y install wget 
  if [ $? != 0 ];then
    echo -e "\e[31m [ERROR]  Failed to get k8s source, please check the network and try again.\e[0m" && exit
  fi
}

install_package(){
  wget ftp://ftp.rhce.cc/k8s/*  -P /etc/yum.repos.d/ &>/dev/null
  yum -y install docker-ce kubelet-1.21.0 kubectl-1.21.0 kubeadm-1.21.0 &>/dev/null
  if [ $? != 0 ];then
   echo -e "\e[31m [ERROR \e Package download failed. please check yum source and try again.\e[0m" && exit
  fi
  systemctl enable docker --now &>/dev/null
  systemctl enable kubelet --now 
  cat > /etc/docker/daemon.json  <<EOF
{
  "registry-mirrors": ["https://ybfg8y7z.mirror.aliyuncs.com"]
}
EOF
  systemctl restart docker    
}

get_images(){
  clear
  echo -e "\e[5;34m 拉取镜像中...\e[0m" 
  rm -rf coredns-1.21.tar
  wget -q 1.116.86.11/k8s-sh/coredns-1.21.tar
  if [ $md5_cordns == `md5sum coredns-1.21.tar | cut -d' ' -f1` ];then
    docker load -i coredns-1.21.tar
  else
    echo -e "\e[31m coredns-1.21.tar download failed, please check the network and try again. \e[0m" && exit
  fi
  rm -rf calico.yaml
  wget -q 1.116.86.11/k8s-sh/calico.yaml
  if [ $md5_cy != `md5sum calico.yaml | cut -d' ' -f1` ];then
    echo -e "\e[31m Calico.yaml download failed, please check the network and try again. \e[0m" && exit 
  fi 
  rm -rf calico-3.19-img.tar
  wget -q 1.116.86.11/k8s-sh/calico-3.19-img.tar
  if [ $md5_clmg != `md5sum calico-3.19-img.tar | cut -d' ' -f1` ];then
    echo -e "\e[31m calico-3.19-img.tar download failed, please check the network and try again. \e[0m" && exit
  else
    docker load -i calico-3.19-img.tar
  fi
}


deploy_master(){
  read -p "请输入pod网段,回车默认(10.244.0.0/16): " pod_addr
  kubeadm init --image-repository registry.aliyuncs.com/google_containers  --kubernetes-version=1.21.0 --pod-network-cidr=${pod_addr:-10.244.0.0/16}
  join_token=`kubeadm token create --print-join-command` 
  mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config
}


deploy_node(){
  while [[ $choose != "end" ]] 
    do 
       read -p "请输入节点(node)IP(输入end结束!!!): " choose
       if [ $choose != end ];then
         ssh $choose "$(typeset -f install_package); install_package"
         ssh $choose "$(typeset -f get_images); get_images"
         ssh $choose "$join_token"
       fi
    done
}

deploy_cni(){
  kubectl apply -f calico.yaml &>/dev/null
  if [ $? != 0 ];then
    echo echo -e "\e[32m [Error] Cni network plug-in deployment failed. please check the network and try again.\e[0m"
  else
    echo -e "\e[93m [Successful] Cni network plug-in deployment completed.\e[0m" 
  fi 
}

print_info(){
  echo -e "[\e[33m
 _____  _   _ 
|__  / | | | |
  / /  | |_| |
 / /_  |  _  |
/____| |_| |_|
博客地址: http://www.zhanghaobk.com 云盘地址: http://www.haoyun66.cloud
QQ群: 706080502
@#######################################################@
默认kubectl不支持tab,需要配置tab功能只需要执行以下操作  #
echo >> "source <(kubectl completion bash)" /etc/profile#
source /etc/profile					#
@#######################################################@
\e[0m"
kubectl get nodes
}

main(){
check
init
install_package
get_images
deploy_master
deploy_node
deploy_cni
print_info
}

main

Q.E.D.