云计算、AI、云原生、大数据等一站式技术学习平台

网站首页 > 教程文章 正文

kubernetes1.31.3集群搭建(上)

jxf315 2024-12-10 16:19:55 教程文章 41 ℃

1 集群规划

1.1 物理机环境

电脑

操作系统

CPU

内存

硬盘

网卡

IP地址(静态)

虚拟机软件

服务器操作系统

联想

Windows11

Intel 12900K 24核

128GB

4TB PcIE4.0

无线网卡

192.168.3.6

VMware WorkStation Pro 17.6.1

RockyLinux9.5

微星

Windows11

Intel 12900hx 24核

64GB

4TB PcIE4.0

无线网卡

192.168.3.197

VMware WorkStation Pro 17.6.1

RockyLinux9.5

1.3 K8S集群节点配置

电脑

节点

CPU

内存

硬盘

网络连接模式

IP地址

微星

k8s-master-1

8核

16G

200G

桥接模式

192.168.3.60

k8s-master-2

8核

16G

200G

桥接模式

192.168.3.61

k8s-master-3

8核

16G

200G

桥接模式

192.168.3.62

联想

k8s-node-1

16核

32G

200G

桥接模式

192.168.3.70

k8s-node-2

16核

32G

200G

桥接模式

192.168.3.71

k8s-node-3

16核

32G

200G

桥接模式

192.168.3.72

1.4 K8s版本

K8s版本:https://github.com/kubernetes/kubernetes/releases

目前主流的K8s版本有1.31、1.30、1.29、1.29、1.27五个大版本,本次我们选择1.31.3

  • 测试环境可以选择最新的正式版本,例如选择1.31.3或者1.30.6
  • 生产环境为了稳定,建议选择小版本大于5的K8s版本,例如选择1.30.6

软件

版本

HA Proxy

HAProxy version 2.4.22-f8e3218

Keepliaved

Keepalived v2.2.8 (04/04,2023)

docker

docker-ce-3:27.3.1-1.el9.x86_64

containerd

containerd.io-1.7.24-3.1.el9.x86_64

K8s

1.31.3

1.5 集群规划注意事项

  1. 网络配置

请确认你的IP地址、子网掩码和默认网关

  1. 合理使用虚拟机快照功能

K8s集群搭建大致分成15大步,如果不能确保K8s集群一次性搭建成功,那么建议每完成1大步拍摄快照,集群搭建失败时方便恢复快照


3 K8s节点基本配置及配置优化

3.1 所有节点配置yum源

https://developer.aliyun.com/mirror/rockylinux

