Linux虚拟化技术 (KVM)

1,查看是否支持虚拟化:

[root@localhost ~]# grep -P "vmx|svm" /proc/cpuinfo
flags        : ... ... vmx
[root@localhost ~]# lsmod |grep kvm
kvm_intel             174841  6 
kvm                   578518  1 kvm_intel
irqbypass              13503  1 kvm

2,安装虚拟化包(libvirtd):

[root@localhost ~]# yum install -y qemu-kvm                                    libvirt-daemon                                    libvirt-daemon-driver-qemu                                    libvirt-client
[root@localhost ~]# systemctl enable --now libvirtd
[root@localhost ~]# virsh version

3,创建虚拟交换机

  A. 创建配置文件 /etc/libvirt/qemu/networks/vbr.xml

[root@localhost ~]# vim /etc/libvirt/qemu/networks/vbr.xml
<network>
  <name>vbr</name>
  <forward mode=nat/>
  <bridge name=vbr stp=on delay=0/>
  <ip address=192.168.100.254 netmask=255.255.255.0>
    <dhcp>
      <range start=192.168.100.100 end=192.168.100.200/>
    </dhcp>
  </ip>
</network>

  B. 创建虚拟交换机

[root@localhost ~]# cd /etc/libvirt/qemu/networks/
[root@localhost ~]# virsh net-define vbr.xml
[root@localhost ~]# virsh net-start vbr
[root@localhost ~]# virsh net-autostart vbr
[root@localhost ~]# ifconfig # 查看验证

4,创建虚拟机磁盘

  A. 官网下载并上传 cirros.qcow2 到主机

[root@localhost ~]# cp cirros.qcow2 /var/lib/libvirt/images/
[root@localhost ~]# cd /var/lib/libvirt/images/通过 qemu-img 创建虚拟机磁盘

  B. 通过 qemu-img 创建虚拟机磁盘

[root@localhost ~]# qemu-img create -f qcow2 -b cirros.qcow2 vmhost.img 30G
[root@localhost ~]# qemu-img info vmhost.img #查看信息

5,创建虚拟机

  A. 虚拟机配置文件从https://libvirt.org/format.html拷贝node_base.xml 到主机中,拷贝 node_base.xml 到 /etc/libvirt/qemu/虚拟机名字.xml

[root@localhost ~]# cp node_base.xml /etc/libvirt/qemu/vmhost.xml
[root@localhost ~]# vim /etc/libvirt/qemu/vmhost.xml
<name>vmhost</name>
<memory unit=KB>1024000</memory>
<currentMemory unit=KB>1024000</currentMemory>
<vcpu placement=static>2</vcpu>
<source file=/var/lib/libvirt/images/vmhost.img/>

  B. 创建虚拟机

[root@localhost ~]# virsh list
[root@localhost ~]# virsh define /etc/libvirt/qemu/vmhost.xml
[root@localhost ~]# virsh start vmhost
[root@localhost ~]# virsh console vmhost # 两次回车
退出使用 ctrl + ]

 

原理理解:

 

Linux虚拟化技术 (KVM)

上一篇:手把手教你玩转nginx负载均衡(二)----安装虚拟机操作系统


下一篇:Linux 如何配置 Nginx