K8S 集群搭建脚本
目前脚本还未优化完成:
#!/bin/bash # 初始化环境 #==============初始化环境================= echo -e "\e[1;31m==============初始化环境=================\e[0m" #关闭防火墙: echo -e "\e[1;31m<<==== 关闭防火墙,并关闭防火墙开机自己启动 ====>>\e[0m" && systemctl stop firewalld && systemctl disable firewalld && #关闭SELinux echo -e "\e[1;31m<<==== 关闭SELinux,并关闭SELinux开机自己启动 ====>>\e[0m" && setenforce 0 && sed -i "s/enforcing/disabled/g" /etc/selinux/config #关闭swap分区 echo -e "\e[1;31m<<==== 关闭SELinux,并关闭SELinux开机自己启动 ====>>\e[0m" && swapoff -a && sed -i 's/.*swap.*/#&/' /etc/fstab && #yum 安装sshpass、wget等必要软件包: yum -y install sshpass wget && yum -y install conntrack ntpdate ntp ipvsadm ipset jq iptables curl sysstat libseccomp wget vim net-tools git &&
# << ========================================================= 主机名设置以及 hosts 文件中记录的添加 ===========================================>> #此条清空规则仅在测试时添加,如果是实际操作,请删除此条 #> /etc/hosts #====================================== > /root/host read -p "请您输入您当前Master节点上用于与Node节点通讯的IP:" Master_IP echo "#K8S搭建集群所用到的解析" >> /etc/hosts #先将本地的master节点的主机解析写入到hosts文件中: echo "$Master_IP master " >> /etc/hosts echo "Please enter your node mount(本次及之后的输入过程中如您输入出错,您可以按Ctrl+Backspace按键删除输入出错的参数再次输入正确的参数):" read mount echo -e "\e[1;31m你的子节点个数是 : $mount 个\e[0m" i=1 while [ $i -le $mount ] do read -p "Enter your 第 $i 个Node节点的IP: " Ip echo "$Ip node$i" >> /etc/hosts && echo "$Ip" >> /root/host let i+=1 done #循环确认密码并复制hosts文件到输入密码后的机器: i=1 while [ $i -le $mount ] do read -s -p "输入您Node_IP为:`sed -n "$i"p /root/host`机器的密码: " Passwd echo '\n' #将添加的hosts文件复制到Node结点上: /usr/bin/sshpass -p$Passwd scp -o stricthostkeychecking=no /etc/hosts root@`sed -n "$i"p /root/host`:/etc/hosts echo "hosts文件已复制到Node节点上" let i+=1 done echo "设置主机名" hostnamectl set-hostname master echo "设置内核参数" cat > /etc/sysctl.d/k8s.conf <<EOF net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 EOF sysctl --system echo "配置国内yum源" #配置国内yum源 yum install -y wget mkdir /etc/yum.repos.d/bak && mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.cloud.tencent.com/repo/centos7_base.repo wget -O /etc/yum.repos.d/epel.repo http://mirrors.cloud.tencent.com/repo/epel-7.repo yum clean all && yum makecache echo "配置国内Kubernetes源" cat <<EOF > /etc/yum.repos.d/kubernetes.repo [kubernetes] name=Kubernetes baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/ enabled=1 gpgcheck=1 repo_gpgcheck=1 gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg EOF echo "配置 docker 源:" wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo echo "安装docker" #安装docker yum install -y docker-ce-18.06.1.ce-3.el7 systemctl enable docker && systemctl start docker docker –version echo "安装kubeadm、kubelet、kubectl" #安装kubeadm、kubelet、kubectl yum install -y kubelet kubeadm kubectl systemctl enable kubelet echo "初始化安装master节点:" #初始化安装master节点: kubeadm init --kubernetes-version=1.15.3 --apiserver-advertise-address=$Master_IP --image-repository registry.aliyuncs.com/google_containers --service-cidr=10.1.0.0/16 --pod-network-cidr=10.244.0.0/16