Calico BGP网络问题

Calico BGP网络问题

排除步骤

1. 检查 calico-node 是否已经启动并运行正常
kubectl get pods -A | grep calico

2. 确保 calico-node Pod 处于运行状态。接着,你可以查看 calico-node 容器是否正常启动
kubectl describe pod -n <NAMESPACE> <calico-node-pod-name>  # 这会显示关于 Pod 的详细信息,包括启动过程中的事件和错误。

3. 查看对应Pod内容器的日志
kubectl logs -n  <NAMESPACE> <calico-node-pod-name> # 

calico-node 正常运行,但在 describe 中有警告信息

错误现象

calico-node 的 descript 中显示

Warning  Unhealthy  7h43m (x2 over 7h43m)  kubelet            Readiness probe failed: calico/node is not ready: BIRD is not ready: Error querying BIRD: unable to connect to BIRDv4 socket: dial unix /var/run/calico/bird.ctl: connect: connection refused

Warning  Unhealthy  33m (x2 over 33m)  kubelet            Readiness probe failed: calico/node is not ready: BIRD is not ready: Error querying BIRD: unable to connect to BIRDv4 socket: dial unix /var/run/calico/bird.ctl: connect: connection refused
Warning  Unhealthy  32m                kubelet            Readiness probe failed: 2024-12-31 08:24:33.955 [INFO][264] confd/health.go 180: Number of node(s) with BGP peering established = 1
calico/node is not ready: BIRD is not ready: BGP not established with 192.168.10.96
  • 可能有用的操作
[root@k8s01 ~]# vi /etc/calico/confd/config/bird.cfg
router id 192.168.10.94;  # 当前节点的 BGP Router ID
protocol bgp {
    local as 64512;  # 本地 AS
    neighbor 192.168.10.95 as 64512;  # BGP 对等体 1(节点 2)
    neighbor 192.168.10.96 as 64512;  # BGP 对等体 2(节点 3)
    import all;
    export all;
}

[root@k8s02 ~]# vi /etc/calico/confd/config/bird.cfg 
router id 192.168.10.95;
protocol bgp {
    local as 64512;
    neighbor 192.168.10.94 as 64512;
    neighbor 192.168.10.96 as 64512;
    import all;
    export all;
}

[root@k8s03 ~]# vi /etc/calico/confd/config/bird.cfg 
router id 192.168.10.96;
protocol bgp {
    local as 64512;
    neighbor 192.168.10.94 as 64512;
    neighbor 192.168.10.95 as 64512;
    import all;
    export all;
}

ip route 路由表中删除 blackhole后又会自动生成

1. 手动删除blackhole路由
ip route del blackhole <ip address/num>

2. 删除现有的 IP Pool 配置
calicoctl delete ippool default-ipv4-ippool

3. 创建 IP Pool
# 方法一:使用yaml文件
apiVersion: projectcalico.org/v3
kind: IPPool
metadata:
  name: default-ipv4-ippool
spec:
  cidr: 10.244.0.0/16
  ipipMode: Never
  natOutgoing: true
  disabled: false
  
calicoctl apply -f ippool.yaml

# 方法二:使用 calicoctl
calicoctl apply -f - <<EOF
apiVersion: projectcalico.org/v3
kind: IPPool
metadata:
  name: default-ipv4-ippool
spec:
  cidr: 10.244.0.0/16
  ipipMode: Never
  natOutgoing: true
  disabled: false
EOF

4. 成功创建 IP Pool 后,你可以检查 IP Pool 配置是否正确
calicoctl get ippool  -o wide

网卡选择无效导致 calico 的 pod 内容器未就绪

问题现象

使用 calico 网络插件时出现 dns 服务异常,master 节点上相关 pod 的 ip 不能 ping 通。
使用tigera-operator方式安装的calico,启动后报错,所有的calico相关的pod都显示CrashLoopBackoff。
未就绪的calico-node 的 describe 中显示

Readiness probe failed: calico/node is not ready: BIRD is not ready: Error querying BIRD: unable to connect to BIRDv4 socket: dial unix /var/ run/calico/bird.ctl: connect: no such file or directory.

原因分析

由于 Calico 自动检测 IP 地址默认使用 first-found 方法,获得错误地址,需要我们手动指定检测方法。
在calico.yaml文件中,IP_AUTODETECTION_METHOD 配置项默认为first-found,这种模式中calico会使用第一获取到的有效网卡,虽然会排除docker网络,localhost啥的,但是在复杂网络环境下还是有出错的可能。

