CentOS 集群初始化设置

0. 前置操作

centos-7.9.2009-isos-x86_64安装包下载_开源镜像站-阿里云

下载CentOS-7-x86_64-DVD-2009.iso即可

1. 配置静态网络

1.1 查看当前网卡
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@k8s-101 ~]# ip address
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether bc:24:11:75:98:ce brd ff:ff:ff:ff:ff:ff
inet 192.168.50.101/24 brd 192.168.50.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet6 fe80::8611:7c0b:51df:bb51/64 scope link tentative noprefixroute dadfailed
valid_lft forever preferred_lft forever
inet6 fe80::4447:3c66:7a30:ad36/64 scope link tentative noprefixroute dadfailed
valid_lft forever preferred_lft forever
inet6 fe80::c575:99dc:8826:b1dd/64 scope link noprefixroute
valid_lft forever preferred_lft forever

网卡名即为etho

1.2 修改网卡配置文件

ifcfg-etho,这个eth0即是上图中的网卡名

1
vi /etc/sysconfig/network-scripts/ifcfg-etho
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=eth0
UUID=24e58ff7-8ef9-46ed-b555-5b157014d3c5
DEVICE=eth0
ONBOOT=yes
IPADDR=192.168.50.101
PREFIX=24
GATEWAY=192.168.50.2
DNS1=192.168.50.2
IPV6_PRIVACY=no
  • 修改BOOTPROTO改为none或者static

  • 新增四项

    1
    2
    3
    4
    5
    IPADDR=192.168.50.101
    PREFIX=24
    # 或者 NETMASK=255.255.255.0 这个和PREFIX=24意思一样
    GATEWAY=192.168.50.2
    DNS1=192.168.50.2

2. 关闭防火墙、SElinux

1
2
3
4
5
6
7
8
9
10
11
## 防火墙
systemctl stop firewalld.service
systemctl disable firewalld.service
# 查看防火墙状态
systemctl status firewalld

## selinux
setenforce
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
# 显示 SElinux 状态
getenforce

3. 修改主机名

1
2
3
4
# 永久
hostnamectl set-hostname xxx
# 生效
bash

4. 配置主机和 IP映射关系

1
2
3
4
5
6
7
8
9
10
11
vi /etc/hosts
## 或者如下方式

# 注意:如果是集群的话,需要配置多个主机 ip 映射!
cat >> /etc/hosts << EFO
192.168.78.150 k8s-master1
192.168.78.151 k8s-master2
192.168.78.152 k8s-master3
192.168.78.153 k8s-node1
192.168.78.154 k8s-node2
EFO

5. 配置阿里网络yum源

5.1 前置操作
1
2
3
4
5
# 安装 wget 工具
yum install -y wget

# 备份本地源
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
5.2 CentOS-Base.repo
1
2
3
4
5
6
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

## 非阿里云ECS要执行
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
epel.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
5.3 docker-ce.repo
1
2
3
4
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo

5.4 kubernetes.repo
1
2
3
4
5
6
7
8
9
10
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF

5.5 yum源准备完毕后,创建元数据
1
2
yum clean all
yum makecache

6. ssh免密

1
2
3
4
5
6
7
8
9
## 生成密钥
ssh-keygen -t rsa

## 公钥复制到其他主机
ssh-copy-id k8s-master1
ssh-copy-id k8s-master2

## 可以测试访问是否成功
ssh k8s-master2

7. 配置时间同步

1
2
3
4
5
6
7
8
9
# 安装 ntpdate 命令
yum install ntpdate -y

# 跟网络时间做同步
ntpdate ntp1.aliyun.com

# 把时间同步做成计划任务
crontab -e
* */1 * * * /usr/sbin/ntpdate ntp1.aliyun.com

8. 下载常用命令

1
yum -y install net-tools tree wget lrzsz vim unzip bash-completion build-essential yum-utils telnet telnet-server device-mapper-persistent-data lvm2 nfs-utils gcc gcc-c++ make cmake libxml2-devel openssl-devel curl curl-devel ntp libaio-devel ncurses-devel autoconf automake zlib-devel python-devel epel-release openssh-server socat conntrack ntpdate ipvsadm sysstat iostat

