linux network namespace概念类似于网络中的 VRF (virtual routing and forwarding)。但是,你不知道VRF的概念也没关系,下面我们通过一个简单的介绍以及 几个实验来了解。

概念

linux network namespace机制可以在一个linux系统中建立多个网络命名空间。各个命名空间互相独立,内部有自己的路由表和iptable,内部的设备名和其它linux network namespace中的设备名可以重名。

命令

这部分简单介绍一下network namespace的命令, 可以跳过这部分先看实验,回头再看这里。

linux network namespace的基本命令是ip。事实上很多之前的网络命令正逐渐被废弃,而采用ip命令来实现。下面是一部分之前命令和新命令的对应关系

ifconfig                                            --> ip addr or just ip a
ifconfig <interface> up/down --> ip link set dev <interface> up/down
ifconfig <interface> <ip> netmask <netmask> --> ip addr add <ip>/<masklen> dev <interface>
netstat -rn --> ip route or just ip r
route add -net <net> netmask <netmask> gw <gateway> --> ip r add <net>/<netmasklen> via <gateway>

实验一

创建一个network namepsace ns01

[root@ES02 ~]# ip netns add ns01

展示现有的 linux network namespace

[root@ES02 ~]# ip netns
ns01

查看一下 ns01 中的接口及状态。 在network namepsace中执行命令用 ip netns exec 命令

[root@ES02 ~]# ip netns exec ns01 ip link
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

把loop back端口启动起来(据说不启动会有奇怪的错误。不知道为什么) (确实。。不启动loopback 的话,你没办法自己ping自己。后面有详细解释)

[root@ES02 ~]# ip netns exec ns01 ip link set dev lo up
[root@ES02 ~]# ip netns exec ns01 ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

创建一个虚拟interface 分配给ns01. (我们没办法把物理 interface 分配给 ns01)

[root@ES02 ~]# ip link add veth-a type veth peer name veth-b
[root@ES02 ~]# ip link set veth-b netns ns01
[root@ES02 ~]# ip netns exec ns01 ip addr add 10.0.0.2/24 dev veth-b
[root@ES02 ~]# ip netns exec ns01 ip link set dev veth-b up

上面的操作还为接口veth-b添加了 ip 。 这里我们为另一个veth 添加 ip。

[root@ES02 ~]# ip addr add 10.0.0.1/24 dev veth-a
[root@ES02 ~]# ip link set dev veth-a up

现在通过veth-a 来访问veth-b 以及相反方向,都可以成功

[root@ES02 ~]# ip netns exec ns01 tracepath 10.0.0.1
1: 10.0.0.2 0.090ms pmtu 1500
1: 10.0.0.1 0.030ms reached
1: 10.0.0.1 0.016ms reached
Resume: pmtu 1500 hops 1 back 64
[root@ES02 ~]#
[root@ES02 ~]#
[root@ES02 ~]# tracepath 10.0.0.2
1: 10.0.0.1 0.130ms pmtu 1500
1: 10.0.0.2 0.055ms reached
1: 10.0.0.2 0.045ms reached
Resume: pmtu 1500 hops 1 back 64

实验 2

我们有 server , client 和gateway 三个namespace。 server是10.0.0.0/24 网段, client是192.168.1.0/24 网段。 希望client 和 server 能够通过gateway互相访问。 做法如下:

创建三个network namepsace

[root@ES02 ~]# clear
[root@ES02 ~]# ip netns add server
[root@ES02 ~]# ip netns add gateway
[root@ES02 ~]# ip netns add client
[root@ES02 ~]#
[root@ES02 ~]# ip netns list
client
gateway
server

创建两对 veth 如下

[root@ES02 ~]# ip link add svr-veth type veth peer name svrgw-veth
[root@ES02 ~]# ip link add cli-veth type veth peer name cligw-veth

我们的计划是把 svr-veth 放在server中,cli-veth放在client中。 svrgw-veth 和 cligw-veth 都放在gateway中。 这样因为 svr 的两个veth 可以构成一个通道, cli 两个veth之间也是一个通道,现在只要gateway中的两个veth能够想通,既可以实现之前的client 和 server两个network namespace互相访问。

接下来,放置veth. svr-veth 在server中 svrgw-veth 在gateway中 cligw-veth在gateway中 cli-veth在 client中。