sed -e 's|^mirrorlist=|#mirrorlist=|g' \
    -e 's|^#baseurl=http://dl.rockylinux.org/$contentdir|baseurl=https://mirrors.aliyun.com/rockylinux|g' \
    -i.bak \
    /etc/yum.repos.d/rocky*.repo
  1. 它会将所有以 mirrorlist= 开始的行注释掉(在行首添加一个 # 符号)。
  2. 它会将所有原本被注释掉(以 #baseurl=http://dl.rockylinux.org/$contentdir 开始)的行取消注释,并且更改 URL 为阿里云的镜像源地址 (https://mirrors.aliyun.com/rockylinux)。
  3. 同时它会对每个被修改的文件创建一个备份,备份文件会在原文件名后加上 .bak 后缀。
dnf makecache

3.2 所有节点必备工具安装

dnf install  wget jq psmisc vim net-tools telnet yum-utils device-mapper-persistent-data lvm2 git rsyslog -y

您正在尝试在基于Red Hat的系统(如CentOS或Fedora)上使用dnf包管理器来安装一系列软件包。以下是命令的解析:

  • wget: 用于从网络下载文件。
  • jq: 用于处理JSON数据的命令行工具。
  • psmisc: 包含一些进程管理工具,如killall。
  • vim: 一个高度可配置的文本编辑器。
  • net-tools: 包含网络配置工具,如ifconfig。
  • telnet: 用于通过Telnet协议连接到远程主机。
  • yum-utils: 提供一些辅助工具,如package-cleanup和repoquery。
  • device-mapper-persistent-data : 是一个用于逻辑卷管理(LVM)和设备映射器(Device Mapper)的软件包。
  • lvm2: 用于逻辑卷管理(LVM)的工具。
  • git: 分布式版本控制系统。
  • rsyslog: 增强的日志处理系统。

3.3 所有节点配置hosts

vim /etc/hosts
192.168.3.60 k8s-master-01
192.168.3.61 k8s-master-02
192.168.3.62 k8s-master-03

192.168.3.70 k8s-node-01
192.168.3.71 k8s-node-02
192.168.3.72 k8s-node-03


修改完成后测试指定的主机名是否能够ping通,ping通过后使用Ctrl+c停止ping

ping k8s-master-02 -c 3
ping k8s-master-03 -c 3
ping k8s-node-01 -c 3
ping k8s-node-02 -c 3
ping k8s-node-03 -c 3

3.4 所有节点禁用防火墙、SELinux、swap分区

  1. 所有节点禁用防火墙
systemctl disable --now firewalld
  1. 所有节点禁用SELinux
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
  1. 所有节点禁用swap分区
swapoff -a && sysctl -w vm.swappiness=0
sed -ri '/^[^#]*swap/s@^@#@' /etc/fstab

3.5 所有节点开启rsyslog

systemctl enable --now rsyslog
 tail -f /var/log/messages #实时查看系统日志 使用Ctrl+C中断

3.6 所有节点同步时间

ntpdate方案和chrony方案二选一即可,我这里选ntpdate

3.6.1 ntpdate方案

  1. 安装并启用Extra Packages for Enterprise Linux (EPEL)仓库
dnf install epel-release -y 
dnf config-manager --set-enable epel
  1. 安装ntpdate
dnf install ntpsec -y
  1. 配置时区为亚洲上海
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 
echo 'Asia/Shanghai' > /etc/timezone 
  1. 连接阿里云的NTP服务器同步系统时间
ntpdate time2.aliyun.com 
  1. 编辑当前用户的定时任务
crontab -e 
  1. 设置每5分钟连接到阿里云的NTP服务器 time2.aliyun.com 并同步系统时间
*/5 * * * *  /usr/sbin/ntpdate time2.aliyun.com

3.6.2 chrony方案(没有测试)

  1. 安装chrony (Rocky Linux 9 默认包含chrony)
dnf install chrony
  1. 配置chrony使用NTP服务器

编辑 /etc/chrony.conf 文件来添加或修改 NTP 服务器。您可以选择公共 NTP 服务器或者使用阿里云提供的 NTP 服务器。

添加阿里云的 NTP 服务器

 vim /etc/chrony.conf
pool ntp1.aliyun.com iburst
pool ntp2.aliyun.com iburst
pool cn.pool.ntp.org iburst
  1. 设置时区为亚洲上海
timedatectl set-timezone Asia/Shanghai
  1. 启动并启用Chrony服务
systemctl enable chronyd
systemctl start chronyd
  1. 验证chrony的工作状态
 chronyc tracking # 查看本地系统时间与NTP服务器之间的偏移量
  1. 强制立即时间同步
chronyc makestep

3.7 所有节点配置limit

ulimit -SHn 65535
vim /etc/security/limits.conf

在文件末尾添加如下内容

* soft nofile 65536
* hard nofile 131072
* soft nproc 65535
* hard nproc 655350
* soft memlock unlimited
* hard memlock unlimited
  • nofile:控制单个进程可以打开的最大文件描述符数量。
    • soft nofile 65536:软限制为 65536,表示普通情况下一个进程可以打开的最大文件数。
    • hard nofile 131072:硬限制为 131072,表示即使有权限提升,一个进程最多也只能打开这么多文件。
  • nproc:限制一个用户可以启动的最大进程数。
    • soft nproc 65535:软限制为 65535,表示普通情况下该用户可以创建的最大进程数。
    • hard nproc 655350:硬限制为 655350,表示即使有权限提升,该用户最多也只能创建这么多进程。
  • memlock:设置最大锁定内存地址空间(以字节为单位),即允许程序锁定在物理内存中不被交换出去的数据量。
    • soft memlock unlimited 和 hard memlock unlimited:意味着没有对锁定内存大小的限制,程序可以根据需要锁定任意量的内存。

3.8 所有节点所有已安装软件包到最新版本

此过程会比较耗时

dnf update -y 

3.9 k8s-master-01节点免密码登录

k8s-master-01节点免密钥登录其他节点,安装过程中生成配置文件和证书均在Master01上操作,集群管理也在Master01上操作,阿里云或者AWS上需要单独一台kubectl服务器。

  1. 生成密钥
ssh-keygen -t rsa

执行ssh-keygen -t rsa后回车三次

  1. 将本地的 SSH 公钥复制到多个远程主机上
for i in k8s-master-01 k8s-master-02 k8s-master-03 k8s-node-01 k8s-node-02 k8s-node-03; do ssh-copy-id -i .ssh/id_rsa.pub $i;done

因为有6台服务器,需要输入6次yes和其他实例的密码

配置完成后从k8s-master-01使用ssh免密钥登录其他节点

ssh k8s-master-02

按Ctrl+D退出

3.10 k8s-master-01节点下载安装文件

cd /root; git clone https://gitee.com/boy_tony_cto/k8s-ha-install.git


K8s节点内核配置优化

4.1 所有节点安装ipvsadm

dnf install ipvsadm ipset sysstat conntrack libseccomp -y
  • ipvsadm:用于管理和配置 IP 虚拟服务器 (IPVS),它实现了 Linux 内核中的负载均衡功能。这对于设置高可用性和负载均衡集群非常有用。
  • ipset:允许创建和管理 IP 地址或端口集合,可以与 iptables 结合使用来简化复杂的防火墙规则。
  • sysstat:提供了一套工具用于监控系统性能,包括 CPU、内存、磁盘 I/O 和网络等资源的使用情况。常用的工具如 sar、iostat、mpstat 都属于这个套件。
  • conntrack:用于查看和操作 netfilter/iptables 连接跟踪表,这对于调试和优化 NAT 或防火墙规则非常有帮助。
  • libseccomp:是一个库,提供了简单易用的接口来限制应用程序的系统调用,增强了系统的安全性。

4.2 所有节点配置ipvs模块

modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack
  1. modprobe -- ip_vs
    • 加载核心的 IPVS 模块,这是实现 IP 虚拟服务器功能的基础。
  1. modprobe -- ip_vs_rr
    • 加载轮询调度算法(Round Robin Scheduling Algorithm),这是一种简单的负载均衡算法,依次将请求分发给后端服务器。
  1. modprobe -- ip_vs_wrr
    • 加载加权轮询调度算法(Weighted Round Robin Scheduling Algorithm),它根据权重分配流量,权重越高的服务器会收到更多的请求。
  1. modprobe -- ip_vs_sh
    • 加载基于源地址哈希的调度算法(Source Hashing Scheduling Algorithm),该算法确保来自同一客户端的请求总是被发送到同一台服务器。
  1. modprobe -- nf_conntrack
    • 加载 Netfilter 连接跟踪模块,这对于状态防火墙、NAT 和其他需要跟踪连接状态的功能至关重要。

4.3 所有节点创建ipvs.conf,并配置开机自动加载

vim /etc/modules-load.d/ipvs.conf
ip_vs
ip_vs_lc
ip_vs_wlc
ip_vs_rr
ip_vs_wrr
ip_vs_lblc
ip_vs_lblcr
ip_vs_dh
ip_vs_sh
ip_vs_fo
ip_vs_nq
ip_vs_sed
ip_vs_ftp
ip_vs_sh
nf_conntrack
ip_tables
ip_set
xt_set
ipt_set
ipt_rpfilter
ipt_REJECT
ipip

确保在系统启动时自动加载指定内核模块,并立即启动该服务的命令

systemctl enable --now systemd-modules-load.service

如下报错不用管

4.4 所有节点内核优化配置

  1. 编辑k8s.conf
cat <<EOF > /etc/sysctl.d/k8s.conf
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
fs.may_detach_mounts = 1
net.ipv4.conf.all.route_localnet = 1
vm.overcommit_memory=1
vm.panic_on_oom=0
fs.inotify.max_user_watches=89100
fs.file-max=52706963
fs.nr_open=52706963
net.netfilter.nf_conntrack_max=2310720

net.ipv4.tcp_keepalive_time = 600
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.tcp_keepalive_intvl =15
net.ipv4.tcp_max_tw_buckets = 36000
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_max_orphans = 327680
net.ipv4.tcp_orphan_retries = 3
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.ip_conntrack_max = 65536
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_timestamps = 0
net.core.somaxconn = 16384
EOF
  1. 所有节点应用配置
sysctl --system
  1. 所有节点配置完内核以后,重启机器
reboot
  1. 查看内核模块是否已经自动加载
lsmod |grep --color=auto -e ip_vs -e nf_conntrack #列出当前已加载的内核模块,并高亮显示包含 ip_vs 或 nf_conntrack 的行


5 K8s集群高可用组件安装

注意:如果安装的不是高可用集群,haproxy和keepalived无需要安装

注意:公有云要有 公有云自带的负载均衡,比如阿里云的SLB、NLB,腾讯云的ELB,用来替代haproxy和keepalived,因为公有云大部分不支持keepalived

5.1 所有Master节点安装、配置HAProxy

Master节点包括(k8s-master-01、k8s-master-02、k8s-master-03)

dnf install  haproxy -y

所有Master节点配置HAProxy,所有Master节点的HAProxy配置相同

  1. 备份HAProxy配置文件
cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bak
  1. 清空HAProxy配置文件
> /etc/haproxy/haproxy.cfg  
  1. 编辑HAProxy配置文件
vim /etc/haproxy/haproxy.cfg
global
  maxconn  2000
  ulimit-n  16384
  log  127.0.0.1 local0 err
  stats timeout 30s

defaults
  log global
  mode  http
  option  httplog
  timeout connect 5000
  timeout client  50000
  timeout server  50000
  timeout http-request 15s
  timeout http-keep-alive 15s

frontend monitor-in
  bind *:33305
  mode http
  option httplog
  monitor-uri /monitor

frontend k8s-master
  bind 0.0.0.0:16443
  bind 127.0.0.1:16443
  mode tcp
  option tcplog
  tcp-request inspect-delay 5s
  default_backend k8s-master

backend k8s-master
  mode tcp
  option tcplog
  option tcp-check
  balance roundrobin
  default-server inter 10s downinter 5s rise 2 fall 2 slowstart 60s maxconn 250 maxqueue 256 weight 100
  server k8s-master-01	192.168.3.60:6443  check
  server k8s-master-02	192.168.3.61:6443  check
  server k8s-master-03	192.168.3.62:6443  check

全局设置 (global)

  • maxconn 2000: 设置HAProxy进程可接受的最大并发连接数为2000。
  • ulimit-n 16384: 设置每个HAProxy进程可以打开的最大文件描述符数量为16384,这对于处理大量并发连接非常重要。
  • log 127.0.0.1 local0 err: 指定日志记录的目标和级别,这里只记录错误级别的日志。
  • stats timeout 30s: 设置统计数据收集的超时时间为30秒。

默认设置 (defaults)

这些设置适用于所有未明确指定其他行为的前端和后端部分。

  • mode http: 定义默认模式为HTTP层(第7层)。
  • log global: 使用全局定义的日志设置。
  • option httplog: 启用HTTP请求的日志记录。
  • timeout connect 5000: 设置与服务器建立连接的超时时间。
  • timeout client 50000: 设置客户端连接的超时时间。
  • timeout server 50000: 设置服务器端连接的超时时间。
  • timeout http-request 15s: HTTP请求的超时时间。
  • timeout http-keep-alive 15s: HTTP保持连接的超时时间。

前端 (frontend) 配置

监控页面 (monitor-in)

  • bind *:33305: 监听所有网络接口上的33305端口。
  • mode http: 操作模式为HTTP。
  • option httplog: 启用HTTP请求的日志记录。
  • monitor-uri /monitor: 提供一个监控检查点,通过访问/monitor来检测HAProxy是否正常运行。

Kubernetes Master (k8s-master)

  • bind 0.0.0.0:16443 和 bind 127.0.0.1:16443: 监听所有网络接口以及本地回环接口上的16443端口。
  • mode tcp: 操作模式为TCP层(第4层),因为Kubernetes API通信是基于TLS加密的。
  • option tcplog: 启用TCP流量的日志记录。
  • tcp-request inspect-delay 5s: 设置TCP请求的检查延迟为5秒,以便有足够的时间去检查TLS握手信息。
  • default_backend k8s-master: 将所有接收到的请求转发给名为k8s-master的后端。

后端 (backend) 配置

Kubernetes Master (k8s-master)

  • mode tcp: 操作模式为TCP层。
  • option tcplog: 启用TCP流量的日志记录。
  • option tcp-check: 启用对后端服务器的TCP健康检查。
  • balance roundrobin: 使用轮询算法在后端服务器之间分配连接。
  • default-server ...: 定义了所有服务器的默认参数,包括检查间隔、失败次数、最大连接数等。
  • server ... check: 列出了后端服务器及其健康检查配置。

此配置文件已经很好地设置了一个面向Kubernetes集群的HAProxy实例,它不仅能够提供高可用性,还能够通过监控页面方便地进行状态检查。如果您有任何特定的需求或需要进一步优化,请根据实际情况调整配置。

5.2 所有Master节点安装、配置Keepalived

5.2.1 所有Master节点安装Keepalived

dnf install  keepalived -y

5.2.2 所有Master节点配置Keepalived

所有Master节点的Keepalived配置不相同

  1. 备份Keepalived配置文件
cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
  1. 清空Keepalived配置文件
 > /etc/keepalived/keepalived.conf


  1. 配置k8s-master-01节点keepalived
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
    router_id LVS_DEVEL
    script_user root
    enable_script_security
}
vrrp_script chk_apiserver {
    script "/etc/keepalived/check_apiserver.sh"
    interval 5
    weight -5
    fall 2  
    rise 1
}
vrrp_instance VI_1 {
    state MASTER
    interface ens160
    mcast_src_ip 192.168.3.60
    virtual_router_id 51
    priority 101
    advert_int 2
    authentication {
        auth_type PASS
        auth_pass K8SHA_KA_AUTH
    }
    virtual_ipaddress {
        192.168.3.50
    }
    track_script {
       chk_apiserver
    }
}
  1. 配置k8s-master-02节点keepalived
vim /etc/keepalived/keepalived.conf
global_defs {
    router_id LVS_DEVEL
    script_user root
    enable_script_security
}
vrrp_script chk_apiserver {
    script "/etc/keepalived/check_apiserver.sh"
    interval 5
    weight -5
    fall 2  
    rise 1
}
vrrp_instance VI_1 {
    state BACKUP
    interface ens160
    mcast_src_ip 192.168.3.61
    virtual_router_id 51
    priority 100
    advert_int 2
    authentication {
        auth_type PASS
        auth_pass K8SHA_KA_AUTH
    }
    virtual_ipaddress {
        192.168.3.50
    }
    track_script {
       chk_apiserver
    }
}
  1. 配置k8s-master-03节点keepalived
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
    router_id LVS_DEVEL
    script_user root
    enable_script_security
}
vrrp_script chk_apiserver {
    script "/etc/keepalived/check_apiserver.sh"
    interval 5
    weight -5
    fall 2  
    rise 1
}
vrrp_instance VI_1 {
    state BACKUP
    interface ens160
    mcast_src_ip 192.168.3.62
    virtual_router_id 51
    priority 100
    advert_int 2
    authentication {
        auth_type PASS
        auth_pass K8SHA_KA_AUTH
    }
    virtual_ipaddress {
        192.168.3.50
    }
    track_script {
       chk_apiserver
    }
}

