Docker容器(五)——Docker靜態化IP


(1).Docker的四種網絡模式

  Docker有以下四種模式,通過--net=[參數]選項(現在也可以使用--network [參數])指定:

    host模式:使用宿主機的IP地址和端口。使用--net=host指定。

    container模式:和一個指定容器共享IP和端口。使用--net=container:[容器實例名稱或ID]指定。

    none模式:關閉容器的網絡功能。使用--net=none指定。

    bridge模式:為容器分配IP,並將容器連接到docker0虛擬網橋,通過docker0網橋以及Iptables nat表配置,實現與宿主機通信。默認模式,使用--net=bridge指定。

  默認選擇bridge的情況下,容器啟動后會通過DHCP自動獲取一個地址。在CentOS7系統上,docker環境下,如果想要給容器分配一個固定IP(可以和宿主機同網段),可以使用pipework腳本(網絡模式選擇none)。

  注意1:docker默認dridge模式相當於VMware中NAT模式;pipework腳本給容器分配固定IP,相當於VMware中橋接模式。

  注意2:當容器重啟時,pipework設置的IP會自動消失,需要重新設置。

(2).為容器配置靜態IP

 1)配置橋接網絡

[root@youxi1 ~]# yum -y install bridge-utils
[root@youxi1 ~]# cd /etc/sysconfig/network-scripts/
[root@youxi1 network-scripts]# cp ifcfg-ens33{,.bak}
[root@youxi1 network-scripts]# ls ifcfg-ens33*
ifcfg-ens33  ifcfg-ens33.bak
[root@youxi1 network-scripts]# vim ifcfg-ens33
#IPADDR="192.168.5.101"  //注釋掉IP、子網掩碼、網卡、DNS
#PREFIX="24"
#GATEWAY="192.168.5.2"
#DNS1="192.168.5.2"
BRIDGE="br0"  //添加橋接到br0
[root@youxi1 network-scripts]# vim ifcfg-br0  //新建br0配置文件
DEVICE="br0"  //設備名稱
NM_CONTROLLED="yes"
ONBOOT="yes"
TYPE="Bridge"  //注意,B要大寫
BOOTPROTO=none
IPADDR=192.168.5.101  //IP地址、子網掩碼、網關、DNS要和上面注釋掉的一樣
NETMASK=255.255.255.0
GATEWAY=192.168.5.2
DNS1=192.168.5.2
[root@youxi1 network-scripts]# systemctl restart network  //重啟網絡
[root@youxi1 network-scripts]# ip a sh
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master br0 state UP group default qlen 1000
    link/ether 00:0c:29:e6:d6:27 brd ff:ff:ff:ff:ff:ff
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
    link/ether 02:42:8b:ee:a5:51 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever
4: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 00:0c:29:e6:d6:27 brd ff:ff:ff:ff:ff:ff
    inet 192.168.5.101/24 brd 192.168.5.255 scope global noprefixroute br0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fee6:d627/64 scope link 
       valid_lft forever preferred_lft forever

 2)下載pipework

  下載pipework地址:https://github.com/jpetazzo/pipework。可以在Windows上下載好后上傳至CentOS放服務器,也可以使用命令git clone https://github.com/jpetazzo/pipework.git直接在CentOS服務器上下載。

[root@youxi1 network-scripts]# yum -y install unzip
[root@youxi1 network-scripts]# cd ~
[root@youxi1 ~]# unzip pipework-master.zip  //解壓
[root@youxi1 ~]# cp pipework-master/pipework /usr/local/bin/  //為了可以直接調用

 3)為容器配置靜態IP

