去评论
海欣资源

Docker网络配置教程

perll
2022/05/29 10:13:48
閰嶇疆Docker缃戠粶鍓嶅缓璁竻绌烘墍鏈夌幆澧
docker rmi -f $(docker images -aq )
    姣忓惎鍔ㄤ竴涓猟ocker瀹瑰櫒,docker灏变細缁檇ocker瀹瑰櫒鍒嗛厤涓涓猧p銆
    鍙瀹夎浜哾ocker,灏变細鏈変竴涓綉鍗ocker0 ( 妗ユ帴妯″紡,浣跨敤鐨勬槸evth-pair鎶鏈 )
1锛屼粈涔堟槸Evth-pair
Evth-pair灏辨槸涓瀵圭殑铏氭嫙璁惧鎺ュ彛,浠栦滑閮芥槸鎴愬鍑虹幇鐨,涓娈佃繛鐫鍗忚,涓娈靛郊姝ょ浉杩.姝e洜涓鸿繖涓壒鎬,evth-pair 鍏呭綋涓涓ˉ姊,杩炴帴鍚勭铏氭嫙缃戠粶璁惧銆
OpenStac锛孌ocker瀹瑰櫒涔嬮棿鐨勮繛鎺, OVS鐨勮繛鎺,閮芥槸浣跨敤 evth-pair 鎶鏈.

2锔忥紝瀹瑰櫒涓庡鍣ㄧ殑杩為
        linux涓巇ocker瀹瑰櫒涔嬮棿閫氳繃docker0鏄彲浠ing閫氱殑
        docker瀹瑰櫒涓庡鍣ㄤ箣闂存槸鍙互鐩镐簰ping閫氱殑


鍒嗘瀽锛歵omcat01鍜宼omcat02鍏敤涓涓矾鐢卞櫒锛屾墍鏈夌殑瀹瑰櫒鍦ㄤ笉鎸囧畾缃戠粶鐨勬儏鍐典笅锛岄兘鐢╠ocker0锛宒ockers榛樿缁欐瘡涓鍣ㄥ垎閰嶄竴涓彲鐢ㄧ殑IP鍦板潃銆

缁撹
        Docker浣跨敤鐨勬槸Linux鐨勬ˉ鎺ワ紝瀹夸富鏈烘槸涓涓狣ocker瀹瑰櫒鐨勭綉妗ocker0
        Docker涓墍鏈夌殑缃戠粶鎺ュ彛閮芥槸铏氭嫙锛岃櫄鎷熺綉鍗¤浆鍙戞晥鐜囬珮
瀹瑰櫒鍒犻櫎锛屽搴旂殑Evth-pair灏变細娑堝け
docker exec -it tomect01 ip addr



鍒犻櫎tomec01

鏌ョ湅Evth-pair娑堝け


瀹瑰櫒鐨勬帴鍙d笌杩炴帴鍒癇ridge涓婄殑veth鎺ュ彛锛屽舰鎴恦eth-pair锛岄粯璁ょ殑Bridge缃戠粶锛岀綉鍏抽粯璁ゆ槸docker0銆
榛樿鐨凚ridge缃戠粶锛宒ocker0鐨勫湴鍧鏄172.17.0.1/24锛岃岀涓涓帴鍏ョ殑璁惧锛屽垯IP鍦板潃涓172.17.0.2/24鈥︿互姝ゅ唴鎺ㄣ
5锔忥紝鏌ョ湅Bridge缃戠粶鐨勭浉鍏冲懡浠
1.ifconfig鍛戒护锛
瀹夸富鏈轰笂锛
docker0鎺ュ彛淇℃伅锛氬彲浠ョ悊瑙d负Bridge鐨凷VI鍙c


ens鍜宭o鎺ュ彛锛氬彲浠ョ悊瑙d负瀹夸富鏈虹殑鐗╃悊鎺ュ彛銆

veth-pair鎺ュ彛锛欱ridge涓婅繛鎺ュ鍣ㄧ殑鎺ュ彛銆

瀹瑰櫒涓細
閫氳繃docker exec -it xxx锛堝鍣ㄥ悕绉帮級ifconfig杩涜鏌ョ湅锛
鍙互鐪嬪埌瀹瑰櫒鐨勨欑墿鐞嗘帴鍙b欏拰鍥炶繕鍙d俊鎭
2.鏌ョ湅Bridge涓婃帴鍙d俊鎭細
CentOS8涓轰緥锛屽懡浠や负bridge link锛岀粨鏋滀负濡備笅锛


鍙互鐪嬪埌锛2涓獀eth鎺ュ彛鍜宒ocker0杩炴帴鍦ㄨ繖涓狟ridge涓娿

3.鏌ョ湅瀹瑰櫒涓嶣ridge涓婄殑veth鎺ュ彛鐨勫搴斿叧绯伙細
鏂规硶1锛氫娇鐢ㄨ剼鏈紝鏌ョ湅瀹瑰櫒ID鍜寁eth鐨勫搴斿叧绯汇傚叿浣撴柟娉曡鍙傝冿細https://blog.csdn.net/tushanpeipei/article/details/116077454锛屾墽琛岀粨鏋滃涓嬶細


