KubeSphere 多机部署主机环境初始化与系统优化

KubeSphere 多机部署前,对每台机器做基线初始化与系统优化时读这篇

02-部署安装 / 部署方式 / KubeSphere多机部署
环境初始化主机名hostnamehosts解析时间同步chronydfirewalldiptablesselinuxswap内核参数br_netfilteripvsipvsadmnf_conntrackoverlay2

KubeSphere 多机部署(一)主机环境初始化与系统优化

这是「KubeSphere 多机部署」系列的第 1 篇,概览与目录见 README。 本篇的操作需要在集群每台机器上执行。

步骤 1:环境初始化

修改主机名

hostnamectl set-hostname [新主机名]

1. 检查操作系统版本

# 此方式下安装 kubernetes 集群要求 CentOS 版本在 7.5 或之上
[root@master ~]# cat /etc/redhat-release
CentOS Linux 7.5.1804 (Core)

# 检查网络
ping www.baidu.com
ping 202.108.22.5

2. 主机名解析

为方便集群节点间直接调用,这里配置一下主机名解析;企业中推荐使用内部 DNS 服务器。

如果使用 KubeSphere(KubeKey 会自动处理),可以不用手动配置该项。

# 编辑三台服务器的 /etc/hosts 文件,添加下面内容
vim /etc/hosts
---
192.168.180.110 node110
192.168.180.101 node101
192.168.180.102 node102

3. 时间同步

kubernetes 要求集群中各节点时间必须精确一致,这里使用 chronyd 服务从网络同步时间;企业中建议配置内部时间同步服务器。

# 启动 chronyd 服务
yum -y install chrony
systemctl start chronyd
systemctl enable chronyd
# 手动同步一次
timedatectl set-ntp true
chronyc -a makestep
date

4. 禁用 iptables 和 firewalld 服务

kubernetes 和 docker 运行中会产生大量 iptables 规则,为避免系统规则与它们混淆,直接关闭系统的规则服务。

# 1 关闭 firewalld 服务
systemctl stop firewalld
systemctl disable firewalld

# 2 关闭 iptables 服务
systemctl stop iptables
systemctl disable iptables

5. 禁用 selinux

selinux 是 Linux 下的安全服务,如果不关闭它,安装集群时会产生各种奇怪问题。

# 编辑 /etc/selinux/config,修改 SELINUX 的值为 disabled
# 注意修改完毕后需要重启 Linux
vim /etc/selinux/config
SELINUX=disabled

6. 禁用 swap 分区

swap 分区即虚拟内存分区,物理内存用完后会把磁盘空间虚拟成内存使用,启用 swap 会对系统性能产生明显负面影响,因此 kubernetes 要求每个节点都禁用 swap。如确实不能关闭,需在集群安装时通过明确参数声明。

# 编辑 /etc/fstab,注释掉 swap 分区那一行(修改后需重启)
vim /etc/fstab
# 注释掉 /dev/mapper/centos-swap swap
# /dev/mapper/centos-swap swap

# 检查:free -m 中 swap 全为 0 即配置完成
free -m

# 临时关闭 swap 分区
sudo swapoff -a

7. 修改 Linux 内核参数

# 编辑 /etc/sysctl.d/kubernetes.conf,添加网桥过滤和地址转发功能
vim /etc/sysctl.d/kubernetes.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1

# 重新加载配置
sysctl -p
# 加载网桥过滤模块
modprobe br_netfilter
# 查看网桥过滤模块是否加载成功
lsmod | grep br_netfilter

8. 配置 ipvs 功能

在 Kubernetes 中 Service 有两种代理模型:基于 iptables 和基于 ipvs。两者相比 ipvs 性能更高,但使用前需要手动载入 ipvs 模块。

# 1. 安装 ipset 和 ipvsadm
yum install ipset ipvsadm -y

# 2. 添加需要加载的模块写入脚本文件
mkdir -p /etc/sysconfig/modules/
cat <<EOF> /etc/sysconfig/modules/ipvs.modules
#!/bin/bash
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack
EOF

# 3. 为脚本添加执行权限
chmod +x /etc/sysconfig/modules/ipvs.modules

# 4. 执行脚本
/bin/bash /etc/sysconfig/modules/ipvs.modules

# 5. 查看模块是否加载成功
lsmod | grep -e ip_vs -e nf_conntrack

高内核版本注意:低版本内核(如 3.x)用 nf_conntrack_ipv4,高版本内核(如 5.x)已将其替换为 nf_conntrack。如果执行时报 modprobe: FATAL: Module nf_conntrack_ipv4 not found.,把脚本里的 modprobe -- nf_conntrack_ipv4 改为 modprobe -- nf_conntrack 即可(上面的脚本已用 nf_conntrack)。

9. 重启检查

getenforce && free -m

getenforce 显示 Disabledfree -m 中 swap 全为 0,即基线初始化完成。


系统优化

完成基线初始化后,进一步对系统做优化(同样在每台机器上执行)。

  • 更新系统:

    yum update
    
  • 添加所需的内核引导参数:

    sudo /sbin/grubby --update-kernel=ALL --args='cgroup_enable=memory cgroup.memory=nokmem swapaccount=1'
    
  • 启用 overlay2 内核模块:

    echo "overlay2" | sudo tee -a /etc/modules-load.d/overlay.conf
    
  • 刷新动态生成的 grub2 配置:

    sudo grub2-set-default 0
    
  • 调整内核参数并使其生效:

    cat <<EOF | sudo tee -a /etc/sysctl.conf
    vm.max_map_count = 262144
    fs.may_detach_mounts = 1
    net.ipv4.ip_forward = 1
    vm.swappiness=1
    kernel.pid_max =1000000
    fs.inotify.max_user_instances=524288
    EOF
    sudo sysctl -p
    
  • 调整系统句柄限制:

    vim /etc/security/limits.conf
    *                soft    nofile         1024000
    *                hard    nofile         1024000
    *                soft    memlock        unlimited
    *                hard    memlock        unlimited
    root             soft    nofile         1024000
    root             hard    nofile         1024000
    root             soft    memlock        unlimited
    
  • 删除旧的限制配置:

    sudo rm /etc/security/limits.d/20-nproc.conf
    
  • 重启系统:

    reboot
    

下一步:下载 KubeKey 并创建 K8s 集群,见 02-安装KubeSphere与K8s集群

最后更新 2026-06-30完整文档以官方仓库为准:GitHub Wiki