ip netns
直接执行这个命令(或ip netns list)读取的是/var/run/netns下的文件名,因此若不存在/var/run/netns,需要mkdir -p /var/run/netns
配置像LXC一样的网络
I. 宿主配置
1. 宿主上升级iproute包,以便支持ip netns指令:
yum install -y http://rdo.fedorapeople.org/rdo-release.rpm
yum update -y iproute
2. 在宿主上配置好桥接:
一. 方法1(不推荐): 敲命令配置桥接(很容易导致网络中断,需要ILO连上操作)
1) 创建桥接网卡br1并激活:brctl addbr br1; ip link set br1 up
2) 配置br1的mac地址,和宿主准备桥接的网卡mac相同,通常为内网网卡eth1:ip link set br1 address xx:xx:xx:xx:xx:xx
3) 给br1配置一个ip地址,或者将eth1的ip地址配置在br1上,2种方法任选其一都可行:
前者:
ifconfig br1 192.168.2.1 netmask 255.255.255.0
后者:
ifconfig eth1 0.0.0.0; ifconfig br1 192.168.2.2 netmask 255.255.255.0
4) 配置宿主网关,从br1出
ip ro del default
ip ro add default via 192.168.2.254 dev br1
5) 将eth1桥接至br1:
brctl addif br1 eth1
二. 方法2(推荐):写网卡配置文件
ifcfg-br1:
DEVICE="br1"
TYPE="Bridge"
NM_CONTROLLED="no"
ONBOOT="yes"
BOOTPROTO="static"
IPADDR=192.168.2.2
NETMASK=255.255.255.0
ifcfg-eth1:
DEVICE="eth1"
BRIDGE="br1"
BOOTPROTO="none"
NM_CONTROLLED="no"
ONBOOT="yes"
TYPE="Ethernet"
注意:要在/etc/sysconfig/network-scripts/ifup-eth里if [ "${TYPE}" = "Bridge" ]; then -> fi段落最后(fi前)加个ip link set br1 address $(get_hwaddr eth1),防止桥接网卡mac地址随机生成导致网络暂时中断
service network restart# 重启网络生效
II. 容器配置:
1. 启动docker容器:
docker run -t -i -d --name="net_test" --net=none centos:latest /bin/bash
记录下输出(即CONTAINER ID),然后通过docker inspect -f '``.`State`.`Pid`' CONTAINER ID获得该容器的pid(也即容器首进程在宿主上的pid),假设为1000
2. 为容器创建网卡命名空间,建立点对点连接(容器命名空间网卡和宿主上生成的网卡点对点)
mkdir -p /var/run/netns#创建网络命名空间目录,ip netns会读取该目录下的文件名
ln -s /proc/1000/ns/net /var/run/netns/1000#将网络命名空间文件软链接到/var/run/netns,以便ip netns能够读取
ip link add vethA type veth peer name vethB#在宿主上创建2张直连网卡(vethA与vethB直连),将vethA作为容器里的网卡,vethB作为宿主上能看到的网卡
ip link set vethB up# 激活网卡vethB
ip link set vethA netns 1000# 将刚才创建的网卡归位网络命名空间
配置vethA网卡参数:
ip netns exec 1000 ip link set vethA name eth1
ip netns exec 1000 ip addr add 192.168.2.3/24 dev eth1
ip netns exec 1000 ip link set eth1 up
ip netns exec 1000 ip route add default via 192.168.2.254 dev eth1
brctl addif br1 vethB# 将eth1桥接至br1
3. 测试:
docker attach登录容器,查看是否能ping通网关及其他子网或公网
–iptables=false
使用这个参数后,就不会再往iptables里生成nat、forward等信息了。
这样启动的容器,登录容器能看到网关是宿主docker0的ip,这样网络是通的,是可以访问外网,但路是这么走的:
1. 容器里的数据包将数据经过point-to-point网卡传送到宿主的对应veth网卡上
2. 宿主veth网卡接收到数据包后发现网关是254,于是通过icmp数据包告知网关是254,然后容器发送数据包时自动将网关更改为254,可以从ping的输出看到:
[ 17:37:23-root@21e77bf38fc0:~ ]#ping www.baidu.com
PING www.a.shifen.com (115.239.210.27) 56(84) bytes of data.
64 bytes from 115.239.210.27: icmp_seq=1 ttl=55 time=13.9 ms
From 192.168.3.1: icmp_seq=2 Redirect Host(New nexthop: 192.168.3.254)
64 bytes from 115.239.210.27: icmp_seq=2 ttl=55 time=13.6 ms
From 192.168.3.1: icmp_seq=3 Redirect Host(New nexthop: 192.168.3.254)
64 bytes from 115.239.210.27: icmp_seq=3 ttl=55 time=13.6 ms
From 192.168.3.1: icmp_seq=4 Redirect Host(New nexthop: 192.168.3.254)
64 bytes from 115.239.210.27: icmp_seq=4 ttl=55 time=14.0 ms
From 192.168.3.1: icmp_seq=5 Redirect Host(New nexthop: 192.168.3.254)
64 bytes from 115.239.210.27: icmp_seq=5 ttl=55 time=14.7 ms
From 192.168.3.1: icmp_seq=6 Redirect Host(New nexthop: 192.168.3.254)
64 bytes from 115.239.210.27: icmp_seq=6 ttl=55 time=13.8 ms
64 bytes from 115.239.210.27: icmp_seq=7 ttl=55 time=13.7 ms
From192.168.3.1: icmp_seq=8 Redirect Host(New nexthop: 192.168.3.254)
64 bytes from 115.239.210.27: icmp_seq=8 ttl=55 time=13.8 ms
64 bytes from 115.239.210.27: icmp_seq=9 ttl=55 time=13.6 ms
64 bytes from 115.239.210.27: icmp_seq=10 ttl=55 time=13.5 ms
From 192.168.3.1: icmp_seq=11 Redirect Host(New nexthop: 192.168.3.254)
64 bytes from 115.239.210.27: icmp_seq=11 ttl=55 time=13.8 ms
64 bytes from 115.239.210.27: icmp_seq=12 ttl=55 time=14.1 ms
64 bytes from 115.239.210.27: icmp_seq=13 ttl=55 time=13.8 ms
64 bytes from 115.239.210.27: icmp_seq=14 ttl=55 time=13.6 ms
64 bytes from 115.239.210.27: icmp_seq=15 ttl=55 time=13.7 ms
64 bytes from 115.239.210.27: icmp_seq=16 ttl=55 time=13.8 ms
From 192.168.3.1: icmp_seq=17 Redirect Host(New nexthop: 192.168.3.254)
64 bytes from 115.239.210.27: icmp_seq=17 ttl=55 time=13.6 ms
64 bytes from 115.239.210.27: icmp_seq=18 ttl=55 time=13.8 ms
64 bytes from 115.239.210.27: icmp_seq=19 ttl=55 time=13.7 ms
64 bytes from 115.239.210.27: icmp_seq=20 ttl=55 time=14.1 ms
64 bytes from 115.239.210.27: icmp_seq=21 ttl=55 time=13.7 ms
64 bytes from 115.239.210.27: icmp_seq=22 ttl=55 time=13.8 ms
64 bytes from 115.239.210.27: icmp_seq=23 ttl=55 time=13.7 ms
64 bytes from 115.239.210.27: icmp_seq=24 ttl=55 time=13.9 ms
64 bytes from 115.239.210.27: icmp_seq=25 ttl=55 time=14.2 ms
64 bytes from 115.239.210.27: icmp_seq=26 ttl=55 time=13.7 ms
64 bytes from 115.239.210.27: icmp_seq=27 ttl=55 time=13.7 ms
64 bytes from 115.239.210.27: icmp_seq=28 ttl=55 time=13.8 ms
64 bytes from 115.239.210.27: icmp_seq=29 ttl=55 time=13.9 ms
64 bytes from 115.239.210.27: icmp_seq=30 ttl=55 time=14.1 ms
64 bytes from 115.239.210.27: icmp_seq=31 ttl=55 time=13.9 ms
64 bytes from 115.239.210.27: icmp_seq=32 ttl=55 time=13.9 ms
64 bytes from 115.239.210.27: icmp_seq=33 ttl=55 time=14.0 ms
64 bytes from 115.239.210.27: icmp_seq=34 ttl=55 time=13.7 ms
64 bytes from 115.239.210.27: icmp_seq=35 ttl=55 time=14.0 ms
64 bytes from 115.239.210.27: icmp_seq=36 ttl=55 time=14.4 ms
64 bytes from 115.239.210.27: icmp_seq=37 ttl=55 time=13.9 ms
Docker HTTPS原理:双向验证。官方说明(https://docs.docker.com/articles/https/):
In daemon mode, it will only allow connections from clients authenticated by a certificate signed by that CA.
In client mode, it will only connect to servers with a certificate signed by that CA.
核心:服务端和客户端的数字证书都由同一个CA签发,因此双方在认证通讯时使用和签发时的同一个CA就能互相认证。
14.5.1. HTTP code 403 while uploading metadata: invalid character ‘<‘ looking for beginning of value
报错示例:
[ 14:50:44-root@localhost:vhosts ]#docker push registry.17173ops.com:82/crosbymichael/dockerui
The push refers to a repository [registry.17173ops.com:82/crosbymichael/dockerui] (len: 1)
Sending image list
Pushing repository registry.17173ops.com:82/crosbymichael/dockerui (1 tags)
511136ea3c5a: Pushing
2014/09/01 14:50:46 HTTP code 403 while uploading metadata: invalid character '<' looking for beginning of value