docker容器网络配置
文章目录
1:Linux内核实现名称空间的创建
ip netns命令
可以借助ip netns命令来完成对 Network Namespace 的各种操作。ip netns命令来自于iproute安装包,一般系统会默认安装,如果没有的话,请自行安装。
注意:ip netns命令修改网络配置时需要 sudo 权限。
可以通过ip netns命令完成对Network Namespace 的相关操作,可以通过ip netns help查看命令帮助信息:
[root@localhost ~]# ip netns help
Usage: ip netns list
ip netns add NAME
ip netns attach NAME PID
ip netns set NAME NETNSID
ip [-all] netns delete [NAME]
ip netns identify [PID]
ip netns pids NAME
ip [-all] netns exec [NAME] cmd ...
ip netns monitor
ip netns list-id [target-nsid POSITIVE-INT] [nsid POSITIVE-INT]
NETNSID := auto | POSITIVE-INT
默认情况下,Linux系统中是没有任何 Network Namespace的,所以ip netns list命令不会返回任何信息。
创建Network Namespace
[root@localhost ~]# ip netns list
[root@localhost ~]# ip netns add dd1
[root@localhost ~]# ip netns list
dd1
新创建的 Network Namespace 会出现在/var/run/netns/目录下。如果相同名字的 namespace 已经存在,命令会报Cannot create namespace file “/var/run/netns/ns0”: File exists的错误。
[root@localhost ~]# ls /var/run/netns/
dd1
[root@localhost ~]# ip netns add dd1
Cannot create namespace file "/var/run/netns/dd1": File exists
对于每个 Network Namespace 来说,它会有自己独立的网卡、路由表、ARP 表、iptables 等和网络相关的资源。
操作Network Namespace
查看新创建 Network Namespace 的网卡信息
[root@localhost ~]# ip netns exec dd1 ip addr
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
可以看到,新创建的Network Namespace中会默认创建一个lo回环网卡,此时网卡处于关闭状态。此时,尝试去 ping 该lo回环网卡,会提示Network is unreachable
[root@localhost ~]# ip netns exec dd1 ping 127.0.0.1
connect: 网络不可达
启用lo回环网卡
[root@localhost ~]# ip netns exec dd1 ip link set lo up
[root@localhost ~]# ip netns exec dd1 ping 127.0.0.1
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.024 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.026 ms
转移设备
我们可以在不同的 Network Namespace 之间转移设备(如veth)。由于一个设备只能属于一个 Network Namespace ,所以转移后在这个 Network Namespace 内就看不到这个设备了。
其中,veth设备属于可转移设备,而很多其它设备(如lo、vxlan、ppp、bridge等)是不可以转移的。
创建veth pair
[root@localhost ~]# ip link add type veth
[root@localhost ~]# ip a
9: veth0@veth1: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN group default qlen 1000
link/ether 46:15:0f:bf:30:77 brd ff:ff:ff:ff:ff:ff
10: veth1@veth0: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN group default qlen 1000
link/ether 7e:ba:f7:db:89:fe brd ff:ff:ff:ff:ff:ff
创建两个命名空间将veth转移进去实现命名空间之间的通信
创建
[root@localhost ~]# ip netns list
[root@localhost ~]# ip netns add dd1
[root@localhost ~]# ip netns add dd2
转移
[root@localhost ~]# ip link set veth0 netns dd1
[root@localhost ~]# ip link set veth1 netns dd2
启用并给予IP地址
[root@localhost ~]# ip netns exec dd1 ip link set veth0 up
[root@localhost ~]# ip netns exec dd2 ip link set veth1 up
[root@localhost ~]# ip netns exec dd1 ip addr add 88.55.44.1/24 dev veth0
[root@localhost ~]# ip netns exec dd2 ip addr add 88.55.44.3/24 dev veth1
测试访问
[root@localhost ~]# ip netns exec dd1 ping 88.55.44.3
PING 88.55.44.3 (88.55.44.3) 56(84) bytes of data.
64 bytes from 88.55.44.3: icmp_seq=1 ttl=64 time=0.039 ms
64 bytes from 88.55.44.3: icmp_seq=2 ttl=64 time=0.066 ms
[root@localhost ~]# ip netns exec dd2 ping 88.55.44.1
PING 88.55.44.1 (88.55.44.1) 56(84) bytes of data.
64 bytes from 88.55.44.1: icmp_seq=1 ttl=64 time=0.027 ms
veth设备重命名
[root@localhost ~]# ip netns exec dd1 ip a
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
9: veth0@if10: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 46:15:0f:bf:30:77 brd ff:ff:ff:ff:ff:ff link-netns dd2
inet 88.55.44.1/24 scope global veth0
valid_lft forever preferred_lft forever
inet6 fe80::4415:fff:febf:3077/64 scope link
valid_lft forever preferred_lft forever
[root@localhost ~]# ip netns exec dd1 ip link set veth0 down
[root@localhost ~]# ip netns exec dd1 ip link set veth0 name ens33
[root@localhost ~]# ip netns exec dd1 ip a
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
9: ens33@if10: <BROADCAST,MULTICAST> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
link/ether 46:15:0f:bf:30:77 brd ff:ff:ff:ff:ff:ff link-netns dd2
inet 88.55.44.1/24 scope global ens33
valid_lft forever preferred_lft forever
2:docker四种网络配置
2.1 bridge
[root@localhost ~]# docker run -it --name dddd --rm --network bridge centos:d1 /bin/bash
[root@25bff5cc3649 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet)
RX packets 73 bytes 335915 (328.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 54 bytes 3170 (3.0 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 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@localhost ~]# docker run -it --rm --name gggg --rm centos:d1 /bin/bash
[root@c939a5de60c1 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet)
RX packets 51 bytes 334704 (326.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 34 bytes 2102 (2.0 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 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
可以看见一模一样,这是因为bridge是docker创建容器默认指定的网络模式
2.2 none模式配置
[root@localhost ~]# docker run -it --rm --name ghhh --network none centos:d1 /bin/bash
[root@f91710c248f9 /]# ip a
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
2.3 container模式配置
启动第一个容器
[root@localhost ~]# docker run -it --rm --name vcvc centos:d1 /bin/bash
[root@7c653bb17720 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet)
RX packets 52 bytes 334762 (326.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 34 bytes 2090 (2.0 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 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
创建第二个容器设置网络配置为container模式并指向第一台容器
[root@localhost ~]# docker run -it --name bb --rm --network container:vcvc centos:d1 /bin/bash
[root@7c653bb17720 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet)
RX packets 118 bytes 668935 (653.2 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 98 bytes 5812 (5.6 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 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
可以看见ip是共享的
在第一个容器里面创建一个文件,然后到第二个容器里面相同的目录查看一下
[root@7c653bb17720 /]# touch 123
[root@7c653bb17720 /]# ls
123 bin dev etc home lib lib64 lost+found media mnt mysql.sh opt proc root run sbin srv sys tmp usr var
[root@7c653bb17720 /]# ls
bin dev etc home lib lib64 lost+found media mnt mysql.sh opt proc root run sbin srv sys tmp usr var
因为文件系统是处于隔离状态,仅仅是共享了网络而已。
2.4 host模式配置
启动容器时直接指明模式为host
root@localhost ~]# docker run -it --rm --name jjj --network host centos:d1 /bin/bash
[root@localhost /]# ifconfig
docker0: 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:abff:feaf:9010 prefixlen 64 scopeid 0x20<link>
ether 02:42:ab:af:90:10 txqueuelen 0 (Ethernet)
RX packets 6878 bytes 297100 (290.1 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 7904 bytes 43227426 (41.2 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.226.139 netmask 255.255.255.0 broadcast 192.168.226.255
inet6 fe80::20c:29ff:fe5a:7a3f prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:5a:7a:3f txqueuelen 1000 (Ethernet)
RX packets 238844 bytes 201877580 (192.5 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 119185 bytes 107032425 (102.0 MiB)
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
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 124 bytes 19760 (19.2 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 124 bytes 19760 (19.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
veth171f451: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet6 fe80::7429:10ff:febe:ad02 prefixlen 64 scopeid 0x20<link>
ether 76:29:10:be:ad:02 txqueuelen 0 (Ethernet)
RX packets 989 bytes 58962 (57.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1172 bytes 5788459 (5.5 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:0e:9d:ef txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
此时如果我们在这个容器中启动一个http站点,我们就可以直接用宿主机的IP直接在浏览器中访问这个容器中的站点了。
3容器常用操作
查看容器的主机名
[root@localhost ~]# docker run -it --name jjjjj centos:d1 /bin/bash
[root@09cf08e63d34 /]# hostname
09cf08e63d34
在容器启动时注入主机名
[root@localhost ~]# docker run -it --rm --hostname xuanning --name hhh centos:d1 /bin/bash
[root@xuanning /]#
[root@xuanning /]# cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2 xuanning
[root@xuanning /]# cat /etc/resolv.conf
# Generated by NetworkManager
search localdomain
nameserver 192.168.226.2
手动指定容器要使用的DNS
[root@localhost ~]# docker run -it name hhh --hostname xuanning --dns 114.114.114.114 --rm centos:d1 /bin/bash
[root@xuanning /]# cat /etc/resolv.conf
search localdomain
nameserver 114.114.114.114
[root@xuanning /]# nslookup -type=a www.baidu.com
Server: 114.114.114.114
Address: 114.114.114.114#53
Non-authoritative answer:
www.baidu.com canonical name = www.a.shifen.com.
Name: www.a.shifen.com
Address: 182.61.200.7
Name: www.a.shifen.com
Address: 182.61.200.6
手动往/etc/hosts文件中注入主机名到IP地址的映射
[root@localhost ~]# docker run -it --name tttt --hostname xuanning --add-host www.xuanning.com:114.114.114.114 --rm centos:d1 /bin/bash
[root@xuanning /]# cat /etc/host
host.conf hostname hosts
[root@xuanning /]# cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
114.114.114.114 www.xuanning.com
172.17.0.2 xuanning
4:开放容器端口
执行docker run的时候有个-p选项,可以将容器中的应用端口映射到宿主机中,从而实现让外部主机可以通过访问宿主机的某端口来访问容器内应用的目的。
-p选项能够使用多次,其所能够暴露的端口必须是容器确实在监听的端口。
-p选项的使用格式:
-p
将指定的容器端口映射至主机所有地址的一个动态端口
-p :
将容器端口映射至指定的主机端口
-p ::
将指定的容器端口映射至主机指定的动态端口
-p ::
将指定的容器端口映射至主机指定的端口
动态端口指的是随机端口,具体的映射结果可使用docker port命令查看。
[root@localhost ~]# docker run --name ggg --rm -p 80 httpd
以上命令执行后会一直占用着前端,我们新开一个终端连接来看一下容器的80端口被映射到了宿主机的什么端口上
[root@localhost ~]# docker port ggg
80/tcp -> 0.0.0.0:49153
80/tcp -> :::49153
由此可见,容器的80端口被暴露到了宿主机的49153端口上,此时我们在宿主机上访问一下这个端口看是否能访问到容器内的站点
[root@localhost ~]# curl 192.168.226.139:49153
<html><body><h1>It works!</h1></body></html>
iptables防火墙规则将随容器的创建自动生成,随容器的删除自动删除规则。
将容器端口映射到指定IP的随机端口
[root@localhost ~]# docker run --name ttt --rm -p 192.168.226.139:888:80 httpd
在另一个终端上查看端口映射情况
[root@localhost ~]# curl 192.168.226.139:888
<html><body><h1>It works!</h1></body></html>
将容器端口映射到宿主机的指定端口
[root@localhost ~]# docker run --name hhh --rm -p 80:80 httpd
在另一个终端上查看端口映射情况
[root@localhost ~]# docker port hhh
80/tcp -> 0.0.0.0:80
80/tcp -> :::80
5:自定义docker0桥的网络属性信息
自定义docker0桥的网络属性信息需要修改/etc/docker/daemon.json配置文件
[root@localhost ~]# vim /etc/docker/daemon.json
[root@localhost ~]# cat /etc/docker/daemon.json
{
"bip": "192.168.1.5/24",
"fixed-cidr": "192.168.1.5/25",
"fixed-cidr-v6": "2001:db8::/64",
"mtu": 1500,
"default-gateway": "10.20.1.1",
"default-gateway-v6": "2001:db8:abcd::89",
"dns": ["10.20.1.2","10.20.1.3"]
}
##6:docker远程连接
dockerd守护进程的C/S,其默认仅监听Unix Socket格式的地址(/var/run/docker.sock),如果要使用TCP套接字,则需要修改/etc/docker/daemon.json配置文件,添加如下内容,然后重启docker服务:
[root@localhost ~]# vim /lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H fd:// --containerd=/run/containerd/containerd.sock
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl restart docker
在客户端上向dockerd直接传递“-H|–host”选项指定要控制哪台主机上的docker容器
[root@localhost ~]# docker -H 192.168.226.139:2375 images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos d1 155d61cf48a8 3 hours ago 355MB
httpd latest f2a976f932ec 7 days ago 145MB
centos latest 5d0da3dc9764 10 months ago 231MB
6:docker创建自定义桥
创建一个额外的自定义桥,区别于docker0
[root@localhost ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
77e31c034589 bridge bridge local
40edade646f4 host host local
8fb413682fa2 none null local
[root@localhost ~]# docker network create --subnet "192.168.88.0/24" --gateway "192.168.88.1" xuanning
18eeea79efc0c3a6eb4933cb58791847a37bebe6fd9da111f12cc003217c14ed
[root@localhost ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
77e31c034589 bridge bridge local
40edade646f4 host host local
8fb413682fa2 none null local
18eeea79efc0 xuanning bridge local
使用新创建的自定义桥来创建容器:
[root@localhost ~]# docker run -it --name hhh --rm --network xuanning centos:d1 /bin/bash
[root@dec671011b06 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.88.2 netmask 255.255.255.0 broadcast 192.168.88.255
ether 02:42:c0:a8:58:02 txqueuelen 0 (Ethernet)
RX packets 65 bytes 337402 (329.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 28 bytes 1766 (1.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 658 (658.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 4 bytes 658 (658.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0