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. 使用Jenkins进行android项目的自动构建(2)

    Maven and POM 1. 什么是Maven? 官方的解释是: http://maven.apache.org/guides/getting-started/index.html#What_is ...

  2. Katalon Studio 安装 配置 简单使用

    本教程只针对Katalon Studio进行演示操作. 一.下载 Katalon 官网下载地址:https://www.katalon.com/download/ (需要注册账号) 二.解压.配置 直 ...

  3. (转)淘淘商城系列——分布式文件系统FastDFS

    http://blog.csdn.net/yerenyuan_pku/article/details/72801777 商品添加的实现,包括商品的类目选择,即商品属于哪个分类?还包括图片上传,对于图片 ...

  4. CSS3 radio 或checkbox 自定义 样式

    .style-radio {position:relative;width:15px;height:15px;outline:none;} .style-radio:after {position:a ...

  5. 使用Latex插入数学公式(二)

    初级运算 关系运算符 希腊字母 集合运算符逻辑运算符 空格问题 矩阵格式 矩阵格式有三种: 无括号的矩阵 matrix 是 Latex 的矩阵命令,矩阵命令中每一行以 \\ 结束,矩阵的元素之间用 & ...

  6. Vim中文编码问题

    1.影响中文编码的设置项 encoding(enc):encoding是Vim的内部使用编码,encoding的设置会影响Vim内部的Buffer.消息文字等.在 Unix环境下,encoding的默 ...

  7. 08CSS边框边距

    CSS边框边距 边框样式——border-style border-top-style border-bottom-style border-left-style border-right-style ...

  8. 【Hadoop】一、分布式数据库HBase简介

    1.分布式数据库特点   说到数据库,我们最熟悉的是类似于mysql这样的关系型数据库,称为RDBMS.关系型数据库作为一种数据存储和数据检索的关键技术,它支持SQL语言的结构化查询,但是它天生不是为 ...

  9. 两种js下载文件的方法(转)

    function DownURL(strRemoteURL, strLocalURL){ try{ var xmlHTTP = new ActiveXObject("Microsoft.XM ...

  10. 并不简单的Integer

    Integer是一个看着挺简单的,其实还是有点不一样,Integer是一个int的包装类,它是可以起到缓存作用的,在java基础里说过它的范围是(-128-127)在这个返回是有缓存的,不会创建新的I ...