共計(jì) 6716 個字符,預(yù)計(jì)需要花費(fèi) 17 分鐘才能閱讀完成。
這篇文章主要介紹“Docker 的網(wǎng)絡(luò)模式介紹”,在日常操作中,相信很多人在 Docker 的網(wǎng)絡(luò)模式介紹問題上存在疑惑,丸趣 TV 小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Docker 的網(wǎng)絡(luò)模式介紹”的疑惑有所幫助!接下來,請跟著丸趣 TV 小編一起來學(xué)習(xí)吧!
1、Docker 的四種網(wǎng)絡(luò)模式
(1)docker 四種網(wǎng)絡(luò)模式如下:
Bridge contauner 橋接式網(wǎng)絡(luò)模式
Host(open) container 開放式網(wǎng)絡(luò)模式
Container(join) container 聯(lián)合掛載式網(wǎng)絡(luò)模式,是 host 網(wǎng)絡(luò)模式的延伸
None(Close) container 封閉式網(wǎng)絡(luò)模式
(2)可以通過 docker network 命令查看
[root@along ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
f23b4899add1 bridge bridge local
65520497f693 host host local
a0c5f18e0f04 none null local
(3)docker run –network 命令可以指定使用網(wǎng)絡(luò)模式
2、Bridge 網(wǎng)絡(luò)模式 2.1 介紹
當(dāng) Docker 進(jìn)程啟動時,會在主機(jī)上創(chuàng)建一個名為 docker0 的虛擬網(wǎng)橋,此主機(jī)上啟動的 Docker 容器會連接到這個虛擬網(wǎng)橋上,所以有默認(rèn)地址 172.17.0.0/16 的地址。虛擬網(wǎng)橋的工作方式和物理交換機(jī)類似,這樣主機(jī)上的所有容器就通過交換機(jī)連在了一個二層網(wǎng)絡(luò)中。
從 docker0 子網(wǎng)中分配一個 IP 給容器使用,并設(shè)置 docker0 的 IP 地址為容器的默認(rèn)網(wǎng)關(guān)。在主機(jī)上創(chuàng)建一對虛擬網(wǎng)卡 veth pair 設(shè)備,Docker 將 veth pair 設(shè)備的一端放在新創(chuàng)建的容器中,并命名為 eth0(容器的網(wǎng)卡),另一端放在主機(jī)中,以 vethxxx 這樣類似的名字命名,并將這個網(wǎng)絡(luò)設(shè)備加入到 docker0 網(wǎng)橋中。可以通過 brctl show 命令查看。
[root@along ~]# brctl show
bridge name bridge id STP enabled interfaces
docker0 8000.024241c45d6e no
bridge 模式是 docker 的默認(rèn)網(wǎng)絡(luò)模式,不寫 –net 參數(shù),就是 bridge 模式。使用 docker run - p 時,docker 實(shí)際是在 iptables 做了 DNAT 規(guī)則,實(shí)現(xiàn)端口轉(zhuǎn)發(fā)功能。可以使用 iptables -t nat -vnL 查看。
[root@along ~]# iptables -t nat -vnL
Chain POSTROUTING (policy ACCEPT 20 packets, 1238 bytes)
pkts bytes target prot opt in out source destination
0 0 MASQUERADE all -- * !docker0 172.17.0.0/16 0.0.0.0/0
2.2 bridge 模式示意圖
2.3 演示
bridge 網(wǎng)絡(luò)模式;–network 不指定,默認(rèn)也是 bridge 模式
[root@along ~]# docker run --name b1 -it --network bridge --rm busybox:latest
/ # ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:AC:11:00:02
inet addr:172.17.0.2 Bcast:0.0.0.0 Mask:255.255.0.0
inet6 addr: fe80::42:acff:fe11:2/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:12 errors:0 dropped:0 overruns:0 frame:0
TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1016 (1016.0 B) TX bytes:508 (508.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
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:1
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
/ # route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 172.17.0.1 0.0.0.0 UG 0 0 0 eth0
172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0
/ # ping 10.11.55.5 正常訪問宿主機(jī)
PING 10.11.55.5 (10.11.55.5): 56 data bytes
64 bytes from 10.11.55.5: seq=0 ttl=64 time=0.292 ms
/ # exit
3、Host 網(wǎng)絡(luò)模式 3.1 介紹
如果啟動容器的時候使用 host 模式,那么這個容器將不會獲得一個獨(dú)立的 Network Namespace,而是和宿主機(jī)共用一個 Network Namespace。容器將不會虛擬出自己的網(wǎng)卡,配置自己的 IP 等,而是使用宿主機(jī)的 IP 和端口。但是,容器的其他方面,如文件系統(tǒng)、進(jìn)程列表等還是和宿主機(jī)隔離的。
3.2 Host 模式示意圖
3.3 演示
[root@along ~]# docker run --name b2 -it --network host --rm busybox:latest
/ # ifconfig -a 和宿主機(jī)一樣
docker0 Link encap:Ethernet HWaddr 02:42:41:C4:5D:6E
inet addr:172.17.0.1 Bcast:0.0.0.0 Mask:255.255.0.0
inet6 addr: fe80::42:41ff:fec4:5d6e/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:90 errors:0 dropped:0 overruns:0 frame:0
TX packets:26 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:5903 (5.7 KiB) TX bytes:2381 (2.3 KiB)
eth0 Link encap:Ethernet HWaddr 00:0C:29:AB:D2:DA
inet addr:10.11.55.5 Bcast:10.11.55.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:feab:d2da/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:3913 errors:0 dropped:0 overruns:0 frame:0
TX packets:3327 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:354314 (346.0 KiB) TX bytes:919096 (897.5 KiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
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:1
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
4、Container 網(wǎng)絡(luò)模式 4.1 介紹
這個模式指定新創(chuàng)建的容器和已經(jīng)存在的一個容器共享一個 Network Namespace,而不是和宿主機(jī)共享。新創(chuàng)建的容器不會創(chuàng)建自己的網(wǎng)卡,配置自己的 IP,而是和一個指定的容器共享 IP、端口范圍等。同樣,兩個容器除了網(wǎng)絡(luò)方面,其他的如文件系統(tǒng)、進(jìn)程列表等還是隔離的。兩個容器的進(jìn)程可以通過 lo 網(wǎng)卡設(shè)備通信。
4.2 Container 模式示意圖
4.3 演示
(1)在一個終端,使用 bridge 網(wǎng)絡(luò)模式啟動容器 b1
[root@along ~]# docker run --name b1 -it --rm busybox:latest
/ # ifconfig b1 的 ip 為 172.17.0.2
eth0 Link encap:Ethernet HWaddr 02:42:AC:11:00:02
inet addr:172.17.0.2 Bcast:0.0.0.0 Mask:255.255.0.0
inet6 addr: fe80::42:acff:fe11:2/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:6 errors:0 dropped:0 overruns:0 frame:0
TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:508 (508.0 B) TX bytes:508 (508.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
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:1
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
/ # echo hello world b1 /tmp/index.html
/ # httpd -h /tmp/ 在 b1 上啟動 httpd 服務(wù)
/ # netstat -nutl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 :::80 :::* LISTEN
(2)在另一個終端使用 Container 網(wǎng)絡(luò)模式創(chuàng)建容器 b2
[root@along ~]# docker run --name b2 -it --network container:b1 --rm busybox:latest
/ # ifconfig -a b2 的 ip 和 b1 一樣
eth0 Link encap:Ethernet HWaddr 02:42:AC:11:00:02
inet addr:172.17.0.2 Bcast:0.0.0.0 Mask:255.255.0.0
inet6 addr: fe80::42:acff:fe11:2/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:8 errors:0 dropped:0 overruns:0 frame:0
TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:648 (648.0 B) TX bytes:648 (648.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
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:1
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
/ # wget -O - -q 127.0.0.1 b1 啟動的 httpd 服務(wù),在 b2 上直接訪問
hello world b1
/ # ls /tmp/ 但是文件系統(tǒng)并不共享,只共享網(wǎng)絡(luò)
5、None 網(wǎng)絡(luò)模式 5.1 介紹
使用 none 模式,Docker 容器擁有自己的 Network Namespace,但是,并不為 Docker 容器進(jìn)行任何網(wǎng)絡(luò)配置。也就是說,這個 Docker 容器沒有網(wǎng)卡、IP、路由等信息,只有 lo 網(wǎng)絡(luò)接口。需要我們自己為 Docker 容器添加網(wǎng)卡、配置 IP 等。
不參與網(wǎng)絡(luò)通信,運(yùn)行于此類容器中的進(jìn)程僅能訪問本地回環(huán)接口;僅適用于進(jìn)程無須網(wǎng)絡(luò)通信的場景中,例如:備份、進(jìn)程診斷及各種離線任務(wù)等。
5.2 Node 模式示意圖
5.3 演示
[root@along ~]# docker run --name b1 -it --network none --rm busybox:latest
/ # ifconfig
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
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:1
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
/ # route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
/ # exit
到此,關(guān)于“Docker 的網(wǎng)絡(luò)模式介紹”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注丸趣 TV 網(wǎng)站,丸趣 TV 小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!