docker实现跨主机互联-05

一、实现跨主机互联

1.1、需要用到路由,所以需要修改docker默认网络。(备注:添加参数:--bip=172.17.42.1/16 )

#docker-node1节点

#快速查找
[root@docker ~]# history |grep service
   46  vim /usr/lib/system/docker.service
   47  vim /usr/lib/systemd/system/docker.service

#修改配置文件
[root@docker ~]# vim /usr/lib/systemd/system/docker.service
[Unit]
Description=Docker Application Container Engine
Documentation=http://docs.docker.com
After=network.target rhel-push-plugin.socket registries.service
Wants=docker-storage-setup.service
Requires=docker-cleanup.timer

[Service]
Type=notify
NotifyAccess=all
EnvironmentFile=-/run/containers/registries.conf
EnvironmentFile=-/etc/sysconfig/docker
EnvironmentFile=-/etc/sysconfig/docker-storage
EnvironmentFile=-/etc/sysconfig/docker-network
Environment=GOTRACEBACK=crash
Environment=DOCKER_HTTP_HOST_COMPAT=1
Environment=PATH=/usr/libexec/docker:/usr/bin:/usr/sbin
ExecStart=/usr/bin/dockerd-current --registry-mirror=https://alzgoonw.mirror.aliyuncs.com --bip=172.18.42.1/16 \
          --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current \
          --default-runtime=docker-runc \
          --exec-opt native.cgroupdriver=systemd \
          --userland-proxy-path=/usr/libexec/docker/docker-proxy-current \
          --init-path=/usr/libexec/docker/docker-init-current \
          --seccomp-profile=/etc/docker/seccomp.json \
          $OPTIONS \
          $DOCKER_STORAGE_OPTIONS \
          $DOCKER_NETWORK_OPTIONS \
          $ADD_REGISTRY \
          $BLOCK_REGISTRY \
          $INSECURE_REGISTRY \
	  $REGISTRIES
ExecReload=/bin/kill -s HUP $MAINPID
LimitNOFILE=1048576
LimitNPROC=1048576
LimitCORE=infinity
TimeoutStartSec=0
Restart=on-abnormal
KillMode=process

[Install]
WantedBy=multi-user.target

#重启服务

systemctl daemon-reload
systemctl restart docker

#查看状态

[root@docker ~]# systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: active (running) since Sun 2018-06-17 06:42:58 CST; 2min 47s ago
     Docs: http://docs.docker.com
 Main PID: 17967 (dockerd-current)
   Memory: 19.0M
   CGroup: /system.slice/docker.service
           ├─17967 /usr/bin/dockerd-current --registry-mirror=https://alzgoonw.mirror.aliyuncs.com --bip=172.17.42.1/1...
           └─17974 /usr/bin/docker-containerd-current -l unix:///var/run/docker/libcontainerd/docker-containerd.sock -...

Jun 17 06:42:58 docker.com dockerd-current[17967]: time="2018-06-17T06:42:58.528426340+08:00" level=info msg="Grap...nds"
Jun 17 06:42:58 docker.com dockerd-current[17967]: time="2018-06-17T06:42:58.529318331+08:00" level=warning msg="m...und"
Jun 17 06:42:58 docker.com dockerd-current[17967]: time="2018-06-17T06:42:58.529920337+08:00" level=info msg="Load...rt."
Jun 17 06:42:58 docker.com dockerd-current[17967]: time="2018-06-17T06:42:58.545359007+08:00" level=info msg="Fire...lse"
Jun 17 06:42:58 docker.com dockerd-current[17967]: time="2018-06-17T06:42:58.695839843+08:00" level=info msg="Load...ne."
Jun 17 06:42:58 docker.com dockerd-current[17967]: time="2018-06-17T06:42:58.706556894+08:00" level=warning msg="N...fix"
Jun 17 06:42:58 docker.com dockerd-current[17967]: time="2018-06-17T06:42:58.713952960+08:00" level=info msg="Daem...ion"
Jun 17 06:42:58 docker.com dockerd-current[17967]: time="2018-06-17T06:42:58.714102166+08:00" level=info msg="Dock...13.1
Jun 17 06:42:58 docker.com dockerd-current[17967]: time="2018-06-17T06:42:58.720252519+08:00" level=info msg="API ...ock"
Jun 17 06:42:58 docker.com systemd[1]: Started Docker Application Container Engine.
Hint: Some lines were ellipsized, use -l to show in full.

