您好,登錄后才能下訂單哦!
這篇文章主要介紹“如何部署點到點的docker網絡”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“如何部署點到點的docker網絡”文章能幫助大家解決問題。
[root@liuxin-test01 ~]# ip a1: 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
--rm 參數講解一下: 在Docker容器退出時,默認容器內部的文件系統仍然被保留,以方便調試并保留用戶數據。 但是,對于foreground容器,由于其只是在開發調試過程中短期運行,其用戶數據并無保留的必要,因而可以在容器啟動時設置--rm選項,這樣在容器退出時就能夠自動清理容器內部的文件系統。 --net=none 無網絡環境 --net=bridge 默認的參數,通過網橋(docker0)來設置容器的網絡。也可以通過DOCKER_OPTS選項的-b參數來指定默認網橋 --net=host 共享主機的網絡環境,不推薦這么設置 --net=container 兩個容器共享IP地址和端口號等網絡資源
下面兩個容器是在兩個終端中創建的
[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 /]#
[root@liuxin-test01 ~]# docker inspect -f '{{.State.Pid}}' f6421682 [root@liuxin-test01 ~]# docker inspect -f '{{.State.Pid}}' cd421832
[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 ~]#
veth 是虛擬的以太設備,類似于網卡設備。這個是linux容器技術引進的,要求必須成對出現
[root@liuxin-test01 ~]# ip link add A type veth peer name B[root@liuxin-test01 ~]# ip a1: 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
我們可以看到,加入到容器之后,再次執行ip a 已經看不到這兩個設備了
[root@liuxin-test01 ~]# ip link set A netns 21682[root@liuxin-test01 ~]# ip link set B netns 21832[root@liuxin-test01 ~]# ip a1: 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
設置兩個容器網絡空間的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
[root@liuxin-test01 ~]# ip netns exec 21682 ip link set A up[root@liuxin-test01 ~]# ip netns exec 21832 ip link set B up
[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
[root@f64cdc7ffff1 /]# ping 192.168.99.2PING 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.1PING 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
關于“如何部署點到點的docker網絡”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識,可以關注億速云行業資訊頻道,小編每天都會為大家更新不同的知識點。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。