[root@ES02 ~]# ip link set svr-veth netns server
[root@ES02 ~]# ip link set svrgw-veth netns gateway
[root@ES02 ~]# ip link set cligw-veth netns gateway
[root@ES02 ~]# ip link set cli-veth netns client

配置各自的ip 并启动。

[root@ES02 ~]# ip netns exec server  ip addr add 10.0.0.1/24 dev svr-veth
[root@ES02 ~]# ip netns exec gateway ip addr add 10.0.0.254/24 dev svrgw-veth
[root@ES02 ~]# ip netns exec gateway ip addr add 192.168.1.254/24 dev cligw-veth
[root@ES02 ~]# ip netns exec client ip addr add 192.168.1.1/24 dev cli-veth
[root@ES02 ~]# ip netns exec server ip link set dev svr-veth up
[root@ES02 ~]# ip netns exec client ip link set dev cli-veth up
[root@ES02 ~]# ip netns exec gateway ip link set dev cligw-veth up
[root@ES02 ~]# ip netns exec gateway ip link set dev svrgw-veth up

我们希望gateway能起到server 和 client两个网络的桥梁的作用,其实它就是一个路由器。连接两个网段。linux 模拟路由器 需要开启ip forward 功能

[root@ES02 ~]# ip netns exec gateway sysctl net.ipv4.ip_forward=1

[root@ES02 ~]# ip netns exec gateway ip route
10.0.0.0/24 dev svrgw-veth proto kernel scope link src 10.0.0.254
192.168.1.0/24 dev cligw-veth proto kernel scope link src 192.168.1.254

可以看到gateway中的路由表已经存在。 下面在server和client中设置路由

[root@ES02 ~]# ip netns exec server ip route add 192.168.1.0/24 via 10.0.0.254
[root@ES02 ~]# ip netns exec client ip route add 10.0.0.0/24 via 192.168.1.254

现在测试

client 到 server 

[root@ES02 ~]# ip netns exec client ping 10.0.0.1  -I 192.168.1.1
PING 10.0.0.1 (10.0.0.1) from 192.168.1.1 : 56(84) bytes of data.
64 bytes from 10.0.0.1: icmp_seq=1 ttl=63 time=0.055 ms
64 bytes from 10.0.0.1: icmp_seq=2 ttl=63 time=0.040 ms server 到 client [root@ES02 ~]# ip netns exec server ping 192.168.1.1 -I 10.0.0.1
PING 192.168.1.1 (192.168.1.1) from 10.0.0.1 : 56(84) bytes of data.
64 bytes from 192.168.1.1: icmp_seq=1 ttl=63 time=0.060 ms client 到 client
[root@ES02 ~]# ip netns exec client ping 192.168.1.1 -I 192.168.1.1 server 到 server
[root@ES02 ~]# ip netns exec server ping 10.0.0.1 -I 10.0.0.1 不通

很奇怪 自己ping自己不通,而且 tracepath 也不通

[root@ES02 ~]# ip netns exec client tracepath 10.0.0.1
1: send failed
Resume: pmtu 65535
You have new mail in /var/spool/mail/root
[root@ES02 ~]#
[root@ES02 ~]# ip netns exec server tracepath 192.168.1.1
1: send failed
Resume: pmtu 65535

记得之前说过 loopback 不启动会有奇怪问题,我们启动一下

[root@ES02 ~]# ip netns exec client ip link set lo up
[root@ES02 ~]# ip netns exec server ip link set lo up
[root@ES02 ~]# ip netns exec gateway ip link set lo up

果然,都ok

[root@ES02 ~]# ip netns exec client tracepath 10.0.0.1
1: 192.168.1.1 0.125ms pmtu 1500
1: 192.168.1.254 0.055ms
1: 192.168.1.254 0.033ms
2: 10.0.0.1 0.047ms reached
Resume: pmtu 1500 hops 2 back 63