鏂规浜岋細閫氳繃绱㈠紩鏉ュ姣旓細
4.docker inspect network鍛戒护锛
鍛戒护濡備笅锛


鍏朵腑锛屼娇鐢╦q鏌ョ湅IPAM.Config鐨勪俊鎭拰姝ょ綉缁滅殑IP銆佺綉鍏充俊鎭細

Containers鐨勪俊鎭负姝ょ綉缁滆繛鎺ョ殑瀹瑰櫒淇℃伅锛

涓夈佷娇鐢ㄤ富鏈哄悕杩涜閫氫俊
1.闂锛
杩炴帴鍒伴粯璁ょ殑Bridge鐨勫鍣紝浜掔浉鍙互浣跨敤IP鍦板潃鏉ラ氫俊锛屼絾鏄棤娉曚娇鐢ㄤ富鏈哄悕鏉ラ氫俊銆
2.瑙e喅鏂规锛
浣跨敤鈥搇ink鏉ラ氫俊銆


3.娉ㄦ剰鐐癸細
鈥搇ink鐨勫師鐞嗗叾瀹炲氨鏄湪鏈湴娣诲姞浜嗗鏂逛富鏈哄悕绉板拰IP鍦板潃鐨勬槧灏勶紝浣嗘槸瀵圭骞舵病鏈夛紝鎵浠ュ绔緷鏃ф棤娉曢氳繃鏈鐨勪富鏈哄悕涓庢湰绔繘琛岄氫俊銆傚鍣ㄥ唴鏄犲皠淇℃伅濡備笅锛

6锛岃嚜瀹氫箟bridge缃戠粶
涓銆 鑷畾涔変笌榛樿鐨刡ridge鐨勫叧绯诲浘绀猴細


浜屻佸懡浠わ細
1.鍒涘缓bridge缃戠粶锛
docker network create -d bridge --subnet x.x.x.x/x(瀛愮綉鍦板潃) --gateway x.x.x.x bridge-net1锛屽垱寤轰竴涓悕涓篵ridge-net1鐨勭綉缁溿


2.灏嗗鍣ㄨ繛鎺ュ埌鑷畾涔塨ridge缃戠粶锛
瀹瑰櫒RUN鏃惰繛鎺ワ細
docker run -it --name docker-net1 --network bridge-net1(缃戠粶鍚嶇О) prin/centos-vim-ifconfig /bin/bash
docker run -it --name docker-net2 --network bridge-net1(缃戠粶鍚嶇О) prin/centos-vim-ifconfig /bin/bash

瀹瑰櫒宸茬粡RUN鍚庤繛鎺ワ細
docker network connect bridge-net1(缃戠粶鍚嶇О)  docker-net1(瀹瑰櫒鍚嶇О)

3.鏌ョ湅鑷畾涔夌綉缁滅殑鐩稿叧淇℃伅锛
1.鍙互浣跨敤鍛戒护docker inspect bridge-net1鏌ョ湅璇︾粏淇℃伅锛


2.鍦ㄥ涓绘満涓婁娇鐢╥fconfig锛屽彲浠ョ湅鍒颁骇鐢熶簡濡備笅鐨勬帴鍙o細
鍏朵腑br-xxxxx鐩稿綋浜庨粯璁ridge鐨刣ocker0鎺ュ彛銆倂eth鎺ュ彛鍒欐槸bridge-net1涓婅繛鎺ュ埌瀹瑰櫒鐨勮櫄鎷熸帴鍙c



涓夈佺壒鐐癸細
1.鍦ㄨ嚜瀹氫箟bridge涓婅繛鎺ョ殑瀹瑰櫒锛岄粯璁ゅ氨鍙互鐢ㄥ鏂瑰悕绉拌繘琛岄氫俊锛

2.涓嶅悓bridge涔嬮棿鑳藉浜掔浉閫氫俊鐨勫師鍥犳槸浣跨敤浜咲ocker Engine涓婄殑DNS Resolver锛岀洿鎺ュIP鍦板潃杩涜瑙f瀽锛

3.涓嶅悓bridge涓婄殑瀹瑰櫒澶╃敓闅旂锛

4.鍚屼竴涓鍣ㄥ彲浠ユ帴鍏ュ埌澶氫釜bridge涓婏紝渚嬪缁忓吀涓変欢濂楋細Nginx-django-psql

7锔忥紝Host缃戠粶
涓銆佹杩帮細
Host缃戠粶鏄粯璁ゅ瓨鍦ㄧ殑缃戠粶锛屽熺敤浜嗗涓绘満鐨勭綉缁溿

浜屻佸懡浠わ細
灏嗗鍣ㄨ繛鎺ュ埌host缃戠粶锛
docker run -it --name centos_host --network=host prin/centos-vim-ifconfig /bin/bash
鏌ョ湅鎺ュ彛淇℃伅锛
ifconfig锛氬彲浠ョ湅鍒板拰瀹夸富鏈虹殑涓鏍枫