global_defs 段

  • router_id LVS_DEVEL: 定义了一个唯一的路由器 ID。
  • script_user root: 指定执行脚本的用户为 root。请确保这是必要的,因为以 root 权限运行脚本可能存在安全风险。
  • enable_script_security: 启用脚本安全功能。

vrrp_script 段

  • chk_apiserver: 定义了一个 VRRP 脚本,用于检查 API 服务器的状态。
    • script "/etc/keepalived/check_apiserver.sh": 执行的脚本路径。
    • interval 5: 每5秒检查一次。
    • weight -5: 如果脚本失败,则将优先级减少5。
    • fall 2: 当连续两次检查失败时,认为服务不可用。
    • rise 1: 当一次检查成功后,认为服务可用。

vrrp_instance VI_1 段

  • state MASTER: 此实例被设置为 Master 状态。 state BACKUP:初始状态被设置为BACKUP,意味着这个节点准备作为备用。
  • interface ens160: 使用 ens160 接口进行 VRRP 广播。
  • mcast_src_ip 192.168.3.62: 设置多播源 IP 地址为 192.168.3.62。
  • virtual_router_id 51: 定义虚拟路由器 ID。
  • priority 101: 设置优先级。
  • advert_int 2: 设置通告间隔为2秒。
  • authentication: 定义认证信息。
    • auth_type PASS: 使用密码认证。
    • auth_pass K8SHA_KA_AUTH: 认证密码,请确保这是一个强密码。
  • virtual_ipaddress: 定义虚拟 IP 地址为 192.168.3.50。
  • track_script: 引用之前定义的 chk_apiserver 脚本。

