很多朋友可能有这个需求,服务器没有那么多,但是想做集群怎么办,windos上还可以安装VMware,但是一到服务器上就茫然了。现在就给你推荐一篇kvm使用,完全解决你的资源问题。
- 确认CPU已经支持虚拟化,intel的CPU虚拟化技术交vmx,AMD的CPU交svm:
grep -E "vmx|svm" /proc/cpuinfo
- 安装kvm平台及工具包
yum install qemu-kvm qemu-kvm-tools libvirt virt-manager virt-install
- 启动libvirtsd并设置为开机启动,librirtd会创建一个桥接的网卡virbr0而且IP地址是192.168.122.1:
systemctl start libvirtd
systemctl enable libvirtd
ifconfig virbr0
- 查看为虚拟机分配ip的服务
ps -aux | grep dns
- 创建磁盘
cd /home
qemu-img create -f raw test1.raw 50G
- 开始安装
virt-install --virt-type kvm --name CentOS-7-x86_64 --ram 1024 --cdrom=/opt/CentOS-7-x86_64-DVD-1511.iso --disk path=/home/test1.raw --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole
virt-install --virt-type kvm --name openstack-middleware1 --ram 4096 --vcpus 4 --cdrom=/usr/local/src/CentOS-7-x86_64-Minimal-1511.iso --disk path=/home/test1.raw --network bridge=br0 --graphics vnc,listen=0.0.0.0 --noautoconsole
qemu-img create -f qcow2 /var/lib/libvirt/images/Windows-2008_r2-x86_64.qcow2 200G
virt-install --virt-type kvm --name Win_2008_r2-x86_64 --ram 3072 --vcpus=2 --os-type=windows --cdrom=/usr/local/src/windows_server_2008_r2.iso --disk path=/var/lib/libvirt/images/Windows-2008_r2-x86_64.qcow2,format=qcow2,bus=virtio --disk path=/usr/local/src/virtio-win-0.1.141_amd64.vfd,device=floppy --network bridge=br0,model=virtio --graphics vnc,listen=0.0.0.0 --noautoconsole
-
virt-install --help命令可自行查看
-
通过vnc客户端连接到安装界面,kvm虚拟机的默认端口从5900开始
-
查看正在运行的虚拟机
virsh list --all
virsh list --help
virsh shutdown test1
virsh start test1
- 配置文件路径
cd /etc/libvirt/qemu/
ls
- 配置说明
cat test1.xml
<domain type='kvm'>
<name>test1</name>
<uuid>447c99b2-932e-49ae-b37d-4ed0ff3618cd</uuid>
<memory unit='KiB'>2097152</memory>
<currentMemory unit='KiB'>2097152</currentMemory>
<vcpu placement='static'>1</vcpu> 1个cpu并且是静态分配
<os>
<type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
<boot dev='hd'/>
</os>
<features>
<acpi/>
<apic/>
</features>
<cpu mode='custom' match='exact' check='partial'>
<model fallback='allow'>Haswell-noTSX</model>
</cpu>
<clock offset='utc'>
<timer name='rtc' tickpolicy='catchup'/>
<timer name='pit' tickpolicy='delay'/>
<timer name='hpet' present='no'/>
</clock>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>destroy</on_crash>
<pm>
<suspend-to-mem enabled='no'/>
<suspend-to-disk enabled='no'/>
</pm>
<devices>
<emulator>/usr/libexec/qemu-kvm</emulator>
<disk type='file' device='disk'>
<driver name='qemu' type='raw'/>
<source file='/home/test1.raw'/>
<target dev='vda' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
</disk>
<disk type='file' device='cdrom'>
<driver name='qemu' type='raw'/>
<target dev='hda' bus='ide'/>
<readonly/>
<address type='drive' controller='0' bus='0' target='0' unit='0'/>
</disk>
<controller type='usb' index='0' model='ich9-ehci1'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x7'/>
</controller>
<controller type='usb' index='0' model='ich9-uhci1'>
<master startport='0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0' multifunction='on'/>
</controller>
<controller type='usb' index='0' model='ich9-uhci2'>
<master startport='2'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x1'/>
</controller>
<controller type='usb' index='0' model='ich9-uhci3'>
<master startport='4'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x2'/>
</controller>
<controller type='pci' index='0' model='pci-root'/>
<controller type='ide' index='0'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
</controller>
<controller type='virtio-serial' index='0'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
</controller>
<interface type='network'>
<mac address='52:54:00:8b:62:b9'/>
<source network='default'/>
<model type='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
<serial type='pty'>
<target type='isa-serial' port='0'>
<model name='isa-serial'/>
</target>
</serial>
<console type='pty'>
<target type='serial' port='0'/>
</console>
<channel type='unix'>
<target type='virtio' name='org.qemu.guest_agent.0'/>
<address type='virtio-serial' controller='0' bus='0' port='1'/>
</channel>
<input type='tablet' bus='usb'>
<address type='usb' bus='0' port='1'/>
</input>
<input type='mouse' bus='ps2'/>
<input type='keyboard' bus='ps2'/>
<graphics type='vnc' port='-1' autoport='yes' listen='0.0.0.0'>
<listen type='address' address='0.0.0.0'/>
</graphics>
<video>
<model type='cirrus' vram='16384' heads='1' primary='yes'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
</video>
<memballoon model='virtio'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
</memballoon>
<rng model='virtio'>
<backend model='random'>/dev/urandom</backend>
<address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>
</rng>
</devices>
</domain>
- dumpxml命令,导出虚拟机的xml文件
virsh dumpxml test1 > test1.bak.xm
- undefine:彻底摧毁一个虚拟机
virsh undefine test1
- define恢复
virsh define /home/test1.xml.bak
virsh list --all
- 修改虚拟机的xml描述文件
virsh edit test1
- 重启虚拟机验证CPU数量
- 动态修改CPU数量
virsh setvcpus test1 4 –live
- 查看新添加的CPU是否在线,即是否处于工作状态
cat /sys/devices/system/cpu/cpu3/online
显示1
- 减小内存,如果创建虚拟机的时候内存是固定的,那么只能减少内存而不能动态添加内存
virsh qemu-monitor-command test1 --hmp --cmd info balloon
virsh qemu-monitor-command test1 --hmp --cmd balloon 4069
virsh qemu-monitor-command test1 --hmp --cmd info balloon
virsh qemu-monitor-command test1 --hmp --cmd balloon 512
virsh qemu-monitor-command test1 --hmp --cmd info balloon
- 创建br0网桥,制作桥接模式
默认是使用的nat网络,会成为虚拟机的瓶颈,因此需要创建一个桥接网卡并把虚拟机桥接至网卡,然后将虚拟机桥接一下即可使用物理网络
brctl addbr br0
brctl addif br0 enp2s0
ip addr del dev enp2s0 192.168.1.79/24
ifconfig br0 192.168.1.79/24 up
route add default gw 192.168.1.1
ping www.baidu.com
- 通过配置文件文件创建桥接网卡
cd /etc/sysconfig/network-scripts/
cp ifcfg-enp2s0 ifcfg-br0
cat ifcfg-enp2s0
BOOTPROTO=static
DEVICE=enp2s0
ONBOOT=yes
BRIDGE=br0
NM_CONTROLLED=no
cat ifcfg-br0
TYPE=Bridge
BOOTPROTO=static
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
NAME=br0
DEVICE=br0
ONBOOT=yes
IPADDR=192.168.1.79
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=114.114.114.114
- 最后重启服务器,验证重启服务器之后br0网卡生效
- 修改虚拟机配置文件
virsh edit test1
<interface type='bridge'>
<mac address='52:54:00:8b:62:b9'/>
<source bridge='br0'/>
<model type='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
- 重启虚拟机
virsh shutdown test1
virsh start test1
- 磁盘格式转换,使用qemu-img工具实现,如果机器上没有,可以通过rpm或yum进行安装,包名为qemu-img
qemu-img convert -f raw centos.img -O qcow2 centos.qcow2
qemu-img convert -O qcow2 my.raw myqow.qcow
qemu-img convert -f vmdk -O qcow2 xxx.vmdk xxx.img