本文是在原文基础上的实际操作验证记录和细节补充。

默认情况下,容器连接到虚拟网桥docker0提供的虚拟子网中,容器之间通过默认网关(虚拟网桥docker0接口地址)建立连接。

如果不使用虚拟网桥,用户也可以通过创建两个容器间的点到点链路,将一对peer接口分别放到两个容器,来实现容器直连。

容器的直连链路不需要子网和子网掩码。

1 - 启动容器

[root@CentOS-7 ~]# docker run -it -d --net=none --name node-A centos /bin/bash
2c5683fbdf0880271013357e9a40b7549ad1c570c855bf591341ad7e7ac3f64e
[root@CentOS-7 ~]#
[root@CentOS-7 ~]# docker run -it -d --net=none --name node-B centos /bin/bash
33c209f70d0b5d48963793873088006349133652190d86444417b408830fd20d
[root@CentOS-7 ~]#
[root@CentOS-7 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
33c209f70d0b centos "/bin/bash" 2 minutes ago Up 2 minutes node-B
2c5683fbdf08 centos "/bin/bash" 2 minutes ago Up 2 minutes node-A
[root@CentOS-7 ~]#
[root@CentOS-7 ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
1d732a1c4f67 bridge bridge local
2e217e480705 host host local
c31d9a1acfc0 none null local
[root@CentOS-7 ~]# docker network inspect none
[
{
"Name": "none",
"Id": "c31d9a1acfc0b2ef806bef75c492b77189c32ae21bdca4eeef709b015ba95923",
"Scope": "local",
"Driver": "null",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": []
},
"Internal": false,
"Containers": {
"2c5683fbdf0880271013357e9a40b7549ad1c570c855bf591341ad7e7ac3f64e": {
"Name": "node-A",
"EndpointID": "73131ca12a7f7c1e036f7dcb26eb7d6d50f315767db1eddd829a89d45f4b17a3",
"MacAddress": "",
"IPv4Address": "",
"IPv6Address": ""
},
"33c209f70d0b5d48963793873088006349133652190d86444417b408830fd20d": {
"Name": "node-B",
"EndpointID": "0be67083bf14916b3accfbf9641c9099a69f8b7597a53eeeb88659315e193117",
"MacAddress": "",
"IPv4Address": "",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
[root@CentOS-7 ~]#

如果不指定"--net=none", 容器可以使用默认bridge网络通信。

2 - 根据进程ID创建网络名称空间跟踪文件

[root@CentOS-7 ~]# pid_A=`docker inspect -f '{{.State.Pid}}' node-A`
[root@CentOS-7 ~]# pid_B=`docker inspect -f '{{.State.Pid}}' node-B`
[root@CentOS-7 ~]#
[root@CentOS-7 ~]# mkdir -p /var/run/netns
[root@CentOS-7 ~]# ln -s /proc/$pid_A/ns/net /var/run/netns/$pid_A
[root@CentOS-7 ~]# ln -s /proc/$pid_B/ns/net /var/run/netns/$pid_B
[root@CentOS-7 ~]#

3 - 创建peer接口并配置路由

[root@CentOS-7 ~]# ip link add node-A type veth peer name node-B
[root@CentOS-7 ~]#
[root@CentOS-7 ~]# ip link set node-A netns $pid_A
[root@CentOS-7 ~]# ip netns exec $pid_A ip addr add 10.1.1.1/32 dev node-A
[root@CentOS-7 ~]# ip netns exec $pid_A ip link set node-A up
[root@CentOS-7 ~]# ip netns exec $pid_A ip route add 10.1.1.2/32 dev node-A
[root@CentOS-7 ~]#
[root@CentOS-7 ~]# ip link set node-B netns $pid_B
[root@CentOS-7 ~]# ip netns exec $pid_B ip addr add 10.1.1.2/32 dev node-B
[root@CentOS-7 ~]# ip netns exec $pid_B ip link set node-B up
[root@CentOS-7 ~]# ip netns exec $pid_B ip route add 10.1.1.1/32 dev node-B
[root@CentOS-7 ~]#

4 - 验证

容器可以相互ping通和建立连接。

[root@CentOS-7 ~]# docker attach node-A
[root@2c5683fbdf08 /]#
[root@2c5683fbdf08 /]# ip addr show |grep node-A
83: node-A@if82: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
inet 10.1.1.1/32 scope global node-A
[root@2c5683fbdf08 /]#
[root@2c5683fbdf08 /]# 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.113 ms
64 bytes from 10.1.1.1: icmp_seq=2 ttl=64 time=0.106 ms
^C
--- 10.1.1.1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.106/0.109/0.113/0.011 ms
[root@2c5683fbdf08 /]# 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.180 ms
64 bytes from 10.1.1.2: icmp_seq=2 ttl=64 time=0.110 ms
^C
--- 10.1.1.2 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.110/0.133/0.180/0.034 ms
[root@2c5683fbdf08 /]# [root@CentOS-7 ~]#
[root@CentOS-7 ~]#
[root@CentOS-7 ~]#
[root@CentOS-7 ~]# docker attach node-B
[root@33c209f70d0b /]#
[root@33c209f70d0b /]# ip addr show |grep node-B
82: node-B@if83: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
inet 10.1.1.2/32 scope global node-B
[root@33c209f70d0b /]#
[root@33c209f70d0b /]# 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.113 ms
64 bytes from 10.1.1.2: icmp_seq=2 ttl=64 time=0.084 ms
^C
--- 10.1.1.2 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.084/0.098/0.113/0.017 ms
[root@33c209f70d0b /]#
[root@33c209f70d0b /]# 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.189 ms
64 bytes from 10.1.1.1: icmp_seq=2 ttl=64 time=0.122 ms
^C
--- 10.1.1.1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.122/0.155/0.189/0.035 ms
[root@33c209f70d0b /]# [root@CentOS-7 ~]#
[root@CentOS-7 ~]#

Docker - 容器直连的更多相关文章

  1. 在docker容器中安装和使用,linux版的powershell

    powershell 传教士 原创文章.始于 2016-09-18 ,2016-10-27修改powershell docker官网.允许转载,但必须保留名字和出处,否则追究法律责任 1 在任意版本的 ...

  2. docker 容器日志集中 ELK + filebeat

    docker 容器日志集中 ELK ELK 基于 ovr 网络下 docker-compose.yaml version: '2' networks: network-test: external: ...

  3. Service 之间如何通信?- 每天5分钟玩转 Docker 容器技术(101)

    微服务架构的应用由若干 service 组成.比如有运行 httpd 的 web 前端,有提供缓存的 memcached,有存放数据的 mysql,每一层都是 swarm 的一个 service,每个 ...

  4. 通过案例学习 Secret - 每天5分钟玩转 Docker 容器技术(110)

    在下面的例子中,我们会部署一个 WordPress 应用,WordPress 是流行的开源博客系统. 我们将创建一个 MySQL service,将密码保存到 secret 中.我们还会创建一个 Wo ...

  5. k8s 重要概念 - 每天5分钟玩转 Docker 容器技术(117)

    在实践之前,必须先学习 Kubernetes 的几个重要概念,它们是组成 Kubernetes 集群的基石. Cluster Cluster 是计算.存储和网络资源的集合,Kubernetes 利用这 ...

  6. DNS 访问 Service - 每天5分钟玩转 Docker 容器技术(138)

    在 Cluster 中,除了可以通过 Cluster IP 访问 Service,Kubernetes 还提供了更为方便的 DNS 访问. kubeadm 部署时会默认安装 kube-dns 组件. ...

  7. docker容器的时间同步

    好久没写博客了,有时间开始陆续整理一下工作中遇到的问题,今天罗列一下docker容器的时间同步问题 我们每次在run容器的时候,会存在时区不同的问题,这样对数据处理会有很大障碍,操作如下: 第一种方式 ...

  8. docker容器持久化卷讲解

    docker容器自身存储数据效率比较低,因此我们为了提高磁盘IO的性能等,需要在容器中挂载一个外部存储设备.关于讲解大致如下: Docker中的数据可以存储在类似于虚拟机磁盘的介质中,在Docker中 ...

  9. Docker容器学习与分享10

    Docker容器向外提供服务 用分享04中的Nginx服务来试一下. 不过这次我直接用Nginx镜像创建容器,先下载Nginx镜像. [root@promote ~]# docker search n ...

随机推荐

  1. Struts2之标签使用

    上一篇我们一起探讨了Struts2中的OGNL表达式的知识,本篇我们一起来学习一下关于Struts2标签的使用,包括:基础标签:property.set.bean.include:判断标签:if el ...

  2. 用Java实现将多级文件夹下的所有文件统一放到一个文件夹中

    每次下了电影(男生懂得呦),每部电影都放在一个单独的文件夹里,看的时候很是不方便啊,一直重复着进入文件夹.后退,再进.再退的操作,而手动把这些电影全部复制出来又太繁琐.因此为了解决这个问题,用IO写了 ...

  3. Jenkis Editable Email Notification Plugin 使用介绍

    Jenkis Editable Email Notification Plugin 使用介绍 前言 Jenkins本身提供的Email插件功能实在有限,只能提供当前Job的基本信息,比如成功.失败以及 ...

  4. 平时自己项目中用到的CSS

    outline  当选中input元素的时候会出现状态线, outline设置成none就没了 input{ outline:none; } contentditable  设置元素内的文本是否可编辑 ...

  5. iOS9,10没有问题,iOS8上面一登录就崩溃,原因Assets的问题

    在项目中开发中,打包成一个ipa的包,发现iOS9,10,运行非常流畅,iOS8上面一运行就崩溃,找了好久,才找到原因竟然是Assets的问题,一开始我把ipa包放在蒲公英上面托管扫码下载的,用iTu ...

  6. Object-C知识点

    Object-C常用的知识点,以下为我在实际开发中用到的知识点,但是又想不起来,需要百度一下的知识点 1. p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: ...

  7. AOP学习笔记一

    软件开发的目的是为了解决各种需求,包括业务需求和系统需求.目前,业界通过使用面向对象的编程思想,已经可以对业务需求等普通关注点进行很好的抽象与封装,并且使之模块化.但是对于系统需求一类的关注点来说,情 ...

  8. 使用JS实现鼠标悬浮切换显示

    实现的是在鼠标悬停在不同链接上,在同一位置切换显示想要显示的内容 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional// ...

  9. 跟着刚哥梳理java知识点——包装类(十)

    Java为8种基本数据类型都提供了对应的包装器类型 装箱和拆箱: public class Main { public static void main(String[] args) { Intege ...

  10. 事件驱动的Python实现

    EventManager事件管理类实现,大概就百来行代码左右.如果有不了事件驱动的工作原理的可以看前一篇<事件驱动的简明讲解> # encoding: UTF-8 # 系统模块 from ...