這篇文章給大家分享的是有關(guān)docker之如何實(shí)現(xiàn)點(diǎn)到點(diǎn)容器的網(wǎng)絡(luò)配置的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。
成都創(chuàng)新互聯(lián)總部坐落于成都市區(qū),致力網(wǎng)站建設(shè)服務(wù)有成都網(wǎng)站制作、成都做網(wǎng)站、網(wǎng)絡(luò)營(yíng)銷策劃、網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站維護(hù)、公眾號(hào)搭建、微信小程序、軟件開發(fā)等為企業(yè)提供一整套的信息化建設(shè)解決方案。創(chuàng)造真正意義上的網(wǎng)站建設(shè),為互聯(lián)網(wǎng)品牌在互動(dòng)行銷領(lǐng)域創(chuàng)造價(jià)值而不懈努力!
一、搭建容器之間的網(wǎng)絡(luò)
1. 查看目前的網(wǎng)絡(luò)環(huán)境
[root@liuxin-test01 ~]# ip a 1: lo:mtu 65536 qdisc noqueue state UNKNOWN qlen 1 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: mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:ca:41:84 brd ff:ff:ff:ff:ff:ff inet 192.168.8.192/24 brd 192.168.8.255 scope global eth0 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:feca:4184/64 scope link valid_lft forever preferred_lft forever 3: eth2: mtu 1500 qdisc noop state DOWN qlen 1000 link/ether 00:0c:29:ca:41:8e brd ff:ff:ff:ff:ff:ff 4: docker0: mtu 1500 qdisc noqueue state DOWN link/ether 02:42:a3:f4:2f:40 brd ff:ff:ff:ff:ff:ff inet 172.17.0.1/16 scope global docker0 valid_lft forever preferred_lft forever inet6 fe80::42:a3ff:fef4:2f40/64 scope link valid_lft forever preferred_lft forever
2.創(chuàng)建兩個(gè)無網(wǎng)絡(luò)的容器
--rm 參數(shù)講解一下:
在Docker容器退出時(shí),默認(rèn)容器內(nèi)部的文件系統(tǒng)仍然被保留,以方便調(diào)試并保留用戶數(shù)據(jù)。
但是,對(duì)于foreground容器,由于其只是在開發(fā)調(diào)試過程中短期運(yùn)行,其用戶數(shù)據(jù)并無保留的必要,因而可以在容器啟動(dòng)時(shí)設(shè)置--rm選項(xiàng),這樣在容器退出時(shí)就能夠自動(dòng)清理容器內(nèi)部的文件系統(tǒng)。
--net=none 無網(wǎng)絡(luò)環(huán)境
--net=bridge 默認(rèn)的參數(shù),通過網(wǎng)橋(docker0)來設(shè)置容器的網(wǎng)絡(luò)。也可以通過DOCKER_OPTS選項(xiàng)的-b參數(shù)來指定默認(rèn)網(wǎng)橋
--net=host 共享主機(jī)的網(wǎng)絡(luò)環(huán)境,不推薦這么設(shè)置
--net=container 兩個(gè)容器共享IP地址和端口號(hào)等網(wǎng)絡(luò)資源
下面兩個(gè)容器是在兩個(gè)終端中創(chuàng)建的
[root@liuxin-test01 ~]# docker run --rm -it --net=none --name='centos01' centos:7.4.1708 [root@f64cdc7ffff1 /]#
[root@liuxin-test01 ~]# docker run --rm -it --net=none --name='centos02' centos:7.4.1708 [root@cd4df383b68e /]#
3. 查看這兩個(gè)容器的進(jìn)程ID
[root@liuxin-test01 ~]# docker inspect -f '{{.State.Pid}}' f64 21682 [root@liuxin-test01 ~]# docker inspect -f '{{.State.Pid}}' cd4 21832
4. 為這兩個(gè)容器創(chuàng)建虛擬的網(wǎng)絡(luò)空間
[root@liuxin-test01 ~]# mkdir -p /var/run/netns [root@liuxin-test01 ~]# ln -s /proc/21682/ns/net /var/run/netns/21682 [root@liuxin-test01 ~]# ln -s /proc/21832/ns/net /var/run/netns/21832 [root@liuxin-test01 ~]#
5. 創(chuàng)建一對(duì)veth,兩端命名為A、B
veth 是虛擬的以太設(shè)備,類似于網(wǎng)卡設(shè)備。這個(gè)是linux容器技術(shù)引進(jìn)的,要求必須成對(duì)出現(xiàn)
[root@liuxin-test01 ~]# ip link add A type veth peer name B [root@liuxin-test01 ~]# ip a 1: lo:mtu 65536 qdisc noqueue state UNKNOWN qlen 1 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: mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:ca:41:84 brd ff:ff:ff:ff:ff:ff inet 192.168.8.192/24 brd 192.168.8.255 scope global eth0 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:feca:4184/64 scope link valid_lft forever preferred_lft forever 3: eth2: mtu 1500 qdisc noop state DOWN qlen 1000 link/ether 00:0c:29:ca:41:8e brd ff:ff:ff:ff:ff:ff 4: docker0: mtu 1500 qdisc noqueue state DOWN link/ether 02:42:a3:f4:2f:40 brd ff:ff:ff:ff:ff:ff inet 172.17.0.1/16 scope global docker0 valid_lft forever preferred_lft forever inet6 fe80::42:a3ff:fef4:2f40/64 scope link valid_lft forever preferred_lft forever 157: B@A: mtu 1500 qdisc noop state DOWN qlen 1000 link/ether de:f7:3b:24:a5:0e brd ff:ff:ff:ff:ff:ff 158: A@B: mtu 1500 qdisc noop state DOWN qlen 1000 link/ether 9a:65:96:de:04:90 brd ff:ff:ff:ff:ff:ff
6. 將兩端分別放到兩個(gè)容器中
我們可以看到,加入到容器之后,再次執(zhí)行ip a 已經(jīng)看不到這兩個(gè)設(shè)備了
[root@liuxin-test01 ~]# ip link set A netns 21682 [root@liuxin-test01 ~]# ip link set B netns 21832 [root@liuxin-test01 ~]# ip a 1: lo:mtu 65536 qdisc noqueue state UNKNOWN qlen 1 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: mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:ca:41:84 brd ff:ff:ff:ff:ff:ff inet 192.168.8.192/24 brd 192.168.8.255 scope global eth0 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:feca:4184/64 scope link valid_lft forever preferred_lft forever 3: eth2: mtu 1500 qdisc noop state DOWN qlen 1000 link/ether 00:0c:29:ca:41:8e brd ff:ff:ff:ff:ff:ff 4: docker0: mtu 1500 qdisc noqueue state DOWN link/ether 02:42:a3:f4:2f:40 brd ff:ff:ff:ff:ff:ff inet 172.17.0.1/16 scope global docker0 valid_lft forever preferred_lft forever inet6 fe80::42:a3ff:fef4:2f40/64 scope link valid_lft forever preferred_lft forever
7. 設(shè)置兩個(gè)容器網(wǎng)絡(luò)空間的ip
[root@liuxin-test01 ~]# ip netns exec 21682 ip addr add 192.168.99.1/32 dev A [root@liuxin-test01 ~]# ip netns exec 21832 ip addr add 192.168.99.2/32 dev B
8. 啟動(dòng)兩個(gè)容器的網(wǎng)絡(luò)
[root@liuxin-test01 ~]# ip netns exec 21682 ip link set A up [root@liuxin-test01 ~]# ip netns exec 21832 ip link set B up
9. 給這兩個(gè)容器設(shè)置一下網(wǎng)關(guān)
[root@liuxin-test01 ~]# ip netns exec 21682 ip route add 192.168.99.2/32 dev A [root@liuxin-test01 ~]# ip netns exec 21832 ip route add 192.168.99.1/32 dev B
10. 測(cè)試
[root@f64cdc7ffff1 /]# ping 192.168.99.2 PING 192.168.99.2 (192.168.99.2) 56(84) bytes of data. 64 bytes from 192.168.99.2: icmp_seq=1 ttl=64 time=0.095 ms
[root@cd4df383b68e /]# ping 192.168.99.1 PING 192.168.99.1 (192.168.99.1) 56(84) bytes of data. 64 bytes from 192.168.99.1: icmp_seq=1 ttl=64 time=0.057 ms
感謝各位的閱讀!關(guān)于“docker之如何實(shí)現(xiàn)點(diǎn)到點(diǎn)容器的網(wǎng)絡(luò)配置”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!