5.2.3 所有Master节点配置keepalived健康检查文件

  1. 编辑keepalived健康检查文件
 vim /etc/keepalived/check_apiserver.sh
#!/bin/bash
# 初始化错误计数器
err=0
# 尝试检查haproxy进程最多三次
for k in $(seq 1 3 )
do
    check_code=$(pgrep haproxy)
    if [[ "$check_code" == "" ]]; then
        err=$(expr $err+1)
        sleep 1
        continue
    else
        err=0
        break
    fi
done
# 如果三次尝试后仍未找到haproxy进程,则停止keepalived服务
if [[ $err != "0" ]]; then
    echo "systemctl stop keepalived"
    /usr/bin/systemctl stop keepalived
    exit 1
else 
    exit 0
fi

  1. check_apiserver.sh脚本添加执行权限
chmod u+x /etc/keepalived/check_apiserver.sh

5.3 所有master节点启动haproxy和keepalived

  1. 重新加载 systemd 的单元配置文件
systemctl daemon-reload
  1. 启用并立即启动 HAProxy 服务
systemctl enable --now haproxy
  1. 启用并立即启动 Keepalived 服务
systemctl enable --now keepalived

5.4 所有节点测试vip

ping 192.168.3.50 -c 4


telnet 192.168.3.50 16443

