如果没有特殊说明,以下操作每台服务器上都要执行

一、关闭防火墙

  1. [root@k8s-5-146 ~]# systemctl stop firewalld
  2. [root@k8s-5-146 ~]# systemctl disable firewalld

二、禁用SELINUX

  1. [root@k8s-5-146 ~]# setenforce 0
  2. [root@k8s-5-146 ~]# cat /etc/selinux/config
  3. SELINUX=disabled

三、添加host解析

  1. [root@k8s-5-146 ~]# cat /etc/hosts
  2. 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 k8s-5-146.k8s.host #每天机器解析自己的服务器名到127.0.0.1
  3. ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
  4. # 每台服务器都添加以下内容
  5. 192.168.5.146 k8s-5-146.k8s.host
  6. 192.168.5.147 k8s-5-147.k8s.host
  7. 192.168.5.148 k8s-5-148.k8s.host

四、添加br_netfilter 模块

由于开启内核 ipv4 转发需要加载 br_netfilter 模块,所以加载下该模块:

  1. [root@k8s-5-146 ~]# modprobe br_netfilter

五、配置网桥

bridge-nf 使得 netfilter 可以对 Linux 网桥上的 IPv4/ARP/IPv6 包过滤。比如,设置net.bridge.bridge-nf-call-iptables=1后,二层的网桥在转发包时也会被 iptables的 FORWARD 规则所过滤。常用的选项包括:

  • net.bridge.bridge-nf-call-arptables:是否在 arptables 的 FORWARD 中过滤网桥的 ARP 包
  • net.bridge.bridge-nf-call-ip6tables:是否在 ip6tables 链中过滤 IPv6 包
  • net.bridge.bridge-nf-call-iptables:是否在 iptables 链中过滤 IPv4 包
  • net.bridge.bridge-nf-filter-vlan-tagged:是否在 iptables/arptables 中过滤打了 vlan 标签的包。
  1. [root@k8s-5-146 ~]# vim /etc/sysctl.d/k8s.conf
  2. net.bridge.bridge-nf-call-ip6tables = 1
  3. net.bridge.bridge-nf-call-iptables = 1
  4. net.ipv4.ip_forward = 1
  5. [root@k8s-5-146 ~]# sysctl -p /etc/sysctl.d/k8s.conf

六、安装 ipvs

  1. [root@k8s-5-146 ~]# cat > /etc/sysconfig/modules/ipvs.modules <<EOF
  2. #!/bin/bash
  3. modprobe -- ip_vs
  4. modprobe -- ip_vs_rr
  5. modprobe -- ip_vs_wrr
  6. modprobe -- ip_vs_sh
  7. modprobe -- nf_conntrack_ipv4
  8. EOF
  9. [root@k8s-5-146 ~]# chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules && lsmod | grep -e ip_vs -e nf_conntrack_ipv4

上面脚本创建了的/etc/sysconfig/modules/ipvs.modules文件,保证在节点重启后能自动加载所需模块。使用lsmod | grep -e ip_vs -e nf_conntrack_ipv4命令查看是否已经正确加载所需的内核模块。

七、安装 ipset 软件包 和管理工具

  1. [root@k8s-5-146 ~]# yum install ipset
  2. [root@k8s-5-146 ~]# yum install ipvsadm

八、同步服务器时间

  1. [root@k8s-5-146 ~]# yum install chrony -y
  2. [root@k8s-5-146 ~]# systemctl enable chronyd
  3. [root@k8s-5-146 ~]# systemctl start chronyd
  4. [root@k8s-5-146 ~]# chronyc sources
  5. 210 Number of sources = 4
  6. MS Name/IP address Stratum Poll Reach LastRx Last sample
  7. ===============================================================================
  8. ^+ ntp2.aas.ru 1 10 377 21m -3839us[-4877us] +/- 122ms
  9. ^+ time.cloudflare.com 3 10 377 512 -1258us[-2388us] +/- 106ms
  10. ^+ ntp.xtom.nl 2 10 277 468 +737us[ -398us] +/- 109ms
  11. ^* sv1.ggsrv.de 2 10 377 261 -3826us[-4986us] +/- 113ms

九、关闭 swap 分区

  1. [root@k8s-5-146 ~]# swapoff -a
  2. # 修改/etc/fstab文件,注释掉 SWAP 的自动挂载
  3. [root@k8s-5-146 ~]# cat /etc/fstab
  4. #
  5. # /etc/fstab
  6. # Created by anaconda on Fri Mar 5 22:06:53 2021
  7. #
  8. # Accessible filesystems, by reference, are maintained under '/dev/disk'
  9. # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
  10. #
  11. /dev/mapper/centos-root / xfs defaults 0 0
  12. UUID=9686e40d-a343-41b3-bcaa-a02da3954665 /boot xfs defaults 0 0
  13. # /dev/mapper/centos-swap swap swap defaults 0 0 ###注释掉这一行
  14. ## 使用free -m确认 swap 已经关闭,如果Swap 全部为0,意味着已经关闭
  15. [root@k8s-5-146 ~]# free -m
  16. total used free shared buff/cache available
  17. Mem: 1821 195 789 9 836 1413
  18. Swap: 0 0 0
  19. ## swappiness 参数调整,修改/etc/sysctl.d/k8s.conf添加内容 vm.swappiness=0
  20. [root@k8s-5-146 ~]# cat /etc/sysctl.d/k8s.conf
  21. net.bridge.bridge-nf-call-ip6tables = 1
  22. net.bridge.bridge-nf-call-iptables = 1
  23. net.ipv4.ip_forward = 1
  24. vm.swappiness=0
  25. # 执行sysctl -p /etc/sysctl.d/k8s.conf使修改生效。
  26. [root@k8s-5-146 ~]# sysctl -p /etc/sysctl.d/k8s.conf

十、安装docker

参考文档
安装Docker