# Kubernetes CNI Calico GuideLine# 时间:20210813-[20:00-22:00]# 主题:Calico BGP RR# 文档:https://www.yuque.com/docs/share/95ed4c58-0416-47a9-a530-55a760d5e3d6?# 《20210813-Calico BGP RR[生产网络选型]》# 作业:BGP RR企业实现方案

# FAQ:EBGP 对等体引入的路由在AS内部使用IBGP RR视为不好用路由---系模拟器bug。# AR4:<AR4>dis bgp routing-tableBGP Local router ID is 2.2.2.2Status codes: * - valid, > - best, d - damped,h - history, i - internal, s - suppressed, S - StaleOrigin : i - IGP, e - EGP, ? - incompleteTotal Number of Routes: 2Network NextHop MED LocPrf PrefVal Path/Ogn*>i 11.11.11.11/32 3.3.3.3 0 100 0 i*> 22.22.22.22/32 10.1.3.2 0 0 123i<AR4><AR4>dis ip routing-tableRoute Flags: R - relay, D - download to fib------------------------------------------------------------------------------Routing Tables: PublicDestinations : 18 Routes : 18Destination/Mask Proto Pre Cost Flags NextHop Interface1.1.1.1/32 OSPF 10 1 D 10.1.1.2 GigabitEthernet0/0/02.2.2.2/32 Direct 0 0 D 127.0.0.1 LoopBack03.3.3.3/32 OSPF 10 1 D 10.1.2.2 GigabitEthernet0/0/110.1.1.0/24 Direct 0 0 D 10.1.1.1 GigabitEthernet0/0/010.1.1.1/32 Direct 0 0 D 127.0.0.1 GigabitEthernet0/0/010.1.1.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet0/0/010.1.2.0/24 Direct 0 0 D 10.1.2.1 GigabitEthernet0/0/110.1.2.1/32 Direct 0 0 D 127.0.0.1 GigabitEthernet0/0/110.1.2.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet0/0/110.1.3.0/24 Direct 0 0 D 10.1.3.1 GigabitEthernet0/0/210.1.3.1/32 Direct 0 0 D 127.0.0.1 GigabitEthernet0/0/210.1.3.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet0/0/211.11.11.11/32 IBGP 255 0 RD 3.3.3.3 GigabitEthernet0/0/122.22.22.22/32 EBGP 255 0 D 10.1.3.2 GigabitEthernet0/0/2127.0.0.0/8 Direct 0 0 D 127.0.0.1 InLoopBack0127.0.0.1/32 Direct 0 0 D 127.0.0.1 InLoopBack0127.255.255.255/32 Direct 0 0 D 127.0.0.1 InLoopBack0255.255.255.255/32 Direct 0 0 D 127.0.0.1 InLoopBack0<AR4># AR6:<AR6>dis bgp routing-tableBGP Local router ID is 3.3.3.3Status codes: * - valid, > - best, d - damped,h - history, i - internal, s - suppressed, S - StaleOrigin : i - IGP, e - EGP, ? - incompleteTotal Number of Routes: 2Network NextHop MED LocPrf PrefVal Path/Ogn*> 11.11.11.11/32 0.0.0.0 0 0 i*>i 22.22.22.22/32 10.1.3.2 0 100 0 123i<AR6><AR6>dis ip routing-tableRoute Flags: R - relay, D - download to fib------------------------------------------------------------------------------Routing Tables: PublicDestinations : 14 Routes : 14Destination/Mask Proto Pre Cost Flags NextHop Interface1.1.1.1/32 OSPF 10 2 D 10.1.2.1 GigabitEthernet0/0/02.2.2.2/32 OSPF 10 1 D 10.1.2.1 GigabitEthernet0/0/03.3.3.3/32 Direct 0 0 D 127.0.0.1 LoopBack010.1.1.0/24 OSPF 10 2 D 10.1.2.1 GigabitEthernet0/0/010.1.2.0/24 Direct 0 0 D 10.1.2.2 GigabitEthernet0/0/010.1.2.2/32 Direct 0 0 D 127.0.0.1 GigabitEthernet0/0/010.1.2.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet0/0/010.1.3.0/24 OSPF 10 2 D 10.1.2.1 GigabitEthernet0/0/011.11.11.11/32 Direct 0 0 D 127.0.0.1 LoopBack122.22.22.22/32 IBGP 255 0 RD 10.1.3.2 GigabitEthernet0/0/0127.0.0.0/8 Direct 0 0 D 127.0.0.1 InLoopBack0127.0.0.1/32 Direct 0 0 D 127.0.0.1 InLoopBack0127.255.255.255/32 Direct 0 0 D 127.0.0.1 InLoopBack0255.255.255.255/32 Direct 0 0 D 127.0.0.1 InLoopBack0<AR6># 从AR6 ping AR7:<AR6>ping -a 11.11.11.11 22.22.22.22PING 22.22.22.22: 56 data bytes, press CTRL_C to breakReply from 22.22.22.22: bytes=56 Sequence=1 ttl=254 time=30 msReply from 22.22.22.22: bytes=56 Sequence=2 ttl=254 time=30 msReply from 22.22.22.22: bytes=56 Sequence=3 ttl=254 time=10 msReply from 22.22.22.22: bytes=56 Sequence=4 ttl=254 time=20 msReply from 22.22.22.22: bytes=56 Sequence=5 ttl=254 time=30 ms--- 22.22.22.22 ping statistics ---5 packet(s) transmitted5 packet(s) received0.00% packet lossround-trip min/avg/max = 10/24/30 ms# 要加源地址ping,不然默认使用出接口ping,这样就没有回程路由了,导致ping失败。
# Calico:calico的好处是endpoints组成的网络是单纯的三层网络,报文的流向完全通过路由规则控制,没有overlay等额外开销。calico的endpoint可以漂移,并且实现了acl。calico的缺点是路由的数目与容器数目相同,非常容易超过路由器、三层交换、甚至node的处理能力,从而限制了整个网络的扩张。calico的每个node上会设置大量(海量)的iptables规则、路由,运维、排障难度大。calico的原理决定了它不可能支持VPC,容器只能从calico设置的网段中获取ip。calico目前的实现没有流量控制的功能,会出现少数容器抢占node多数带宽的情况。calico的网络规模受到BGP网络规模的限制。在calico中,全网路由的数目和endpoints的数目一致,通过为node分配网段,可以减少路由数目,但不会改变数量级。如果有1万个endpoints,那么就至少要有一台能够处理1万条路由的设备。无论用哪种方式部署始终会有一台设备上存放着calico全网的路由。当要部署calico网络的时候,第一步就是要确认,网络中处理能力最强的设备最多能设置多少条路由。
0.Why BGP
# https://www.tigera.io/blog/why-bgp/# 为什么Calico使用BGP宣告endpoints的路由?Calico工程的其中一个设计概要就是:使用通用的互联网工具和技术去实现大规模网络互连结构,其主要原因是(互联网)行业在运营真正大型网络已经上积累了几十年的经验,使用的工具和技术也已经过长时间的磨练(比如BGP),如果把这些工作全部丢进垃圾桶就比较愚蠢了。随着数据终端网络(end point networks)已经接近的互联网的规模:如今,在基于VM的云环境中一个pod可以轻松承载数千到数万个计算服务器,并运行着几万甚至小几十万个VM (在Calico被称为的end points)。而在基于容器的云环境中,endpoints的数量可能还会增加一个或两个数量级。所以,我们也应该使用与互联网运营商同样的工具。在Calico设计的网络中,可能会出现几万台路由器,以及潜在的几百万条路由信息或者endpoints,这种数量级是与IGP的设计不匹配的,但是BGP可以,特别是当我们使用路由反射器去扩展路由器数量的时候。简而言之,BGP是Calico网络的唯一选择。# 如果使用基础架构路由(IGP)会怎么样?我们没有完全拒绝IGP,只是在很多情况下,Calico网络不需要基础设施路由协议。例如,在后面会提到的“以太网互联结构的Calico网络模型”中,每个计算服务器vRouter彼此间是直连的,也就不再需要IGP协议了。而在使用“IP互联结构的Calico网络模型”中,如果互联结构被设计的比较复杂,则可能需要计算服务器vRouter去参与一些IGP进程。需要注意的是,在IP互联结构,这些中间路由器/交换机也必须是BGP路由拓扑的成员。由于IGP大多想要得到全网的路由设计了非常复杂的算法:动态路由算法大致可以分为两类:#1.距离矢量路由算法#2.链路状态路由算法# 距离矢量路由算法(Distance Vector Routing),它是网络上最早使用的动态路由算法,也称为Bellman-Ford或者Ford-Fulkerson算法。基于这类算法实现的协议有:# RIP、BGP 等。这类算法的基本思路是:网络中每一个路由器都要维护一张 矢量表 ,这个 矢量表 中的每一行都记录了从当前位置能到达的目标路由器的最佳出口(接口)和距离(跳数)。每隔一段时间当前路由器会向所有的邻居节点发送自己的这个表,同时它也会接收每个邻居发来的它们的表。并会将邻居的表和自己的表做一个对比更新。比如当前 路由器X 离 邻居Y路由器 的距离是m,此时收到 邻居Y 发来的表中写到了“ 邻居Y离路由器Z的距离是n ”,那 当前路由器X 就知道它离 路由器Z 的距离可能就是 m+n 了,如图:m nX-------->Y-------->Z就这样继续类推,要不了多久,每个路由器就可以将网络中所有路由节点和子网线路都汇聚起来了。这样的话,每个路由器只需要查找自己的表就可以很容易的知道到达目的地的最佳出口(接口)是哪个了。# [距离矢量路由算法」的优点很明显:非常简单清晰,且任何加入到网络中的新节点都能很快的与其它节点建立起联系获得补充信息。# 缺点呢,首先就是每次发送信息的时候,要发送整个全局路由表,太大了,因为每个路由器需要在矢量表中记录下整个网络的信息,导致需要较大存储、CPU、网络开销,对资源的要求越来越高。还有一个问题就是收敛时间太慢,也就是路由器共享路由信息并使各台路由器掌握的网络情况达到一致所需的时间比较久,收敛速度慢会导致有些路由器的表更新慢,从而造成路由环路的问题。# 链路状态路由算法链路状态路由算法(Link State Routing ),基于Dijkstra算法,它是以图论作为理论基础,用图来表示网络拓扑结构,用图论中的最短路径算法来计算网络间的最佳路由。基于这类算法实现的协议有:OSPF 等。这类算法的基本思路是:采用的是不停的拼接地图的方式。每一个路由器首先都会发现自己身边的邻居节点,然后将自己与邻居节点之间的链路状态包广播出去,发送到整个网络。这样,当某个路由器收到从网络中其它路由器广播来的路由信息包(链路状态包)之后,会将这个包中的信息与自己路由器上的信息进行拼装,最终形成一个全网的拓扑视图。当路由器中形成了全网的拓扑视图后,它就可以通过最短路径算法来计算当前节点到其它路由器之间的最短路径了。当某台路由器的链路状态发生变化时,路由器采用洪泛法向所有路由器发送此信息,其它路由器使用收到的信息重新计算最佳路径,重新生成路由表(拓扑图)。# 总结起来就两句话,BGP在做矢量路径,IGP在做网络地图。
1.Calico over Ethernet fabrics
# calico over an Ethernet interconnect fabric中介绍了在Ethernet interconnect fabric部署calico网络方案。在每个vlan中部署一套calico。下图中我们可以部署4套calico的环境:In the particular case of this diagram, each ToR is segmented into four logical switches (possibly by using ‘port VLANs’), 2 and each compute server has a connection to each of those logical switches. We will identify those logical switches by their color. Each ToR would then have a blue, green, orange, and red logical switch. Those ‘colors’ would be members of a given plane, so there would be a blue plane, a green plane, an orange plane, and a red plane. Each plane would have a dedicated spine switch. and each ToR in a given spine would be connected to its spine, and only its spine.# Each plane would constitute an IP network, so the blue plane would be 2001:db8:1000::/36, the green would be 2001:db8:2000::/36, and the orange and red planes would be 2001:db8:3000::/36 and 2001:db8:4000::/36 respectively.

