cni网络容器接口&cni插件calico(重点是理解cni)

   calico官方: https://www.tigera.io/
   Tigera 是 Calico 项目的发明者和维护者.




   



 

一、 CNI(Containernetworking Interface)网络插件

What is CNI?

CNI (Container Network Interface), a Cloud Native Computing Foundation project, consists of a specification and libraries for writing plugins to configure network interfaces in Linux containers, along with a number of supported plugins. CNI concerns itself only with network connectivity of containers and removing allocated resources when the container is deleted. Because of this focus, CNI has a wide range of support and the specification is simple to implement. 

参看地址:
github地址: https://github.com/containernetworking/cni
https://www.cni.dev/docs/
官方: https://www.cni.dev/
它是cncf项目,你可以在cncf孵化结束的项目中找到它 https://www.cncf.io/projects/


      cni本身是一个容器网络接口规范,它是一个由cncf(隶属Linux基金会)维护的开源项目,它本身提供不了容器网络需要依靠第三方实现的容器网络实现。你要区分开cni规范和官方实现的cni网络插件 ,注意cncf cni插件实现和cni规范是不同的。
      cni规范: https://github.com/containernetworking/cni 
       cni网络插件官方实现: https://github.com/containernetworking/cni/releases/tag/v0.5.0 (还有其他版本)
         你可以看下: https://github.com/containernetworking/cni/blob/main/Documentation/spec-upgrades.md
      在Kubernetes中,可以通过第三方实现的CNI网络插件如著名的 calico、flannel等来为Pod提供网络连接。
      目前市面上典型的第三方CNI网络插件有:

       calico:采用BGP协议实现高效的容器网络互连; (calico是第三方的cni网络容器插件,它不是cncf项目,官方网站)
       flannel:使用VXLAN技术实现网络隔离和扁平化IP;
       官方实现的cni网络插件:  https://github.com/containernetworking/cni/releases/tag/v0.5.0 (里面有cni插件实现的辅助工具)
      更多列表见 https://github.com/containernetworking/cni

     cni容器网络接口规范:

cni规范 规范地址 参看规范
spec-v0.4.0 https://github.com/containernetworking/cni/releases/tag/spec-v0.4.0 0.4.0
spec-v0.3.1 https://github.com/containernetworking/cni/releases/tag/spec-v0.3.1 0.3.1
spec-v0.3.0 https://github.com/containernetworking/cni/releases/tag/spec-v0.3.0 0.3.0
spec-v0.2.0 https://github.com/containernetworking/cni/releases/tag/spec-v0.2.0 0.2.0
spec-v0.1.0 https://github.com/containernetworking/cni/releases/tag/spec-v0.1.0 0.1.0

     模型: kubelet-->cni接口-->calico/flannel

   二、K8S网络设计原则:
   1、每个Pod都拥有一个独立IP地址,Pod内所有容器共享一个网络命名空间
   2、集群内所有Pod都在一个直接连通的扁平网络中,可通过IP直接访问
     所有容器之间无需NAT就可以直接互相访问
     所有Node和所有容器之间无需NAT就可以直接互相访问
    容器自己看到的IP跟其他容器看到的一样
   3、Service cluster IP可在集群内部访问,外部请求需要通过NodePort、LoadBalance或者Ingress来访问

    Container Network Interface (CNI)是目前CNCF主推的网络模型,它由两部分组成:
    CNI Plugin负责给容器配置网络,它包括两个基本的接口
       配置网络: AddNetwork(net *NetworkConfig, rt *RuntimeConf) (types.Result, error)
       清理网络: DelNetwork(net *NetworkConfig, rt *RuntimeConf) error
      IPAM Plugin负责给容器分配IP地址

 三、Kubernetes Pod的网络(k8s与calico沟通的机制):

 1.每个Pod创建时,首先会创建基础容器:google_container/pause-amd64:v3.1 (tar包)
      pause容器 全称 infrastucture container(又叫infra)基础容器。
  2.kubelet负责创建基础 infra容器(pause)并生成 network namespace
  3.kubelet调用网络CNI driver,由cni 接口根据网络配置调用具体的cni 插件(calico,flannel等)
       
模型: kubelet-->cni ---->calico (类似与kubelet--> cri ---> containerd/docker的libcontainerd)

  这里需要去了解cni的规范,它是对容器网络的抽象,包括增加容器到网络、从网络中删除容器等。
  如果你开发过程序很容易理解这个 cni接口是统一的操作,简单理解它只规定动作比如:加入容器到网络,
  至于怎么加入的由具体的插件实现,例如:kubelet的cni发出删除容器操作,则calico来负责把容器从网络中删除。
  如果你的网络解决方案采用flannel,则kuelet的cni发出删除容器操作,交由flannel来执行删除。 这样真正做到了
  不同网络解决方案接入到kubernetes中。


   容器网络包括
  1)、将容器接入到主机网桥
  2)、分配ip和端口
  3)、创建的网络接口名称,例如eth0
  4)、容器网络命名空间的路径 (/run/nsenter/[nsname])
四、cni接口 

        1.1 形成cni插件概念
        1.1版本中设计了网络插件(之后成为cni的雏形)概念在 pkg/kubelet/network/plugins.go
        1.2 版本开始正式集成cni接口和cni插件

版本 接口实现
 1.2 和1.3 https://github.com/appc/cni/libcni
