k8s系列04

百科知识2025-04-271

本文主要在centos7系统上基于docker和cilium组件部署v1.23.6版本的k8s原生集群,由于集群主要用于自己平时学习和测试使用,加上资源有限,暂不涉及高可用部署。

此前写的一些关于k8s基础知识和集群搭建的一些方案,有需要的同学可以看一下。

1、准备工作

1.1 cilium-集群节点信息

机器均为8C8G的虚拟机,硬盘为100G。

IP Hostname 10.31.188.1 tiny-cilium-master-188-1. 10.31.188.11 tiny-cilium-worker-188-11. 10.31.188.12 tiny-cilium-worker-188-12. 10.188.0.0/18 serviceSubnet

1.2 检查mac和product_uuid

同一个k8s集群内的所有节点需要确保mac地址和product_uuid均唯一,开始集群初始化之前需要检查相关信息

检查mac地址

ip link
ifconfig -a

检查product_uuid

sudo cat /sys/class/dmi/id/product_uuid

1.3 配置ssh免密登录(可选)

如果k8s集群的节点有多个网卡,确保每个节点能通过正确的网卡互联访问

在root用户下面生成一个公用的key,并配置可以使用该key免密登录

su root
ssh-keygen
cd /root/.ssh/
cat id_rsa.pub >> authorized_keys
chmod 600 authorized_keys

cat >> ~/.ssh/config <<EOF
Host tiny-cilium-master-188-1.
HostName 10.31.188.1
User root
Port 22
IdentityFile ~/.ssh/id_rsa

Host tiny-cilium-worker-188-11.
HostName 10.31.188.11
User root
Port 22
IdentityFile ~/.ssh/id_rsa

Host tiny-cilium-worker-188-12.
HostName 10.31.188.12
User root
Port 22
IdentityFile ~/.ssh/id_rsa
EOF

1.4 修改hosts文件
cat >> /etc/hosts <<EOF
10.31.188.1 tiny-cilium-master-188-1 tiny-cilium-master-188-1.
10.31.188.11 tiny-cilium-worker-188-11 tiny-cilium-worker-188-11.
10.31.188.12 tiny-cilium-worker-188-12 tiny-cilium-worker-188-12.
EOF

1.5 关闭swap内存

使用命令直接关闭swap内存

swapoff -a

修改fstab文件禁止开机自动挂载swap分区

sed -i '/swap / s/^(.*)$/#\1/g' /etc/fstab

1.6 配置时间同步

这里可以根据自己的习惯选择ntp或者是chrony同步均可,同步的时间源服务器可以选择阿里云的或者是国家时间中心的。

使用ntp同步

使用yum安装ntpdate工具

yum install ntpdate -y

使用国家时间中心的源同步时间

ntpdate

最后查看一下时间

hwclock

使用chrony同步

使用yum安装chrony

yum install chrony -y

设置开机启动并开启chony并查看运行状态

systemctl enable
systemctl start
systemctl status

当然也可以自定义时间服务器

vim /etc/

修改前

$ grep server /etc/

Use public servers from the project.

server 0. iburst
server 1. iburst
server 2. iburst
server 3. iburst

修改后

$ grep server /etc/

Use public servers from the project.

server iburst

重启服务使配置文件生效

systemctl restart

查看chrony的ntp服务器状态

chronyc sourcestats -v
chronyc sources -v

1.7 关闭selinux

使用命令直接关闭

setenforce 0

也可以直接修改/etc/selinux/config文件

sed -i 's/^SELINUX=enforcing$/SELINUX=disabled/' /etc/selinux/config

1.8 配置防火墙

k8s集群之间通信和服务暴露需要使用较多端口,为了方便,直接禁用防火墙

centos7使用systemctl禁用默认的firewalld服务

systemctl disable

1.9 配置netfilter参数

这里主要是需要配置内核加载br_netfilter和iptables放行ipv6和ipv4的流量,确保集群内的容器能够正常通信。

cat <<EOF | sudo tee /etc//
br_netfilter
EOF

cat <<EOF | sudo tee /etc//
-nf-call-ip6tables = 1
-nf-call-iptables = 1
EOF
sudo sysctl --system

1.10 关闭IPV6(不建议)

和之前部署其他的CNI不一样,cilium很多服务监听默认情况下都是双栈的(使用cilium-cli操作的时候),因此建议开启系统的IPV6网络支持(即使没有可用的IPV6路由也可以)

当然没有ipv6网络也是可以的,只是在使用cilium-cli的一些开启port-forward命令时会报错而已。

直接在内核中添加ipv6禁用参数

grubby --update-kernel=ALL --args==1

1.11 配置IPVS(建议)

IPVS是专门设计用来应对负载均衡场景的组件,kube-proxy 中的 IPVS 实现通过减少对 iptables 的使用来增加可扩展性。在 iptables 输入链中不使用 PREROUTING,而是创建一个假的接口,叫做 kube-ipvs0,当k8s集群中的负载均衡配置变多的时候,IPVS能实现比iptables更高效的转发性能。

因为cilium需要升级系统内核,因此这里的内核版本高于4.19

注意在4.19之后的内核版本中使用nf_conntrack模块来替换了原有的nf_conntrack_ipv4模块

(Notes: use nf_conntrack instead of nf_conntrack_ipv4 for Linux kernel 4.19 and later)

在使用ipvs模式之前确保安装了ipset和ipvsadm

sudo yum install ipset ipvsadm -y

