Virtual network card and docker

Principle of virtual NIC

qemu-kvm technology: you can make a huge physical machine, pulled out a small machine.

First, the virtual machine must have a network card, by tun on linux tap technology to achieve /. A virtual machine is a software ran on a physical machine, this software can open a file like any other application, open a character tun / tap the file who are after, you will see a virtual tap the card on a physical machine. VM application will fill all network packets are sent.

Network virtualization software package to here, the network packets into the file stream, write a character device, like writing a file the same. Kernel tun / tap character device driver will receive the file write stream to the virtual network card driver, turn again into network packets to tcp / ip protocol stack, tap the card is sent from the virtual to become the standard network packets .

Virtual card to connect to the cloud

Note that cloud point in the network:

  1. Sharing, virtual machine has multiple network cards, but may have limited physical machine card, then how multiple virtual NICs share the same outlet.
  2. Isolation: security isolation and traffic isolation, security isolation, two virtual machines belonging to two users, how to ensure that data is not being tapped. Traffic isolation, a crazy download will not lead to another unable to get online.
  3. Interoperability: two virtual machines on the same machine, how to communicate.
  4. Flexible: will often create, delete. From one machine to another machine drift.
  • Sharing and Interoperability:

    On a physical machine, a virtual switch, linux can create a virtual bridge to create a virtual network card out later, two virtual machines, are connected to the virtual bridge, so that two virtual machines configured on the same subnet They can communicate with each other up.

If you want to access external, there are two ways

  1. bridging

    On your physical machine, you will find more than a few cards, in fact, is a virtual switch, this switch virtual machines connected together in bridge mode, the physical network adapter is also connected to the virtual switch. Log in to see the virtual machine ip address will find, and colleagues around the machine and the physical address of the virtual machine is a network segment, because the equivalent of the physical and virtual machines together on the same bridge, the equivalent of the net bridge has three machines, is a segment. As shown below

  2. NAT mode

    In this mode, log in to view the virtual machine ip address, find the network's virtual machines, physical machines network is a physical machine, two different virtual confidential access to the physical machine when you need to address NAT become the address of the physical machine.

    另外,它还会在你的实体机里内置一个DHCP服务器,为笔记本电脑上的虚拟机动态分配ip地址。为什么桥接方式不需要呢,因为桥接将网络打平了,虚拟机的ip是由物理网络的DHCP服务器分配。

  • 隔离

    brctl创建的网桥支持VLAN功能,设置两个虚拟的tag,这样在这个虚拟网桥上,两个机器是不互通的,如果想要互通,有一个命令vconfig,基于物理网卡创建带VLAN的虚拟网卡,从这个虚拟网卡出去的包,都带这个VLAN。

首先每个用户分配不同的VLAN,不同的用户使用不同的虚拟网桥,带VLAN的虚拟网卡也连接到虚拟网桥上。网桥不通,不能相互通信,也不会转发到另一个网桥上,另外,出了物理机,也是带vlan id的,只要物理交换机支持vlan,就可以转发给相同vlan的网卡和网桥,所以跨物理机,不同的vlan也不会相互通信。

VLANid只有4096个,在大规模平台中明显不够用,另外流量的隔离还没有实现,有大量改进的空间。

容器网络

  • 封闭环境的两种技术

    1. 看起来隔离的技术,成为namespace,也即每个namespace 中的应用看到的是不同的 IP 地址、用户空间、程号等。
    2. 用起来是隔离的技术,称为cgroup,也即明明整台机器有很多的 CPU、内存,而一个应用只能用其中的一部分。
  • 命名空间 namespace

    linux下,很多资源是全局的,比如进程有全局的进程id,但是,当一台 Linux 上跑多个进程的时候,如果我们觉得使用不同的路由策略,这些 进程可能会冲突,那就需要将这个进程放在一个独立的 namespace 里面,这样就可以独立配置 网络了。 网络的 namespace 由 ip netns 命令操作。它可以创建、删除、查询 namespace。

  • 机制网络 cgroup

    cgroup全称 control groups,是linux内核提供的一种可以限制,隔离进程使用的机制。他有如下子系统:

    • CPU子系统使用调度进程为进程控制CPU的访问
    • cpuset,如果是多核心cpu,子系统会为进程分配单独的cpu和内存
    • memory子系统,设置进程的内存限制以及产生内存资源报告
    • blkio,设置限制每个块设备的输入输出控制
    • net_cls,这个子系统使用等级识别符标记网络数据包,可允许linux流量控制程序tc识别从具体cgroup中生成的数据包。

容器网络中如何融入物理网络

docker run的机构:

容器里面有张网卡,容器外有张网卡,容器外的网卡练到docker0网桥,通过这个网桥,容器直接实现相互访问。在linux下可以创建一对veth pair网卡,一边发送包,另一边就能收到。

一边可以达到docker0网桥上,另一端如何放到容器里呢。一个容器启动会对应一个namespace,先找到namespace,pid就是namespace的名字,然后就可以将另一端veth2赛道namespace里面。这样就使一台容器内不得互相访问没有问题了,那么如何访问外网?docker默认使用NAT模式,如果内部访问外部就要通过SNAT。

  1. 所有从容器内部发出来的包,都要做地址伪装,将源ip地址,转换为物理网卡的ip地址,如果有多个容器,所有的容器共享一个外网的ip地址。

  2. 当服务器返回结果的时候,到达物理机,取出原来的私网ip,通过DNAT将地址转还为私网ip,通过网桥docker0实现对内的访问。

  3. 如果在容器内部属于一个服务,例如部署一个网站,提供给外部进行访问,需要通过 Docker 的端口映射技术,将容器内部的端口映射到物理机上来。

    例如容器内部监听 80 端口,可以通 Docker run 命令中的参数 -p 10080:80,将物理机上的10080 端口和容器的 80 端口映射起来, 当外部的客户端访问这个网站的时候,通过访问物理机的 10080 端口,就能访问到容器内的 80 端口了。

docker有两种方式,一种是通过一个进程docker-proxy的方式,监听10080,转换为80端口,另一种是通过DNAT方式,将端口10080的DNAT成为容器的私有网络,这样就可以实现容器和物理网络之间的互通了。

Guess you like

Origin www.cnblogs.com/jimmyhe/p/11310861.html