Cloud Native应用交付

  • 首页
  • 关于本站
  • 个人介绍
  • Downloads
  • Repo
    • Github
    • Container
  • F5
    • F5 Python SDK
    • F5-container
    • F5-LBaaS
  • 社交
    • 联系我
    • 微信/微博
    • 公众号
    • 打赏赞助
行至水穷处 坐看云起时
Cloud Native Application Services: cnadn.net
  1. 首页
  2. 容器/k8s
  3. 正文

docker容器跨宿主机通信方法(2):路由方式

2017年06月24日 14714点热度 0人点赞 0条评论

路由方式互联跨宿主机容器:

linux在容器docker0网路与host的网卡网咯之间是可以执行ip forwarding的,因此跨主机的容器网络可以通过路由的方式来实现彼此互通,如下结构:

只需要在host1 添加到达172.27.0.0/16网络的路由,在host2上添加172.17.0.0/16的路由即可。

需要注意的前提:

各个host上的容器docker0网络不能相同,也就是说各个host里的容器的ip地址不能存在网络重叠情况,而缺省情况下,docker在建立docker0的时候都默认使用172.17.0.0/16网络,因此有必要对docker这一默认行为进行修改,实验环境版本下的修改方法如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
[root@docker2 system]# pwd
/usr/lib/systemd/system
[root@docker2 system]# vi docker.service
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target
 