问题解决

方式一:直接修改集群内相关配置

对于 calico.yaml 方式安装的calico
1. 打开 calico-node 的 DaemonSet 配置文件
使用以下命令编辑 calico-node 的 DaemonSet 配置:
kubectl edit ds calico-node -n calico-system

2. 查找 env 配置项
在 calico-node 的 DaemonSet 配置文件中,查找 env 部分(环境变量),这部分配置包含了 Calico 节点的环境变量。如果没有找到,可以手动添加。

3. 添加或修改 IP_AUTODETECTION_METHOD
在 env 部分添加 IP_AUTODETECTION_METHOD 配置项。如果已经存在,可以直接修改它。修改后示例如下:

yaml
复制代码
spec:
  template:
    spec:
      containers:
        - name: calico-node
          env:
            - name: IP_AUTODETECTION_METHOD
              value: "interface=eth0"  # 这里根据实际网卡名称调整

4. 更新 DaemonSet
保存并退出编辑器后,Kubernetes 会自动更新 DaemonSet 的配置,重新启动相应的 calico-node Pods。


5. 检查 Pod 是否重启
使用以下命令查看 Calico 节点的 Pods 状态,确保它们被成功重启并运行:
kubectl get pods -A | grep calico
对于 tigera-operator 方式安装的calico

直接修改calico-node的statefulset是不起作用的,会被operator改回去。

[root@k8s03 ~]# kubectl get Installation
NAME      AGE
default   41h

[root@k8s03 ~]# kubectl edit Installation default
将其中nodeAddressAutodetectionV4:的配置修改为
    nodeAddressAutodetectionV4:
      interface: ens.* # ens 根据实际网卡开头配置,支持正则表达式

方式二:修改对应的部署 yaml 文件,并重新部署

对于 calico.yaml 方式安装的calico

calico.yaml 文件添加以下配置

   - name: IP_AUTODETECTION_METHOD
      value: "interface=ens.*"  # ens 根据实际网卡开头配置,支持正则表达式

配置示例:

            - name: CLUSTER_TYPE
              value: "k8s,bgp"
            - name: IP_AUTODETECTION_METHOD
              value: "interface=ens.*"
            # Auto-detect the BGP IP address.
            - name: IP
              value: "autodetect"
            # Enable IPIP
            - name: CALICO_IPV4POOL_IPIP
              value: "Always"

重新应用calico.yaml

kubectl apply -f calico.yaml
对于tigera-operator 方式安装的calico
1. 删除 tigera-operator
kubectl delete -f https://docs.projectcalico.org/manifests/tigera-operator.yaml
2. 删除custom-resources
kubectl delete -f https://calico-v3-25.netlify.app/archive/v3.25/manifests/custom-resources.yaml
3. 确保删除所有calico相关的部署及Pod

修改 custom-resources.yaml

  • 修改前
apiVersion: operator.tigera.io/v1
kind: Installation
metadata:
  name: default
spec:
  # Configures Calico networking.
  calicoNetwork:
    # Note: The ipPools section cannot be modified post-install.
    ipPools:
    - blockSize: 26
      cidr: 10.244.0.0/16
      encapsulation: VXLANCrossSubnet
      natOutgoing: Enabled
      nodeSelector: all()
  • 修改后
apiVersion: operator.tigera.io/v1
kind: Installation
metadata:
  name: default
spec:
  # Configures Calico networking.
  calicoNetwork:
    # Note: The ipPools section cannot be modified post-install.
    ipPools:
    - blockSize: 26
      cidr: 10.244.0.0/16
      encapsulation: VXLANCrossSubnet
      natOutgoing: Enabled
      nodeSelector: all()
    nodeAddressAutodetectionV4:
      interface: ens.* # ens 根据实际网卡开头配置,支持正则表达式
1. 重新部署 tigera-operator
kubectl create -f https://docs.projectcalico.org/manifests/tigera-operator.yaml

2. 重新部署修改后的  custom-resources
kubectl create -f custom-resources.yaml

证书时间问题导致 calico 的 pod 内容器未就绪

问题现象

未就绪的calico-node 的 describe 中显示