#网络修改成功
[root@docker ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:11:a5:28 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.26/24 brd 192.168.0.255 scope global dynamic eth0
       valid_lft 3366sec preferred_lft 3366sec
    inet6 fe80::20c:29ff:fe11:a528/64 scope link 
       valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN 
    link/ether 02:42:f5:78:e6:21 brd ff:ff:ff:ff:ff:ff
    inet 172.17.42.1/16 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:f5ff:fe78:e621/64 scope link 
       valid_lft forever preferred_lft forever

1.2、准备两个VM主机,配置好基础环境

#环境准备:

#docker-node1 节点

[root@docker-node1 ~]# hostname
docker-node1
[root@docker-node1 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:11:a5:28 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.26/24 brd 192.168.0.255 scope global dynamic eth0
       valid_lft 2238sec preferred_lft 2238sec
    inet6 fe80::20c:29ff:fe11:a528/64 scope link 
       valid_lft forever preferred_lft forever
3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP 
    link/ether 02:42:38:5f:21:a2 brd ff:ff:ff:ff:ff:ff
    inet 172.17.42.1/16 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:38ff:fe5f:21a2/64 scope link 
       valid_lft forever preferred_lft forever
5: veth20fa52c@if4: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP 
    link/ether c6:48:a8:fd:7c:b4 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet6 fe80::c448:a8ff:fefd:7cb4/64 scope link 
       valid_lft forever preferred_lft forever

#修改配置文件,参数:--bip=172.17.42.1/16 (备注:在docker-node2节点)

#修改配置文件
[root@docker ~]# vim /usr/lib/systemd/system/docker.service
[Unit]
Description=Docker Application Container Engine
Documentation=http://docs.docker.com
After=network.target rhel-push-plugin.socket registries.service
Wants=docker-storage-setup.service
Requires=docker-cleanup.timer

[Service]
Type=notify
NotifyAccess=all
EnvironmentFile=-/run/containers/registries.conf
EnvironmentFile=-/etc/sysconfig/docker
EnvironmentFile=-/etc/sysconfig/docker-storage
EnvironmentFile=-/etc/sysconfig/docker-network
Environment=GOTRACEBACK=crash
Environment=DOCKER_HTTP_HOST_COMPAT=1
Environment=PATH=/usr/libexec/docker:/usr/bin:/usr/sbin
ExecStart=/usr/bin/dockerd-current --registry-mirror=https://alzgoonw.mirror.aliyuncs.com --bip=172.17.42.1/16 \
          --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current \
          --default-runtime=docker-runc \
          --exec-opt native.cgroupdriver=systemd \
          --userland-proxy-path=/usr/libexec/docker/docker-proxy-current \
          --init-path=/usr/libexec/docker/docker-init-current \
          --seccomp-profile=/etc/docker/seccomp.json \
          $OPTIONS \
          $DOCKER_STORAGE_OPTIONS \
          $DOCKER_NETWORK_OPTIONS \
          $ADD_REGISTRY \
          $BLOCK_REGISTRY \
          $INSECURE_REGISTRY \
	  $REGISTRIES
ExecReload=/bin/kill -s HUP $MAINPID
LimitNOFILE=1048576
LimitNPROC=1048576
LimitCORE=infinity
TimeoutStartSec=0
Restart=on-abnormal
KillMode=process

[Install]
WantedBy=multi-user.target

#重启服务

systemctl daemon-reload
systemctl restart docker

#修改主机名称和查看ip地址,过程省略

[root@docker-node2 ~]# hostname
docker-node2
[root@docker-node2 ~]# ifconfig
docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.42.1  netmask 255.255.0.0  broadcast 0.0.0.0
        inet6 fe80::42:2cff:fe65:d1d8  prefixlen 64  scopeid 0x20<link>
        ether 02:42:2c:65:d1:d8  txqueuelen 0  (Ethernet)
        RX packets 2040  bytes 99450 (97.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2644  bytes 9831934 (9.3 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.0.28  netmask 255.255.255.0  broadcast 192.168.0.255
        inet6 fe80::20c:29ff:fe64:da45  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:64:da:45  txqueuelen 1000  (Ethernet)
        RX packets 7843  bytes 10148134 (9.6 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2728  bytes 241919 (236.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vethd0c5cb5: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::b0:2dff:fec5:a273  prefixlen 64  scopeid 0x20<link>
        ether 02:b0:2d:c5:a2:73  txqueuelen 0  (Ethernet)
        RX packets 2032  bytes 127362 (124.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2644  bytes 9831934 (9.3 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

#软件安装 (备注:安装好docker服务,并下载好docker 镜像)

#在docker-node1 节点操作

#启动一个docker镜像
[root@docker-node1 ~]# docker run -it --name node1 centos bash

#安装net-tools软件
[root@f21acbd1553c /]# yum install net-tools -y

#查看ip
[root@f21acbd1553c /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.18.0.1  netmask 255.255.0.0  broadcast 0.0.0.0
        inet6 fe80::42:acff:fe11:1  prefixlen 64  scopeid 0x20<link>
        ether 02:42:ac:11:00:01  txqueuelen 0  (Ethernet)
        RX packets 2393  bytes 9824410 (9.3 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2250  bytes 179384 (175.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

##在docker-node2节点操作

选中一台VM,再copy 一台VM。修改ip地址,注释掉uuid.重启网络服务。修改主机名称。

[root@docker-node2 ~]# docker run -it --name node2 centos bash
WARNING: IPv4 forwarding is disabled. Networking will not work.

 解决办法:

参考: https://www.cnblogs.com/nulige/articles/9204841.html

#docker-node2节点

[root@docker-node2 ~]# docker run -it --name node2 centos bash

[root@1bef70b89dc8 /]# yum install net-tools -y

[root@1bef70b89dc8 /]# ifconfig 
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 0.0.0.0
        inet6 fe80::42:acff:fe11:1  prefixlen 64  scopeid 0x20<link>
        ether 02:42:ac:11:00:01  txqueuelen 0  (Ethernet)
        RX packets 2644  bytes 9831934 (9.3 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2032  bytes 127362 (124.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

 

 1.3、查看两个节点ip

#docker-node1

[root@e3b4998face3 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.18.0.1  netmask 255.255.0.0  broadcast 0.0.0.0
        inet6 fe80::42:acff:fe12:1  prefixlen 64  scopeid 0x20<link>
        ether 02:42:ac:12:00:01  txqueuelen 0  (Ethernet)
        RX packets 3066  bytes 9856601 (9.3 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2539  bytes 182009 (177.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

#ping docker-node2 的ip地址 [root@e3b4998face3 /]# ping 172.17.0.1 PING 172.17.0.1 (172.17.0.1) 56(84) bytes of data.

#docker-node2

[root@1bef70b89dc8 /]# ifconfig 
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 0.0.0.0
        inet6 fe80::42:acff:fe11:1  prefixlen 64  scopeid 0x20<link>
        ether 02:42:ac:11:00:01  txqueuelen 0  (Ethernet)
        RX packets 2644  bytes 9831934 (9.3 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2032  bytes 127362 (124.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@1bef70b89dc8 /]# ping 172.18.0.1
PING 172.18.0.1 (172.18.0.1) 56(84) bytes of data.

1.4、给docker-node1做路由

route add -net 192.168.158.0/24 gw 10.0.0.80

  

 

 

 

 

 

 

 

posted @ 2018-06-20 17:00  努力哥  阅读(1021)  评论(0编辑  收藏  举报