1. 服务器环境
[root@localhost~]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
2. 脚本内容
#!/bin/bash#本文针对CentOS7系统#1)关闭交换分区swap
#有的话需要关闭掉,本文在创建虚拟机时已关闭
disable_swap(){echo -e "\e[32m1)开始关闭swap\e[0m"echo "ok"
}#2)关闭防火墙
disable_firewalld(){echo -e "\e[32m2)开始关闭firewalld\e[0m"systemctl disable firewalld --now
}#3)关闭selinux
disable_selinux(){echo -e "\e[32m3)开始关闭selinux\e[0m"sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
}#4)配置hosts文件【根据实际情况配置】
config_hosts(){echo -e "\e[32m4)开始配置hosts\e[0m"cat >> /etc/hosts <<EOF
10.10.14.151 test-master151
10.10.14.154 test-node154
10.10.14.155 test-node155
10.10.14.156 test-node156
10.10.14.157 test-node157
10.10.14.132 k8s-registry132
EOF
}#5)配置阿里云yum源
config_aliyun_yum(){echo -e "\e[32m5)开始配置阿里云yum源\e[0m"mkdir -p /etc/yum.repos.d/bakmv /etc/yum.repos.d/CentOS* /etc/yum.repos.d/bak/cd /etc/yum.repos.d && curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repoyum clean allyum makecache fast
}#6)配置chrony时间同步
config_chrony(){echo -e "\e[32m6)开始配置chrony时间同步\e[0m"yum -y install chrony cat > /etc/chrony.conf <<EOF
server ntp.aliyun.com iburst
stratumweight 0
driftfile /var/lib/chrony/drift
rtcsync
makestep 10 3
bindcmdaddress 127.0.0.1
bindcmdaddress ::1
keyfile /etc/chrony.keys
commandkey 1
generatecommandkey
logchange 0.5
logdir /var/log/chrony
EOFsystemctl enable chronyd --now
}#7)yum安装工具包
install_base_rpm(){echo -e "\e[32m7)开始安装基础工具包\e[0m"yum -y install socat conntrack telnet wget net-tools screen vim
}#节点之间配置免密登录(可选)#配置上述子项
config_all(){disable_swapdisable_firewallddisable_selinuxconfig_hostsconfig_aliyun_yumconfig_chronyinstall_base_rpm
}check_config(){echo -e "\e[32m1)开始检查swap\e[0m"free -mecho -e "\e[32m2) 开始检查firewalld\e[0m"iptables -Lecho -e "\e[32m3) 开始检查selinux\e[0m"sestatusecho -e "\e[32m4) 开始检查hosts\e[0m"cat /etc/hostsecho -e "\e[32m5) 开始检查yum源\e[0m"yum repolistecho -e "\e[32m6) 开始检查chrony\e[0m"chronyc sources -vecho -e "\e[32m7) 开始检查基础安装包\e[0m"rpm -qa conntrack-tools socat
}read -p '请输入需要部署的服务对应编号:
0-check_config;
1-disable_swap;
2-disable_firewalld;
3-disable_selinux;
4-config_hosts;
5-config_aliyun_yum;
6-config_chrony;
7-install_base_rpm:
8-config_all: ' numcase "$num" in0)check_config;;1)disable_swap;;2)disable_firewalld;;3)disable_selinux;;4)config_hosts;;5)config_aliyun_yum;;6)config_chrony;;7)install_base_rpm;;8)config_all;;*)exit 1;;
esac