[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd --bip 172.27.0.1/16
ExecReload=/bin/kill -s HUP $MAINPID
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
# Uncomment TasksMax if your systemd version supports it.
# Only systemd 226 and above support this version.
#TasksMax=infinity
TimeoutStartSec=0
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes
# kill only the docker process, not all processes in the cgroup
KillMode=process
# restart the docker process if it exits prematurely
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s
 
[Install]
WantedBy=multi-user.target

修改缺省的 docker服务启动文件,在 execstart中增加 --bip参数来指定docker0网桥地址从而实现对应docker网络的修改。注意:修改此配置文件后,需要在ifconfig输出里没有docker0网桥或者docker0网桥的IP和你修改的配置文件里一致才可以正常启动服务。

修改完毕后,启动服务:

1
2
systemctl daemon-reload
systemctl start docker

检查docker0设置:

1
2
3
4
5
6
7
8
9
[root@docker2 system]# ifconfig
docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.27.0.1  netmask 255.255.0.0  broadcast 0.0.0.0
        inet6 fe80::42:24ff:fe2c:e4c7  prefixlen 64  scopeid 0x20<link>
        ether 02:42:24:2c:e4:c7  txqueuelen 0  (Ethernet)
        RX packets 551  bytes 45332 (44.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 27  bytes 1582 (1.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

host2增加路由;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@docker2 system]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.0.1     0.0.0.0         UG    100    0        0 eno16777736
172.16.199.0    0.0.0.0         255.255.255.0   U     100    0        0 eno50332184
172.27.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
192.168.0.0     0.0.0.0         255.255.255.0   U     100    0        0 eno16777736
[root@docker2 system]# route add -net 172.17.0.0/16 gw 172.16.199.17
[root@docker2 system]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.0.1     0.0.0.0         UG    100    0        0 eno16777736
172.16.199.0    0.0.0.0         255.255.255.0   U     100    0        0 eno50332184
172.17.0.0      172.16.199.17   255.255.0.0     UG    0      0        0 eno50332184
172.27.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
192.168.0.0     0.0.0.0         255.255.255.0   U     100    0        0 eno16777736

host1增加路由:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@docker1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.0.1     0.0.0.0         UG    100    0        0 eno16777736
172.16.199.0    0.0.0.0         255.255.255.0   U     100    0        0 eno50332184
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
172.19.0.0      0.0.0.0         255.255.0.0     U     0      0        0 br-2aeddaf04b59
192.168.0.0     0.0.0.0         255.255.255.0   U     100    0        0 eno16777736
[root@docker1 ~]# route add -net 172.27.0.0/16 gw 172.16.199.27
[root@docker1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.0.1     0.0.0.0         UG    100    0        0 eno16777736
172.16.199.0    0.0.0.0         255.255.255.0   U     100    0        0 eno50332184
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
172.19.0.0      0.0.0.0         255.255.0.0     U     0      0        0 br-2aeddaf04b59
172.27.0.0      172.16.199.27   255.255.0.0     UG    0      0        0 eno50332184
192.168.0.0     0.0.0.0         255.255.255.0   U     100    0        0 eno16777736

 

测试:

host1上的容器里ping host2上的容器IP:

1
2
3
4
5
6
[root@docker1 ~]# docker exec busybox-11 ping 172.27.0.2
Error response from daemon: No such container: busybox-11
[root@docker1 ~]# docker exec busybox-1 ping 172.27.0.2
PING 172.27.0.2 (172.27.0.2): 56 data bytes
64 bytes from 172.27.0.2: seq=0 ttl=62 time=0.721 ms
64 bytes from 172.27.0.2: seq=1 ttl=62 time=0.948 ms

可以正常ping 通。测试成功。

手工配置的问题及Quagga自动化方案

但是在一个生产环境中,会有很多主机,手工维护这样一个静态路由的添加修改必然是一件很恐怖的事情,因此有了这样一个工具可以帮助自动完成这些工作:  http://www.quagga.net

需要在系统里运行这样一个软件路由器系统来实现彼此学习各个host里的docker0网络。为了简单化,可以将该软件容器化,然后以特权模式(--privileged)启动该容器,便可以自动彼此学习路由。网上灵雀云提供的那个容器image地址已经失效,可以手工从这里下载该镜像 https://pan.baidu.com/s/1sj26X8T

下载完毕后,将tar文件上传到所有host机器上,并执行命令:

1
2
3
[root@docker1 tmp]# ls
abrt  route.tar
[root@docker1 tmp]# docker load -i route.tar

tar文件将被load为image

1
2
3
4
5
6
[root@docker1 tmp]# docker image ls
REPOSITORY                      TAG                 IMAGE ID            CREATED             SIZE
nginx                           latest              46102226f2fd        8 weeks ago         109MB
busybox                         latest              00f017a8c2a6        3 months ago        1.11MB
hello-world                     latest              48b5124b2768        5 months ago        1.84kB
index.alauda.cn/georce/router   latest              cc9f1eb39091        2 years ago         139MB

在启动该容器前,检查一下当前路由表条目:

1
2
3
4
5
6
7
8
[root@docker1 tmp]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.0.1     0.0.0.0         UG    100    0        0 eno16777736
172.16.199.0    0.0.0.0         255.255.255.0   U     100    0        0 eno50332184
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
172.19.0.0      0.0.0.0         255.255.0.0     U     0      0        0 br-2aeddaf04b59
192.168.0.0     0.0.0.0         255.255.255.0   U     100    0        0 eno16777736

所有host上关闭系统防火墙 systemctl stop firewalld.service
所有host上启动quagga容器:

1
docker run -itd --name router --privileged --net=host index.alauda.cn/georce/router

稍等片刻,再次检查路由表,发现系统路由表已增加一条到达172.27.0.0/16的路由

1
2
3
4
5
6
7
8
9
[root@docker1 tmp]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.0.1     0.0.0.0         UG    100    0        0 eno16777736
172.16.199.0    0.0.0.0         255.255.255.0   U     100    0        0 eno50332184
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
172.19.0.0      0.0.0.0         255.255.0.0     U     0      0        0 br-2aeddaf04b59
172.27.0.0      192.168.0.183   255.255.0.0     UG    20     0        0 eno16777736
192.168.0.0     0.0.0.0         255.255.255.0   U     100    0        0 eno16777736

再次执行ping,可以成功ping通:

1
2
3
4
[root@docker1 tmp]# docker exec busybox-1 ping 172.27.0.2
PING 172.27.0.2 (172.27.0.2): 56 data bytes
64 bytes from 172.27.0.2: seq=0 ttl=62 time=0.678 ms
64 bytes from 172.27.0.2: seq=1 ttl=62 time=0.907 ms

如果此时连接到quagga, telnet localhost 2601: (密码zebra)可以看到当前路由表,docker容器启用了ospf和rip两个动态路由协议

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Router# show ip route
Codes: K - kernel route, C - connected, S - static, R - RIP, O - OSPF,
       I - ISIS, B - BGP, > - selected route, * - FIB route
 
K>* 0.0.0.0/0 via 192.168.0.1, eno16777736
C>* 127.0.0.0/8 is directly connected, lo
O   172.16.199.0/24 [110/10] is directly connected, eno50332184, 00:25:21
C>* 172.16.199.0/24 is directly connected, eno50332184
O   172.17.0.0/16 [110/10] is directly connected, docker0, 00:25:21
K * 172.17.0.0/16 is directly connected, docker0
C>* 172.17.0.0/16 is directly connected, docker0
O   172.19.0.0/16 [110/10] is directly connected, br-2aeddaf04b59, 00:25:21
C>* 172.19.0.0/16 is directly connected, br-2aeddaf04b59
O>* 172.27.0.0/16 [110/20] via 192.168.0.183, eno16777736, 00:13:35
  *                        via 172.16.199.27, eno50332184, 00:13:35
R   172.27.0.0/16 [120/2] via 192.168.0.183, eno16777736, 00:13:38
O   192.168.0.0/24 [110/10] is directly connected, eno16777736, 00:25:21
C>* 192.168.0.0/24 is directly connected, eno16777736

 

相关文章

  • DOCKER容器跨宿主机通信方法-(3):Openvswitch
  • docker容器跨宿主机通信方法(1)
  • Docker学习备忘2
  • 博客Docker化实践
  • DOCKER容器跨宿主机通信方法(4):Flannel-Vxlan
本作品采用 知识共享署名-非商业性使用 4.0 国际许可协议 进行许可
标签: docker docker网络 quagga 容器网络 跨主机容器互联
最后更新:2017年07月2日

纳米

linjing.io

打赏 点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理。

页面AI聊天助手
文章目录
  • 路由方式互联跨宿主机容器:
    • 需要注意的前提:
    • host2增加路由;
    • host1增加路由:
    • 测试:
      • host1上的容器里ping host2上的容器IP:
    • 手工配置的问题及Quagga自动化方案

纳米

linjing.io

☁️迈向Cloud Native ADC ☁️

认证获得:
TOGAF: ID 152743
Kubernetes: CKA #664
Microsoft: MCSE MCDBA
Cisco: CCNP
Juniper: JNCIS
F5:
F5 Certified Solution Expert, Security
F5 Certified Technology Specialist, LTM/GTM/APM/ASM
F5 Certified BIG-IP Administrator
  • 点击查看本博技术要素列表
  • 归档
    分类
    • AI
    • Automation
    • Avi Networks
    • Cisco ACI
    • CISCO资源
    • F5 with ELK
    • F5-Tech tips
    • F5技术
    • Juniper
    • Linux
    • NGINX
    • SDN
    • ServiceMesh
    • WEB编程
    • WINDOWS相关
    • 业界文章
    • 交换机技术
    • 化云为雨/Openstack
    • 协议原理
    • 容器/k8s
    • 我的工作
    • 我的生活
    • 网站技术
    • 路由器技术
    • 项目案例
    标签聚合
    neutron irule openstack DNS network flannel nginx docker k8s F5 api gtm bigip istio envoy
    最近评论
    汤姆 发布于 8 个月前(09月10日) 嗨,楼主,里面的json怎么下载啊,怎么收费啊?
    汤姆 发布于 8 个月前(09月09日) 大佬,kib的页面可以分享下吗?谢谢
    zhangsha 发布于 1 年前(05月12日) 资料发给我下,谢谢纳米同志!!!!lyx895@qq.com
    李成才 发布于 1 年前(01月02日) 麻烦了,谢谢大佬
    纳米 发布于 1 年前(01月02日) 你好。是的,因为以前下载系统插件在一次升级后将所有的下载生成信息全弄丢了。所以不少文件无法下载。DN...
    浏览次数
    • Downloads - 183,779 views
    • 联系我 - 118,966 views
    • 迄今为止最全最深入的BIGIP-DNS/GTM原理及培训资料 - 116,519 views
    • Github - 103,665 views
    • F5常见log日志解释 - 79,774 views
    • 从传统ADC迈向CLOUD NATIVE ADC - 下载 - 74,627 views
    • Sniffer Pro 4 70 530抓包软件 中文版+视频教程 - 74,320 views
    • 迄今为止最全最深入的BIGIP-DNS/GTM原理及培训资料 - 67,770 views
    • 关于本站 - 60,917 views
    • 这篇文档您是否感兴趣 - 55,496 views
    链接表
    • F5SE创新
    • Jimmy Song‘s Blog
    • SDNlab
    • Service Mesh社区
    • 三斗室
    • 个人profile
    • 云原生社区

    COPYRIGHT © 2023 Cloud Native 应用交付. ALL RIGHTS RESERVED.

    Theme Kratos Made By Seaton Jiang

    京ICP备14048088号-1

    京公网安备 11010502041506号