[root@youxi1 ~]# docker images
REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
centos                         httpd               65f64a254fcc        6 days ago          346MB
centos                         latest              67fa590cfc1c        4 weeks ago         202MB
hub.c.163.com/library/tomcat   latest              72d2be374029        2 years ago         292MB
[root@youxi1 ~]# docker run -itd --network none --privileged centos:latest bash
51c03944e31297272541cc035e235f326a8933d066021e945d8ea4caf8fd1c17
[root@youxi1 ~]# pipework br0 51c03944e312 192.168.5.102/24@192.168.5.2
[root@youxi1 ~]# ping 192.168.5.102
PING 192.168.5.102 (192.168.5.102) 56(84) bytes of data.
64 bytes from 192.168.5.102: icmp_seq=1 ttl=64 time=0.218 ms
64 bytes from 192.168.5.102: icmp_seq=2 ttl=64 time=0.085 ms
^C
--- 192.168.5.102 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.085/0.151/0.218/0.067 ms
[root@youxi1 ~]# systemctl stop firewalld.service  //如果想ping域名以及安裝net-tools,請關閉防火牆。
[root@youxi1 ~]# docker exec -it 51c03944e312 bash
[root@51c03944e312 /]# ping www.baidu.com
PING www.a.shifen.com (180.101.49.11) 56(84) bytes of data.
64 bytes from 180.101.49.11 (180.101.49.11): icmp_seq=1 ttl=128 time=7.92 ms
64 bytes from 180.101.49.11 (180.101.49.11): icmp_seq=2 ttl=128 time=7.99 ms
^C
--- www.a.shifen.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 7.922/7.960/7.999/0.097 ms
[root@51c03944e312 /]# yum -y install net-tools
[root@51c03944e312 /]# ifconfig 
eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.5.102  netmask 255.255.255.0  broadcast 192.168.5.255
        ether b2:68:6f:3f:2d:dd  txqueuelen 1000  (Ethernet)
        RX packets 1532  bytes 8274432 (7.8 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1378  bytes 78622 (76.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 4  bytes 336 (336.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4  bytes 336 (336.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

  注意:由於我不知道設置靜態IP后需要添加什么端口,所以我將防火牆關閉了。

4)擴展

  --privileged選項表示開啟特權模式。在沒有該選項的容器實例中,雖然顯示的是root用戶,其實只是宿主機(物理機)的一個普通用戶,包含設備文件和部分命令在內都無法使用。如果使用了該選項就可以調用這部分內容,例如mount命令。

  測試如下:

[root@youxi1 ~]# docker run -it centos:latest bash
[root@d1d63355d418 /]# ls /dev/
console  fd    mqueue  ptmx  random  stderr  stdout  urandom
core     full  null    pts   shm     stdin   tty     zero
[root@05b2b38259cf /]# exit
exit
[root@youxi1 ~]# docker run -it --privileged centos:latest bash
[root@08065470bd72 /]# ls /dev/  //可以看到差距相當大
agpgart          mem                 snd     tty27  tty50    usbmon1
autofs           midi                sr0     tty28  tty51    usbmon2
bsg              mqueue              stderr  tty29  tty52    vcs
btrfs-control    net                 stdin   tty3   tty53    vcs1
bus              network_latency     stdout  tty30  tty54    vcs2
console          network_throughput  tty     tty31  tty55    vcs3
core             null                tty0    tty32  tty56    vcs4
cpu              nvram               tty1    tty33  tty57    vcs5
cpu_dma_latency  oldmem              tty10   tty34  tty58    vcs6
crash            port                tty11   tty35  tty59    vcsa
dm-0             ppp                 tty12   tty36  tty6     vcsa1
dm-1             ptmx                tty13   tty37  tty60    vcsa2
dmmidi           pts                 tty14   tty38  tty61    vcsa3
dri              random              tty15   tty39  tty62    vcsa4
fb0              raw                 tty16   tty4   tty63    vcsa5
fd               rtc0                tty17   tty40  tty7     vcsa6
full             sda                 tty18   tty41  tty8     vfio
fuse             sda1                tty19   tty42  tty9     vga_arbiter
hidraw0          sda2                tty2    tty43  ttyS0    vhci
hpet             sdb                 tty20   tty44  ttyS1    vhost-net
hwrng            sdb1                tty21   tty45  ttyS2    vmci
input            sg0                 tty22   tty46  ttyS3    vsock
kmsg             sg1                 tty23   tty47  uhid     zero
loop-control     sg2                 tty24   tty48  uinput
mapper           shm                 tty25   tty49  urandom
mcelog           snapshot            tty26   tty5   usbmon0

  

參考:https://www.cnblogs.com/zuxing/articles/8780661.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM