当前位置:网站首页>Quickly build KVM virtual machine on # yyds dry goods inventory # physical machine

Quickly build KVM virtual machine on # yyds dry goods inventory # physical machine

2022-06-24 23:27:00 Feng, crazy

1. Download the virtual machine image file

Find the image file by yourself (cdsn Downloading usually requires points ), Or contact me ( I use it centos Mirror image , The kernel version is 2015 Years old , You need to upgrade later )

      
      
wget filepath // Download the file
  • 1.

#yyds Dry inventory # Fast build on the physical machine kvm virtual machine _kvm

2. Enable virtualization configuration for physical machine

      
      
grep vmx /proc/cpuinfo
  • 1.

If there is vmx Information output , It means that we support VT; If there is no output , Explain your cpu不 Support , Will not be available KVM virtual machine

      
      
lsmod | grep kvm
  • 1.

If there is no output information, you need to load kvm

#yyds Dry inventory # Fast build on the physical machine kvm virtual machine _kvm_02

      
      
// Unloading module
modprobe -r kvm_intel // You don't have to execute
// Reload module
modprobe kvm
modprobe kvm_intel // Reload module
lsmod | grep kvm
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

#yyds Dry inventory # Fast build on the physical machine kvm virtual machine _centos_03

3. Nested virtualization ( If not, please skip )

      
      
cat /sys/module/kvm_intel/parameters/nested
Y
modprobe kvm-intel nested=1
  • 1.
  • 2.
  • 3.

4. to update yum The warehouse is Alibaba cloud

Easy to install packages are the latest

      
      
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
&&
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
  • 1.
  • 2.
  • 3.

5. loading virsh Required plug-ins

      
      
yum install -y qemu-kvm qemu-kvm-tools
&&yum install -y libvirt
&&yum install -y virt-install
&&yum install -y python-urllib3
&& yum install libguestfs-tools
//libguestfs-tool yes virt Related packages
// View the virtual machine installation log
cat /var/log/libvirt/qemu/centos7_15.log
virsh list --all // Error, please restart libvirtd
systemctl status libvirtd
// View the service status
systemctl start libvirtd
// Start the service
systemctl enable libvirtd
// Boot up
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

6. Set up virtual machine network

      
      
brctl addbr br0
//192.168.66.254 It's the gateway , The network segment can be selected by itself 192.168.66.0/24
ifconfig br0 192.168.66.254/24(yum install net-tools)
// Visit the Internet enp24s0f1 It is a physical network card
iptables -t nat -A POSTROUTING -s 192.168.66.0/24 -o enp24s0f1 -j MASQUERADE
//192.168.103.50 It's a physical machine ip, The following is remote forwarding 192.168.66.1 Is the first virtual machine
iptables -t nat -A PREROUTING -d 192.168.103.50 -p tcp --dport 53301 -j DNAT --to-destination 192.168.66.1:22
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

7. establish kvm

frequently-used kvm The format is raw Follow qcow2

      
      
qemu-img create -f qcow2 /home/vm/vm1.qcow2 200G
virt-install \
--virt-type kvm \
--name vm1 \
--vcpus 16 \
--memory 32768 \
--cdrom=/home/vm/CentOS-7-x86_64-Minimal-1511.iso \
--disk path=/home/vm/vm1.qcow2 \
--network bridge=br0 \
--graphics vnc,listen=0.0.0.0 \
--os-type=linux \
--os-variant=rhel7
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

installation is complete  

      
      
virsh list // View virtual machine
virsh vncdisplay vm1 // see vnc port vnc Log in to use VNC Server Fill in this column ip:port
  • 1.
  • 2.

#yyds Dry inventory # Fast build on the physical machine kvm virtual machine _kvm_04

Need to download one vnc Client to connect vm1 Deploy the operating system ( The first installation requires vnc  The latter is used directly virt-clone machine )

Just follow VMware The same steps as installing the virtual machine , But faster , Because there is no graphical interface

#yyds Dry inventory # Fast build on the physical machine kvm virtual machine _kvm_05

#yyds Dry inventory # Fast build on the physical machine kvm virtual machine _kvm_06

frequently-used virsh command :

      
      
virsh list
virsh start vm1
virsh shutdown vm1
virsh edit vm1 // Profile can be modified
virsh vncdisplay vm1
virsh undefine vm1 // Delete with then rm -rf vm1.qcow2
virsh destroy vm1 // closed
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

8. Network card configuration

eth0 Is the name of the network card

      
      
vi /etc/sysconfig/network-scripts/ifcfg-eth0

TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=192.168.66.1
NETMASK=255.255.255.0
GATEWAY=192.168.66.254
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.

9. Kernel upgrade

      
      
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org

rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rpm

yum --enablerepo=elrepo-kernel install kernel-ml -y

vi /etc/default/grub
# Set up GRUB_DEFAULT=0, intend GRUB The first kernel of the initialization page will be the default kernel
grub2-mkconfig -o /boot/grub2/grub.cfg

reboot // Reselect the first one to boot the kernel
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

10. Clone virtual machine

      
      
virt-clone -o vm1 -n vm2 -f /home/vm/vm2.qcow2
  • 1.

After cloning, you can directly use ssh Sign in , The premise is to shut down the clone machine , Otherwise ip Conflict

      
      
qemu-img convert -f raw -O qcow2 /home/vm/vm1.raw /home/vm/vm1.qcow2
// Two kinds of kvm format conversion qcow2 Fast raw Good storage performance
  • 1.
  • 2.

11. Possible network problems

iptables Command Reference :

      
      
iptables -t nat --line-number -nvL

iptables -t nat -D PREROUTING 1

// Visit the Internet
iptables -t nat -A POSTROUTING -s 192.168.55.0/24 -o enp24s0f1 -j MASQUERADE

//ip route add default via 192.168.63.1

iptables -t nat -A PREROUTING -d 192.168.103.50 -p tcp --dport 53301 -j DNAT --to-destination 192.168.44.1:22
//vnc to open up vnc Access port
iptables -I INPUT -p tcp --dport 5900:5920 -j ACCEPT
// Firewall problem
iptables -I FORWARD -s 192.168.66.0/24 -j ACCEPT//dns Unavailability issues
iptables -I FORWARD -d 192.168.66.0/24 -j ACCEPT// This machine 533 Port login
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
原网站

版权声明
本文为[Feng, crazy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211127044462.html