? ? ? ?Calico是一个纯三层的协议,使用虚拟路由代替虚拟交换,每一台虚拟路由通过BGP协议可达信息(路由)到剩余数据中心。与 Flannel 不同的是 Calico 不使用隧道或 NAT 来实现转发,而是巧妙的把所有二三层流量转换成三层流量,并通过 host 上路由配置完成跨 Host 转发
? ? ??把 Host 当作 Internet 中的路由器,同样使用 BGP 同步路由,并使用 iptables 来做安全访问策略,最终设计出了 Calico 方案
?
? ? ?多数的虚拟化平台实现中,通常使用二层隔离技术实现 workloads 的网络,二层的技术有一些弊端,比如依赖 VLAN、bridge 和隧道等技术,其中 bridge 带来了复杂性,vlan 隔离和 tunnel 隧道则消耗更多的资源并对物理环境有要求,随着网络规模的增大,整体会变得越加复杂
? ? ?二层网络通讯需要依赖广播消息机制,广播消息的开销与 host 的数量呈指数级增长,Calico 使用的三层路由方法,则完全抑制了二层广播,减少了资源开销
? ? 参考:?https://www.projectcalico.org/why-calico/
?
? ? ?Calico在每一个节点利用高效的vRouter来负责数据转发,而每个vRouter通过BGP协议负责把自己上运行的workload的路由信息向整个Calico广播,小规模部署可以直接互联,大规模下可通过指定的BGP route reflector来完成
Calico组件:
? ? ?Felix:Calico agent? ? ?运行在每台node上,为容器设置网络信息:IP,路由规则,iptable规则等
? ? ?BIRD:? BGP Client: 监听 Host 上由 Felix 注入的路由信息,然后通过 BGP 协议广播告诉其他?Host 节点,从而实现网络互通
? ? ?BGP Route Reflector: 大型网络仅仅使用 BGP client 形成 mesh 全网互联的方案就会导致规模限制,所有节点需要 N^2 个连接,为了解决这个规模问题,可以采用 BGP 的 Router Reflector 的方法,使所有 BGP Client 仅与特定 RR 节点互联并做路由同步,从而大大减少连接数
? ? ?calicoctl: calico命令行管理工具
?
- ? ? IPIP 把 IP 层封装到 IP 层的一个 tunnel。的作用其实基本上就相当于一个基于IP层的网桥!一般来说,普通的网桥是基于mac层的,根本不需 IP,而这个 ipip 则是通过两端的路由做一个 tunnel,把两个本来不通的网络通过点对点连接起来。
- ? ? ?BGP 边界网关协议(Border Gateway Protocol, BGP)是互联网上一个核心的去中心化自治路由协议。BGP不使用传统的内部网关协议(IGP)的指标
?
- ? ? 容器内所有数据包都转发到一个虚假的地址 169.254.1.1,发送 ARP 请求?
- ? ??Host?的 veth 端收到 ARP 请求时开启网卡的 proxy arp 功能把自己的 MAC 地址返回给容器
- ? ??使用了 169.254.1.1 这样的地址,Host 判断为三层路由转发,查询本地路由发送给对端 host,如果配置 BGP,这里会看到 proto 协议为 BIRD
- ? ??当 host 收到数据包时查找本地路由表匹配,转发到对应的 veth?端从而到达容器
Kernel IP routing table
Destination ? ? Gateway ? ? ? ? Genmask ? ? ? ? Flags Metric Ref ? ?Use Iface
0.0.0.0 ? ? ? ? 169.254.1.1 ? ? 0.0.0.0 ? ? ? ? UG ? ?0 ? ? ?0 ? ? ? ?0 eth0
169.254.1.1 ? ? 0.0.0.0 ? ? ? ? 255.255.255.255 UH ? ?0 ? ? ?0 ? ? ? ?0 eth0
?
? ??https://docs.projectcalico.org/v3.8/reference/architecture/
?
? ?Installing with the Kubernetes API datastore—50 nodes or less
下载 calico.yaml
curl https://docs.projectcalico.org/v3.8/manifests/calico.yaml -O
修改pod的子网掩妈
POD_CIDR="<your-pod-cidr>" \ sed -i -e "s?192.168.0.0/16?$POD_CIDR?g" calico.yaml
Apply the manifest using the following command.
kubectl apply -f calico.yaml
?
? ?参考:?https://docs.projectcalico.org/v3.8/getting-started/kubernetes/installation/calico
? ? ?
? ?如果使用 etcd 作为 store 后端
? # Configure this with the location of your etcd cluster.
? etcd_endpoints: "http://192.168.1.8:2379"
? # If you're using TLS enabled etcd uncomment the following.
? # You must also populate the Secret below with these files.
? etcd_ca: "" ? # "/calico-secrets/etcd-ca"
? etcd_cert: "" # "/calico-secrets/etcd-cert"
? etcd_key: "" ?# "/calico-secrets/etcd-key"
? # Typha is disabled.
? typha_service_name: "none"
? # Configure the backend to use.
? calico_backend: "bird"
?
? ?install-cni:在各Node上安装CNI二进制文件到/opt/cni/bin目录下,并安装相应的网络配置文件到/etc/cni/net.d目录下
? ? ? initContainers:
? ? ? ? # This container performs upgrade from host-local IPAM to calico-ipam.
? ? ? ? # It can be deleted if this is a fresh installation, or if you have already
? ? ? ? # upgraded to use calico-ipam.
? ? ? ? - name: upgrade-ipam
? ? ? ? ? image: calico/cni:v3.7.4
? ? ? ? ? command: ["/opt/cni/bin/calico-ipam", "-upgrade"]
? ? ? ? # This container installs the CNI binaries
? ? ? ? # and CNI network config file on each node.
? ? ? ? - name: install-cni
? ? ? ? ? image: calico/cni:v3.7.4
? ? ? ? ? command: ["/install-cni.sh"]
?
?
????? ? ?CALICO_IPV4POOL_CIDR: Calico IPAM的IP地址池,Pod的IP地址将从该池中进行分配。
????? ? ?CALICO_IPV4POOL_IPIP:是否启用IPIP模式,Calico将在node上创建一个tunl0的虚拟隧道。使用IPIP模式时,设置?CALICO_IPV4POOL_IPIP="always",不使用IPIP模式时,设置为"off",此时将使用BGP模式。
?
# cat 10-calico.conflist?
{
? "name": "k8s-pod-network",
? "cniVersion": "0.3.0",
? "plugins": [
? ? {
? ? ? "type": "calico",
? ? ? "log_level": "info",
? ? ? "datastore_type": "kubernetes",
? ? ? "nodename": "master-node",
? ? ? "mtu": 1440,
? ? ? "ipam": {
? ? ? ? ? "type": "calico-ipam"
? ? ? },
? ? ? "policy": {
? ? ? ? ? "type": "k8s"
? ? ? },
? ? ? "kubernetes": {
? ? ? ? ? "kubeconfig": "/etc/cni/net.d/calico-kubeconfig"
? ? ? }
? ? },
? ? {
? ? ? "type": "portmap",
? ? ? "snat": true,
? ? ? "capabilities": {"portMappings": true}
? ? }
? ]
}
?
?# cat /etc/calico/calicoctl.cfg?
apiVersion: projectcalico.org/v3
kind: CalicoAPIConfig
metadata:
spec:
? datastoreType: "kubernetes"
? kubeconfig: "/root/.kube/config"
# calicoctl node status
Calico process is running.IPv4 BGP status
+---------------+-------------------+-------+----------+-------------+
| PEER ADDRESS ?| ? ? PEER TYPE ? ? | STATE | ?SINCE ? | ? ?INFO ? ? |
+---------------+-------------------+-------+----------+-------------+
| 192.168.73.44 | node-to-node mesh | up ? ?| 07:34:34 | Established |
+---------------+-------------------+-------+----------+-------------+IPv6 BGP status
No IPv6 peers found.
?