Centos作为host主机部署kvm虚拟化平台

Centos作为host主机部署kvm虚拟化平台

1. 准备工作

  • 关闭宿主机的防火墙,下面为centos7系统
    firewall-cmd --state 查看防火墙工作状态
    systemctl stop firewalld.service 关闭当前防火墙,重启无效
    systemctl disable firewalld.service 关闭防火墙开机自启动
  • 确认机器CPU支持虚拟化
grep -E '(vmx|svm)' /proc/cpuinfo

2. 安装kvm相关的工具

yum install -y qemu-kvm qemu-kvm-tools libvirt

3. 启动 libvirtd

[root@bogon th]# systemctl start libvirtd
[root@bogon th]# systemctl enable libvirtd

启动后,系统自动启动一个dnsmasq的程序

[root@bogon th]# ps aux | grep dns
nobody     9972  0.0  0.0  53884  1132 ?        S    08:43   0:00 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshelper
root       9975  0.0  0.0  53856   372 ?        S    08:43   0:00 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshelper

4. 创建虚拟机硬盘

qemu-img

[root@bogon opt]# qemu-img create -f raw /opt/centos7_x86_64.raw 20G
Formatting '/opt/centos7_x86_64.raw', fmt=raw size=21474836480 
[root@bogon opt]# ls -lh
总用量 0
-rw-r--r--. 1 root root 20G 9月  10 08:59 centos7_x86_64.raw

5. 准备一个虚拟机的镜像文件

/opt/CentOS-7-x86_64-DVD-1810.iso

6. 安装 virt-install 工具

yum install -y virt-install

7. 创建虚拟机

virt-install --virt-type kvm --name centos7_t1 --ram 4096 --cdrom /opt/CentOS-7-x86_64-DVD-1810.iso --disk path=/opt/centos7_x86_64.raw --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole

8. vnc连接虚拟机

查看vnc的端口映射,默认5900

netstat -ntlp

通过vnc connect直接连接宿主机既可,安装对应的系统;

9. 查看虚机的状态

[root@bogon opt]# virsh list --all
 Id    名称                         状态
----------------------------------------------------
 1     centos7_t1                     running

10. 启动虚机

virsh start centos7_t1

11. 查看宿主机中运行虚机的线程

[root@bogon opt]# ps -aux | grep kvm
root       8514  0.0  0.0      0     0 ?        S<   08:43   0:00 [kvm-irqfd-clean]
qemu      12791 71.0 90.0 5524644 3475676 ?     Sl   09:10  10:43 /usr/libexec/qemu-kvm -name centos7_t1 -S -machine pc-i440fx-rhel7.0.0,accel=kvm,usb=off,dump-guest-core=off -cpu Broadwell-IBRS,-hle,-rtm -m 4096 -realtime mlock=off -smp 1,sockets=1,cores=1,threads=1 -uuid 7e90fb7e-6e00-48b3-ba89-c4b102333899 -no-user-config -nodefaults -chardev socket,id=charmonitor,path=/var/lib/libvirt/qemu/domain-1-centos7_t1/monitor.sock,server,nowait -mon chardev=charmonitor,id=monitor,mode=control -rtc base=utc,driftfix=slew -global kvm-pit.lost_tick_policy=delay -no-hpet -no-reboot -global PIIX4_PM.disable_s3=1 -global PIIX4_PM.disable_s4=1 -boot strict=on -device ich9-usb-ehci1,id=usb,bus=pci.0,addr=0x4.0x7 -device ich9-usb-uhci1,masterbus=usb.0,firstport=0,bus=pci.0,multifunction=on,addr=0x4 -device ich9-usb-uhci2,masterbus=usb.0,firstport=2,bus=pci.0,addr=0x4.0x1 -device ich9-usb-uhci3,masterbus=usb.0,firstport=4,bus=pci.0,addr=0x4.0x2 -device virtio-serial-pci,id=virtio-serial0,bus=pci.0,addr=0x5 -drive file=/opt/centos7_x86_64.raw,format=raw,if=none,id=drive-virtio-disk0 -device virtio-blk-pci,scsi=off,bus=pci.0,addr=0x6,drive=drive-virtio-disk0,id=virtio-disk0,bootindex=2 -drive file=/opt/CentOS-7-x86_64-DVD-1810.iso,format=raw,if=none,id=drive-ide0-0-0,readonly=on -device ide-cd,bus=ide.0,unit=0,drive=drive-ide0-0-0,id=ide0-0-0,bootindex=1 -netdev tap,fd=26,id=hostnet0,vhost=on,vhostfd=28 -device virtio-net-pci,netdev=hostnet0,id=net0,mac=52:54:00:01:57:bd,bus=pci.0,addr=0x3 -chardev pty,id=charserial0 -device isa-serial,chardev=charserial0,id=serial0 -chardev socket,id=charchannel0,path=/var/lib/libvirt/qemu/channel/target/domain-1-centos7_t1/org.qemu.guest_agent.0,server,nowait -device virtserialport,bus=virtio-serial0.0,nr=1,chardev=charchannel0,id=channel0,name=org.qemu.guest_agent.0 -device usb-tablet,id=input0,bus=usb.0,port=1 -vnc 0.0.0.0:0 -vga cirrus -device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x7 -msg timestamp=on
root      12830  0.0  0.0      0     0 ?        S    09:10   0:00 [kvm-pit/12791]

12. libvirtd停止对虚拟机的影响,不影响运行,但是virsh无法管理虚拟机

[root@bogon th]# systemctl stop libvirtd

你可能感兴趣的:(#,KVM,kvm,虚拟机,centos,虚拟化,virsh)