如果 ping不通且 telnet 没有出现 ] ,则认为 VIP 不可以,不可在继续往下执行,

需要排查keepalived 的问题,比如防火墙和 selinux,haproxy 和 keepalived 的状态,监听端口等

所有节点查看防火墙状态必须为disable和inactive:systemctl status firewalld

所有节点查看selinux状态,必须为disable:getenforce

master节点查看haproxy和keepalived状态:systemctl status keepalived haproxy

master节点查看监听端口:netstat-lntp

如果以上都没有问题,需要确认:

  1. 是否是公有云机器
  2. 是否是私有云机器(类似0penStack)

上述公有云一般都是不支持keepalived,私有云可能也有限制,需要和自己的私有云管理员咨询


6 K8s Runtime安装

如果安装的版本低于1.24,选择Docker和Containerd均可,高于1.24选择Containerd作为Runtime。


6.1 所有节点配置安装源

dnf config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

6.2 所有节点安装docker-ce、containerd

 dnf install docker-ce containerd -y



可以无需启动Docker,只需要配置和启动Containerd即可。

6.3 所有节点配置Containerd

  1. 配置Containerd所需的模块
cat <<EOF | sudo tee /etc/modules-load.d/containerd.conf
overlay
br_netfilter
EOF
  1. 加载模块
