xiabingbao

博客园 首页 新随笔 联系 订阅 管理

之前遇到因为底层的虚拟化平台使用vxlan的8472端口,k3s默认也是。导致了我的k3s跨节点无法正常通信。下面是具体的修改过程。
首先在k3s的配置目录中创建个flannel的网络配置文件

  • server部分的修改
#创建k3s网络配置文件
vim /etc/rancher/k3s/flannel-config.json
#需要修改下面内容为实际的ip地址端和vxlan的端口
{
  "Network": "10.42.0.0/24",
  "Backend": {
    "Type": "vxlan",
    "VNI": 1,
    "Port": 9877  
  }
}

然后接下来需要对k3s的service进行修改并重新启动k3s服务

#操作前停止k3s服务
systemctl stop k3s

vim /etc/systemd/system/k3s.service

#下面是service文件,最后一行的--flannel-conf是后加入的,其它的保持原本的内容不要动
[Unit]
Description=Lightweight Kubernetes
Documentation=https://k3s.io
Wants=network-online.target
After=network-online.target

[Install]
WantedBy=multi-user.target

[Service]
Type=notify
EnvironmentFile=-/etc/default/%N
EnvironmentFile=-/etc/sysconfig/%N
EnvironmentFile=-/etc/systemd/system/k3s.service.env
KillMode=process
Delegate=yes
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=1048576
LimitNPROC=infinity
LimitCORE=infinity
TasksMax=infinity
TimeoutStartSec=0
Restart=always
RestartSec=5s
ExecStartPre=/bin/sh -xc '! /usr/bin/systemctl is-enabled --quiet nm-cloud-setup.service 2>/dev/null'
ExecStartPre=-/sbin/modprobe br_netfilter
ExecStartPre=-/sbin/modprobe overlay
ExecStart=/usr/local/bin/k3s \
    server \
	'--advertise-address=10.1.193.4' \
	'--cluster-cidr=10.42.0.0/16' \
	'--node-external-ip=10.1.193.4' \
	'--tls-san=10.1.193.4' \
	'--flannel-conf=/etc/rancher/k3s/flannel-config.json' \

#重新加载服务
systemctl daemon-reload
#启动服务
systemctl start k3s
#查看udp的端口是否修改成功,如果不成功再重启一下看看,需要等一会儿。
  • angent的部分修改
#创建k3s网络配置文件
vim /etc/rancher/k3s/flannel-config.json
#需要修改下面内容为实际的ip地址端和vxlan的端口 这部分同server的类似,如果要是多个master节点,一定要注意ip的实际情况信息。
{
  "Network": "10.42.0.0/24",
  "Backend": {
    "Type": "vxlan",
    "VNI": 1,
    "Port": 9877  
  }
}

修改anget的service文件

#操作前停止k3s服务
systemctl stop k3s-agent

vim /etc/systemd/system/k3s-agent.service

#下面是service文件,最后一行的--flannel-conf是后加入的,其它的保持原本的内容不要动

[Unit]
Description=Lightweight Kubernetes
Documentation=https://k3s.io
Wants=network-online.target
After=network-online.target

[Install]
WantedBy=multi-user.target

[Service]
Type=notify
EnvironmentFile=-/etc/default/%N
EnvironmentFile=-/etc/sysconfig/%N
EnvironmentFile=-/etc/systemd/system/k3s-agent.service.env
KillMode=process
Delegate=yes
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=1048576
LimitNPROC=infinity
LimitCORE=infinity
TasksMax=infinity
TimeoutStartSec=0
Restart=always
RestartSec=5s
ExecStartPre=/bin/sh -xc '! /usr/bin/systemctl is-enabled --quiet nm-cloud-setup.service 2>/dev/null'
ExecStartPre=-/sbin/modprobe br_netfilter
ExecStartPre=-/sbin/modprobe overlay
ExecStart=/usr/local/bin/k3s \
    agent \
	'--node-external-ip=10.1.193.5' \
	'--flannel-conf=/etc/rancher/k3s/flannel-config.json' \
posted on 2025-06-04 11:51  夏冰雹先生  阅读(53)  评论(0)    收藏  举报