涓夈佹敞鎰忥細
濡傛灉鍦╤ost鐨勫鍣ㄥ拰瀹夸富涓婂悓鏃跺紑鏀句竴涓鍙o紝浼氭姤閿欍
None缃戠粶锛

涓銆佹杩帮細
none缃戠粶鏄粯璁ゅ瓨鍦ㄧ殑缃戠粶锛岄粯璁ゅ彧鏈変竴涓洖鐜帴鍙o紝鏃犳硶涓庡鐣岄氫俊銆

浜屻佸懡浠わ細
灏嗗鍣ㄨ缃负none缃戠粶锛
docker run -it --name centos_none --network=none prin/centos-vim-ifconfig /bin/bash
鏌ョ湅瀹瑰櫒鎺ュ彛淇℃伅锛
ifconfig锛氬彲浠ョ湅鍒板彧鏈塴o鎺ュ彛銆


8锔忥紝Vxlan缃戠粶

涓銆佺洰鐨勶細
杩炴帴涓嶅悓瀹夸富鏈轰笂鐨勫鍣紝鍒涘缓闆嗙兢銆

浜屻佸師鐞嗭細
鍥剧ず锛


鍦ㄤ笉鍚屽涓绘満鐨勫鍣ㄤ笂鍒涘缓eth1鎺ュ彛锛屽湪鍚屼竴缃戞锛屽缓绔媜verlay鐨勯毀閬撱傚悓鏃秂th0浣滀负underlay鐨勬帴鍙o紝淇濊瘉搴曞眰鑳藉閫氫俊銆
褰揌ost1鍜孒ost2涓婂鍣ㄩ氫俊鏃讹紝棣栧厛杩涜overlay鐨勫皝瑁咃紝鐒跺悗鍐嶈繘琛寀nderlay鐨勫皝瑁咃紝鐒跺悗灏嗘暟鎹彂閫佸埌瀵圭锛屽绔啀杩涜瑙e皝瑁呫

涓夈佺函鎵嬪伐鏂瑰紡锛
娉ㄦ剰锛氭鏂瑰紡涓嶆帹鑽愶紝浜嗚В鍗冲彲锛涘悗缁瑪璁颁腑璁板綍鑷姩鍖栫殑鏂瑰紡銆
姝ラ锛
1.鍑嗗涓や釜铏氭嫙鏈篋ocker_docker1(10.1.1.1)锛孌ocker_docker2(10.1.1.2)锛屽苟淇敼涓绘満鍚嶇О锛


2.鍒嗗埆鍦ㄤ袱鍙拌澶囦笂璁剧疆鍙橀噺锛


3.鍦ㄤ袱鍙拌澶囦笂涓嬭浇骞惰В鍘嬪垎甯冨紡瀛樺偍鏂囦欢銆傝祫婧愬彲浠ュ湪缃戜笂鎼滅储etcd-v(鐗堟湰鍙) -linux-amd64杩涜涓嬭浇锛

4.浣跨敤鑴氭湰鍦ㄤ袱鍙拌澶囦笂婵娲绘湇鍔★細
Docker_docker1锛

Docker_docker2:鍚岀悊
5.鍦ㄤ袱鍙拌澶囦笂鍒嗗竷鏌ョ湅闆嗙兢鐘跺喌锛


6.鍋滄docker鏈嶅姟锛屽苟浣跨敤etcd闆嗙兢閲嶆柊鍚姩docker锛

7.鍦ㄤ竴鍙拌澶囦笂鍒涘缓Overlay缃戠粶锛岄粯璁や細鍚屾鍒伴泦缇や腑锛

8.鍒嗗竷鍦ㄥ涓绘満涓婂垱寤哄鍣紝杩炴帴鍒皁verlay缃戠粶涓細

9.娴嬭瘯缃戠粶閫氫俊鐘跺喌(鍦ㄥ鍣ㄩ噷闈簰鐩竝ing瀵圭鍚嶇О)鍜屾煡鐪嬬綉缁滀俊鎭(dockek network ls/docker network inspect qyt_overlay )銆
9锔忥紝鐗╃悊VLAN缃戠粶锛圡AC VLAN锛

涓銆佹蹇碉細
鍦ㄥ涓绘満涓婁娇鐢ㄥ瓙鎺ュ彛鐨勬柟寮忥紝灏嗗鍣ㄦ帴鍏ュ埌vlan涓紝浠ヤ繚璇侀氫俊锛屽叾搴旂敤鍦烘櫙灏戙

浜屻佸浘绀猴細
鍚屼竴涓涓绘満涓細绫讳技浜庡崟鑷傝矾鐢便

涓嶅悓涓绘満涓殑瀹瑰櫒閫氫俊锛氫袱涓墿鐞嗕富鏈鸿繛鎺ュ埌鍚屼竴涓猇LAN锛孖P鍦ㄥ悓涓缃戞杩涜閫氫俊銆