modprobe -- overlay
modprobe -- br_netfilter
  1. 配置Containerd所需的内核
cat <<EOF | sudo tee /etc/sysctl.d/99-kubernetes-cri.conf
net.bridge.bridge-nf-call-iptables  = 1
net.ipv4.ip_forward                 = 1
net.bridge.bridge-nf-call-ip6tables = 1
EOF
  1. 加载内核
sysctl --system
  1. 配置Containerd的配置文件
mkdir -p /etc/containerd
containerd config default | tee /etc/containerd/config.toml
  1. 将Containerd的Cgroup改为Systemd
sed -i 's#SystemdCgroup = false#SystemdCgroup= true #g' /etc/containerd/config.toml
sed -i 's#k8s.gcr.io/pause#registry.cn-hangzhou.aliyuncs.com/google_containers/pause#g' /etc/containerd/config.toml
sed -i 's#registry.gcr.io/pause#registry.cn-hangzhou.aliyuncs.com/google_containers/pause#g' /etc/containerd/config.toml
sed -i 's#registry.k8s.io/pause#registry.cn-hangzhou.aliyuncs.com/google_containers/pause#g' /etc/containerd/config.toml
vim /etc/containerd/config.toml

你提供的 sed 命令用于修改 /etc/containerd/config.toml 文件中的特定配置项。这些命令将:

  • 将 SystemdCgroup = false 修改为 SystemdCgroup = true。
  • 将所有出现的 k8s.gcr.io/pause 替换为 registry.cn-hangzhou.aliyuncs.com/google_containers/pause。
  • 将所有出现的 registry.gcr.io/pause 替换为 registry.cn-hangzhou.aliyuncs.com/google_containers/pause。
  • 将所有出现的 registry.k8s.io/pause 替换为 registry.cn-hangzhou.aliyuncs.com/google_containers/pause。


  1. 启动Containerd,并配置开机启动
