Shell脚本一键部署Kubenetes(k8s)前置环境
【代码】Shell脚本一键部署KubeSphere前置环境。
·
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"
#备份fstab
sudo cp /etc/fstab /etc/fstab.bak_$(date +%F)
#注释掉包含 'swap' 且行首不是 '#' 的行
sed -i '/^[^#]*swap/ s/^/#/' /etc/fstab
if [ $? -eq 0 ]; then
echo -e "已更新/etc/fstab,Swap开机自启已禁用。"
else
echo -e "错误:修改 /etc/fstab 失败。"
exit 1
fi
}
#2)关闭防火墙
disable_firewalld(){
echo -e "\e[32m2)开始关闭firewalld\e[0m"
if [ "$(systemctl is-active firewalld)" != "active" ]; then
echo "防火墙firewalld已关闭"
else
#关闭防火墙并禁用开机自启
systemctl disable firewalld --now
fi
}
#3)关闭selinux
disable_selinux(){
echo -e "\e[32m3)开始关闭selinux\e[0m"
SELINUX_CONFIG="/etc/selinux/config"
if [ -f "$SELINUX_CONFIG" ]; then # 获取当前配置文件中的 SELINUX 设置值 (提取 SELINUX=enforcing 中的 enforcing)
CURRENT_SELINUX=$(grep "^SELINUX=" "$SELINUX_CONFIG" | awk -F= '{print $2}')
if [ "$CURRENT_SELINUX" == "enforcing" ]; then
echo "SELinux当前为enforcing 模式,正在修改配置..."
# 修改配置文件,将 enforcing 替换为 disabled
sed -i 's/^SELINUX=enforcing/SELINUX=disabled/' "$SELINUX_CONFIG"
# 立即临时关闭 SELinux (无需重启即刻生效)
setenforce 0
echo "SELinux 配置文件已修改,且已临时关闭。"
echo "注意:彻底禁用 SELinux 通常建议重启系统以确保所有服务识别。"
elif [ "$CURRENT_SELINUX" == "permissive" ]; then
echo "SELinux 当前为 enforcing 模式,正在修改配置..."
# 修改配置文件,将 enforcing 替换为 disabled
sed -i 's/^SELINUX=enforcing/SELINUX=disabled/' "$SELINUX_CONFIG"
# 立即临时关闭SELinux(无需重启即刻生效)
setenforce 0
echo "SELinux 配置文件已修改,且已临时关闭。"
echo "注意:彻底禁用 SELinux 通常建议重启系统以确保所有服务识别。"
elif [ "$CURRENT_SELINUX" == "disabled" ]; then
echo "SELinux已经是disabled状态,跳过。"
else
echo "无法识别SELinux状态 ($CURRENT_SELINUX),跳过。"
fi
else
echo "未找到SELinux配置文件,跳过。"
fi
}
#4)配置阿里云yum源
config_aliyun_yum(){
echo -e "\e[32m4)开始配置阿里云yum源\e[0m"
REPO_DIR="/etc/yum.repos.d/"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
# 检查是否存在包含 aliyun 的 repo 文件
if grep -q "aliyun" ${REPO_DIR}/*repo ; then
echo "检测到已配置阿里云YUM源,跳过配置。"
else
echo "未检测到阿里云YUM源,正在配置..."
# 1.1 备份现有的 repo 文件
mkdir -p /etc/yum.repos.d/backup_${TIMESTAMP}
mv $REPO_DIR/*.repo /etc/yum.repos.d/backup_${TIMESTAMP}/ 2>/dev/null
echo "原有repo文件已备份至backup_${TIMESTAMP}"
# 1.2 下载阿里云 CentOS 7 repo 文件
# 使用 curl 或 wget,这里优先使用 curl
if command -v curl &> /dev/null; then
curl -o $REPO_DIR/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
elif command -v wget &> /dev/null; then
wget -O $REPO_DIR/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
else
echo "错误: 系统中未安装 curl 或 wget,无法下载 repo 文件。"
exit 1
fi
# 1.3 生成缓存
yum clean all
yum makecache fast
echo "阿里云 YUM 源配置完成。"
fi
}
#5)配置chrony时间同步
config_chrony(){
local CHRONY_CONFIG="/etc/chrony.conf"
local TIMESTAMP=$(date +%Y%m%d_%H%M%S)
echo -e "\e[32m5)开始配置chrony时间同步\e[0m"
if command -v chronyd &> /dev/null; then
echo "Chrony软件已安装。"
# 检查配置中是否已包含阿里云 NTP 服务器
if grep -q "aliyun" "$CHRONY_CONFIG"; then
echo "检测到Chrony已配置阿里云NTP源,跳过配置。"
# 即使配置已存在,也确保服务是启动状态
if [ "$(systemctl is-active chronyd)" != "active" ]; then
echo "检测到服务未运行,正在启动 Chrony..."
systemctl start chronyd
systemctl enable chronyd
fi
else
echo "Chrony已安装但未配置阿里云源,正在更新配置..."
cp -p "$CHRONY_CONFIG" "${CHRONY_CONFIG}.bak_${TIMESTAMP}"
# 注释默认源并追加阿里云源
sed -i '/^server/ s/^/#/' "$CHRONY_CONFIG"
cat >> "$CHRONY_CONFIG" << 'EOF'
# --- 阿里云 NTP 服务器 ---
server ntp1.aliyun.com iburst
server ntp2.aliyun.com iburst
server ntp3.aliyun.com iburst
server ntp4.aliyun.com iburst
EOF
echo "配置文件已更新。"
systemctl restart chronyd
systemctl enable chronyd
chronyc makestep
echo "Chrony配置更新并重启完成。"
fi
else
# 如果未安装,则执行安装和配置
echo "Chrony未安装,正在安装并配置..."
yum install -y chrony
# 备份并配置
cp -p "$CHRONY_CONFIG" "${CHRONY_CONFIG}.bak_${TIMESTAMP}"
#注释默认源并追加阿里云源
sed -i '/^server/ s/^/#/' "$CHRONY_CONFIG"
cat >> "$CHRONY_CONFIG" << 'EOF'
# --- 阿里云 NTP 服务器 ---
server ntp1.aliyun.com iburst
server ntp2.aliyun.com iburst
server ntp3.aliyun.com iburst
server ntp4.aliyun.com iburst
EOF
systemctl start chronyd
systemctl enable chronyd
chronyc makestep
echo "Chrony安装、配置并启动完成。"
fi
}
#6)yum安装工具包
install_base_rpm(){
echo -e "\e[32m6)开始安装基础工具包\e[0m"
yum -y install socat conntrack telnet wget net-tools screen vim
}
#7)节点之间配置免密登录(可选)
#8)配置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
#}
#配置上述子项
config_all(){
disable_swap
disable_firewalld
disable_selinux
config_aliyun_yum
config_chrony
install_base_rpm
echo -e "\033[0;31m 核实备份后,请重启服务器,使配置生效\033[0m"
}
check_config(){
echo -e "\e[32m1)开始检查swap\e[0m"
free -m
echo -e "\e[32m2) 开始检查firewalld\e[0m"
iptables -L
echo -e "\e[32m3) 开始检查selinux\e[0m"
sestatus
echo -e "\e[32m5) 开始检查yum源\e[0m"
yum repolist
echo -e "\e[32m6) 开始检查chrony\e[0m"
chronyc sources -v
echo -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_aliyun_yum;
5-config_chrony;
6-install_base_rpm:
7-config_all: ' num
case "$num" in
0)
check_config
;;
1)
disable_swap
;;
2)
disable_firewalld
;;
3)
disable_selinux
;;
4)
config_aliyun_yum
;;
5)
config_chrony
;;
6)
install_base_rpm
;;
7)
config_all
;;
*)
exit 1
;;
esac更多推荐



所有评论(0)