Docker 網絡管理

網絡模式html

容器網絡訪問原理node

橋接宿主機網絡和配置固定IP地址mysql

Docker 支持五種網絡模式:git

一、網絡模式: --net github

1.1 bridge web

默認網絡,Docker啓動後默認建立一個docker0網橋,默認建立的容器也是添加到這個網橋中sql

1.2 hostdocker

容器不會得到一個獨立的network namespace,而是和宿主機共用一個,使用方法 --net  host,這個通常用的比較小,這種狀況下,好比說咱們容器啓用了80的端口,其實也就是佔用了宿主機的80端口ubuntu

1.3 nonebash

獲取獨立的network namespace,但不爲容器進行任何網絡配置,進去沒有eth0網絡,可是有獨立的網絡空間

1.4 container

和指定的容器使用同一個network namespace,網卡配置也是相同的,(多個容器能夠指定共用一個網絡)

1.5 自定義

自定義網橋,默認和bridge網絡同樣

bridge:

[root@node02 ~]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
c8f4ff0091d8        bridge              bridge              local
88e3f6329cf3        host                host                local
173e62eaa733        lnmp                bridge              local
b6d9db4c6d31        none                null                local
[root@node02 ~]# docker inspect lnmp
[
    {
        "Name": "lnmp",
        "Id": "173e62eaa733c8783e1d6ee0cb4e65afab7b4469d0eb36a034ccd43e84650da7",
        "Created": "2018-09-10T18:12:55.98358342+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.17.0.0/16",
                    "Gateway": "172.17.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "306965c1bd589f8f6fe754280b5daeb724153fac8137837c9328acdfacad7cda": {
                "Name": "lnmp_web",
                "EndpointID": "bdddc0a754cb30c1c3d7fa512dfb3f620daee665c8ce19fd2a9ab433443aaccb",
                "MacAddress": "02:42:ac:11:00:03",
                "IPv4Address": "172.17.0.3/16",
                "IPv6Address": ""
            },
            "91bba818c0e5d246794d66898e33e5947d6995302f428128dd83e97989781635": {
                "Name": "lnmp_mysql",
                "EndpointID": "db553cf6aa4e294731da08cd7a9754a3183b62faa565d4e72b19c4412d4e98dd",
                "MacAddress": "02:42:ac:11:00:02",
                "IPv4Address": "172.17.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]
[root@node02 ~]# ifconfig 
br-173e62eaa733: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        inet6 fe80::42:9bff:fe5d:dd78  prefixlen 64  scopeid 0x20<link>
        ether 02:42:9b:5d:dd:78  txqueuelen 0  (Ethernet)
        RX packets 3406918  bytes 299604640 (285.7 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3406918  bytes 299604640 (285.7 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.30.39.1  netmask 255.255.255.0  broadcast 172.30.39.255
        inet6 fe80::42:50ff:fe75:a932  prefixlen 64  scopeid 0x20<link>
        ether 02:42:50:75:a9:32  txqueuelen 0  (Ethernet)
        RX packets 52  bytes 5776 (5.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 69  bytes 6044 (5.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0


默認狀況下docker的網絡方式是bridge,在宿主機會建立一個網橋docker0

 

Docker 容器的網絡的訪問原理:

 

 veth 是一個虛擬的網絡設備和Docker的eth一對一對應

 這裏咱們裝一下查看網橋的工具

yum install -y bridge-utils 

bridge show

 而後docker0 經過轉發和宿主機的eth0進行轉發和外部網絡通訊

 

容器訪問外部
# iptables -t nat -nL
Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
MASQUERADE all -- 172.17.0.0/16 0.0.0.0/0
外部訪問容器
# iptables -t nat -nL
Chain DOCKER (2 references)
target prot opt source destination
DNAT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:88 to:172.18.0.2:80

 

橋接宿主機網絡與配置固定IP地址
臨時生效:
# 網橋名稱
br_name=br0
# 添加網橋
brctl addbr $br_name
# 給網橋設置IP
ip addr add 192.168.0.211(宿主機IP)/24 dev $br_name
# 刪除已存在的eth0網卡配置
ip addr del 192.168.0.211/24 dev eth0
# 激活網橋
ip link set $br_name up
# 添加eth0到網橋
brctl addif $br_name eth0
# 添加路由
ip route add default via 192.168.0.1 dev br0

執行上面的腳本,br0就具備物理網卡了 還須要在Docker啓動時橋接這個網橋: # vi /usr/lib/systemd/system/docker.service ExecStart=/usr/bin/dockerd -b=br0 # systemctl restart docker 永久生效: # vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 TYPE=Ethernet ONBOOT=yes BRIDGE=br0 # vi /etc/sysconfig/network-scripts/ifcfg-br0 DEVICE=br0 TYPE=Bridge ONBOOT=yes BOOTPROTO=static IPADDR=192.168.0.211 NETMASK=255.255.255.0 GATEWAY=192.168.0.1 DNS1=114.114.114.114

  

pipework工具配置容器固定IP
git clone https://github.com/jpetazzo/pipework.git
cp pipework/pipework /usr/local/bin/
docker run -itd --net=none --name test01 ubuntu
pipework br0 test01 192.168.0.123/24@192.168.0.1

 這個配置過網絡以後,容器獲得的IP和宿主機是在一個網段的,這個時候能夠上外網了。可是這個時候咱們要是重啓這個容器的話docker restart test01,這個容器的IP就沒有了

 

咱們看一下這個腳本

C_ID=$(docker run -itd --net=none ubuntu)
C_PID=$(docker inspect -f '{{.State.Pid}}' $C_ID)
# 建立network namespace目錄並將容器的network namespace軟鏈接到此目錄,以便ip netns命令讀取
mkdir -p /var/run/netns
ln -s /proc/$C_PID/ns/net /var/run/netns/$C_PID
# 添加虛擬網卡veth+容器PID,類型是veth pair,名稱是vp+容器PID
ip link add veth$C_PID type veth peer name vp$C_PID
# 添加虛擬網卡到br0網橋
brctl addif br0 veth$C_PID
# 激活虛擬網卡
ip link set veth$C_PID up
# 設置容器網絡信息
IP='192.168.0.123/24'
GW='192.168.0.1'
# 給進程配置一個network namespace
ip link set vp$C_PID netns $C_PID
# 在容器進程裏面設置網卡信息
ip netns exec $C_PID ip link set dev vp$C_PID name eth0
ip netns exec $C_PID ip link set eth0 up
ip netns exec $C_PID ip addr add $IP dev eth0
ip netns exec $C_PID ip route add default via 192.168.1.1

  

 博客總結來源於:http://edu.51cto.com/course/10659.html

相關文章
相關標籤/搜索