這篇文章主要介紹“Docker如何實現同Ip網段聯通”,在日常操作中,相信很多人在Docker如何實現同Ip網段聯通問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Docker如何實現同Ip網段聯通”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
10年積累的成都網站制作、成都網站設計、外貿營銷網站建設經驗,可以快速應對客戶對網站的新想法和需求。提供各種問題對應的解決方案。讓選擇我們的客戶得到更好、更有力的網絡服務。我雖然不認識你,你也不認識我。但先做網站設計后付款的網站建設流程,更有長順免費網站建設讓你可以放心的選擇與我們合作。
例如
宿主機a 和宿主機b是網絡聯通關系,在宿主機a上面創(chuàng)建了多個容器組成集群,但是我希望通過宿主機b也可以訪問到宿主機a的容器,當然,你也可能會說,端口映射非常方便,如果我需要的端口比較多,或者著如果我臨時需要增加某些端口,可能設置起來比較麻煩,那么如果我們將宿主機a里面的容器的ip與宿主機的ip在同一個網絡,不就可以直接來進行互聯互通了么。

1、安裝docker(linux服務器)
安裝 docker
yum install docker
2、 使用pipework為docker容器配置獨立ip
安裝pipework這個工具可以使用一條命令就可以實現更改容器的ip,更準確來說為容器ip添加一個新的網卡。
wget https://github.com/jpetazzo/pipework/archive/master.zip unzip master.zip cp pipework-master/pipework /usr/local/bin/ chmod +x /usr/local/bin/pipework
3、編輯ip的配置文件,eh0
編輯默認ip配置文件,eth0或者ens33(不同操作系統(tǒng),名稱不一致,例如我操作的這臺機器的名稱為ifcfg-ens33)
vim /etc/sysconfig/network-scripts/ifcfg-ens33
輸入i進入到編輯模式,將下面的內容復制到文件中
type=ethernet proxy_method=none browser_only=no bootproto=dhcp defroute=yes ipv4_failure_fatal=no ipv6init=yes ipv6_autoconf=yes ipv6_defroute=yes ipv6_failure_fatal=no ipv6_addr_gen_mode=stable-privacy name=ens33 uuid=36b40bc6-6775-4e02-8161-e245d0e3892f device=ens33 #以下為橋接部分設置 onboot=yes bridge=br0 peerDNS=yes peerroutes=yes ipv6_peerdns=yes ipv6_peerroutes=yes
4、創(chuàng)建自定義網橋br0
vim ifcfg-br0
并且將配置內容復制到配置文件中
device=br0 bootproto=static nm_cintroller=no onboot=yes type=bridge ipaddr=192.168.186.128 netmask=255.255.255.0
重啟虛擬機網絡服務
systemctl restart network
5、修改docker配置文件,指定網橋
修改docker的配置文件/etc/sysconfig/
vim /etc/sysconfig/docker
修改內容如下
options='--selinux-enabled --log-driver=journald --signature-verification=false'
修改為:
options='--selinux-enabled -b=br0'
修改完之后:
# /etc/sysconfig/docker
# modify these options if you want to change the way the docker daemon runs
#options='--selinux-enabled --log-driver=journald --signature-verification=false'
options='--selinux-enabled -b=br0'
if [ -z "${docker_cert_path}" ]; then
docker_cert_path=/etc/docker
fi
# do not add registries in this file anymore. use /etc/containers/registries.conf
# instead. for more information reference the registries.conf(5) man page.
# location used for temporary files, such as those created by
# docker load and build operations. default is /var/lib/docker/tmp
# can be overriden by setting the following environment variable.
# docker_tmpdir=/var/tmp
# controls the /etc/cron.daily/docker-logrotate cron job status.
# to disable, uncomment the line below.
# logrotate=false
# docker-latest daemon can be used by starting the docker-latest unitfile.
# to use docker-latest client, uncomment below lines
#dockerbinary=/usr/bin/docker-latest
#dockerdbinary=/usr/bin/dockerd-latest
#docker_containerd_binary=/usr/bin/docker-containerd-latest
#docker_containerd_shim_binary=/usr/bin/docker-containerd-shim-latest
other_args='-b br0'5、重啟docker服務
systemctl restart docker
6、創(chuàng)建docker容器實例
docker run -itd --name test1 --net=none centos /bin/bash
--net=none代表容器的網卡都是為空的,需要通過pipework進行自定義指定
7、指定網卡
pipework br0 test1 192.168.186.111/24@192.168.186.128
8、進入到容器,嘗試ping宿主機和同網段ip是否能夠ping通
# 進入到容器 docker attach test1 # ping 宿主機 ping 192.168.186.22
8.1 修改同網段主機ip
修改主機ip,網段與宿主機a網橋ip段保持一致。設置后,宿主機a,b之間可以互相ping通
# ping 同網段ip ping 192.168.186.33
到此,關于“Docker如何實現同Ip網段聯通”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關知識,請繼續(xù)關注創(chuàng)新互聯網站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
文章題目:Docker如何實現同Ip網段聯通
網頁鏈接:http://chinadenli.net/article10/jpsgdo.html
成都網站建設公司_創(chuàng)新互聯,為您提供自適應網站、網站維護、云服務器、移動網站建設、微信公眾號、網站導航
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