docker跨主机通信

扶醉桌前 提交于 2020-02-27 15:35:53

一.环境说明

操作系统:centos7
docker:19.03.6

主机名 IP地址 类型
root@master 192.168.1.191 masters
root@slave01 192.168.1.192 slave01

二.master服务器

[root@master ~]# hostnamectl set-hostname master
[root@master ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
[root@master ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@master ~]# yum makecache fast
[root@master ~]# yum -y install docker-ce
[root@master ~]# yum install etcd -y
[root@master ~]# cp /etc/etcd/etcd.conf /etc/etcd/etcd.conf_bak
[root@master ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.191 etcd
192.168.1.191 master
192.168.1.192 slave01
[root@master ~]# scp /etc/hosts 192.168.1.192:/etc/
[root@master ~]# cat  <<EOF > /etc/etcd/etcd.conf
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379,http://0.0.0.0:4001"
ETCD_NAME="master"
ETCD_ADVERTISE_CLIENT_URLS="http://etcd:2379,http://etcd:4001"
EOF
[root@master ~]# systemctl start etcd
[root@master ~]# systemctl enableetcd
[root@master ~]# exit
[root@master ~]# systemctl restart etcd
[root@master ~]# systemctl status etcd.service
[root@master ~]# yum install flannel -y
[root@master ~]# cp /etc/sysconfig/flanneld /etc/sysconfig/flanneld_bak
[root@master ~]# etcdctl mk /atomic.io/network/config '{ "Network": "10.10.0.0/16","SubnetLen": 24,"Backend": {"Type": "vxlan"} }'
[root@master ~]# etcdctl get /atomic.io/network/config
[root@master ~]# systemctl restart flanneld
[root@master ~]# FLANNEL_SUBNET=$(cat /run/flannel/subnet.env| grep "FLANNEL_SUBNET"|awk -F \= '{print $2}')
[root@master ~]# FLANNEL_MTU=$(cat /run/flannel/subnet.env| grep "FLANNEL_MTU"|awk -F \= '{print $2}')
[root@master ~]# FLANNEL_IPMASQ=$(cat /run/flannel/subnet.env| grep "FLANNEL_IPMASQ"|awk -F \= '{print $2}')
[root@master ~]# sed "s#ExecStart.*#\0 --bip=${FLANNEL_SUBNET} --ip-masq=${FLANNEL_IPMASQ} --mtu=${FLANNEL_MTU}#" /lib/systemd/system/docker.service
[root@master ~]# systemctl daemon-reload &&  systemctl restart docker
[root@master ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:29:99:77 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.191/24 brd 192.168.1.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::fd11:1c2e:86b7:51d1/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
4: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UP group default 
    link/ether 02:42:bb:5a:b3:8b brd ff:ff:ff:ff:ff:ff
    inet 10.10.55.1/24 brd 10.10.55.255 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:bbff:fe5a:b38b/64 scope link 
       valid_lft forever preferred_lft forever
7: flannel.1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UNKNOWN group default 
    link/ether 72:07:98:e9:24:83 brd ff:ff:ff:ff:ff:ff
    inet 10.10.55.0/32 scope global flannel.1
       valid_lft forever preferred_lft forever
    inet6 fe80::7007:98ff:fee9:2483/64 scope link 
       valid_lft forever preferred_lft forever
[root@master ~]# docker run -itd --name alpine alpine
[root@master ~]# docker exec  -it alpine  ip a      
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
26: eth0@if27: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1450 qdisc noqueue state UP 
    link/ether 02:42:0a:0a:37:02 brd ff:ff:ff:ff:ff:ff
    inet 10.10.55.2/24 brd 10.10.55.255 scope global eth0
       valid_lft forever preferred_lft forever

三.slave01服务器

[root@slave01 ~]# hostnamectl set-hostname slave01
[root@slave01 ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
[root@slave01 ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@slave01 ~]# yum makecache fast
[root@slave01 ~]# yum -y install docker-ce
[root@slave01 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.191 etcd
192.168.1.191 slave01
192.168.1.192 slave01
[root@slave01 ~]# yum install flannel -y
[root@slave01 ~]# cp /etc/sysconfig/flanneld /etc/sysconfig/flanneld_bak
[root@slave01 ~]# systemctl restart flanneld
[root@slave01 ~]# systemctl enable flanneld
[root@slave01 ~]# FLANNEL_SUBNET=$(cat /run/flannel/subnet.env| grep "FLANNEL_SUBNET"|awk -F \= '{print $2}')

[root@slave01 ~]# FLANNEL_MTU=$(cat /run/flannel/subnet.env| grep "FLANNEL_MTU"|awk -F \= '{print $2}')

[root@slave01 ~]# FLANNEL_IPMASQ=$(cat /run/flannel/subnet.env| grep "FLANNEL_IPMASQ"|awk -F \= '{print $2}')

[root@slave01 ~]# sed "s#ExecStart.*#\0 --bip=${FLANNEL_SUBNET} --ip-masq=${FLANNEL_IPMASQ} --mtu=${FLANNEL_MTU}#" /lib/systemd/system/docker.service

[root@slave01 ~]# systemctl daemon-reload &&  systemctl restart docker &&  systemctl enable docker
[root@slave01 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:d2:e4:48 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.192/24 brd 192.168.1.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::7700:b7a:b19c:7b16/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
4: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
    link/ether 02:42:1a:53:9d:49 brd ff:ff:ff:ff:ff:ff
    inet 10.10.60.1/24 brd 10.10.60.255 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:1aff:fe53:9d49/64 scope link 
       valid_lft forever preferred_lft forever
11: flannel.1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UNKNOWN group default 
    link/ether 2a:ad:44:76:cf:42 brd ff:ff:ff:ff:ff:ff
    inet 10.10.60.0/32 scope global flannel.1
       valid_lft forever preferred_lft forever
    inet6 fe80::28ad:44ff:fe76:cf42/64 scope link 
       valid_lft forever preferred_lft forever
[root@slave01 ~]# docker run -itd --name alpine alpine
[root@slave01 ~]#  docker exec  -it alpine  ip a 
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
18: eth0@if19: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1450 qdisc noqueue state UP 
    link/ether 02:42:0a:0a:3c:02 brd ff:ff:ff:ff:ff:ff
    inet 10.10.60.2/24 brd 10.10.60.255 scope global eth0
       valid_lft forever preferred_lft forever

四.验证

[root@master ~]# docker exec  -it alpine  ping 10.10.60.1 -c 2  
PING 10.10.60.1 (10.10.60.1): 56 data bytes
64 bytes from 10.10.60.1: seq=0 ttl=63 time=1.373 ms
64 bytes from 10.10.60.1: seq=1 ttl=63 time=1.058 ms

--- 10.10.60.1 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 1.058/1.215/1.373 ms


[root@slave01 ~]# docker exec  -it alpine  ping 10.10.55.1 -c 2
PING 10.10.55.1 (10.10.55.1): 56 data bytes
64 bytes from 10.10.55.1: seq=0 ttl=63 time=0.480 ms
64 bytes from 10.10.55.1: seq=1 ttl=63 time=0.561 ms

--- 10.10.55.1 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.480/0.520/0.561 ms
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!