[toc]
環(huán)境準(zhǔn)備
-
物理服務(wù)器
ip:192.168.110.175/24 192.168.110.1
-
兩臺(tái)虛機(jī)
- vm1: 10.0.1.1/24 10.0.1.254
- vm2: 10.0.1.2/24 10.0.1.254
使用鏡像
使用鏡像由cirros-0.3.5-x86_64-disk.img
分別復(fù)制出兩份vm1.qcow2
小渊、vm2.qcow2
來(lái)使用伊者。
# ls -l /images/cirros
total 38880
-rw-r--r-- 1 root root 13267968 Sep 4 12:55 cirros-0.3.5-x86_64-disk.img
-rw-r--r-- 1 root root 13267968 Sep 4 13:13 vm1.qcow2
-rw-r--r-- 1 root root 13267968 Sep 4 13:13 vm2.qcow2
安裝KVM
# modprobe kvm
# yum install qemu-kvm
創(chuàng)建軟鏈接
# ln -sv /usr/libexec/qemu-kvm /usr/bin
‘/usr/bin/qemu-kvm’ -> ‘/usr/libexec/qemu-kvm’
編輯接口啟動(dòng)腳本
# cat /etc/qemu-ifup
#!/bin/bash
#
bridge=br-in
if [ -n "$1" ]; then
ip link set $1 up
brctl addif $bridge $1
[ $? -eq 0 ] && exit 0 || exit 1
else
echo "Error: no interface specified."
exit 1
fi
語(yǔ)法檢測(cè)正常檀训,再加執(zhí)行權(quán)限
# bash -n /etc/qemu-ifup
# chmod +x /etc/qemu-ifup
添加物理橋br-ex
并啟用
# brctl addbr br-ex
# ip link set br-ex up
撤去物理網(wǎng)卡的ip配置到網(wǎng)橋,并將物理網(wǎng)卡加入到網(wǎng)橋
# ip addr del 192.168.110.175/24 dev ens33; ip addr add 192.168.88.129/24 dev br-ex; brctl addif br-ex ens33;
再創(chuàng)建br-in
橋并啟用
# brctl addbr br-in
# ip link set br-in up
添加一個(gè)網(wǎng)絡(luò)名稱空間r1
# ip netns add r1
創(chuàng)建一對(duì)網(wǎng)卡田巴,同時(shí)啟用
# ip link add rinr type veth peer name rins
# ip link set rinr up
# ip link set rins up
將rinr網(wǎng)卡推送到網(wǎng)絡(luò)名稱空間r1中并改名激活
# ip link set rinr netns r1
# ip netns exec r1 ip link set rinr name eth0
# ip netns exec r1 ifconfig eth0 10.0.1.254/24 up
將rins網(wǎng)卡添加到br-in
網(wǎng)橋
# brctl addif br-in rins
永久開啟路由轉(zhuǎn)發(fā)功能
# echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
# sysctl -p
net.ipv4.ip_forward = 1
再創(chuàng)建一對(duì)網(wǎng)卡
# ip link add rexr type veth peer name rexs
將rexs
加入到br-ex
并啟用
# brctl addif br-ex rexs
# ip link set rexs up
對(duì)rexr網(wǎng)卡推送到r1名稱空間并改名為eth1,同時(shí)設(shè)置ip 并啟用:
# ip link set rexr netns r1
# ip netns exec r1 ip link set rexr name eth1
# ip netns exec r1 ifconfig eth1 192.168.110.176/24 up
虛機(jī)配置
新開一個(gè)終端慕的,創(chuàng)建vm1:
# qemu-kvm -m 128 -smp 1 -name vm1 -drive file=/images/cirros/vm1.qcow2,if=virtio,media=disk -net nic,macaddr=52:54:00:aa:bb:cc -net tap,ifname=vif1.0,script=/etc/qemu-ifup --nographic
再開一個(gè)終端祥款,創(chuàng)建vm2:
# qemu-kvm -m 128 -smp 1 -name vm2 -drive file=/images/cirros/vm2.qcow2,if=virtio,media=disk -net nic,macaddr=52:54:00:aa:bb:dd -net tap,ifname=vif2.0,script=/etc/qemu-ifup --nographic
啟動(dòng)登錄vm主機(jī),使用默認(rèn)的用戶名cirros
器腋,密碼cubswin:)
登錄,再執(zhí)行 sudo su -
切換到root 用戶钩杰。
使用ifconfig
命令確認(rèn)mac等信息正常纫塌。
配置vm1的ip和默認(rèn)路由
# ifconfig eth0 10.0.1.1/24 up
# route add default gw 10.0.1.254
配置vm2的ip和默認(rèn)路由
# ifconfig eth0 10.0.1.2/24 up
# route add default gw 10.0.1.254
最后操作
此時(shí)vm虛機(jī)中是可以ping通192.168.110.176
這個(gè)地址的,但無(wú)法ping通192.168.110.175
這個(gè)地址讲弄,這是由于icmp報(bào)文可以到達(dá)192.168.110.175
措左,但報(bào)文無(wú)法送回。這個(gè)時(shí)候就需要在路由器上加SNAT規(guī)則避除。
# ip netns exec r1 iptables -t nat -A POSTROUTING -s 10.0.1.0/24 ! -d 10.0.1.0/24 -j MASQUERADE
或者
# ip netns exec r1 iptables -t nat -A POSTROUTING -s 10.0.1.0/24 ! -d 10.0.1.0/24 -j SNAT --to-source 192.168.110.176
此時(shí)怎披,vm虛機(jī)上就能ping通外部網(wǎng)絡(luò)地址了,也可以抓包進(jìn)行查看:
# tcpdump -i rins -nn icmp
配置dhcp功能
在host主機(jī)安裝dnsmasq:
# yum install dnsmasq
啟動(dòng)dnsmasq
# ip netns exec r1 dnsmasq -F 10.0.1.151,10.0.1.160 --dhcp-option option:router,10.0.1.254
可以對(duì)啟動(dòng)進(jìn)程進(jìn)行查看:
# ip netns exec r1 ps aux | grep dnsmasq
注意:這個(gè)進(jìn)程在host主機(jī)執(zhí)行時(shí)瓶摆,也能看到凉逛,但它只作用于名稱空間r1.
在vm節(jié)點(diǎn)測(cè)試dns服務(wù)
# udhcpc -R
正常情況下是可以獲取到ip和網(wǎng)關(guān)地址的,但由于cirros 系統(tǒng)的問(wèn)題群井,此處報(bào)錯(cuò):
# udhcpc -R
udhcpc (v1.20.1) started
WARN: '/usr/share/udhcpc/default.script' should not be used in cirros. Replaced by cirros-dhcpc.
Sending discover...
Sending select for 10.0.1.151...
Lease of 10.0.1.151 obtained, lease time 3600
WARN: '/usr/share/udhcpc/default.script' should not be used in cirros. Replaced by cirros-dhcpc.
發(fā)現(xiàn)并沒有獲取到地址状飞,但我們的操作過(guò)程是正確的。