Readiness probe failed: calico/node is not ready: BIRD is not ready: Error querying BIRD: unable to connect to BIRDv4 socket: dial unix /var/run/calico/bird.ctl: connect: no such file or directory

查看对应pod的logs发现

# 这表示证书的有效期开始时间在当前时间之前,而当前时间是 2024-12-31T07:09:08Z,而证书的有效期从 2024-12-31T14:34:53Z 开始。
error=x509: certificate has expired or is not yet valid: current time 2024-12-31T07:09:08Z is before 2024-12-31T14:34:53Z

问题分析

在出问题的节点系统中执行下面的操作

date # 检查系统时间
[root@k8s03 ~]# date
20241231日 星期二 15:09:40 CST

timedatectl # 确认时区和系统时间设置

[root@k8s03 ~]# timedatectl
      Local time: 二 2024-12-31 15:09:54 CST
  Universal time: 二 2024-12-31 07:09:54 UTC
        RTC time: 二 2024-12-31 07:09:54
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a

系统时间和时区设置看起来是正确的。当前本地时间为 2024年12月31日 15:09:54 CST,与错误日志中的时间差异主要是时区转换的问题。

问题解决

在 Kubernetes 中,容器将使用主机的系统时间。只要主机时间正确,容器也会与主机保持一致的 UTC 时间。因此,一旦你将主机时间同步为 UTC,Kubernetes 容器也将遵循相同的时间设置。
在所有节点中执行下面的的操作

1. 检查 chronyd 服务状态: 确认 NTP 服务是否正常运行。
sudo systemctl status chronyd

如果没有安装 NTP 服务,则需要先安装
sudo yum install -y ntp # 安装 NTP 服务

如果 chronyd 服务没有运行,使用下面的命令启动
sudo systemctl start chronyd

2. 确认时间是否同步成功,如果未成功
sudo chronyc tracking # 显示 NTP 状态
sudo chronyc sources -v # 显示 NTP 时间源

3. 重新启用 NTP: 如果上面操作后问题依然存在,尝试重新启用 NTP 服务
sudo systemctl restart chronyd 
sudo timedatectl set-ntp true

4. 检查网络连接: 如果您的服务器无法访问外部时间服务器,可能会导致同步失败。确保服务器能够访问 NTP 服务器。

5. 再次确认 timedatectl 状态: 使用以下命令再次检查时间同步的状态:
timedatectl status

由于我这里是因为时区转换的问题,所以我将时区改为UTC即可正常
sudo timedatectl set-timezone UTC # 此命令将系统时区更改为 UTC。
sudo systemctl restart ntpd # 重启 ntpd 服务
date # 验证系统的当前时间是否与 UTC 时间同步

bird.cfg 配置文件编写

问题现象

bird: Unable to open configuration file /etc/calico/confd/config/bird.cfg: No such file or directory

问题解决

将下面的配置分别写入到集群中每个节点的bird.cfg中

  • 节点一:
mkdir -p /etc/calico/confd/config/
sudo vi /etc/calico/confd/config/bird.cfg
router id 192.168.10.94;  # 当前节点的 BGP Router ID
protocol bgp {
    local as 64512;  # 本地 AS
    neighbor 192.168.10.95 as 64512;  # BGP 对等体 1(节点 2)
    neighbor 192.168.10.96 as 64512;  # BGP 对等体 2(节点 3)
    import all;
    export all;
}
  • 节点二:
mkdir -p /etc/calico/confd/config/
sudo vi /etc/calico/confd/config/bird.cfg
router id 192.168.10.95;
protocol bgp {
    local as 64512;
    neighbor 192.168.10.94 as 64512;
    neighbor 192.168.10.96 as 64512;
    import all;
    export all;
}
  • 节点三:
mkdir -p /etc/calico/confd/config/
sudo vi /etc/calico/confd/config/bird.cfg
router id 192.168.10.96;
protocol bgp {
    local as 64512;
    neighbor 192.168.10.94 as 64512;
    neighbor 192.168.10.95 as 64512;
    import all;
    export all;
}

参考

https://juejin.cn/post/7257184836971053113
https://cloud.tencent.com/developer/article/2138086
https://developer.baidu.com/article/details/2807475
https://devpress.youkuaiyun.com/k8s/66c97e9a13e4054e7e7d40e5.html
官方:https://docs.tigera.io/calico/latest/getting-started/kubernetes/quickstart

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值