创建命名空间

# ip netns add mhc

# ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: enp3s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
    link/ether dc:fe:07:d6:10:67 brd ff:ff:ff:ff:ff:ff
3: wlp2s0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq state DOWN mode DORMANT qlen 1000
    link/ether 2a:3d:4b:78:d6:34 brd ff:ff:ff:ff:ff:ff
4: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT qlen 1000
    link/ether 52:54:00:ea:e7:08 brd ff:ff:ff:ff:ff:ff
5: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN mode DEFAULT qlen 1000
    link/ether 52:54:00:ea:e7:08 brd ff:ff:ff:ff:ff:ff
6: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT
    link/ether 02:42:f1:10:e3:bc brd ff:ff:ff:ff:ff:ff
10: veth879059f@if9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP mode DEFAULT
    link/ether f2:5a:39:f5:94:63 brd ff:ff:ff:ff:ff:ff link-netnsid 0

# ip netns exec mhc ip link show
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

添加一对veth设备

ip link add veth0 type veth peer name veth1

# ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: enp3s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
    link/ether dc:fe:07:d6:10:67 brd ff:ff:ff:ff:ff:ff
3: wlp2s0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq state DOWN mode DORMANT qlen 1000
    link/ether be:6d:49:a4:79:2e brd ff:ff:ff:ff:ff:ff
4: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT qlen 1000
    link/ether 52:54:00:ea:e7:08 brd ff:ff:ff:ff:ff:ff
5: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN mode DEFAULT qlen 1000
    link/ether 52:54:00:ea:e7:08 brd ff:ff:ff:ff:ff:ff
6: docker0: <BROADCAST,MUL# ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: enp3s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
    link/ether dc:fe:07:d6:10:67 brd ff:ff:ff:ff:ff:ff
3: wlp2s0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq state DOWN mode DORMANT qlen 1000
    link/ether be:6d:49:a4:79:2e brd ff:ff:ff:ff:ff:ff
4: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT qlen 1000
    link/ether 52:54:00:ea:e7:08 brd ff:ff:ff:ff:ff:ff
5: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN mode DEFAULT qlen 1000
    link/ether 52:54:00:ea:e7:08 brd ff:ff:ff:ff:ff:ff
6: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT
    link/ether 02:42:f1:10:e3:bc brd ff:ff:ff:ff:ff:ff
10: veth879059f@if9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP mode DEFAULT
    link/ether f2:5a:39:f5:94:63 brd ff:ff:ff:ff:ff:ff link-netnsid 0
27: veth1@veth0: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 1000
    link/ether 82:72:ea:99:16:2c brd ff:ff:ff:ff:ff:ff
28: veth0@veth1: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 1000
    link/ether 56:44:ef:da:0f:42 brd ff:ff:ff:ff:ff:ff

TICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT
    link/ether 02:42:f1:10:e3:bc brd ff:ff:ff:ff:ff:ff
10: veth879059f@if9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP mode DEFAULT
    link/ether f2:5a:39:f5:94:63 brd ff:ff:ff:ff:ff:ff link-netnsid 0
27: veth1@veth0: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 1000
    link/ether 82:72:ea:99:16:2c brd ff:ff:ff:ff:ff:ff
28: veth0@veth1: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 1000
    link/ether 56:44:ef:da:0f:42 brd ff:ff:ff:ff:ff:ff

将veht1 移到另一个namespace

ip link set veth1 netns mhc

# ip netns exec mhc ip link show
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
27: veth1@if28: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 1000
    link/ether 82:72:ea:99:16:2c brd ff:ff:ff:ff:ff:ff link-netnsid 0

设置ip地址

# ip netns exec mhc ip addr add 10.1.1.1/24 dev veth1

# ip addr add 10.1.1.2/24 dev veth0

启用设备

# ip netns exec mhc ip link set dev veth1 up

# ip link set dev veth0 up

# ping 10.1.1.1
PING 10.1.1.1 (10.1.1.1) 56(84) bytes of data.
64 bytes from 10.1.1.1: icmp_seq=1 ttl=64 time=0.065 ms
64 bytes from 10.1.1.1: icmp_seq=2 ttl=64 time=0.076 ms

# ip netns exec mhc ping 10.1.1.2
PING 10.1.1.2 (10.1.1.2) 56(84) bytes of data.
64 bytes from 10.1.1.2: icmp_seq=1 ttl=64 time=0.088 ms
64 bytes from 10.1.1.2: icmp_seq=2 ttl=64 time=0.040 ms

如何查看对端

# ethtool -S veth0
NIC statistics:
     peer_ifindex: 27

# ip netns exec mhc ip link|grep 27
27: veth1@if28: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT qlen 1000

