K8S集群搭建——containerd版
| 主机名 | IP地址 | 角色 | 操作系统 | 硬件配置 |
|---|---|---|---|---|
| master | 100.100.35.66/22 | 管理节点 | CentOS 9 | 2CPU/4G内存/50G |
| node01 | 100.100.35.67/22 | 工作节点 | CentOS 9 | 1CPU/2G内存/50G |
| node02 | 100.100.35.68/22 | 工作节点 | CentOS 9 | 1CPU/2G内存/50G |
一.前期准备
网络配置
nmcli connection modify ens160 ipv4.method manual ipv4.addresses 100.100.35.68/22 ipv4.gateway 100.100.32.254 ipv4.dns 100.100.36.36 ipv4.dns 100.100.36.12 connection.autoconnect yes
1.更新软件包(每个节点都要做)
yum update -y
yum upgrade -y
#安装要使用的插件,基本的命令工具
yum -y install net-tools lrzsz wget tree vim unzip bash-completion
echo "source /usr/share/bash-completion/bash_completion" >> /etc/profile
source /etc/profile
2.关闭防火墙,SELinux,swap分区(修改完SELinux需要重启主机,每个节点都要做)
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i 's/enforcing/disabled/' /etc/selinux/config
sed -ri 's/.*swap.*/#&/' /etc/fstab
swapon -a
df -h
# -r 支持扩展正则+ ? () {} |
# -i:直接修改文件,而不是输出到标准输出。这意味着命令会直接更改 /etc/fstab 文件的内容,而不是仅仅显示更改。
# 's/.*swap.*/#&/':这是 sed 的命令模式,其中:
# s:表示替换操作。
# &:在替换模式中,& 表示匹配的文本(即所有匹配到的 swap 相关的行)。
# #&:在替换模式中,# 是注释符号,所以 #& 表示将匹配到的行前面添加 #,从而注释掉这些行。
3.将桥接的IPv4流量传递到iptables的链(每个节点都要做)
bridge(桥接) 是 Linux 系统中的一种虚拟网络设备,它充当一个虚拟的交换机,为集群内的容器提供网络通信功能,容器就可以通过这个 bridge 与其他容器或外部网络通信了。
vim modprobe.sh
#!/bin/bash
# 确保脚本以 root 权限运行
if [ "$(id -u)" -ne 0 ]; then
echo "请以 root 权限运行此脚本"
exit 1
fi
# 加载 br_netfilter 模块
modprobe br_netfilter
# 创建模块加载配置文件
echo "br_netfilter" | tee /etc/modules-load.d/br_netfilter.conf
# 创建 sysctl 配置文件
cat > /etc/sysctl.d/k8s.conf <<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
# 应用 sysctl 配置
sysctl -p /etc/sysctl.d/k8s.conf
# 重启 NetworkManager 服务以应用更改
systemctl restart NetworkManager
# 验证 br_netfilter 模块是否已加载
if lsmod | grep -q br_netfilter; then
echo "br_netfilter 模块已成功加载"
else
echo "br_netfilter 模块加载失败"
fi
# 验证 IP 转发是否已启用
if sysctl net.ipv4.ip_forward | grep -q "net.ipv4.ip_forward = 1"; then
echo "IP 转发已启用"
else
echo "IP 转发未启用"
fi
echo "配置完成"
4.开启ipvs模块
#!/bin/bash
#安装ipvsadm
yum install ipvsadm -y
# 确保脚本以 root 权限运行
if [ "$(id -u)" != "0" ]; then
echo "此脚本需要 root 权限执行" >&2
exit 1
fi
# 加载 IPVS 模块
modprobe ip_vs || { echo "加载 ip_vs 模块失败"; exit 1; }
# 创建模块加载配置文件以确保模块在启动时自动加载
echo "ip_vs" | tee /etc/modules-load.d/ip_vs.conf
# 创建 IPVS 配置文件
cat <<EOF | tee /etc/sysctl.d/ipvs.conf
ip_vs
ip_vs_rr
ip_vs_wrr
ip_vs_sh
nf_conntrack_ipv4
EOF
# 应用 IPVS 配置
systemctl restart systemd-modules-load.service ; lsmod | grep ip_vs
# 重启网络服务以应用更改
systemctl restart NetworkManager || systemctl restart network
# 验证 IPVS 模块是否已加载
if lsmod | grep -q ip_vs; then
echo "IPVS 模块已成功加载"
else
echo "IPVS 模块加载失败"; exit 1;
fi
# 验证 IP 转发是否已启用
if sysctl net.ipv4.ip_forward | grep -q "net.ipv4.ip_forward = 1"; then
ech

最低0.47元/天 解锁文章
4122






