本帖最后由 rsgg03 于 2015-3-4 01:37 编辑
问题导读
1.nova就无法区分到底是哪个虚拟机请求metadata是如何解决的?
2.namespace的作用是什么?
版本:Grizzly master分支 2013.6.17
部署:三节点(controller + network + compute)
网络类型:vlan
之前的一篇blog中碰到了虚拟机访问169.254.169.254的问题,在F版中,会在网络节点上做NAT转换,直接访问nova的metadata服务,但这种方法,在使用namespace时就不生效了,因为namespace支持IP地址重叠,这样nova就无法区分到底是哪个虚拟机请求metadata。
该问题在G版得到解决,blueprint在此。采取的方法是在HTTP头部识别是哪个虚拟机。同时,G版在Quantum中加入了两个服务:namespace metadata proxy和metadata agent。一个虚拟机访问169.254.169.254的流程如下图:
在细讲流程之前,估计很多人并不清楚metadata的作用和由来。不懂的童鞋看这里。
1、虚拟机发出请求虚拟机启动时会访问169.254.169.254获取一些内容。这个从我之前出错的日志中就可以看出来:
'http://169.254.169.254/2009-04-04/meta-data/instance-id' failed [0/120s]: http error [404]
在虚拟机内部访问169.254.169.254,看一下都有什么:
- ubuntu@ubuntu-test:~$ curl http://169.254.169.254/2009-04-04/meta-data
- ami-id
- ami-launch-index
- ami-manifest-path
- block-device-mapping/
- hostname
- instance-action
- instance-id
- instance-type
- kernel-id
- local-hostname
- local-ipv4
- placement/
- public-hostname
- public-ipv4
- public-keys/
- ramdisk-id
复制代码
那么这些内容是如何获取到的?因为虚拟机内部没有特殊的路由,所以数据包会直接发送到虚拟机的默认网关,而默认网关是在network node上。虚拟机的网卡信息如下:
- ubuntu@ubuntu-test:~$ ip -4 address show dev eth0
- 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
- inet 10.1.1.2/24 brd 10.1.1.255 scope global eth0
复制代码
虚拟机的路由信息:
- ubuntu@ubuntu-test:~$ ip route
- default via 10.1.1.1 dev eth0 metric 100
- 10.1.1.0/24 dev eth0 proto kernel scope link src 10.1.1.2
复制代码
2、namespace-metadata-proxy
因为使用了namespace,在network node上每个namespace里都会有相应的iptables规则和网络设备。
先来看看对应的iptables规则:
- -A quantum-l3-agent-PREROUTING -d 169.254.169.254/32 -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 9697
- -A quantum-l3-agent-INPUT -d 127.0.0.1/32 -p tcp -m tcp --dport 9697 -j ACCEPT
复制代码
虚拟机默认网关设备信息的地址信息,可以看到虚拟机网关的IP(10.1.1.1):
- root@network232:~# ip netns exec qrouter-b147a74b-39bb-4c7a-aed5-19cac4c2df13 ip addr show qr-f7ec0f5c-2e
- 12: qr-f7ec0f5c-2e: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN
- link/ether fa:16:3e:81:9c:69 brd ff:ff:ff:ff:ff:ff
- inet 10.1.1.1/24 brd 10.1.1.255 scope global qr-f7ec0f5c-2e
- inet6 fe80::f816:3eff:fe81:9c69/64 scope link
- valid_lft forever preferred_lft forever
复制代码
iptables规则中,会把目的地址169.254.169.254的数据包,重定向到本地端口9697,那么看一下network node上,在该端口的监听进程:
- root@network232:~# ip netns exec qrouter-b147a74b-39bb-4c7a-aed5-19cac4c2df13 netstat -nlpt | grep 9697
- tcp 0 0 0.0.0.0:9697 0.0.0.0:* LISTEN 13249/python
复制代码
进程号13249,看一下这个进程:
- root@network232:~# ps -f --pid 13249 | fold -s -w 85
- UID PID PPID C STIME TTY TIME CMD
- root 13249 1 0 16:30 ? 00:00:00 python
- /usr/bin/quantum-ns-metadata-proxy
- --pid_file=/var/lib/quantum/external/pids/b147a74b-39bb-4c7a-aed5-19cac4c2df13.pid
- --router_id=b147a74b-39bb-4c7a-aed5-19cac4c2df13 --state_path=/var/lib/quantum
- --metadata_port=9697 --debug --verbose
- --log-file=quantum-ns-metadata-proxyb147a74b-39bb-4c7a-aed5-19cac4c2df13.log
- --log-dir=/var/log/quantum
复制代码
可能很多人想知道,这个进程从何而来,解释这个问题,可能要贴点代码:
- def _spawn_metadata_proxy(self, router_info):
- def callback(pid_file):
- proxy_cmd = ['quantum-ns-metadata-proxy',
- '--pid_file=%s' % pid_file,
- '--router_id=%s' % router_info.router_id,
- '--state_path=%s' % self.conf.state_path,
- '--metadata_port=%s' % self.conf.metadata_port]
- proxy_cmd.extend(config.get_log_args(
- cfg.CONF, 'quantum-ns-metadata-proxy-%s.log' %
- router_info.router_id))
- return proxy_cmd
-
- pm = external_process.ProcessManager(
- self.conf,
- router_info.router_id,
- self.root_helper,
- router_info.ns_name())
- pm.enable(callback)
复制代码
可见,启用namespace场景下,对于每一个router,都会创建这样一个进程。该进程监听9697端口,其主要功能:
1、向请求头部添加X-Forwarded-For和X-Quantum-Router-ID,分别表示虚拟机的fixedIP和router的ID
2、将请求代理至Unix domain socket(/var/lib/quantum/metadata_proxy)
3、Quantum Metadata Agent
network node上的metadata agent监听/var/lib/quantum/metadata_proxy:
- root@network232:~# netstat -lxp | grep metadata
- unix 2 [ ACC ] STREAM LISTENING 46859 21025/python /var/lib/quantum/metadata_proxy
- root@network232:~# ps -f --pid 21025 | fold -s
- UID PID PPID C STIME TTY TIME CMD
- quantum 21025 1 0 16:31 ? 00:00:00 python
- /usr/bin/quantum-metadata-agent --config-file=/etc/quantum/quantum.conf
- --config-file=/etc/quantum/metadata_agent.ini
- --log-file=/var/log/quantum/metadata-agent.log
复制代码
该进程的功能是,根据请求头部的X-Forwarded-For和X-Quantum-Router-ID参数,向Quantum service查询虚拟机ID,然后向Nova Metadata服务发送请求(默认端口8775),消息头:X-Forwarded-For,X-Instance-ID、X-Instance-ID-Signature分别表示虚拟机的fixedIP,虚拟机ID和虚拟机ID的签名。
4、Nova Metadata ServiceNova的metadata service是随着nova-api启动的,会同时启动三个服务:ec2, osapi_compute, metadata。虚拟机访问169.254.169.254的返回内容,其实就是metadata服务向nova-conductor查询后,返回给network node的metadata agent,再由metadata agent返回给metadata proxy,然后返回给虚拟机的。
- root@controller231:~# netstat -nlpt | grep 8775
- tcp 0 0 0.0.0.0:8775 0.0.0.0:* LISTEN 1714/python
- root@controller231:~# ps -f --pid 1714
- UID PID PPID C STIME TTY TIME CMD
- nova 1714 1 0 16:40 ? 00:00:01 /usr/bin/python /usr/bin/nova-api --config-file=/etc/nova/nova.conf
复制代码
上面讲了一个虚拟机如何通过router的namespace-metadata-proxy访问169.254.169.254获取一些自定义信息。其实在Quantum中,还有dhcp的namespace-metadata-proxy可以提供访问。在使用前,需要在dhcp agent的配置文件中增加一个配置:
- # The DHCP server can assist with providing metadata support on isolated
- # networks. Setting this value to True will cause the DHCP server to append
- # specific host routes to the DHCP request. The metadata service will only
- # be activated when the subnet gateway_ip is None. The guest instance must
- # be configured to request host routes via DHCP (Option 121).
- enable_isolated_metadata = True
复制代码
然后,创建subnet,注意不要指定gateway:
- root@controller231:/usr/lib/python2.7/dist-packages# quantum subnet-create testnet01 172.17.17.0/24 --no-gateway --name=sub_no_gateway
- Created a new subnet:
- +------------------+--------------------------------------------------+
- | Field | Value |
- +------------------+--------------------------------------------------+
- | allocation_pools | {"start": "172.17.17.1", "end": "172.17.17.254"} |
- | cidr | 172.17.17.0/24 |
- | dns_nameservers | |
- | enable_dhcp | True |
- | gateway_ip | |
- | host_routes | |
- | id | 34168195-f101-4be4-8ca8-c9d07b58d41a |
- | ip_version | 4 |
- | name | sub_no_gateway |
- | network_id | 3d42a0d4-a980-4613-ae76-a2cddecff054 |
- | tenant_id | 6fbe9263116a4b68818cf1edce16bc4f |
- +------------------+--------------------------------------------------+
复制代码
此时,DHCP agent就会在所在的节点上,在dhcp namespace内,添加一个地址169.254.169.254,并启动metadata namespace proxy监听TCP的80端口。
- root@network232:~# ip netns | grep qdhcp
- qdhcp-9daeac7c-a98f-430f-8e38-67f9c044e299
- qdhcp-3d42a0d4-a980-4613-ae76-a2cddecff054
- root@network232:~# ip netns exec qdhcp-3d42a0d4-a980-4613-ae76-a2cddecff054 ip -4 a
- 11: tap332ce137-ec: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN
- inet 10.1.1.3/24 brd 10.1.1.255 scope global tap332ce137-ec
- inet 10.0.0.2/24 brd 10.0.0.255 scope global tap332ce137-ec
- 14: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
- inet 127.0.0.1/8 scope host lo
- 21: tap21b5c483-84: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN
- inet 10.1.1.3/24 brd 10.1.1.255 scope global tap21b5c483-84
- inet 169.254.169.254/16 brd 169.254.255.255 scope global tap21b5c483-84
- inet 10.0.10.2/24 brd 10.0.10.255 scope global tap21b5c483-84
- inet 10.0.2.2/24 brd 10.0.2.255 scope global tap21b5c483-84
- inet 172.17.17.1/24 brd 172.17.17.255 scope global tap21b5c483-84
- root@network232:~# ip netns exec qdhcp-3d42a0d4-a980-4613-ae76-a2cddecff054 route -n
- Kernel IP routing table
- Destination Gateway Genmask Flags Metric Ref Use Iface
- 10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 tap332ce137-ec
- 10.0.2.0 0.0.0.0 255.255.255.0 U 0 0 0 tap21b5c483-84
- 10.0.10.0 0.0.0.0 255.255.255.0 U 0 0 0 tap21b5c483-84
- 10.1.1.0 0.0.0.0 255.255.255.0 U 0 0 0 tap332ce137-ec
- 10.1.1.0 0.0.0.0 255.255.255.0 U 0 0 0 tap21b5c483-84
- 169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 tap21b5c483-84
- 172.17.17.0 0.0.0.0 255.255.255.0 U 0 0 0 tap21b5c483-84
- root@network232:~# ip netns exec qdhcp-3d42a0d4-a980-4613-ae76-a2cddecff054 netstat -4 -anpt
- Active Internet connections (servers and established)
- Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
- tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 7035/python
- tcp 0 0 10.1.1.3:53 0.0.0.0:* LISTEN 14592/dnsmasq
- tcp 0 0 169.254.169.254:53 0.0.0.0:* LISTEN 14592/dnsmasq
- tcp 0 0 10.0.10.2:53 0.0.0.0:* LISTEN 14592/dnsmasq
- tcp 0 0 10.0.2.2:53 0.0.0.0:* LISTEN 14592/dnsmasq
- tcp 0 0 172.17.17.1:53 0.0.0.0:* LISTEN 14592/dnsmasq
- root@network232:~# ps -f --pid 7035 | fold -s -w 82
- UID PID PPID C STIME TTY TIME CMD
- root 7035 1 0 Jun17 ? 00:00:00 python
- /usr/bin/quantum-ns-metadata-proxy
- --pid_file=/var/lib/quantum/external/pids/3d42a0d4-a980-4613-ae76-a2cddecff054.pid
- --network_id=3d42a0d4-a980-4613-ae76-a2cddecff054 --state_path=/var/lib/quantum
- --metadata_port=80 --debug --verbose
- --log-file=quantum-ns-metadata-proxy3d42a0d4-a980-4613-ae76-a2cddecff054.log
- --log-dir=/var/log/quantum
- root@network232:~# ps -f --pid 14592 | fold -s -w 82
- UID PID PPID C STIME TTY TIME CMD
- nobody 14592 1 0 15:34 ? 00:00:00 dnsmasq --no-hosts --no-resolv
- --strict-order --bind-interfaces --interface=tap21b5c483-84 --except-interface=lo
- --pid-file=/var/lib/quantum/dhcp/3d42a0d4-a980-4613-ae76-a2cddecff054/pid
- --dhcp-hostsfile=/var/lib/quantum/dhcp/3d42a0d4-a980-4613-ae76-a2cddecff054/host
- --dhcp-optsfile=/var/lib/quantum/dhcp/3d42a0d4-a980-4613-ae76-a2cddecff054/opts
- --dhcp-script=/usr/bin/quantum-dhcp-agent-dnsmasq-lease-update --leasefile-ro
- --dhcp-range=set:tag0,10.0.2.0,static,120s
- --dhcp-range=set:tag1,172.17.17.0,static,120s
- --dhcp-range=set:tag2,10.0.10.0,static,120s
- --dhcp-range=set:tag3,10.1.1.0,static,120s --conf-file= --domain=openstacklocal
- root@network232:~# cat /var/lib/quantum/dhcp/3d42a0d4-a980-4613-ae76-a2cddecff054/opts
- tag:tag0,option:router,10.0.2.1
- tag:tag1,option:classless-static-route,169.254.169.254/32,172.17.17.1
- tag:tag1,option:router
- tag:tag2,option:dns-server,8.8.8.7,8.8.8.8
- tag:tag2,option:router,10.0.10.1
- tag:tag3,option:dns-server,8.8.8.7,8.8.8.8
- tag:tag3,option:router,10.1.1.1
复制代码
当DHCP客户端请求DHCP option 121时,就会收到静态路由的配置信息,在虚拟机上其实就会执行:
- ip route add 169.254.169.254/32 via 172.17.17.1
复制代码
(需要注意的是,cirros镜像不会发送dhcp option 121请求)
当虚拟机内有该条静态路由后,到169.254.169.254:80的请求,就会发送到network node上dhcp namespace里的metadata nameserver proxy,proxy就会为消息添加X-Quantum-Network-ID和X-Forwarded-For头部,分别表示network-id和instance-id,然后通过Unix domain socket发送给quantum-metadata-agent,然后的流程就可以参考前一篇blog了。
|