配置docker靜態IP地址
本節所講內容:
實驗環境: 一個還原到之前安裝了docker的虛擬機器快照:
Docker的4種網路模式
1、Docker有以下4種網路模式:
host模式,使用--net=host指定。
container模式,使用--net=container:NAME_or_ID指定。
none模式,使用--net=none指定。
bridge模式,使用--net=bridge指定,預設設定。
預設選擇bridge的情況下,容器啟動後會通過DHCP獲取一個地址,這可能不是我們想要的,在centos7系統上, docker環境下可以使用pipework指令碼對容器分配固定IP(這個IP可以是和物理機同網段IP)。
注: docker 預設是bridge(--net=bridge)模式,相當於VMware中NAT模式。
docker環境下可以使用pipework指令碼對容器分配固定IP,相當於VMware中橋接模式。
注:Pipework有個缺陷,容器重啟後IP設定會自動消失,需要重新設定。
配置橋接網路:
橋接本地物理網路的目的,是為了區域網內使用者方便訪問docker例項中服務,不要需要各種埠對映即可訪問服務。 但是這樣做,又違背了docker容器的安全隔離的原則,工作中辯證的選擇.
建立橋裝置:
安裝包:
[[email protected] ~]# rpm -ivh /mnt/Packages/bridge-utils-1.5-9.el7.x86_64.rpm
把ens33綁到br0橋裝置上:
[[email protected] ~]# cd /etc/sysconfig/network-scripts/
[[email protected] network-scripts]# cp ifcfg-ens33 /opt/ #備份一下eth0
[[email protected] network-scripts]# vim ifcfg-ens33#編輯配置檔案為以下內容
[[email protected] network-scripts]# vim ifcfg-ens33
TYPE="Ethernet"
BOOTPROTO="none"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
NAME="ens33"
UUID="7a556ff6-f865-4549-b08f-9e526c9bb638"
DEVICE="ens33"
ONBOOT="yes"
IPADDR="192.168.1.63" #刪除這些IP地址相關內容
PREFIX="24"
GATEWAY="192.168.1.1"
DNS1="8.8.8.8"
IPV6_PEERDNS="yes"
IPV6_PEERROUTES="yes"
IPV6_PRIVACY="no"
BRIDGE="br0" #在檔案最後插入這一行
生成橋裝置br0的配置檔案:
[[email protected] network-scripts]# vim ifcfg-br0 #建立ifcfg-br0 檔案,並寫入以下內容
DEVICE="br0"
NM_CONTROLLED="yes"
ONBOOT="yes"
TYPE="Bridge"
BOOTPROTO=none
IPADDR=192.168.1.63
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=114.114.114.114
注:TYPE="Bridge" B要大寫。 不大寫也可以。
[[email protected] network-scripts]# service network restart
Restarting network (via systemctl): [ 確定 ]
測試br0:
[email protected] network-scripts]# ifconfig
[[email protected] network-scripts]# ping g.cn
PING g.cn (203.208.37.20) 56(84) bytes of data.
64 bytes from 203.208.37.20: icmp_seq=1 ttl=57 time=12.3 ms
下載pipework 包
方法1:直接下載pipework zip包
https://github.com/jpetazzo/pipework
把pipework-master.zip上傳到Linux中
擴充套件:
方法2:使用git獲得:
git下載連結:https://github.com/jpetazzo/pipework
下載pipework工具:https://github.com/jpetazzo/pipework.git
[[email protected] ~]# rpm -qf `which git`
git-1.8.3.1-5.el7.x86_64
[[email protected] ~]# cd /opt/
[[email protected] opt]# git clone https://github.com/jpetazzo/pipework.git
咱們使用方法1:
將 pipework-master.zip上傳xuegod63上:
[[email protected] ~]# unzip pipework-master.zip # 不需要編譯,因為pipework 是一個shell指令碼
檢視:
[[email protected] ~]# vim ./pipework-master/pipework
[[email protected] ~]# cp /root/pipework-master/pipework /usr/local/bin/ #方便後期使用pipework命令
到此 pipework已經安裝成功。
[[email protected] ~]# systemctl start docker
把centos-lastest-docker-image.tar 映象上傳Linux上,並匯入docker平臺
[[email protected] ~]# docker load -i centos-lastest-docker-image.tar
使用靜態IP啟動一個docker例項
例:以none模式,使用--net=none 啟動一個容器,並且開啟docker特權模式。
[[email protected] ~]# docker run -itd --net=none --privileged=true centos bash
e4698f625a56661edd2678269215ba42d4fa41c2da881768a741a72b4a3d0c60
擴充套件:
--privileged=true #允許開啟特權功能
privileged [ˈprɪvəlɪdʒd]
在docker 0.6版以後,privileged被引入docker。使用該引數,container內的root擁有真正的root許可權。否則,container內的root只是外部物理機的一個普通使用者許可權。
使用privileged啟動的容器,可以看到很多host上的裝置,並且可以執行mount。甚至允許你在docker容器中啟動docker容器。不啟用privileged,容器中root使用者不能執行mount。
擴充套件: 測試privileged 特權功能 可以:1
1、未設定privileged啟動的容器:
[[email protected] ~]# docker run -it centos:latest bash
[[email protected] /]# ls /dev #可以看到的裝置檔案比較少
console fd full fuse kcore null ptmx pts random shm stderr stdin stdout tty urandom zero
[[email protected] /]# mount -o bind /etc /opt/
mount: permission denied
而在物理機是可以掛載成功的:
[[email protected] ~]# mount -o bind /etc /opt/
[[email protected] /]# exit
2、使用privileged啟動的容器
[[email protected] ~]# docker run -it --privileged centos:latest bash
[[email protected] /]# ls /dev/ #可以看到很多裝置檔案
[[email protected] /]# mount -o bind /etc /opt/ #可以掛載成功
[[email protected] /]# mount /dev/sda1 /opt/ #可以掛載物理機上的sda1分割槽
[[email protected] /]# ls /opt/
[[email protected] /]# init 0 #不行,還是使用exit退出docker
Couldn't find an alternative telinit implementation to spawn.
[[email protected] /]# exit
對開特權模式的docker例項有了解:1 沒有:2
直接吸收:80%的技術! 拿個本紙
擴充套件結束,接著給容器配置地址
[[email protected] ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e4698f625a56 centos "bash" 30 seconds ago Up 27 seconds suspicious_colden
可以看到容器啟動的ID ,比如是e4698f625a56
pipework語法:pipework 網橋名 容器例項ID 分配給容器的IP/掩碼@閘道器
[[email protected] ~]# pipework br0 c88c4c7f01f9 192.168.1.71/[email protected]
測試IP:
[[email protected] ~]# ping 192.168.1.71 #可以看到docker例項的IP已經可以使用
PING 192.168.1.71 (192.168.1.71) 56(84) bytes of data.
64 bytes from 192.168.1.71: icmp_seq=1 ttl=64 time=0.639 ms
[[email protected] ~]# docker inspect 容器例項ID #檢視容器的詳細情況
進入容器,測試網路:
[[email protected] ~]# docker exec -it 87fadc0249a9 /bin/bash #進入容器
[[email protected] /]# cat /etc/resolv.conf
# Generated by NetworkManager
search xuegod63.cn
nameserver 114.114.114.114
[[email protected] /]# yum install -y net-tools #安裝ifconfig命令
[[email protected] /]# ifconfig
eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.71 netmask 255.255.255.0 broadcast 192.168.1.255
[[email protected] /]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth1
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
到此,docker例項配置靜態IP成功。
實戰1: 使用靜態IP啟動的docker例項執行,一個web伺服器
[[email protected] ~]# yum install httpd -y #安裝
[[email protected] ~]# systemctl start httpd #這個方式,無法啟動
[[email protected] ~]# httpd #直接執行 httpd命令
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::a0be:f1ff:feeb:484. Set the 'ServerName' directive globally to suppress this message
[[email protected] ~]# netstat -antup | grep 80 #發現80已經監聽
[[email protected] ~]# cd /var/×××w/html/ #
[[email protected] ~]# echo aaaaa > index.html
檢視結果即可
總結:
1、建立一個br0橋接裝置
2、下載pipework 包並安裝
3、安裝並執行docker
4、匯入centos docker 映象
5、啟動一個docker例項 注意加引數: --net=none --privileged=true
6、使用pipework 給docker例項配置IP