systemctl daemon-reload
systemctl enable --now containerd
  1. 检查containerd状态是否正常
ctr plugin ls

确认ID为overlayfscri的状态必须OK

  1. 配置crictl客户端连接的运行时位置(可选)
cat > /etc/crictl.yaml <<EOF
runtime-endpoint: unix:///run/containerd/containerd.sock
image-endpoint: unix:///run/containerd/containerd.sock
timeout: 10
debug: false
EOF

安装完成k8s后,会同步安装crictl

没有安装k8s前使用crictl会提示command not found


7 K8s集群组件安装

7.1 所有节点配置源(注意版本号)

https://developer.aliyun.com/mirror/kubernetes

  1. 阿里云k8s镜像仓库配置
cat <<EOF | tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes-new/core/stable/v1.31/rpm/
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes-new/core/stable/v1.31/rpm/repodata/repomd.xml.key
EOF
  1. k8s-master-01节点查看最新的Kubernates版本是多少
dnf list kubeadm.x86_64 --showduplicates | sort -r

7.2 所有节点安装1.31最新版本kubeadm、kubelet和kubectl

注意:公有云环境下,可能需要将kubectl放在一个非master节点上

dnf install kubeadm-1.31* kubelet-1.31* kubectl-1.31* -y

7.3所有节点设置Kubelet开机自启动

  1. 所有节点设置Kubelet开机自启动
systemctl enable --now kubelet
  1. 查看系统日志,Ctrl+c中断查看
tail -f /var/log/messages 

由于还未初始化,没有kubelet的配置文件,此时kubelet无法启动,无需管理

Tags:

最近发表
标签列表