一、Linux内核实现名称空间的创建
1.1 ip netns命令
可以借助ip netns命令来完成对 Network Namespace 的各种操作。ip netns命令来自于iproute安装包,一般系统会默认安装,如果没有的话,请自行安装。
注意:ip netns命令修改网络配置时需要 sudo 权限。
可以通过ip netns命令完成对Network Namespace 的相关操作,可以通过ip netns help查看命令帮助信息:
[root@localhost ~]
Usage: ip netns list
ip netns add NAME
ip netns attach NAME PID
ip netns set NAME NETNSID
ip [-all] netns delete [NAME]
ip netns identify [PID]
ip netns pids NAME
ip [-all] netns exec [NAME] cmd ...
ip netns monitor
ip netns list-id [target-nsid POSITIVE-INT] [nsid POSITIVE-INT]
NETNSID := auto | POSITIVE-INT
默认情况下,Linux系统中是没有任何 Network Namespace的,所以ip netns list命令不会返回任何信息。
[root@localhost ~]
[root@localhost ~]
1.2 创建Network Namespace
通过命令创建一个名为ns0的命名空间:
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
ns0
新创建的 Network Namespace 会出现在/var/run/netns/目录下。如果相同名字的 namespace 已经存在,命令会报Cannot create namespace file “/var/run/netns/ns0”: File exists的错误。
[root@localhost ~]
ns0
[root@localhost ~]
Cannot create namespace file "/var/run/netns/ns0": File exists
对于每个 Network Namespace 来说,它会有自己独立的网卡、路由表、ARP 表、iptables 等和网络相关的资源。
1.3 操作Network Namespace
ip命令提供了ip netns exec 子命令可以在对应的 Network Namespace 中执行命令。
查看新创建 Network Namespace 的网卡信息
[root@localhost ~]
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
可以看到,新创建的Network Namespace中会默认创建一个lo回环网卡,此时网卡处于关闭状态。此时,尝试去 ping 该lo回环网卡,会提示Network is unreachable
[root@localhost ~]
connect: Network is unreachable
通过下面的命令启用lo回环网卡:
[root@localhost ~]
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
[root@localhost ~]
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.023 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.025 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.054 ms
1.4 转移设备
我们可以在不同的 Network Namespace 之间转移设备(如veth)。由于一个设备只能属于一个 Network Namespace ,所以转移后在这个 Network Namespace 内就看不到这个设备了。
其中,veth设备属于可转移设备,而很多其它设备(如lo、vxlan、ppp、bridge等)是不可以转移的。
1.5 veth pair
veth pair 全称是 Virtual Ethernet Pair,是一个成对的端口,所有从这对端口一 端进入的数据包都将从另一端出来,反之也是一样。 引入veth pair是为了在不同的 Network Namespace 直接进行通信,利用它可以直接将两个 Network Namespace 连接起来。
1.6 创建veth pair
// 先过滤veth是否已创建
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
4: veth0@veth1: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN group default qlen 1000
5: veth1@veth0: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN group default qlen 1000
[root@localhost ~]
......已省略
4: veth0@veth1: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN group default qlen 1000
link/ether 6e:b8:6c:27:26:88 brd ff:ff:ff:ff:ff:ff
5: veth1@veth0: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN group default qlen 1000
link/ether 32:40:6d:f5:9b:cc brd ff:ff:ff:ff:ff:ff
可以看到,此时系统中新增了一对veth pair,将veth0和veth1两个虚拟网卡连接了起来,此时这对 veth pair 处于”未启用“状态。
1.7 实现Network Namespace间通信
下面我们利用veth pair实现两个不同的 Network Namespace 之间的通信。刚才我们已经创建了一个名为ns0的 Network Namespace,下面再创建一个信息Network Namespace,命名为ns1
[root@localhost ~]
[root@localhost ~]
ns1
ns0
然后我们将veth0加入到ns0,将veth1加入到ns1
[root@localhost ~]
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
[root@localhost ~]
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
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
4: veth0@if5: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
link/ether 6e:b8:6c:27:26:88 brd ff:ff:ff:ff:ff:ff link-netns ns1
[root@localhost ~]
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
5: veth1@if4: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
link/ether 32:40:6d:f5:9b:cc brd ff:ff:ff:ff:ff:ff link-netns ns0
然后我们分别为这对veth、pair配置上ip地址,并启用它们
[root@localhost ~]
[root@localhost ~]
// 查看这对veth pair的状态
[root@localhost ~]
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
4: veth0@if5: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state LOWERLAYERDOWN group default qlen 1000
link/ether 6e:b8:6c:27:26:88 brd ff:ff:ff:ff:ff:ff link-netns ns1
inet 192.168.91.100/24 scope global veth0
valid_lft forever preferred_lft forever
[root@localhost ~]
// 启动lo环回网卡
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
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
5: veth1@if4: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 32:40:6d:f5:9b:cc brd ff:ff:ff:ff:ff:ff link-netns ns0
inet 192.168.91.200/24 scope global veth1
valid_lft forever preferred_lft forever
inet6 fe80::3040:6dff:fef5:9bcc/64 scope link
valid_lft forever preferred_lft forever
从上面可以看出,我们已经成功启用了这个veth pair,并为每个veth设备分配了对应的ip地址。我们尝试在ns1中访问ns0中的ip地址:
// 可以看到,veth pair成功实现了两个不同Network Namespace之间的网络交互
[root@localhost ~]
PING 192.168.91.138 (192.168.91.138) 56(84) bytes of data.
64 bytes from 192.168.91.138: icmp_seq=1 ttl=64 time=0.037 ms
64 bytes from 192.168.91.138: icmp_seq=2 ttl=64 time=0.030 ms
64 bytes from 192.168.91.138: icmp_seq=3 ttl=64 time=0.034 ms
64 bytes from 192.168.91.138: icmp_seq=4 ttl=64 time=0.031 ms
64 bytes from 192.168.91.138: icmp_seq=5 tt
1.8 veth设备重命名
[root@localhost ~]
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
4: veth0@if5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 6e:b8:6c:27:26:88 brd ff:ff:ff:ff:ff:ff link-netns ns1
inet 192.168.91.138/24 scope global veth0
valid_lft forever preferred_lft forever
inet6 fe80::6cb8:6cff:fe27:2688/64 scope link
valid_lft forever preferred_lft forever
// 表示设备或资源繁忙
[root@localhost ~]
RTNETLINK answers: Device or resource busy
// 需要先停止设备
[root@localhost ~]
// 更改名字为amu
[root@localhost ~]
// 重新启动
[root@localhost ~]
// 查看
[root@localhost ~]
[root@localhost ~]
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
4: amu@if5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 6e:b8:6c:27:26:88 brd ff:ff:ff:ff:ff:ff link-netns ns1
inet 192.168.91.138/24 scope global amu
valid_lft forever preferred_lft forever
inet6 fe80::6cb8:6cff:fe27:2688/64 scope link
valid_lft forever preferred_lft forever
二、 四种网络模式配置
2.1 bridge模式配置
[root@localhost ~]# [root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
gaofan1225/php v0.1 9bb6f6ec5b7c 31 hours ago 1.53GB
gaofan1225/mysql v0.1 7abe6fc81912 33 hours ago 3.81GB
gaofan1225/nginx v0.1 453bfb1a13ae 34 hours ago 575MB
centos latest 5d0da3dc9764 2 months ago 231MB
[root@localhost ~]# docker run -it --name test1 --rm --network bridge centos
[root@73c2a81abf26 /]# 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
8: eth0@if9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
[root@localhost ~]# docker run -it --name test1 --rm centos
[root@e5e20f1ac901 /]# 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
6: eth0@if7: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
// 以上就会发现添加 --network bridge与不加--network选项效果是一致的,bridge模式是docker的默认网络模式
2.2 none模式配置
[root@localhost ~]# docker run -it --name test2 --rm --network none centos
[root@2a7191135cd4 /]# 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
2.3 container模式配置
启动第一个容器
[root@localhost ~]# docker pull busybox
Using default tag: latest
latest: Pulling from library/busybox
3aab638df1a9: Pull complete
Digest: sha256:52817dece4cfe26f581c834d27a8e1bcc82194f914afe6d50afad5a101234ef1
Status: Downloaded newer image for busybox:latest
docker.io/library/busybox:latest
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
gaofan1225/php v0.1 9bb6f6ec5b7c 32 hours ago 1.53GB
gaofan1225/mysql v0.1 7abe6fc81912 34 hours ago 3.81GB
gaofan1225/nginx v0.1 453bfb1a13ae 34 hours ago 575MB
busybox latest d23834f29b38 5 days ago 1.24MB
centos latest 5d0da3dc9764 2 months ago 231MB
[root@localhost ~]# docker run -it --name test3 --rm busybox
/ # ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:AC:11:00:02
inet addr:172.17.0.2 Bcast:172.17.255.255 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:9 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:766 (766.0 B) TX bytes:0 (0.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
启动第二个容器
[root@localhost ~]# docker run -it --name test4 --rm busybox
/ # ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:AC:11:00:03
inet addr:172.17.0.3 Bcast:172.17.255.255 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:6 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:516 (516.0 B) TX bytes:0 (0.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
可以看到名为 test4 的容器IP地址 test3 容器的IP地址不是一样的,也就是说并没有共享网络,此时如果我们将第二个容器的启动方式改变一下,就可以使名为 test4 的容器IP与 test3 容器IP一致,也即共享IP,但不共享文件系统。
[root@localhost ~]# docker run -it --name test4 --rm --network container:test3 busybox
/ # ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:AC:11:00:02
inet addr:172.17.0.2 Bcast:172.17.255.255 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:12 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:976 (976.0 B) TX bytes:0 (0.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
此时我们在 test3 容器上创建一个目录
/
/
/
amu
到 test4 容器上检查/tmp目录会发现并没有这个目录,因为文件系统是处于隔离状态,仅仅是共享了网络而已。
/
/
在 test4 容器上部署一个站点
/ # echo "hello amu" > /tmp/index.html
/ # ls /tmp/
index.html
/ # httpd -h /tmp/
/ # netstat -antl
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 :::80 :::* LISTEN
:::* LISTEN
在 test3 容器上用本地地址去访问此站点
/ # wget -O - -q 127.0.0.1:80
hello amu
由此可见,container模式下的容器间关系就相当于一台主机上的两个不同进程
2.4 host模式配置
启动容器时直接指明模式为host
[root@localhost ~]# docker run -it --name t1 --rm --network host busybox
/ # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue 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 fq_codel qlen 1000
link/ether 00:0c:29:c0:44:3d brd ff:ff:ff:ff:ff:ff
inet 192.168.91.138/24 brd 192.168.91.255 scope global dynamic noprefixroute ens33
valid_lft 1051sec preferred_lft 1051sec
inet6 fe80::b44a:bd1a:fcf1:9863/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue
link/ether 02:42:49:05:a8:fe brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
valid_lft forever preferred_lft forever
inet6 fe80::42:49ff:fe05:a8fe/64 scope link
valid_lft forever preferred_lft forever
// 本机ip
[root@localhost ~]# 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 fq_codel state UP group default qlen 1000
link/ether 00:0c:29:c0:44:3d brd ff:ff:ff:ff:ff:ff
inet 192.168.91.138/24 brd 192.168.91.255 scope global dynamic noprefixroute ens33
valid_lft 1022sec preferred_lft 1022sec
inet6 fe80::b44a:bd1a:fcf1:9863/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
link/ether 02:42:49:05:a8:fe brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
valid_lft forever preferred_lft forever
inet6 fe80::42:49ff:fe05:a8fe/64 scope link
valid_lft forever preferred_lft forever
此时如果我们在这个容器中启动一个 http站点,我们就可以直接用宿主机的IP直接在浏览器中访问这个容器中的站点了。
/
/
index.html
/
/
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 192.168.91.138:22 192.168.91.1:55089 ESTABLISHED
tcp 0 0 192.168.91.138:22 192.168.91.1:65291 ESTABLISHED
tcp 0 0 192.168.91.138:22 192.168.91.1:55088 ESTABLISHED
tcp 0 52 192.168.91.138:22 192.168.91.1:65290 ESTABLISHED
tcp 0 0 :::22 :::* LISTEN
tcp 0 0 :::80 :::* LISTEN
// 关闭防火墙和selinux
[root@localhost ~]
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
SELINUX=disbaled
页面访问
三、容器的常用操作
3.1 查看容器的主机名
[root@localhost ~]# docker run -it --name t2 --rm busybox
/ # hostname
f7d47fd81637
/ #
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f7d47fd81637 busybox "sh" 19 seconds ago Up 18 seconds t2
[root@localhost ~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f7d47fd81637 busybox "sh" 59 seconds ago Up 58 seconds t2
// 我们可以发现这里的名字和你运行的容器ID一模一样
3.2 在容器启动时注入主机名
// 开始注入主机名
[root@localhost ~]# docker run -it --name t2 --rm --hostname host1 busybox
/ # hostname
host1
/ # cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2 host1 // 注入主机名时会自动创建主机名到IP的映射关系
/ # cat /etc/resolv.conf
# Generated by NetworkManager
search localdomain
nameserver 192.168.91.2 // DNS也会自动配置为宿主机的DNS
3.3 手动指定容器要使用的DNS
[root@localhost ~]# docker run -it --name t2 --rm --hostname host1 --dns 8.8.8.8 busybox
/ # cat /etc/resolv.conf
search localdomain
nameserver 8.8.8.8
3.4 手动往/etc/hosts文件中注入主机名到IP地址的映射
[root@localhost ~]# docker run -it --name t2 --rm --hostname node1 --add-host www.t2.com:8.8.8.8 busybox
/ # cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
8.8.8.8 www.t2.com
172.17.0.2 node1
3.5 开放容器端口
执行docker run的时候有个-p选项,可以将容器中的应用端口映射到宿主机中,从而实现让外部主机可以通过访问宿主机的某端口来访问容器内应用的目的。
-p选项能够使用多次,其所能够暴露的端口必须是容器确实在监听的端口。
-p选项的使用格式:
[root@localhost ~]
REPOSITORY TAG IMAGE ID CREATED SIZE
gaofan1225/php v0.1 9bb6f6ec5b7c 33 hours ago 1.53GB
gaofan1225/mysql v0.1 7abe6fc81912 35 hours ago 3.81GB
gaofan1225/nginx v0.1 453bfb1a13ae 35 hours ago 575MB
busybox latest d23834f29b38 5 days ago 1.24MB
centos latest 5d0da3dc9764 2 months ago 231MB
[root@localhost ~]
[root@a8ff680fc2bb /]
[root@a8ff680fc2bb /]
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
新开一个控制终端连接来看一下容器的80端口被映射到了宿主机的什么端口上
[root@docker ~]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a8ff680fc2bb 453bfb1a13ae "/bin/bash /start.sh" About a minute ago Up About a minute 0.0.0.0:49153->80/tcp, :::49153->80/tcp nginx
[root@docker ~]
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:49153 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 128 [::]:49153 [::]:*
由此可见,容器的80端口被暴露到了宿主机的49153端口上,此时我们在宿主机上访问一下这个端口看是否能访问到容器内的站点
curl http://127.0.0.1:49153
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@docker ~]
做了nginx的端口映射,所以用宿主机的ip地址加上映射的随机端口也能访问到nginx页面
// iptables防火墙规则将随容器的创建自动生成,随容器的删除自动删除规则。
[root@Docker ~]
......以上省略
Chain DOCKER (2 references)
pkts bytes target prot opt in out source destination
0 0 RETURN all -- docker0 * 0.0.0.0/0 0.0.0.0/0
2 96 DNAT tcp -- !docker0 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:49153 to:172.17.0.2:80
- -p < hostPort >:< containerPort >
[root@Docker ~]
[root@83d3b461bcb6 /]
[root@83d3b461bcb6 /]
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
//在另一个终端上查看端口映射情况
[root@localhost ~]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
83d3b461bcb6 453bfb1a13ae "/bin/bash" 22 seconds ago Up 24 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp nginx
用本主机的IP网站测试,这里不需要跟80端口
- -p < ip >::< containerPort >
[root@Docker ~]
[root@3294c11d18c3 /]
[root@3294c11d18c3 /]
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
// 再打开一个终端上查看端口映射情况
[root@Docker ~]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3294c11d18c3 453bfb1a13ae "/bin/bash" About a minute ago Up About a minute 192.168.91.138:49153->80/tcp nginx
[root@Docker ~]
80/tcp -> 192.168.91.138:49153
- -p < ip >:< hostPort >:< containerPort >
[root@Docker ~]
[root@497354a35a18 /]
[root@497354a35a18 /]
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
// 再打开一个终端上查看端口映射情况
[root@Docker ~]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
497354a35a18 453bfb1a13ae "/bin/bash" 20 seconds ago Up 20 seconds 192.168.91.138:80->80/tcp nginx
[root@Docker ~]
80/tcp -> 192.168.91.138:80
四、自定义docker0桥的网络属性信息
官方文档相关配置
自定义docker0桥的网络属性信息需要修改/etc/docker/daemon.json 配置文件
注意: 修改的网桥不能和自己的默认网卡网段一致(ens33 192.168.91.138)
{
"bip": "192.168.1.5/24",
"fixed-cidr": "192.168.1.5/25",
"fixed-cidr-v6": "2001:db8::/64",
"mtu": 1500,
"default-gateway": "10.20.1.1",
"default-gateway-v6": "2001:db8:abcd::89",
"dns": ["10.20.1.2","10.20.1.3"]
}
核心选项为bip,即bridge ip之意,用于指定docker0桥自身的IP地址;其它选项可通过此地址计算得出。
[root@localhost ~]
{
"bip": "192.168.80.133/24",
"registry-mirrors": ["https://kgdsiwq8.mirror.aliyuncs.com"] // 镜像加速器
}
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
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 fq_codel state UP group default qlen 1000
link/ether 00:0c:29:c0:44:3d brd ff:ff:ff:ff:ff:ff
inet 192.168.91.138/24 brd 192.168.91.255 scope global dynamic noprefixroute ens33
valid_lft 1524sec preferred_lft 1524sec
inet6 fe80::b44a:bd1a:fcf1:9863/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
link/ether 02:42:fb:e1:e1:9f brd ff:ff:ff:ff:ff:ff
inet 192.168.80.133/24 brd 192.168.44.255 scope global docker0
valid_lft forever preferred_lft forever
4.1 docker远程连接
dockerd守护进程的C/S,其默认仅监听Unix Socket格式的地址(/var/run/docker.sock),如果要使用TCP套接字,则需要修改/etc/docker/daemon.json配置文件,添加如下内容,然后重启docker服务:
"hosts": ["tcp://0.0.0.0:2375", "unix:///var/run/docker.sock"]
在客户端上向dockerd直接传递“-H|–host”选项指定要控制哪台主机上的docker容器
docker -H 192.168.10.145:2375 ps
4.2 docker创建自定义桥
创建一个额外的自定义桥,区别于docker0
[root@localhost ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
e1e202a3730b bridge bridge local
6a1e12ee95d3 host host local
464d28c1143a none null local
[root@localhost ~]# docker network create -d bridge --subnet "192.168.25.0/24" --gateway "192.168.25.1" br0
a6779eff66ec87efd42dc751efaf316e3c369923f69cef0080919cfe45957e4d
[root@localhost ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
a6779eff66ec br0 bridge local
e1e202a3730b bridge bridge local
6a1e12ee95d3 host host local
464d28c1143a none null local
// 使用新创建的自定义桥来创建容器
[root@localhost ~]# docker run -it --name tt --rm --network br0 busybox
/ # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue 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
65: eth0@if66: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
link/ether 02:42:c0:a8:18:02 brd ff:ff:ff:ff:ff:ff
inet 192.168.25.2/24 brd 192.168.25.255 scope global eth0
valid_lft forever preferred_lft forever
/ # route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.25.1 0.0.0.0 UG 0 0 0 eth0
192.168.25.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
试想一下,此时的 b1 与 b2 能否互相通信?如果不能该如何实现通信?
我们可以把桥连接过去,br0是我们上传创建的,把默认桥连接到刚刚创建的桥
[root@localhost ~]
NETWORK ID NAME DRIVER SCOPE
a6779eff66ec br0 bridge local
e1e202a3730b bridge bridge local
6a1e12ee95d3 host host local
464d28c1143a none null local
[root@localhost ~]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
039bd707sf61 busybox "sh" 6 minutes ago Up 6 minutes b2 // 默认桥
2524fa3b17d7 busybox "sh" 7 minutes ago Up 7 minutes b1
[root@localhost ~]
再到 b2 容器中去ping 192.168.25.2
/
PING 192.168.9.2 (192.168.9.2): 56 data bytes
64 bytes from 192.168.9.2: seq=0 ttl=64 time=0.571 ms
64 bytes from 192.168.9.2: seq=1 ttl=64 time=0.116 ms
64 bytes from 192.168.9.2: seq=2 ttl=64 time=0.221 ms
|