环境
CentOS release 7.2 x86_64 4核8G内存
查看是否支持虚拟技术
# cat /proc/cpuinfo | egrep 'vmx|svm' ......eagerfpu pni pclmulqdq dtes64 monitor ds_cpl ......
安装kvm
yum install qemu-kvm qemu-img virt-manager libvirt libvirt-python python-virtinst libvirt-client virt-install virt-viewer libguestfs-tools -y
创建网桥dr0
# cat /etc/sysconfig/network-scripts/ifcfg-dr0 DEVICE=dr0 TYPE=Bridge ONBOOT=yes NM_CONTROLLED=no BOOTPROYO=static IPADDR=192.168.2.249 GATEWAY=192.168.2.1 NETMASK=255.255.255.0 # cat /etc/sysconfig/network-scripts/ifcfg-enp2s0 TYPE="Ethernet" BOOTPROTO="none" DEFROUTE="yes" NAME="enp2s0" UUID="e94f67f1-5abd-4cd8-a856-15ab9a4b8834" DEVICE="enp2s0" ONBOOT="yes" #IPADDR="192.168.2.249" PREFIX="24" #GATEWAY="192.168.2.1" BRIDGE=dr0 NM_CONTROLLED="no"
重启网络:
# service network restart # ifconfig #查看网络 centos7 无ifconfig命令 yum install net-tools dr0: flags=4163mtu 1500 inet 192.168.2.249 netmask 255.255.255.0 broadcast 192.168.2.255 inet6 fe80::feaa:14ff:feef:1d77 prefixlen 64 scopeid 0x20 ether fc:aa:14:ef:1d:77 txqueuelen 0 (Ethernet) RX packets 241231 bytes 487712833 (465.1 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 95332 bytes 31500606 (30.0 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 enp2s0: flags=4163 mtu 1500 inet6 fe80::feaa:14ff:feef:1d77 prefixlen 64 scopeid 0x20 ether fc:aa:14:ef:1d:77 txqueuelen 1000 (Ethernet) RX packets 604728 bytes 604285997 (576.2 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 136489 bytes 35905234 (34.2 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10 loop txqueuelen 0 (Local Loopback) RX packets 68 bytes 5508 (5.3 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 68 bytes 5508 (5.3 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
在添加一个网桥(作为虚拟机内网)
# brctl addbr dr1 # ifconfig dr1 10.0.2.1 up #启动dr1网桥,并附加一个IP 查看网络: dr0: flags=4163mtu 1500 inet 192.168.2.249 netmask 255.255.255.0 broadcast 192.168.2.255 inet6 fe80::feaa:14ff:feef:1d77 prefixlen 64 scopeid 0x20 ether fc:aa:14:ef:1d:77 txqueuelen 0 (Ethernet) RX packets 3471895 bytes 3437758241 (3.2 GiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1612573 bytes 1630220826 (1.5 GiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 dr1: flags=4163 mtu 1500 inet 10.0.2.1 netmask 255.0.0.0 broadcast 10.255.255.255 inet6 fe80::5003:41ff:fe4e:93e2 prefixlen 64 scopeid 0x20 ether fe:54:00:28:5b:31 txqueuelen 0 (Ethernet) RX packets 7376 bytes 335487 (327.6 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 25 bytes 1754 (1.7 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 enp2s0: flags=4163 mtu 1500 inet6 fe80::feaa:14ff:feef:1d77 prefixlen 64 scopeid 0x20 ether fc:aa:14:ef:1d:77 txqueuelen 1000 (Ethernet) RX packets 9195872 bytes 5168391001 (4.8 GiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 5197454 bytes 2893988504 (2.6 GiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10 loop txqueuelen 0 (Local Loopback) RX packets 1215 bytes 88597 (86.5 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1215 bytes 88597 (86.5 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
创建目录
# mkdir -p /data/kvm/images/ #镜像存储路径 # mkdir -p /data/kvm/kvmimg/ #虚拟机存储路径 # mkdir -p /data/kvm/template/ #虚拟机配置模板路径
至此kvm安装完成
创建kvm虚拟机
# virt-install --name tem --ram 1024 --vcpus=1 --disk path=/data/kvm/kvmimg/tem.img,size=40 --bridge=dr0 --bridge=dr1 --accelerate --cdrom=/data/kvm/images/CentOS-6.7-x86_64-minimal.iso --vnc --vncport=9998 --vnclisten=0.0.0.0 参数说明:--name 虚拟机名 --ram 内存(单位MB) --vcpus cpu个数 --disk 存储配置 path 路径 size 大小(单位G)这里[dr0比如为外网IP ,dr1 为内网IP] --bridge=[网桥名] 网络配置 如需要多个网络就配置多个[--bridge=[网桥名]] --accelerate KVM或KQEMU内核加速 --cdrom 镜像路径 --vnc vnc配置 vncport 端口号
用vnc连接工具安装(系统安装步骤略...)
附加硬盘
# qemu-img create /data/kvm/kvmimg/data-host01.img 100G 创建一个100G的磁盘 # virsh attach-disk [hostname] /data/kvm/kvmimg/data-host01.img vda
取消附加硬盘
# virsh detach-disk [hostname] vda
使用虚拟机作为模板
1. # rm /etc/udev/rules.d/70-persistent-net.rules -rf (centos 6.X) 2. # init 0
kvm 快速克隆脚本
创建配置模板
cat /data/kvm/template/ifcfg-eth0 #网卡1配置模板 DEVICE=eth0 TYPE=Ethernet ONBOOT=yes NM_CONTROLLED=yes BOOTPROTO=static GATEWAY=192.168.2.1 NATMASK=255.255.255.0 IPADDR=0.0.0.0 cat /data/kvm/template/ifcfg-eth1 #网卡2配置模板 DEVICE=eth1 TYPE=Ethernet ONBOOT=yes NM_CONTROLLED=yes BOOTPROTO=static IPADDR=0.0.0.0 cat /data/kvm/template/network #主机名配置模板 NETWORKING=yes HOSTNAME=localhost.localdomain
提供shell脚本如下:
cat /data/bin/kvm.sh #!/bin/bash #定义虚拟机名 read -p "Enter the host name:" hostname #公网IP地址 read -p "Enter the public IP number:" public #私网IP地址 read -p "Enter the private IP number:" private #模板主机[必须是停止状态] template=tem #img文件存放位置 kvmimg_path=/data/kvm/kvmimg/ #外网网卡配置 eht0_path=/data/kvm/template/ifcfg-eth0 #内网网卡配置 eht1_path=/data/kvm/template/ifcfg-eth1 #虚拟机网卡配置文件路径 path=/etc/sysconfig/network-scripts/ #主机名模板 hostname_path=/data/kvm/template/network #虚拟机主机名路径 kvm_hostname_path=/etc/sysconfig/ while true do read -p "Confirm the virtual machine name($hostname) and public IP($public) and private IP($private)[y or n]:" statuss if [ "$statuss" = "y" ]; then break; elif [ "$statuss" = "n" ]; then break; fi done if [ "$statuss" = "y" ] then /usr/bin/virt-clone -o $template -n $hostname -f $kvmimg_path$hostname.img if [[ $? -eq 0 ]]; then #修改本地配置文件 sed -i "s/IPADDR=0.0.0.0/IPADDR=$public/g" $eht0_path sed -i "s/IPADDR=0.0.0.0/IPADDR=$private/g" $eht1_path sed -i "s/HOSTNAME=localhost.localdomain/HOSTNAME=$hostname/g" $hostname_path #拷贝配置文件到虚拟机 virt-copy-in -d $hostname $eht0_path $path virt-copy-in -d $hostname $eht1_path $path virt-copy-in -d $hostname $hostname_path $kvm_hostname_path #还原本地配置文件 sed -i "s/IPADDR=$public/IPADDR=0.0.0.0/g" $eht0_path sed -i "s/IPADDR=$private/IPADDR=0.0.0.0/g" $eht1_path sed -i "s/HOSTNAME=$hostname/HOSTNAME=localhost.localdomain/g" $hostname_path #启动虚拟机 virsh start $hostname else echo "克隆虚拟机($hostname)失败!"; fi else echo "退出!" fi
# /data/bin/kvm.sh Enter the host name:web04 Enter the public IP number:192.168.2.219 Enter the private IP number:10.0.2.7 Confirm the virtual machine name(web04) and public IP(192.168.2.219) and private IP(10.0.2.7)[y or n]:y WARNING Setting the graphics device port to autoport, in order to avoid conflicting. Allocating 'web04.img' | 40 GB 00:00:32 Clone 'web04' created successfully. Domain web04 started
查看创建的虚拟机
# ssh 192.168.2.219 The authenticity of host '192.168.2.219 (192.168.2.219)' can't be established. RSA key fingerprint is d4:7a:0d:fa:c4:d3:b7:24:e4:4f:ac:9c:b5:c1:a2:e2. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.2.219' (RSA) to the list of known hosts. [email protected]'s password: Last login: Sat Aug 6 09:39:19 2016 from 192.168.1.75 [root@web04 ~]# ifconfig eth0 Link encap:Ethernet HWaddr 52:54:00:E0:A8:42 inet addr:192.168.2.219 Bcast:192.168.2.255 Mask:255.255.255.0 inet6 addr: fe80::5054:ff:fee0:a842/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:1189 errors:0 dropped:0 overruns:0 frame:0 TX packets:83 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:63407 (61.9 KiB) TX bytes:13360 (13.0 KiB) Interrupt:11 eth1 Link encap:Ethernet HWaddr 52:54:00:DB:D6:31 inet addr:10.0.2.7 Bcast:10.255.255.255 Mask:255.0.0.0 inet6 addr: fe80::5054:ff:fedb:d631/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:12 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 b) TX bytes:720 (720.0 b) Interrupt:11 Base address:0x2000 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:12 errors:0 dropped:0 overruns:0 frame:0 TX packets:12 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:944 (944.0 b) TX bytes:944 (944.0 b) [root@web04 ~]# ping www.baidu.com PING www.a.shifen.com (61.135.169.121) 56(84) bytes of data. 64 bytes from 61.135.169.121: icmp_seq=1 ttl=53 time=37.3 ms 64 bytes from 61.135.169.121: icmp_seq=2 ttl=53 time=37.3 ms 64 bytes from 61.135.169.121: icmp_seq=3 ttl=53 time=37.6 ms 64 bytes from 61.135.169.121: icmp_seq=4 ttl=53 time=37.6 ms # ping 10.0.2.1 PING 10.0.2.1 (10.0.2.1) 56(84) bytes of data. 64 bytes from 10.0.2.1: icmp_seq=1 ttl=64 time=0.149 ms 64 bytes from 10.0.2.1: icmp_seq=2 ttl=64 time=0.337 ms 64 bytes from 10.0.2.1: icmp_seq=3 ttl=64 time=0.336 ms 64 bytes from 10.0.2.1: icmp_seq=4 ttl=64 time=0.312 ms