虚拟化环境模拟实例
Posted 运维扫盲人
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了虚拟化环境模拟实例相关的知识,希望对你有一定的参考价值。
A.使用brctl桥桥接至外网
[root@rs-2 ~]# yum install -y bridge-utils-1.5-9.el7.x86_64
[root@rs-2 ~]# cat /etc/sysconfig/network-scripts/ifcfg_business
TYPE=Bridge
BOOTPROTO=none
DEFROUTE=yes
NAME=br_business
DEVICE=br_business
ONBOOT=yes
[root@rs-2 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
DEFROUTE=yes
NAME=ens33
BRIDGE=br_business
DEVICE=ens33
ONBOOT=yes
[root@rs-2 ~]# ifconfig br_business
br_business: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet6 fe80::20c:29ff:fe73:97e0 prefixlen 64 scopeid 0x20<link>
inet6 2408:84fb:106:80ee:20c:29ff:fe73:97e0 prefixlen 64 scopeid 0x0<global>
ether 00:0c:29:73:97:e0 txqueuelen 1000 (Ethernet)
RX packets 1624 bytes 172556 (168.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 9 bytes 766 (766.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@rs-2 ~]# cat /etc/qemu-ifup
#!/bin/bash
#
Bridge=br_business
if [ -n "$1" ]; then
ip link set $1 up
brctl addif $Bridge $1
[ $? -eq 0 ] && exit 0 || exit 1
else
echo "Error: no port specified."
exit 1
fi
[root@rs-2 ~]# /usr/libexec/qemu-kvm -m 128 -cpu host -smp 2 -name vm1 -drive file=/images/cirros/test1.qcow2,if=virtio,media=disk -net nic,macaddr=52:54:00:12:34:68 -net tap,ifname=vif1.0,script=/etc/qemu-ifup,downscript=/etc/qemu-ifdown --nographic
虚拟机vm1:
# ifconfig eth0 192.168.43.118 up
# route add default gw 192.168.43.1
# ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8): 56 data bytes
64 bytes from 8.8.8.8: seq=0 ttl=113 time=219.130 ms
64 bytes from 8.8.8.8: seq=1 ttl=113 time=179.908 ms
64 bytes from 8.8.8.8: seq=2 ttl=113 time=121.887 ms
--- 8.8.8.8 ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 121.887/173.641/219.130 ms
[root@rs-2 ~]# tcpdump -i br_business -nn icmp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on br_business, link-type EN10MB (Ethernet), capture size 262144 bytes
22:26:08.876672 IP 192.168.43.118 > 8.8.8.8: ICMP echo request, id 10753, seq 0, length 64
22:26:09.094212 IP 8.8.8.8 > 192.168.43.118: ICMP echo reply, id 10753, seq 0, length 64
22:26:09.878446 IP 192.168.43.118 > 8.8.8.8: ICMP echo request, id 10753, seq 1, length 64
22:26:10.056877 IP 8.8.8.8 > 192.168.43.118: ICMP echo reply, id 10753, seq 1, length 64
22:26:10.880183 IP 192.168.43.118 > 8.8.8.8: ICMP echo request, id 10753, seq 2, length 64
22:26:10.999837 IP 8.8.8.8 > 192.168.43.118: ICMP echo reply, id 10753, seq 2, length 64
B.使用ovs桥桥接至外网
同一宿主机上不同虚拟机可以互通
[root@bl ~]# ovs-vsctl add-br br_business
[root@bl ~]# ovs-vsctl add-port br_business ens33
[root@bl ~]# ovs-vsctl show
3a5e6312-a1b4-4c30-88d0-7c23ebb52b33
Bridge br_business
Port br_business
Interface br_business
type: internal
Port "vif1.0"
Interface "vif1.0"
Port "ens33"
Interface "ens33"
Port "vif2.0"
Interface "vif2.0"
ovs_version: "2.6.1"
vm1:
# ifconfig
eth0 Link encap:Ethernet HWaddr 52:54:00:12:34:68
inet addr:192.168.3.33 Bcast:192.168.3.255 Mask:255.255.255.0
inet6 addr: fe80::5054:ff:fe12:3468/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:248 errors:0 dropped:0 overruns:0 frame:0
TX packets:144 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:18055 (17.6 KiB) TX bytes:7363 (7.1 KiB)
vm2:
# ifconfig
eth0 Link encap:Ethernet HWaddr 52:54:00:12:34:79
inet addr:192.168.3.34 Bcast:192.168.3.255 Mask:255.255.255.0
inet6 addr: fe80::5054:ff:fe12:3479/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:216 errors:0 dropped:0 overruns:0 frame:0
TX packets:145 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:14460 (14.1 KiB) TX bytes:7346 (7.1 KiB)
补充:ovs桥先添加物理网卡ens33,虚拟机启动时可自动获取IP地址和网关;
补充:使用VLAN来隔离虚拟机并保证虚拟机可以连互联网需要开启VMware Workstation的VLAN功能;
以上是关于虚拟化环境模拟实例的主要内容,如果未能解决你的问题,请参考以下文章