1.4版本之后  https://github.com/containernetworking/cni/libcni

cni容器网络规范
https://github.com/containernetworking/cni/blob/spec-v0.1.0/SPEC.md 0.1规范
https://github.com/containernetworking/cni/blob/spec-v0.4.0/SPEC.md 0.4规范
https://github.com/containernetworking/cni/blob/spec-v1.0.0/SPEC.md 1.0规范
https://kubernetes.io/zh-cn/docs/concepts/extend-kubernetes/compute-storage-net/network-plugins/ 介绍cni集成
官方
https://www.cni.dev/docs/spec/
https://www.cni.dev/
https://github.com/containernetworking/cni
https://github.com/containernetworking/cni/blob/spec-v0.4.0/SPEC.md#ip-allocation

CNI是CNCF的项目,它定义了容器网络接口和库文件,并且包括一些支持的容器网络插件
代码分别在:
https://github.com/containernetworking/cni
https://github.com/containernetworking/plugins

                 本地:
                      E:\k8s源码\kubernetes-1.3.0\vendor\github.com\appc\cni\libcni

  1、然后 CNI 插件给基础容器配置网络
  2、最后 Pod 中其他的容器共享使用基础容器的网络
     CNI driver 调用calico 插件来配置kubernetes的网络,常用CNI插件有 flannel calico weave等等,这些插件各有优势,也在互相借鉴学习优点,比如:在所有node节点都在一个二层网络时候,flannel提供hostgw实现,避免vxlan实现的udp封装开销,估计是目前最高效的;calico也针对L3 Fabric,推出了IPinIP的选项,利用了GRE隧道封装;因此这些插件都能适合很多实际应用场景,这里选择calico,主要考虑它支持 kubernetes network policy

五、开源的calico版本
  1、
2015年7月16日, calico开始从Kubernetes v1.0版本提供支持
  2、2016年1月22日  Kubernetes Calico 插件发布了 1.0 里程碑版本, 此版本使用容器网络接口 (CNI)。CNI 标准是用于配置容器网络的开放接口
      https://github.com/projectcalico/cni-plugin/releases/tag/v1.0.0 (发布时间为2016年1月19日) 
      https://github.com/projectcalico/cni-plugin/tags?after=v1.2.0
   2、2021年5月11日

   在 Calico 的早期版本中,需要使用“calicoctl”命令行工具来正确管理 Calico API 资源。
   在 Calico v3.19 中,我们引入了一项新的技术预览功能,该功能允许您使用可选的 API 服务器插件
   直接使用 kubectl 管理所有 projectcalico.org API 资源

   早期的calico、calicoctl版本可以到  
   https://github.com/projectcalico/cni-plugin/releases/tag/v1.11.1 (calico)
   https://github.com/projectcalico/calicoctl/tree/master/docs/cni/kubernetes/README.md (calicoctl)
   链接下的 tag 标签
   

    

  https://github.com/projectcalico/calicoctl/tree/v0.0.1
  

 六、参看
    2023 年 Calico 开源状况 
     coredns官方: https://coredns.io/    https://blog.coredns.io   https://github.com/coredns/coredns
     etcd
官方地址: https://etcd.io/      https://github.com/etcd-io/etcd
     ETCD是CoreOS开源的一个强一致性的分布式键值存储服务,它并不是为kubernetes而设计。

    etcd的第一个版本  https://github.com/etcd-io/etcd/releases?page=22
    二进制以及代码都在 https://github.com/etcd-io/etcd/releases
    由于版本太多,这里就是不在介绍。 
   https://thenewstack.io/project-calico-now-fully-supports-kubernetes/
   https://www.tigera.io/blog/the-results-are-in-for-the-2016-calico-user-survey/
   https://docs.projectcalico.org/v2.3/getting-started/kubernetes/installation/hosted/
   https://info.tigera.io/rs/805-GFH-732/images/Calico-Open-Source-Usage-Adoption-Report-2023.pdf    (2023 calico使用情况)
  https://object-storage-ca-ymq-1.vexxhost.net/swift/v1/6e4619c416ff4bd19e1c087f27a43eea/www-assets-prod/presentation-media/k8s-calico-v1.0.pdf(2016年)
https://docs.projectcalico.org/v2.3/getting-started/kubernetes/installation/integration
https://quay.io/repository/calico/node?tab=tags    (redhat)
https://github.com/projectcalico/cni-plugin  (source code 转到了https://github.com/projectcalico/calico)
https://docs.tigera.io/calico/latest/about/
https://docs.tigera.io/calico/latest/getting-started/kubernetes/quickstart
https://docs.tigera.io/calico/latest/operations/calicoctl/install
 https://www.cncf.io/projects/   (cncf下的项目)
  https://quay.io/repository/calico/node?tab=tags
 https://nirajrules.wordpress.com/2019/10/17/overview-of-calico-cni-for-kubernetes/
https://www.cncf.io/wp-content/uploads/2020/11/CNCF_Survey_Report_2020.pdf
https://www.tigera.io/blog/calico-networking-for-kubernetes-1-0/ (2015年7月16日)
 https://www.tigera.io/blog/announcing-1-0-calico-cni-integration-for-kubernetes/ (calico1.0版本)

posted @ 2022-02-17 10:48  jinzi  阅读(61)  评论(0编辑  收藏  举报