手动加载ipvs相关模块

modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack

配置开机自动加载ipvs相关模块

cat <<EOF | sudo tee /etc//
ip_vs
ip_vs_rr
ip_vs_wrr
ip_vs_sh
nf_conntrack
EOF

sudo sysctl --system

最好重启一遍系统确定是否生效

$ lsmod | grep -e ip_vs -e nf_conntrack
nf_conntrack_netlink 49152 0
nfnetlink 20480 2 nf_conntrack_netlink
ip_vs_sh 16384 0
ip_vs_wrr 16384 0
ip_vs_rr 16384 0
ip_vs 159744 6 ip_vs_rr,ip_vs_sh,ip_vs_wrr
nf_conntrack 159744 5 xt_conntrack,nf_nat,nf_conntrack_netlink,xt_MASQUERADE,ip_vs
nf_defrag_ipv4 16384 1 nf_conntrack
nf_defrag_ipv6 24576 2 nf_conntrack,ip_vs
libcrc32c 16384 4 nf_conntrack,nf_nat,xfs,ip_vs
$ cut -f1 -d " " /proc/modules | grep -e ip_vs -e nf_conntrack
nf_conntrack_netlink
ip_vs_sh
ip_vs_wrr
ip_vs_rr
ip_vs
nf_conntrack

1.12 配置Linux内核(cilium必选)

cilium和其他的cni组件最大的不同在于其底层使用了ebpf技术,而该技术对于Linux的系统内核版本有较高的要求,完成的要求可以查看官网的详细链接,这里我们着重看内核版本、内核参数这两个部分。

Linux内核版本

默认情况下我们可以参考cilium官方给出的一个系统要求总结。因为我们是在k8s集群中部署(使用容器),因此只需要关注Linux内核版本和etcd版本即可。根据前面部署的经验我们可以知道1.23.6版本的k8s默认使用的etcd版本是3.5.+,因此重点就来到了Linux内核版本这里。

Requirement Minimum Version In cilium container Linux kernel >= 4.9.17 no Key-Value store (etcd) >= 3.1.0 no clang+LLVM >= 10.0 yes iproute2 >= 5.9.0 yes

This requirement is only needed if you run cilium-agent natively. If you are using the Cilium container image cilium/cilium, clang+LLVM is included in the container image.

iproute2 is only needed if you run cilium-agent directly on the host machine. iproute2 is included in the cilium/cilium container image.

毫无疑问CentOS7内置的默认内核版本3.版本的内核是无法满足需求的,但是在升级内核之前,我们再看看其他的一些要求。

cilium官方还给出了一份列表描述了各项高级功能对内核版本的要求:

Cilium Feature Minimum Kernel Version IPv4 fragment handling >= 4.10 Restrictions on unique prefix lengths for CIDR policy rules >= 4.11 IPsec Transparent Encryption in tunneling mode >= 4.19 WireGuard Transparent Encryption >= 5.6 Host-Reachable Services >= 4.19.57, >= 5.1.16, >= 5.2 Kubernetes Without kube-proxy >= 4.19.57, >= 5.1.16, >= 5.2 Bandwidth Manager >= 5.1 Local Redirect Policy (beta) >= 4.19.57, >= 5.1.16, >= 5.2 Full support for Session Affinity >= 5.7 BPF-based proxy redirection >= 5.7 BPF-based host routing >= 5.10 Socket-level LB bypass in pod netns >= 5.7 Egress Gateway (beta) >= 5.2 VXLAN Tunnel Endpoint (VTEP) Integration >= 5.2

可以看到如果需要满足上面所有需求的话,需要内核版本高于5.10,本着学习测试研究作死的精神,反正都升级了,干脆就升级到新一些的版本吧。这里我们可以直接使用elrepo源来升级内核到较新的内核版本。

查看elrepo源中支持的内核版本

$ yum --disablerepo="*" --enablerepo="elrepo-kernel" list available
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Available Packages
7.0-5. elrepo-kernel
kernel-lt.x86_64 5.4.192-1. elrepo-kernel
kernel-lt-devel.x86_64 5.4.192-1. elrepo-kernel
5.4.192-1. elrepo-kernel
kernel-lt-headers.x86_64 5.4.192-1. elrepo-kernel
kernel-lt-tools.x86_64 5.4.192-1. elrepo-kernel
kernel-lt-tools-libs.x86_64 5.4.192-1. elrepo-kernel
kernel-lt-tools-libs-devel.x86_64 5.4.192-1. elrepo-kernel
kernel-ml.x86_64 5.17.6-1. elrepo-kernel
kernel-ml-devel.x86_64 5.17.6-1. elrepo-kernel
5.17.6-1. elrepo-kernel
kernel-ml-headers.x86_64 5.17.6-1. elrepo-kernel
kernel-ml-tools.x86_64 5.17.6-1. elrepo-kernel
kernel-ml-tools-libs.x86_64 5.17.6-1. elrepo-kernel
kernel-ml-tools-libs-devel.x86_64 5.17.6-1. elrepo-kernel
perf.x86_64 5.17.6-1. elrepo-kernel
python-perf.x86_64 5.17.6-1. elrepo-kernel

看起来ml版本的内核比较满足我们的需求,直接使用yum进行安装

sudo yum --enablerepo=elrepo-kernel install kernel-ml -y

使用grubby工具查看系统中已经安装的内核版本信息

sudo grubby --info=ALL
#