linux network name space的更多相关文章

  1. Queueing in the Linux Network Stack !!!!!!!!!!!!!!!

    https://www.coverfire.com/articles/queueing-in-the-linux-network-stack/ Queueing in the Linux Networ ...

  2. Netstat Commands for Linux Network Management

    netstat (network statistics) is a command line tool for monitoring network connections both incoming ...

  3. Linux network 资料链接

    1.iptables 基础 https://wiki.centos.org/HowTos/Network/IPTables 2.HOWTOs on netfilter site http://www. ...

  4. Netruon 理解(11):使用 NAT 将 Linux network namespace 连接外网

    学习 Neutron 系列文章: (1)Neutron 所实现的虚拟化网络 (2)Neutron OpenvSwitch + VLAN 虚拟网络 (3)Neutron OpenvSwitch + GR ...

  5. Netruon 理解(12):使用 Linux bridge 将 Linux network namespace 连接外网

    学习 Neutron 系列文章: (1)Neutron 所实现的虚拟化网络 (2)Neutron OpenvSwitch + VLAN 虚拟网络 (3)Neutron OpenvSwitch + GR ...

  6. Linux Network Namespace

    Linux Network Namespaces Linux kernel在2.6.29中加入了namespaces,用于支持网络的隔离,我们看一下namespace是如何使用的 创建与配置 创建一个 ...

  7. (转)Linux Network IO Model、Socket IO Model - select、poll、epoll

    Linux Network IO Model.Socket IO Model - select.poll.epoll  原文:https://www.cnblogs.com/LittleHann/p/ ...

  8. 【转】理解Docker容器网络之Linux Network Namespace

    原文:理解Docker容器网络之Linux Network Namespace 由于2016年年中调换工作的原因,对容器网络的研究中断过一段时间.随着当前项目对Kubernetes应用的深入,我感觉之 ...

  9. Linux Network Related Drive

    catalog . 通过套接字通信 . 网络实现的分层模型 . 网络命名空间 . 套接字缓冲区 . 网络访问层 . 网络层 . 传输层 . 应用层 . 内核内部的网络通信 1. 通过套接字通信 Lin ...

随机推荐

  1. struts2的action是线程安全的,struts1的action不是线程安全的真正原因

    为什么struts2的action是线程安全的,struts1的action不是线程安全的? 先对struts1和struts2的原理做一个简单的讲解 对于struts1 ,当第一次**.do的请求过 ...

  2. [Windows Server 2008] IIS配置伪静态方法(Web.config模式的IIS rewrite)

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:安装伪静态(w ...

  3. 物联网初学者智能家居必备迅为iTOP-4412开发板

    更情点击了解:http://www.topeetboard.com 1.  手把手全视频教程: 第一部分:迅为电子开发板入门视频 第二部分:Linux系统编程 第三部分:Itop-4412开发板硬件设 ...

  4. Swift 关键字 inout - 让值类型以引用方式传递

    两种参数传递方式 值类型 传递的是参数的一个副本,这样在调用参数的过程中不会影响原始数据. 引用类型 把参数本身引用(内存地址)传递过去,在调用的过程会影响原始数据. 在 Swift 众多数据类型中, ...

  5. CREATE OPERATOR CLASS - 定义一个新的操作符类

    SYNOPSIS CREATE OPERATOR CLASS name [ DEFAULT ] FOR TYPE data_type USING index_method AS { OPERATOR ...

  6. list.extend的结果是None

    执行list.exend()方法后,会直接修改list本身,而不会产生返回值 In [97]: d=(43,) In [98]: type(d) Out[98]: tuple In [99]: c O ...

  7. 删除目录文件夹时出现:rm: cannot remove `/data/wwwroot/backidc': Is a directory

    rm -f 删除目录文件夹时出现:rm: cannot remove `/data/wwwroot/backidc': Is a directory cannot remove is a direct ...

  8. Perl字符集[\d\D]表示任何字符(所有数字和非数字,包括换行符),“.”表示除了换行符以外的所有字符。

    Perl字符集[\d\D]表示任何字符(所有数字和非数字,包括换行符),“.”表示除了换行符以外的所有字符.

  9. viewDidLoad等相关函数调用

    viewDidLoad 此方法只有当view从nib文件初始化的时候才被调用.viewDidLoad用于初始化,加载时用到的. loadView 此方法在控制器的view为nil的时候被调用.虽然经常 ...

  10. qrcode.js

    (function(r){r.fn.qrcode=function(h){var s;function u(a){this.mode=s;this.data=a}function o(a,c){thi ...