centos7 docker配置桥接,与容器网络互联

环境:

[root@localhost ~]# cat /etc/redhat-release 
CentOS Linux release 7.7.1908 (Core)

[root@localhost ~]# uname -r
3.10.0-1062.el7.x86_64

[root@localhost ~]# ifconfig 
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.111  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::13c3:5f21:11b7:51c5  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:bf:81:71  txqueuelen 1000  (Ethernet)
        RX packets 80  bytes 9527 (9.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 90  bytes 12443 (12.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 1000  (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

安装配置

执行下面命令,可以直接放在脚本中运行,这里的网卡是ens33

#!/bin/bash
#by auth toyix
#20208711:51:54
#auto install config docker bridge 
##########################################
#安装基础环境
yum install docker* -y
yum install bridge-utils -y
systemctl start docker
systemctl stop docker
 
#停止及删除docker0网桥
ifconfig docker0 down
brctl delbr docker0
#查看网桥信息
brctl show
 
#改ens33网卡配置文件
cd /etc/sysconfig/network-scripts/
cat>ifcfg-ens33 <<-EOF 
	DEVICE=ens33
	BOOTPROTO=none
	NM_CONTROLLED=no
	ONBOOT=yes
	TYPE=Ethernet
	BRIDGE="br0"
	IPADDR=192.168.1.111
	NETMASK=255.255.255.0
	GATEWAY=192.168.1.1
	USERCTL=no
EOF

#创建桥接网卡br0配置文件
cat>ifcfg-br0 <<-EOF
	DEVICE="br0" 
	BOOTPROTO=none
	IPV6INIT=no
	NM_CONTROLLED=no
	ONBOOT=yes
	TYPE="Bridge"
	IPADDR=192.168.1.111
	NETMASK=255.255.255.0
	GATEWAY=192.168.1.1
	USERCTL=no
EOF

#docker启用br0设置
cat>/etc/sysconfig/docker-network <<-EOF
	# /etc/sysconfig/docker-network
	DOCKER_NETWORK_OPTIONS="-b=br0"
EOF

#备份DNS文件
cp /etc/resolv.conf{,.bak}
cat /etc/resolv.conf

#git下载pipework
git clone https://github.com/jpetazzo/pipework
\cp pipework/pipework /usr/local/bin/

#网络重启dns归还
/etc/init.d/network restart
\cp /etc/resolv.conf.bak /etc/resolv.conf

#启动docker服务,查看服务是否启动
systemctl start docker
ps -ef|grep docker

执行结果

Cloning into 'pipework'...
remote: Enumerating objects: 8, done.
remote: Counting objects: 100% (8/8), done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 518 (delta 2), reused 5 (delta 2), pack-reused 510
Receiving objects: 100% (518/518), 182.72 KiB | 0 bytes/s, done.
Resolving deltas: 100% (272/272), done.
Restarting network (via systemctl):                        [  OK  ]
root      2104  2012  0 12:01 pts/0    00:00:00 sh autodockerbridge.sh
root      2962     1 10 12:02 ?        00:00:00 /usr/bin/dockerd-current --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 --selinux-enabled --log-driver=journald --signature-verification=false --storage-driver overlay2 -b=br0
root      2969  2962  1 12:02 ?        00:00:00 /usr/bin/docker-containerd-current -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start-timeout 2m --state-dir /var/run/docker/libcontainerd/containerd --shim docker-containerd-shim --runtime docker-runc --runtime-args --systemd-cgroup=true
root      3068  2104  0 12:02 pts/0    00:00:00 grep docker

查看网络信息

[root@localhost src]# ifconfig 
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.111  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::20c:29ff:febf:8171  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:bf:81:71  txqueuelen 1000  (Ethernet)
        RX packets 54329  bytes 1219558719 (1.1 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 58619  bytes 4603307 (4.3 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 0.0.0.0
        ether 02:42:f3:14:02:7d  txqueuelen 0  (Ethernet)
        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

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::20c:29ff:febf:8171  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:bf:81:71  txqueuelen 1000  (Ethernet)
        RX packets 924985  bytes 1353945527 (1.2 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 77928  bytes 6473759 (6.1 MiB)
        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 1000  (Local Loopback)
        RX packets 4  bytes 344 (344.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4  bytes 344 (344.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

创建容器测试

先导入一个centos7 镜像 - centos7 ssh

[root@localhost src]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
[root@localhost src]# 
[root@localhost src]# docker load <centos73_sshOK.tar 
34e7b85d83e4: Loading layer [==================================================>] 199.9 MB/199.9 MB
0d1585b29470: Loading layer [==================================================>] 171.6 MB/171.6 MB
f8c414e271fb: Loading layer [==================================================>] 2.048 kB/2.048 kB
7794e20d52b7: Loading layer [==================================================>] 3.072 kB/3.072 kB
596e51307fcb: Loading layer [==================================================>] 2.048 kB/2.048 kB
cf4eb7184a66: Loading layer [==================================================>] 91.05 MB/91.05 MB
668603ce0b52: Loading layer [==================================================>] 129.9 MB/129.9 MB
06b53c6c558b: Loading layer [==================================================>] 120.2 MB/120.2 MB
1e08aef36d2c: Loading layer [==================================================>] 120.1 MB/120.1 MB
e5ae92625a0e: Loading layer [==================================================>] 120.5 MB/120.5 MB
c2e6c38e2e62: Loading layer [==================================================>] 120.3 MB/120.3 MB
83bc22ec0afa: Loading layer [==================================================>] 140.4 MB/140.4 MB
6d9fb7ba9909: Loading layer [==================================================>]  2.56 kB/2.56 kB
aa8d7a6a7075: Loading layer [==================================================>] 6.144 kB/6.144 kB
9b3bb5b16837: Loading layer [==================================================>] 3.072 kB/3.072 kB
Loaded image ID: sha256:0cb9685cd88518d16b567b69d00424d276e884c7ec0ac75ccd9340f8917f92d2
[root@localhost src]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
<none>              <none>              0cb9685cd885        2 days ago          1.2 GB
改个镜像名称
[root@localhost src]# docker tag 0cb9685cd885 centos7_ssh:v7
[root@localhost src]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos7_ssh         v7                  0cb9685cd885        2 days ago          1.2 GB

创建容器

[root@localhost src]# docker run -itd --privileged=true --net=none --name=bigdata_192.168.1.51 --restart=always centos_ssh:v7 /usr/sbin/init
3b45aff33f3a5c0b7dcb0f2cb3e5ca494b751fe6d0462a717096035f2abb8015
改IP
[root@localhost src]# pipework br0 bigdata_192.168.1.51 192.168.1.51/24@192.168.1.111

在局域网中找一台机器连接测试

[root@localhost ~]# ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.112  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::d469:1174:96ea:fb70  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:5d:3f:32  txqueuelen 1000  (Ethernet)
        RX packets 3577007  bytes 4379177482 (4.0 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4620226  bytes 10030369535 (9.3 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
连接测试
[root@localhost ~]# ssh root@192.168.1.51      
The authenticity of host '192.168.1.51 (192.168.1.51)' can't be established.
ECDSA key fingerprint is SHA256:n265ygRGIyut9OirZ+wtwmCjP1NKZoXBvWtCaJQlhFk.
ECDSA key fingerprint is MD5:d7:83:5f:3b:24:73:0d:b5:4e:d3:2f:91:08:06:cd:82.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.51' (ECDSA) to the list of known hosts.
root@192.168.1.51's password: 
Last login: Fri Aug  7 04:28:34 2020 from 192.168.1.103
[root@3b45aff33f3a ~]# ifconfig 
eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.51  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::741e:24ff:fe0a:affe  prefixlen 64  scopeid 0x20<link>
        ether 76:1e:24:0a:af:fe  txqueuelen 1000  (Ethernet)
        RX packets 215  bytes 24986 (24.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 99  bytes 19139 (18.6 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 1000  (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

注:

连接时,如果提示

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @
WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ IT IS
POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY! Someone could be
eavesdropping on you right now (man-in-the-middle attack)! It is also
possible that a host key has just been changed. The fingerprint for
the ECDSA key sent by the remote host is
SHA256:n265ygRGIyut9OirZ+wtwmCjP1NKZoXBvWtCaJQlhFk. Please contact
your system administrator. Add correct host key in
/root/.ssh/known_hosts to get rid of this message. Offending ECDSA key
in /root/.ssh/known_hosts:6 ECDSA host key for 192.168.1.51 has
changed and you have requested strict checking. Host key verification
failed.

进入/root/.ssh/known_hosts删除对应IP即可
-------------------------end

你可能感兴趣的:(docker,docker,桥接)