linux network name space
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的更多相关文章
- Queueing in the Linux Network Stack !!!!!!!!!!!!!!!
https://www.coverfire.com/articles/queueing-in-the-linux-network-stack/ Queueing in the Linux Networ ...
- Netstat Commands for Linux Network Management
netstat (network statistics) is a command line tool for monitoring network connections both incoming ...
- Linux network 资料链接
1.iptables 基础 https://wiki.centos.org/HowTos/Network/IPTables 2.HOWTOs on netfilter site http://www. ...
- Netruon 理解(11):使用 NAT 将 Linux network namespace 连接外网
学习 Neutron 系列文章: (1)Neutron 所实现的虚拟化网络 (2)Neutron OpenvSwitch + VLAN 虚拟网络 (3)Neutron OpenvSwitch + GR ...
- Netruon 理解(12):使用 Linux bridge 将 Linux network namespace 连接外网
学习 Neutron 系列文章: (1)Neutron 所实现的虚拟化网络 (2)Neutron OpenvSwitch + VLAN 虚拟网络 (3)Neutron OpenvSwitch + GR ...
- Linux Network Namespace
Linux Network Namespaces Linux kernel在2.6.29中加入了namespaces,用于支持网络的隔离,我们看一下namespace是如何使用的 创建与配置 创建一个 ...
- (转)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/ ...
- 【转】理解Docker容器网络之Linux Network Namespace
原文:理解Docker容器网络之Linux Network Namespace 由于2016年年中调换工作的原因,对容器网络的研究中断过一段时间.随着当前项目对Kubernetes应用的深入,我感觉之 ...
- Linux Network Related Drive
catalog . 通过套接字通信 . 网络实现的分层模型 . 网络命名空间 . 套接字缓冲区 . 网络访问层 . 网络层 . 传输层 . 应用层 . 内核内部的网络通信 1. 通过套接字通信 Lin ...
随机推荐
- iOS-UI控件之UITableView(二)- 自定义不等高的cell
不等高的cell 给模型增加frame数据 所有子控件的frame cell的高度 @interface XMGStatus : NSObject /**** 文字\图片数据 ****/ // ... ...
- laravel 只有/login路由403,如何解决
链接/login自动转跳到/login/导致找不到 /public/login/ 目录导致403; 将路由中\login改为\login1访问正常,但login依然403,而不是未找到路由 链接/lo ...
- g20学习笔记
BALProblem.h---------定义BALProblem类. BALProblem类保存我们的BA所需要的所有数据,包括相机与路标之间的联系,相机变量+路标变量的初始值.这些数据的原始信息都 ...
- selenium webdriver 常用断言
断言常用的有: assertLocation(判断当前是在正确的页面). assertTitle(检查当前页面的title是否正确). assertValue(检查input的值, checkbox或 ...
- 邮箱地址自动提示jQuery插件
// mailAutoComplete.js v1.0 邮箱输入自动提示// 2010-06-18 v2.0 使用CSS class类代替CSS对象,同时增强代码可读性// 2010-06-18 v2 ...
- PHP—通过HTML网页请求,PHP页面显示源码不能解析
对于初学者来说,可能会碰到这样一个问题,那就是我们通过html网页,在表单的action中填入后台处理的php文件后,虽然可以跳转到php网页上,但是却显示一大堆php源码而不是处理请求.像这样: ...
- 零基础入门学习Python(36)--类和对象:给大家介绍对象
知识点 Python3 面向对象 Python从设计之初就已经是一门面向对象的语言,正因为如此,在Python中创建一个类和对象是很容易的.本章节我们将详细介绍Python的面向对象编程. 如果你以前 ...
- DB2隔离级别
四.隔离级别与锁 数据库是利用锁和隔离级别来共同处理数据库的并发的.DB2数据库用来尝试实施并发性的方法之一是通过使用隔离级别,它决定在第一个事务访问数据时,如何对其他事务锁定或隔离该事务所使用的数据 ...
- HTML5地理定位-Geolocation API
HTML5提供了一组Geolocation API,来自navigator定位对象的子对象,获取用户的地理位置信息Geolocation API使用方法:1.判断是否支持 navigator.geol ...
- leetcode-240搜索二维矩阵II
搜索二维矩阵II class Solution: def searchMatrix(self, matrix, target): """ :type matrix: Li ...