9. 清除邮件提示消息

1
2
3
4
5
6
# 清除 "您在 /var/spool/mail/root 中有新邮件"信息
echo "unset MAILCHECK" >> /etc/profile
source /etc/profile

# 清空邮箱数据站空间
cat /dev/null > /var/spool/mail/root

10. 升级内核

10.1 查看当前内核版本
1
2
[root@master1 data]# uname -r
3.10.0-957.el7.x86_64
10.2 设置ELRepo源
1
2
3
4
5
## 导入公钥
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org

## 安装yum源
yum install https://www.elrepo.org/elrepo-release-7.el7.elrepo.noarch.rpm
10.3 查看可用内核
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@master1 data]# yum --disablerepo \* --enablerepo elrepo-kernel list available 
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* elrepo-kernel: mirrors.tuna.tsinghua.edu.cn
可安装的软件包
kernel-lt.x86_64 5.4.95-1.el7.elrepo elrepo-kernel
kernel-lt-devel.x86_64 5.4.95-1.el7.elrepo elrepo-kernel
kernel-lt-doc.noarch 5.4.95-1.el7.elrepo elrepo-kernel
kernel-lt-headers.x86_64 5.4.95-1.el7.elrepo elrepo-kernel
kernel-lt-tools.x86_64 5.4.95-1.el7.elrepo elrepo-kernel
kernel-lt-tools-libs.x86_64 5.4.95-1.el7.elrepo elrepo-kernel
kernel-lt-tools-libs-devel.x86_64 5.4.95-1.el7.elrepo elrepo-kernel
kernel-ml.x86_64 5.10.13-1.el7.elrepo elrepo-kernel
kernel-ml-devel.x86_64 5.10.13-1.el7.elrepo elrepo-kernel
kernel-ml-doc.noarch 5.10.13-1.el7.elrepo elrepo-kernel
kernel-ml-headers.x86_64 5.10.13-1.el7.elrepo elrepo-kernel
kernel-ml-tools.x86_64 5.10.13-1.el7.elrepo elrepo-kernel
kernel-ml-tools-libs.x86_64 5.10.13-1.el7.elrepo elrepo-kernel
kernel-ml-tools-libs-devel.x86_64 5.10.13-1.el7.elrepo elrepo-kernel
perf.x86_64 5.10.13-1.el7.elrepo elrepo-kernel
python-perf.x86_64 5.10.13-1.el7.elrepo elrepo-kernel
10.4 安装lt内核
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
## 安装
yum --enablerepo elrepo-kernel -y install kernel-lt

## 查看当前所有内核
grubby --info=ALL

## 设置5.4内核为默认启动内核
grub2-set-default 0
grub2-reboot 0

grep menuentry /boot/efi/EFI/centos/grub.cfg
grub2-set-default 'CentOS Linux (5.4.95-1.el7.x86_64) 7 (Core)'

## 查看修改结果
grub2-editenv list

## 重启服务器
systemctl reboot
10.5 验证内核版本
1
2
3
4
5
[root@master1 ~]# uname -r 
5.4.95-1.el7.elrepo.x86_64

[root@master2 ~]# uname -r
5.4.95-1.el7.elrepo.x86_64

11. 开启内核路由转发功能

1
2
3
4
5
6
7
8
9
10
11
12
13
14
## 临时开始,写入内存
echo 1 > /proc/sys/net/ipv4/ip_forward

## 永久开启写入内核参数
echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf

## 加载配置
sysctl -p

## 验证是否生效
[root@master2 ~]# sysctl -a | grep 'ip_forward'
net.ipv4.ip_forward = 1
net.ipv4.ip_forward_update_priority = 1
net.ipv4.ip_forward_use_pmtu = 0

12. 关闭swap分区

1
2
3
4
5
## 临时关闭
swapoff -a

## 永久关闭
sed -i "s/.*swap.*//" /etc/fstab

CentOS 集群初始化设置
https://itweeks.xyz/2024/03/23/CentOS 集群初始化设置/
作者
Wave Mu
发布于
2024年3月23日
许可协议