为了保证链路可靠,图中设计了四个并列的二层网,形成fabric。每个node同时接入四个二层网络,对应拥有四个不同网段的IP。在每个二层网络中,node与node之间用RR模式建立BGP通信链路:一个node做为RR,其余的node连接到做为RR的node整个网络中最终有四个RR,分别负责四个网络中的BGP当从node上去访问另一个node上的endpoint的时候,会有四条下一跳为不同网段的等价路由。# 根据ECMP协议,报文将会平均分配给这四个等价路由,提高了可靠性的同时增加了网络的吞吐能力。# ECMP: 等价路由ECMP存在多条不同链路到达同一目的地址的网络环境中,如果使用传统的路由技术,发往该目的地址的数据包只能利用其中的一条链路,其它链路处于备份状态或无效状态,并且在动态路由环境下相互的切换需要一定时间,而等值多路径路由协议可以在该网络环境下同时使用多条链路,不仅增加了传输带宽,并且可以无时延无丢包地备份失效链路的数据传输。ECMP最大的特点是实现了等值情况下,多路径负载均衡和链路备份的目的,在静态路由和OSPF中基本上都支持ECMP功能。但是实际情况是,各路径的带宽、时延和可靠性等不一样,把Cost认可成一样,不能很好地利用带宽,尤其在路径间差异大时,效果会非常不理想。例如,路由器两个出口,两路径,一个带宽是100M,一个是2M,如果部署是ECMP,则网络总带宽只能达到4M的利用率。
2.Calico over IP fabrics
The AS Per Rack model:

