目录
一 宿主机环境准备
1. CPU开启虚拟化
2. 验证是否开启虚拟化
3. 安装KVM工具包:
二 创建bridge网络虚拟机
1. 创建br0桥接网卡
2. 上传镜像并安装虚拟机:
3. 验证虚拟机桥接网络通信:
三 虚拟机管理命令virsh:
四 快速创建虚拟机
一 宿主机环境准备
KVM需要宿主机CPU必须支持虚拟化功能,因此如果是在vmware workstation上使用虚拟机做宿主机,那么必须要在虚拟机配置界面的处理器选项中开启虚拟机化功能。
1. CPU开启虚拟化
2. 验证是否开启虚拟化
[root@s1 ~]# grep -E "vmx|svm" /proc/cpuinfo | wc -l
3. 安装KVM工具包:
Ubuntu 18.04:
https://ubuntu.com/server/docs/virtualization-libvirt
# apt install qemu-kvm virt-manager libvirt-daemon-system
# kvm-ok #验证是否支持kvm
INFO: /dev/kvm exists
KVM acceleration can be used
CentOS 7.X:
# yum install qemu-kvm qemu-kvm-tools libvirt libvirt-client virt-manager virt-install
# systemctl start libvirtd
# systemctl enable libvirtd
# ifconfig virbr0 #验证是否生成NAT网卡virbr0
virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:89:c5:75 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
# grep "192.168.122.1" /etc/libvirt/ -R
/etc/libvirt/qemu/networks/autostart/default.xml: <ip address='192.168.122.1'
netmask='255.255.255.0'>
/etc/libvirt/qemu/networks/default.xml: <ip address='192.168.122.1'
netmask='255.255.255.0'>
二 创建bridge网络虚拟机
桥接网络可以让运行在宿主机上的虚拟机使用和宿主机同网段IP,并且可以从外部直接访问到虚拟机,目前企业中大部分场景都使用桥接网络。
vim /etc/libvirt/qemu/linux49-nginx-web1.xml 虚拟机元数据信息
1. 创建br0桥接网卡
Ubuntu 18.04创建桥接网卡:
# cat /etc/netplan/01-netcfg.yaml
# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: no
dhcp6: no
bridges:
br0:
dhcp4: no
dhcp6: no
addresses: [172.18.0.20/16]
gateway4: 172.18.0.1
nameservers:
addresses: [223.6.6.6]
interfaces:
- eth0
Centos 7.X创建桥接网卡:
[root@s1 src]
# pwd
/etc/sysconfig/network-scripts
# vim ifcfg-br0
TYPE=Bridge
BOOTPROTO=static
NAME=br0
DEVICE=br0
ONBOOT=yes
IPADDR=172.18.200.101
NETMASK=255.255.0.0
GATEWAY=172.18.0.1
DNS1=172.18.0.1
# vim ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=static
NAME=eth0
DEVICE=eth0
ONBOOT=yes
BRIDGE=br0
# systemctl restart network
2. 上传镜像并安装虚拟机:
# ll /usr/local/src/CentOS-7-x86_64-Minimal-1908.iso
-rw-r--r-- 1 libvirt-qemu kvm 987758592 Nov 20 12:03 /usr/local/src/CentOS-7-x86_64-Minimal-1908.iso
#创建系统磁盘:
# qemu-img create -f qcow2 /var/lib/libvirt/images/centos7-bridge.qcow2 10G
Formatting '/var/lib/libvirt/images/centos7-bridge.qcow2', fmt=qcow2 size=10737418240 cluster_size=65536 lazy_refcounts=off refcount_bits=16
#创建基于桥接网络的虚拟机
# virt-install --virt-type kvm --name centos7-bridge --ram 1024 --vcpus 2 --cdrom=/usr/local/src/CentOS-7-x86_64-Minimal-1908.iso
--disk path=/var/lib/libvirt/images/centos7-bridge.qcow2 --network bridge=br0 --graphics vnc,listen=0.0.0.0 --noautoconsole
注:iso文件不能放到root家目录下
一定要开启cpu虚拟化,如果是物理宿主机,在bios里开启
运行virt-manager报错(can‘t open display),要打开CRT的x11,重连
启动libvirt服务 systemctl start libvirtd
systemctl enable libvirtd
3. 验证虚拟机桥接网络通信:
# ssh 172.18.144.43
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.18.144.43' (ECDSA) to the list of known hosts.
root@172.18.144.43's password:
Last login: Fri Jun 14 18:38:05 2019
[root@localhost ~]# yum install pciutils #查看PCI设备
[root@localhost ~]# lspci
...
00:05.0 Communication controller: Red Hat, Inc. Virtio console #virtio为半虚拟化驱动
...
三 虚拟机管理命令virsh:
# virsh list #列出当前开机的
# virsh list --inactive #列出关闭的虚拟机
# virsh list --all #列出所有
# virsh shutdown CentOS-7-x86_64 #正常关机
# virsh start CentOS-7-x86_64 #正常开机
# virsh destroy centos7 #强制停止/关机
# virsh undefine Win_2008_r2-x86_64 #强制删除
# virsh autostart centos7 #设置当前虚拟机开机自启动
四 快速创建虚拟机
通过copy虚拟机磁盘文件,可以快速创建虚拟机
[root@s2 ~]# cd /var/lib/libvirt/images/
# 复制已经配置好的kvm磁盘文件做模板,模板最好已安装必要的服务并做好相关配置,以此为模板创建的kvm就无需再重复这些操作。
# cp centos1.qcow2 centos1-bak.qcow2
# cp centos1-bak.qcow2 centos2.qcow2
# virt-install --virt-type kvm \
--name centos2 \
--ram 1024 \
--vcpus 2 \
--cdrom=/usr/local/src/CentOS-7-x86_64-Minimal-1908.iso \
--disk path=/var/lib/libvirt/images/centos2.qcow2 \
--network bridge=br0 \
--graphics vnc,listen=0.0.0.0 \
--noautoconsole #虚拟机不要开始安装过程,即虚拟机启动后立即强制关机重新启动即可进入到虚拟机系统
或 设置从hd启动
# virt-install --virt-type kvm \
--name centos2 \
--ram 1024 \
--vcpus 2 \
--disk path=/var/lib/libvirt/images/centos2.qcow2 \
--network bridge=br0 \
--graphics vnc,listen=0.0.0.0 \
--noautoconsole \
--boot hd