删除veth设备(同时在另一个nemespace的设备也被删除了)

# ip link delete veth0

删除网络命名空间

# ip netns ls
mhc (id: 1)
# ip netns delete mhc

容器网络之 veth设备的更多相关文章

  1. [Linux网络、命名空间、veth设备对、docker的host模式、container模式、none模式、brideg模式、网桥的增删查,容器与网桥的连接断开]

    [Linux网络.命名空间.veth设备对.docker的host模式.container模式.none模式.brideg模式.网桥的增删查,容器与网桥的连接断开] 网络名称空间 为了支持网络协议栈的 ...

  2. 【Networking】容器网络大观 && SDN 资料汇总

    SDNLAB技术分享(十五):容器网络大观   SDNLAB君• 16-06-17 •2957 人围观 编者按:本文系SDNLAB技术分享系列,本次分享来自SDN撕X群(群主:大猫猫)群直播,我们希望 ...

  3. [Kubernetes]浅谈容器网络

    Veth Pair 这部分内容主要介绍一个设备: Veth Pair . 作为一个容器,它可以声明直接使用宿主机的网络栈,即:不开启 Network Namespace .在这种情况下,这个容器启动后 ...

  4. 容器网络——从CNI到Calico

    从容器诞生开始,存储和网络这两个话题就一直为大家津津乐道.我们今天这个环境下讲网络这个问题,其实是因为容器对网络的需求,和传统物理.虚拟环境对网络环境需求是有差别的,主要面临以下两个问题: 过去Iaa ...

  5. docker容器网络—单主机容器网络

    当我们在单台物理机或虚拟机中运行多个docker容器应用时,这些容器之间是如何进行通信的呢,或者外界是如何访问这些容器的? 这里就涉及了单机容器网络相关的知识.docker 安装后默认 情况下会在宿主 ...

  6. Docker容器网络篇

    Docker容器网络篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Docker的网络模型概述 如上图所示,Docker有四种网络模型: 封闭式网络(Closed conta ...

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

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

  8. Docker容器网络-实现篇

    通常,Linux容器的网络是被隔离在它自己的Network Namespace中,其中就包括:网卡(Network Interface).回环设备(Loopback Device).路由表(Routi ...

  9. 一文读懂 Kubernetes 容器网络

    点击上方"开源Linux",选择"设为星标" 回复"学习"获取独家整理的学习资料! 在Kubernetes中要保证容器之间网络互通,网络至关 ...

随机推荐

  1. windows下编译Hugin时的template错误

    Hugin依赖于VIGRA,而VIGRA包含大量的模板,编译Hugin是出现如下的大量错误: "unexpected token(s) preceding ':'; skipping app ...

  2. bzoj1367

    题解: 左偏树模板题 维护n/2的好多课左偏树 每一次加进来一个点的时候,只有一个点 然后每次中位数比前面小的时候,那么和前面合并 代码: #include<bits/stdc++.h> ...

  3. 『转』Bitdefender Internet Security 2013 – 免费1年

    活动中可以选择获取Bitdefender Internet Security 2013+Bitdefender Mobile Security (手机版)各一年激活码申请地址:https://part ...

  4. 实现react中的自动保存--定时任务

    1. 定义和用法 setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式. setInterval() 方法会不停地调用函数,直到 clearInterval() 被调用或 ...

  5. HDU 1285:确定比赛名次(拓扑排序)

    确定比赛名次 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  6. 安装node

    vuejs:通过npm安装vue框架(印象深刻的问题出在了npm上) 以下所有事我自己在windows系统下遇到的问题 一.安装nodejs node下载地址https://nodejs.org/en ...

  7. poj3311 经典tsp问题

    题目的大概意思就是一个人到一些城市送披萨,要求找到一条路径可以遍历每个城市后返回出发点,而且路径距离最短.最后输出最短距离就可以. 注意:每个城市可反复訪问多次. 因为题中明白说了两个城市间的直接可达 ...

  8. c# 子线程如何通知主线程,个人总结

    我要实现的功能如下:程序中有2个线程,主线程和子线程,主线程中有一个变量:X主线程运行中激活子线程,子线程会做出计算改变 X 的值,主线程继续做其它的事,直到 X 的值发生改变时,才会响应,并在tex ...

  9. Ubuntu14.04安装有道词典(openyoudao)

    1. Openyoudao介绍 Openyoudao是有道字典在linux下的客户端,在取词翻译的基础上,对查询到的信息进行有效的整合.目前已经发布了0.4版本,新增了google翻译功能,可提供72 ...

  10. mark TODO:完善拦截规则;日志分析;web仪表盘展示;终极目标动态配置规则