- [This diagram shows the AS per rack model where the ToR switches are physically meshed via a set of Ethernet switching planes.]

- [This diagram shows the AS per rack model where the ToR switches are physically meshed via a set of discrete BGP spine routers, each in their own AS.] ```json 在这种方案里,每个Tor 交换机到Tor 交换机或者TOR交换机到 spine switch 的链路是EBGP对等的。那就意味着北向 tor 交换机无法使用 RR(Route Reflector)。 如果使用了2层的方式,结果就是每个Tor 交换机之间必须彼此对等(可能有上百个对等),可能会造成负载过重。
如果使用3层的方式,那么每个TOR 交换机只需要和上级的 spine switch 对等即可。虽然spine switch 下会有许多TOR 交换机,可以使用RR(如上图所示),而且绝大部分spine switch拥有比TOR交换机更好的平面控制能力,在多数环境下更易扩展。
两者的配置基本相同,只是在TOR 交换机的北向配置上有些差异。
TOR 交换机,作为EBGP router 会获取其他TOR switch 以及数据中心上的路由,重新分配到该AS下的每个计算节点上。并且会将自身AS内的所有路由信息向外通告。这就意味着,每个计算节点会将该AS内的TOR 交换机视为到外部路由的下一跳。外部路由到该AS的下一跳则是该AS下的某台计算节点
<a name="SGWWn"></a>## The AS per Computer Server model:- [x] This diagram shows the AS per compute server model where the ToR switches are physically meshed via a set of Ethernet switching planes.- [x] This diagram shows the AS per compute server model where the ToR switches are physically connected to a set of independent routing planes.```json从上图不难发现,TOR 和 spine swtich 和 AS per rack 模式中一样。真正的不同在于,计算节点和TOR处于不同的AS中。为了扩展集群,需要使用4字节的AS编号(RFC 4893). 如果不使用4字节的AS编号(默认2字节),那么calico 平面中的TOR 和计算节点大约只有5000左右的私有AS编号可使用。如果采用了4字节,则会有接近92,000000私有AS编号可用。4字节AS*AS (Autonomous System number,自治域系统号)是指拥有同一选路策略,在同一技术管理部门下运行的一组路由器的集合。BGP的RFC1771里留给AS的范围是2个字节,所以AS的取值范围为1-65535,其中64512以上的为私有AS。由于BGP在邻居协商以及路由发送接受的时候都需要使用AS属性, 鉴于IPv4地址空间不够这个前车之鉴,在RFC4893里定义了一个BGP的新功能——4字节AS(BGP Support for Four-octet AS Number,一般用M.N来描述)。还有个差异,就是在per AS Compute Server 模式中没有 RR(Router Reflector)。所有设备都是EBGP对等的。当同一机架下的两个计算节点之间需要通信时,会通过TOR来路由。可以将这种模式看做The AS per rack 的缩影。
3.The Downward Default model
最后一种模式会显得有些不同。在上面介绍的架构中,路由器需要收集机构中所有的路由表,并使他们的AS路径保持原样。这种模式在路由阶段将AS号移除。
所有TOR使用一个AS,结算节点使用另一个AS的做法简化了部署(使用标准配置即可),带来的好处就是简化了TOR中的路由表。这种模式下,每个 Router 都向上级节点汇报自己的路由信息(vRouter –> TOR –> spine switch)。however, 作为回应,上级节点仅仅返回一条默认路由。在这种情况下,vRouter 只有本机Endpoint的路由以及到TOR的默认路由。同样,对于TOR来说只有到直连vRouter和spine switch的路由。即使我们一个机架下有80台计算节点,每个计算节点上有200个Endpoint, 那在TOR上也就16000条记录左右。(大多数交换机都能达到这个数目)由于路由下发默认是由 spine 发起的,所以下游的AS接受者无法下发路由,这就避免了AS的干扰问题。这种模式下,有个主要的缺点:非法目的地址(eg. 目的地址不存在)的流量都会被送往spine switch。值得注意的是,spine switch会收集 calico 网络中所有的路由条目。这种模式并不会使spine switch 中的路由条目比原来更多,但却着实减少了TOR上的路由信息。同时也减少了vRouter上的路由信息。但这并不是关注的重点,因为Calico中的完整路由表所消耗的内存量是现代计算服务器上可用总内存的一部分。#############################################################################################################################################################如果TOR和SPINE可以容纳所产生的路由条目,Calico 团队建议使用 AS per rack 模式。记得考虑后期扩容问题。如果担心TOR交换机路由表大小,Calico 团队建议使用 Downward default 模式。如果对TOR路由表大小不放心,以及希望运行非常简单的2层架构来连接Calico节点,则考虑上文介绍的Ethernet fabric(2层平面模式)。如果Calico用户对每个计算节点一个AS的模式感兴趣,则Project Calico团队将非常有兴趣讨论该模型的部署。
4.Migrate a Kubernetes cluster from flannel/Canal to Calico
Migrate an existing Kubernetes cluster with flannel/Canal to Calico networking.
Value
If you are already using flannel for networking, it is easy to migrate to Calico’s native VXLAN networking. Calico VXLAN is fully equivalent to flannel vxlan, but you get the benefits of the broader range of features offered by Calico with an active maintainer community.
Concepts
Limitations of host-local IPAM in flannel
Flannel networking uses the host-local IPAM (IP address management) CNI plugin, which provides simple IP address management for your cluster. Although simple, it has limitations:
- When you create a node, it is pre-allocated a CIDR. If the number of pods per-node exceeds the number of IP addresses available per node, you must recreate the cluster. Conversely, if the number of pods is much smaller than the number of addresses available per node, IP address space is not efficiently used; as you scale out and IP addresses are depleted, inefficiencies become a pain point.
- Because each node has a pre-allocated CIDR, pods must always have an IP address assigned based on the node it is running on. Being able to allocate IP addresses based on other attributes (for example, the pod’s namespace), provides flexiblity to meet use cases that arise.
Migrating to Calico IPAM solves these use cases and more. For advantages of Calico IPAM, see Blog: Live Migration from Flannel to Calico.
Methods for migrating to Calico networking
There are two ways to switch your cluster to use Calico networking. Both methods give you a fully-functional Calico cluster using VXLAN networking between pods.
- Create a new cluster using Calico and migrate existing workloadsIf you have the ability to migrate worloads from one cluster to the next without caring about downtime, this is the easiest method: create a new cluster using Calico.
Live migration on an existing clusterIf your workloads are already in production, or downtime is not an option, use the live migration tool that performs a rolling update of each node in the cluster.
Before you begin…
Required
A cluster with flannel for networking using the VXLAN backend.
- Flannel version v0.9.1 or higher (Canal version v3.7.0 or greater).
- Flannel must have been installed using a Kubernetes daemon set and configured:
- To use the Kubernetes API for storing its configuration (as opposed to etcd)
- With DirectRouting disabled (default)
Cluster must allow for:
Migrate from flannel networking to Calico networking, live migration
- Modify flannel configuration
- View migration status
- View migration logs
- Revert migration
Migrate from flannel networking to Calico networking, live migration
- Install Calico.kubectl apply -f https://docs.projectcalico.org/manifests/flannel-migration/calico.yaml
- Start the migration controller.kubectl apply -f https://docs.projectcalico.org/manifests/flannel-migration/migration-job.yaml You will see nodes begin to update one at a time.
- Monitor the migration.kubectl get jobs -n kube-system flannel-migration When the host node is upgraded, the migration controller may be rescheduled several times. The installation is complete when the output of the above command shows 1/1 completions. For example:NAME COMPLETIONS DURATION AGE flannel-migration 1/1 2m59s 5m9s
- Delete the migration controller.kubectl delete -f https://docs.projectcalico.org/manifests/flannel-migration/migration-job.yaml
Modify flannel configuration
The migration controller autodetects your flannel configuration, and in most cases, does not require additional configuration. If you require special configuration, the migration tool provides the following options, which can be set as environment variables within the pod.
| Configuration options | Description | Default |
|---|---|---|
| FLANNEL_NETWORK | IPv4 network CIDR used by flannel for the cluster. | Automatically detected |
| FLANNEL_DAEMONSET_NAME | Name of the flannel daemon set in the kube-system namespace. | kube-flannel-ds-amd64 |
| FLANNEL_MTU | MTU for the flannel VXLAN device. | Automatically detected |
| FLANNEL_IP_MASQ | Whether masquerading is enabled for outbound traffic. | Automatically detected |
| FLANNEL_SUBNET_LEN | Per-node subnet length used by flannel. | 24 |
| FLANNEL_ANNOTATION_PREFIX | Value provided via the kube-annotation-prefix option to flannel. | flannel.alpha.coreos.com |
| FLANNEL_VNI | The VNI used for the flannel network. | 1 |
| FLANNEL_PORT | UDP port used for VXLAN. | 8472 |
| CALICO_DAEMONSET_NAME | Name of the calico daemon set in the kube-system namespace. | calico-node |
| CNI_CONFIG_DIR | Full path on the host in which to search for CNI config files. | /etc/cni/net.d |
View migration status
View the controller’s current status.
kubectl get pods -n kube-system -l k8s-app=flannel-migration-controller
View migration logs
View migration logs to see if any actions are required.
kubectl logs -n kube-system -l k8s-app=flannel-migration-controller
Revert migration
If you need to revert a cluster from Calico back to flannel, follow these steps.
- Remove the migration controller and Calico.kubectl delete -f https://docs.projectcalico.org/manifests/flannel-migration/migration-job.yaml kubectl delete -f https://docs.projectcalico.org/manifests/flannel-migration/calico.yaml
- Determine the nodes that were migrated to Calico.kubectl get nodes -l projectcalico.org/node-network-during-migration=calico
Then, for each node found above, run the following commands to delete Calico.
- Cordon and drain the node.kubectl drain
- Log in to the node and remove the CNI configuration.rm /etc/cni/net.d/10-calico.conflist
- Reboot the node.
- Enable flannel on the node.kubectl label node
projectcalico.org/node-network-during-migration=flannel —overwrite - Uncordon the node.kubectl uncordon
After the above steps have been completed on each node, perform the following steps.
- Remove the nodeSelector from the flannel daemonset.kubectl patch ds/kube-flannel-ds-amd64 -n kube-system -p ‘{“spec”: {“template”: {“spec”: {“nodeSelector”: null}}}}’
- Remove the migration label from all nodes.kubectl label node —all projectcalico.org/node-network-during-migration-
