在Docker中自定義網(wǎng)絡(luò)配置,實現(xiàn)容器與主機的連通性
【實驗步驟】
[root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens33
ONBOOT=yes
[root@localhost ~]# systemctl restart network
[root@localhost ~]# mkdir /opt/centos -p??
[root@localhost ~]# mount /dev/cdrom /opt/centos/
mount: /dev/sr0 is write-protected, mounting read-only
[root@localhost ~]# cd /opt/
[root@localhost opt]# ls -l??????\\查看是否有以下文件centos,Packages.tar.gz ,postgres.tar
total 472742
drwxrwxr-x. 8 root root ?????2048 Nov 26 ?2018 centos
-rw-r--r--. 1 root root 108512388 Jun ?2 17:51 Packages.tar.gz
-rw-r--r--. 1 root root 375567360 Jun ?2 17:52 postgres.tar
[root@localhost opt]# tar -zxvf Packages.tar.gz
[root@localhost opt]# ls -l
total 472758
drwxrwxr-x. 8 root root ?????2048 Nov 26 ?2018 centos
drwxr-xr-x. 3 root root ????12288 Mar ?8 ?2020 Packages
-rw-r--r--. 1 root root 108512388 Jun ?2 17:51 Packages.tar.gz
-rw-r--r--. 1 root root 375567360 Jun ?2 17:52 postgres.tar
[root@localhost opt]#
[root@localhost opt]# vi /etc/yum.repos.d/CentOS-Base.repo?
[centos]
name=centos
baseurl=file:///opt/centos
gpgcheck=0
enabled=1
[Packages]
name=Packages
baseurl=file:///opt/Packages
gpgcheck=0
enabled=1
使用yum安裝docker容器
[root@localhost opt]# ?yum -y install docker-io
啟動docker容器服務
[root@localhost opt]# systemctl start docker
查看當前docker狀態(tài)
[root@localhost opt]# docker images
REPOSITORY ?????????TAG ????????????????IMAGE ID ???????????CREATED ????????????VIRTUAL SIZE
上傳一個鏡像 postgres.tar
將鏡像上傳docker容器
[root@localhost opt]# docker load -i postgres.tar
再次查看當前docker狀態(tài)
[root@localhost opt]# docker images
REPOSITORY ?????????TAG ????????????????IMAGE ID ???????????CREATED ????????????VIRTUAL SIZE
training/postgres ??latest ?????????????1ca72400eeb5 ???????9 years ago ????????364.6 MB
[root@localhost opt]#
用戶使用--net=none后,可以自行配置網(wǎng)絡(luò),讓容器達到跟平常一樣具有訪問網(wǎng)絡(luò)的權(quán)限。通過這個過程,可以了解Docker配置網(wǎng)絡(luò)的細節(jié)。
1?首先,啟動一個/bin/bash容器,指定--net=none參數(shù)。
|
2?重新打開一個遠程連接在本地主機查找容器的進程id,并為它創(chuàng)建網(wǎng)絡(luò)命名空間。
?[root@localhost ~]# docker ps -a????//用于查找docker容器的name
CONTAINER ID ???????IMAGE ??????????????COMMAND ????????????CREATED ????????????STATUS ?????????????PORTS ??????????????NAMES
7792787a5b3b ???????training/postgres ??"/bin/bash" ????????43 seconds ago ?????Up 42 seconds ??????????????????????????jolly_pike
|
3?檢查橋接網(wǎng)卡的IP和子網(wǎng)掩碼信息。
|
4?創(chuàng)建一對veth pair接口A和B,綁定A到網(wǎng)橋docker0,并啟用它。
|
5?將B放到容器的網(wǎng)絡(luò)命名空間,命名為eth0,啟動它并配置一個可用IP(橋接網(wǎng)段)和默認網(wǎng)關(guān)。
|
以上,就是Docker配置網(wǎng)絡(luò)的具體過程。
查看更改后的容器IP。
|
測試連通性
root@7792787a5b3b:/# ping -c 4 192.168.50.128
PING 192.168.50.128 (192.168.50.128) 56(84) bytes of data.
64 bytes from 192.168.50.128: icmp_seq=1 ttl=64 time=0.315 ms
64 bytes from 192.168.50.128: icmp_seq=2 ttl=64 time=0.081 ms
64 bytes from 192.168.50.128: icmp_seq=3 ttl=64 time=0.080 ms
64 bytes from 192.168.50.128: icmp_seq=4 ttl=64 time=0.164 ms
--- 192.168.50.128 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3001ms
rtt min/avg/max/mdev = 0.080/0.160/0.315/0.095 ms
root@7792787a5b3b:/#
[root@localhost ~]# ping -c 4 172.17.42.99
PING 172.17.42.99 (172.17.42.99) 56(84) bytes of data.
64 bytes from 172.17.42.99: icmp_seq=1 ttl=64 time=0.326 ms
64 bytes from 172.17.42.99: icmp_seq=2 ttl=64 time=0.073 ms
64 bytes from 172.17.42.99: icmp_seq=3 ttl=64 time=0.070 ms
64 bytes from 172.17.42.99: icmp_seq=4 ttl=64 time=0.072 ms
--- 172.17.42.99 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3000ms
rtt min/avg/max/mdev = 0.070/0.135/0.326/0.110 ms
[root@localhost ~]#
【步驟解釋】文章來源:http://www.zghlxwxcb.cn/news/detail-473285.html
- 在編輯器中打開 /etc/sysconfig/network-scripts/ifcfg-ens33 文件,并將 ONBOOT 參數(shù)設(shè)置為 yes。這樣可以在啟動時自動激活網(wǎng)絡(luò)接口。
- 使用 systemctl restart network 命令重啟網(wǎng)絡(luò)服務,以使更改生效。
- 創(chuàng)建一個目錄 /opt/centos,并將光盤設(shè)備 /dev/cdrom 掛載到該目錄下。如果光盤是只讀的,會顯示 "mount: /dev/sr0 is write-protected, mounting read-only"。
- 進入 /opt/ 目錄,使用 ls -l 命令查看是否存在以下文件:centos、Packages.tar.gz、postgres.tar。
- 使用 tar -zxvf Packages.tar.gz 命令解壓 Packages.tar.gz 文件。
- 編輯 /etc/yum.repos.d/CentOS-Base.repo 文件,在文件中添加一個名為 centos 的倉庫,其 baseurl 設(shè)置為 file:///opt/centos,同時將 enabled 參數(shù)設(shè)置為 1。
- 使用 yum -y install docker-io 命令安裝 Docker 容器。
- 使用 systemctl start docker 命令啟動 Docker 服務。
- 使用 docker images 命令查看當前可用的 Docker 鏡像。
- 使用 docker load -i postgres.tar 命令將 postgres.tar 鏡像加載到 Docker 容器中。
- 再次使用 docker images 命令確認鏡像加載成功。
- 使用 docker run -i -t --rm --net=none training/postgres /bin/bash 命令啟動一個名為 training/postgres 的容器,并指定 --net=none 參數(shù)以便自行配置網(wǎng)絡(luò)。
- 在本地主機上打開一個新的終端連接,并使用 docker ps -a 命令查找容器的進程 ID(CONTAINER ID)。
- 使用 docker inspect -f '{{.State.Pid}}' <CONTAINER ID> 命令獲取容器的進程 ID(PID)。
- 創(chuàng)建一個網(wǎng)絡(luò)命名空間,將容器的網(wǎng)絡(luò)命名空間鏈接到本地主機的 /var/run/netns 目錄。
- 使用 ip addr show docker0 命令檢查橋接網(wǎng)卡 docker0 的 IP 地址和子網(wǎng)掩碼信息。
- 創(chuàng)建一對 veth 接口 A 和 B,將接口 A 綁定到橋接網(wǎng)卡 docker0 上,并啟用它。
- 將接口 B 移動到容器的網(wǎng)絡(luò)命名空間中,并將其重命名為 eth0,然后啟用接口 eth0 并為其配置一個可用的 IP 地址(在橋接網(wǎng)段中)和默認網(wǎng)關(guān)。
- 使用 ip add 命令檢查更改后容器中的 IP 地址。
- 進行連通性測試,使用 ping 命令測試容器和本地主機之間的連通性。
通過這個實驗,您可以了解到在Docker中如何配置網(wǎng)絡(luò),并自行配置容器的網(wǎng)絡(luò)連接,使其具有訪問網(wǎng)絡(luò)的權(quán)限。文章來源地址http://www.zghlxwxcb.cn/news/detail-473285.html
到了這里,關(guān)于在Docker中自定義網(wǎng)絡(luò)配置,實現(xiàn)容器與主機的連通性的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!