CentOS版本:
[root@bogon ~]# cat /etc/centos-release
CentOS Linux release 7.3.1611 (Core)
安装KVM
- 先验证CPU是否支持KVM;如果结果中有vmx(Intel)或svm(AMD)字样,就说明CPU的支持的。运行后没有显示,那么你的处理器不支持硬件虚拟化.
注意: 您可能需要在BIOS中启用虚拟化支持
[root@bogon ~]# egrep '(vmx|svm)' /proc/cpuinfo
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc ape
rfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm ida arat epb pln pts dtherm tpr_shadow vnmi flexpri
ority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc ape
rfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm ida arat epb pln pts dtherm tpr_shadow vnmi flexpri
ority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc ape
rfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm ida arat epb pln pts dtherm tpr_shadow vnmi flexpri
ority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc ape
rfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm ida arat epb pln pts dtherm tpr_shadow vnmi flexpri
ority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt
- 关闭SELinux,将 /etc/sysconfig/selinux 中的 SELinux=enforcing 修改为SELinux=disabled,并重启使设置生效
[root@bogon ~]# vim /etc/sysconfig/selinux
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
[root@localhost ~]# reboot
#重启之后
[root@localhost ~]# /usr/sbin/sestatus ##SELinux status参数为disabled即为已关闭状态
SELinux status: disabled
[root@localhost ~]# getenforce #selinux状态
Disabled
- 安装KVM及其依赖项
[root@bogon ~]# yum install -y qemu-kvm libvirt virt-install bridge-utils
- 验证安装结果
[root@localhost default]# lsmod|grep kvm
kvm_intel 170181 4
kvm 554609 1 kvm_intel
irqbypass 13503 1 kvm
提示:
有时lsmod
查看并没有kvm_intel
,如果输入以下命令:
[root@localhost ~]# modprobe kvm_intel
could not insert 'kvm_intel': Operation not supported
[root@localhost ~]# dmesg |grep kvm
[ 9.161622] kvm: disabled by bios
[ 9.170352] kvm: disabled by bios
[1233191.671294] kvm: disabled by bios
[1233504.807593] kvm: disabled by bios
[1315137.732973] kvm: disabled by bios
很明了,BIOS里把 virtualization禁掉了.Enable
之后,保存重启,重新加载即可.
- kvm设置其开机自动启动
#如果 Active: 不是active, 运行systemctl start libvirtd启动
[root@bogon ~]# systemctl status libvirtd
● libvirtd.service - Virtualization daemon
Loaded: loaded (/usr/lib/systemd/system/libvirtd.service; enabled; vendor preset: enabled)
Active: active (running) since 三 2018-08-22 18:19:57 CST; 2s ago
Docs: man:libvirtd(8)
https://libvirt.org
Main PID: 23406 (libvirtd)
Memory: 37.7M
CGroup: /system.slice/libvirtd.service
├─23043 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --...
├─23044 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --...
└─23406 /usr/sbin/libvirtd
8月 22 18:19:56 bogon systemd[1]: Starting Virtualization daemon...
8月 22 18:19:57 bogon systemd[1]: Started Virtualization daemon.
8月 22 18:19:57 bogon dnsmasq[23043]: read /etc/hosts - 3 addresses
8月 22 18:19:57 bogon dnsmasq[23043]: read /var/lib/libvirt/dnsmasq/default.addnhosts - 0 addresses
8月 22 18:19:57 bogon dnsmasq-dhcp[23043]: read /var/lib/libvirt/dnsmasq/default.hostsfile
8月 22 18:19:57 bogon libvirtd[23406]: 2018-08-22 10:19:57.143+0000: 23422: info : libvirt version:...org)
8月 22 18:19:57 bogon libvirtd[23406]: 2018-08-22 10:19:57.143+0000: 23422: info : hostname: bogon
8月 22 18:19:57 bogon libvirtd[23406]: 2018-08-22 10:19:57.143+0000: 23422: warning : virQEMUCapsIn...info
8月 22 18:19:57 bogon libvirtd[23406]: 2018-08-22 10:19:57.198+0000: 23422: warning : virLXCDriverC...info
Hint: Some lines were ellipsized, use -l to show in full.
#开机自动启动
[root@bogon ~]# systemctl is-enabled libvirtd
disabled
[root@bogon ~]# systemctl enable libvirtd
Created symlink from /etc/systemd/system/multi-user.target.wants/libvirtd.service to /usr/lib/systemd/system/libvirtd.service.
Created symlink from /etc/systemd/system/sockets.target.wants/virtlockd.socket to /usr/lib/systemd/system/virtlockd.socket.
Created symlink from /etc/systemd/system/sockets.target.wants/virtlogd.socket to /usr/lib/systemd/system/virtlogd.socket.
[root@bogon ~]# systemctl is-enabled libvirtd
enabled
安装虚拟机
- 安装镜像,可根据需要点击 centOS下载地址 下载自己所需要的版本
[root@bogon ~]# cd /tmp/
[root@bogon tmp]# curd -O http://mirror.lzu.edu.cn/centos/7/isos/x86_64/CentOS-7-x86_64-DVD-1804.iso
- 创建vm目录并创建虚拟机
[root@bogon ~]# mkdir –p /home/yx/kvm
[root@bogon ~]# cd /home/yx/
[root@bogon yx]#
# -n, --name: 虚拟机名字
# -r, --ram: 内存大小,MB
# --vcpus: VCPU数目
# --disk: 存储设备,format: 磁盘映像格式,如raw、qcow2、vmdk,
# --os-type: 操作系统类型,如linux、unix或windows等
# --network bridge=virbr0: 连接至virbr0的桥接设备
# -c,--cdrom /tmp/CentOS-7-x86_64-DVD-1804.iso: 光盘 (镜像文件)路径
# --vnc --vncport=5901 --vnclisten=0.0.0.0: 使用vnc,监听vnc5902 图形安装界面
[root@bogon yx]# virt-install -n virt1 -r 2048 --vcpus=2 --disk kvm/virt1.img,format=qcow2,size=20 --os-type=linux --network bridge=virbr0 --cdrom /tmp/CentOS-7-x86_64-DVD-1804.iso --vnc --vncport=5902 --vnclisten=0.0.0.0
- 打开防火墙端口或关闭防火墙,
[root@bogon yx]# firewall-cmd --zone=public --add-port=5910/tcp --permanent
[root@bogon yx]# firewall-cmd –-reload
#或停止(关闭)firewalld
[root@bogon yx]# systemctl stop(disable) firewalld
- 使用VNC Viewer可
virt-manager
命令连接到虚拟机,进入CentOS安装,需要说明一下的是
如果不设置用户,则默认是root,安装完毕之后,reboot
重启
- 查看
#如果virt1的State不为running状态,运行virsh start virt1使之启动
[root@bogon yx]# virsh list --all
Id Name State
----------------------------------------------------
8 virt1 running
[root@bogon yx]# virsh console virt1
Connected to domain virt1
Escape character is ^]
CentOS Linux 7 (Core)
Kernel 3.10.0-862.el7.x86_64 on an x86_64
bogon login: root
Password:
Last login: Mon Aug 20 21:54:52 from 10.67.12.86
[root@bogon ~]#
[root@bogon ~]# cd /etc/sysconfig/network-scripts
[root@bogon network-scripts]# lltotal 228
-rw-r--r--. 1 root root 278 Aug 14 00:55 ifcfg-ens3
-rw-r--r--. 1 root root 254 Jan 2 2018 ifcfg-lo
lrwxrwxrwx. 1 root root 24 Aug 14 00:25 ifdown -> ../../../usr/sbin/ifdown
-rwxr-xr-x. 1 root root 654 Jan 2 2018 ifdown-bnep
-rwxr-xr-x. 1 root root 6569 Jan 2 2018 ifdown-eth
-rwxr-xr-x. 1 root root 781 Jan 2 2018 ifdown-ippp
-rwxr-xr-x. 1 root root 4540 Jan 2 2018 ifdown-ipv6
lrwxrwxrwx. 1 root root 11 Aug 14 00:25 ifdown-isdn -> ifdown-ippp
-rwxr-xr-x. 1 root root 2102 Jan 2 2018 ifdown-post
-rwxr-xr-x. 1 root root 1068 Jan 2 2018 ifdown-ppp
-rwxr-xr-x. 1 root root 870 Jan 2 2018 ifdown-routes
-rwxr-xr-x. 1 root root 1456 Jan 2 2018 ifdown-sit
-rwxr-xr-x. 1 root root 1621 Mar 17 2017 ifdown-Team
-rwxr-xr-x. 1 root root 1556 Mar 17 2017 ifdown-TeamPort
-rwxr-xr-x. 1 root root 1462 Jan 2 2018 ifdown-tunnel
lrwxrwxrwx. 1 root root 22 Aug 14 00:25 ifup -> ../../../usr/sbin/ifup
-rwxr-xr-x. 1 root root 12415 Jan 2 2018 ifup-aliases
-rwxr-xr-x. 1 root root 910 Jan 2 2018 ifup-bnep
-rwxr-xr-x. 1 root root 13442 Jan 2 2018 ifup-eth
-rwxr-xr-x. 1 root root 12075 Jan 2 2018 ifup-ippp
-rwxr-xr-x. 1 root root 11893 Jan 2 2018 ifup-ipv6
lrwxrwxrwx. 1 root root 9 Aug 14 00:25 ifup-isdn -> ifup-ippp
-rwxr-xr-x. 1 root root 650 Jan 2 2018 ifup-plip
-rwxr-xr-x. 1 root root 1064 Jan 2 2018 ifup-plusb
-rwxr-xr-x. 1 root root 4981 Jan 2 2018 ifup-post
-rwxr-xr-x. 1 root root 4154 Jan 2 2018 ifup-ppp
-rwxr-xr-x. 1 root root 2001 Jan 2 2018 ifup-routes
-rwxr-xr-x. 1 root root 3303 Jan 2 2018 ifup-sit
-rwxr-xr-x. 1 root root 1755 Mar 17 2017 ifup-Team
-rwxr-xr-x. 1 root root 1876 Mar 17 2017 ifup-TeamPort
-rwxr-xr-x. 1 root root 2711 Jan 2 2018 ifup-tunnel
-rwxr-xr-x. 1 root root 1836 Jan 2 2018 ifup-wireless
-rwxr-xr-x. 1 root root 5419 Jan 2 2018 init.ipv6-global
-rw-r--r--. 1 root root 19948 Jan 2 2018 network-functions
-rw-r--r--. 1 root root 31027 Jan 2 2018 network-functions-ipv6
[root@localhost network-scripts]# vi ifcfg-ens3
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=dhcp
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens3
UUID=f44eee6c-1a1e-4747-aae6-01d73c7e59a5
DEVICE=ens3
ONBOOT=no #发现ONBOOT为no,更改为Yes,保存
[root@bogon network-scripts]# systemctl restart network
[root@bogon network-scripts]# ip addr # 可以看到ip为10.67.12.103
1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens3: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 52:54:00:9e:86:6b brd ff:ff:ff:ff:ff:ff
inet 10.67.12.103/24 brd 10.67.12.255 scope global noprefixroute dynamic ens3
valid_lft 67653sec preferred_lft 67653sec
inet6 fe80::3e8c:4d64:ec26:c3f9/64 scope link noprefixroute
valid_lft forever preferred_lft forever
提示:
若 virsh console virt1
执行卡住,则可以通过VNC Viewer或virt-manager 连接进入虚拟机 执行如下命令并重启即可
[root@localhost ~]# grubby --update-kernel=ALL --args="console=ttyS0"
[root@localhost ~]# reboot
虚拟机常用工具安装
[root@localhost ~]# yum install -y epel-release net-tools vim unzip zip wget ftp lrzsz dstat
java:
[root@localhost ~]# yum install –y java-1.8.0-open.jdk*
tomcat: 不建议yum安装,可自行去官方下载自己所需要的版本
[root@localhost ~]# cd /home/
[root@localhost home]# mkdir tomcat
[root@localhost home]# curl -O http://mirrors.shu.edu.cn/apache/tomcat/tomcat-8/v8.5.33/bin/apache-tomcat-8.5.33.tar.gz
[root@localhost home]# tar zxvf apache-tomcat-8.5.33.tar.gz -C tomcat/
[root@localhost home]# cd tomcat/apache-tomcat-8.5.33/
[root@localhost apache-tomcat-8.5.33]# ll
total 92
drwxr-x---. 2 root root 4096 Aug 20 22:53 bin
drwx------. 3 root root 254 Aug 20 22:39 conf
drwxr-x---. 2 root root 4096 Aug 20 06:44 lib
-rw-r-----. 1 root root 57092 Jun 20 15:53 LICENSE
drwxr-x---. 2 root root 197 Aug 20 22:39 logs
-rw-r-----. 1 root root 1723 Jun 20 15:53 NOTICE
-rw-r-----. 1 root root 7138 Jun 20 15:53 RELEASE-NOTES
-rw-r-----. 1 root root 16246 Jun 20 15:53 RUNNING.txt
drwxr-x---. 2 root root 30 Aug 20 06:44 temp
drwxr-x---. 3 root root 40 Aug 20 22:57 webapps
drwxr-x---. 3 root root 22 Aug 20 22:57 work
[root@localhost tomcat]# ./bin/startup.sh
提示
如果tomcat启动,访问不了,可以停止(关闭)防火墙或开放访问端口
monitorix: 轻量级监控
[root@localhost ~]# yum install -y monitorix
[root@localhost ~]# vim /etc/monitorix/monitorix.conf #修改访问端口
...
enabled = y
host =
port = 1234 #修改为1234
user = nobody
group = nobody
log_file = /var/log/monitorix-httpd
hosts_deny =
hosts_allow =
enabled = n
msg = Monitorix: Restricted access
htpasswd = /var/lib/monitorix/htpasswd
...
[root@localhost ~]# systemctl start monitorix
接下来通过 http://10.67.12.103:1234/monitorix/ 访问监控页面
克隆
本地克隆
- 先关闭虚拟机
[root@localhost kvm]# virsh shutdown virt1
Domain virt1 is being shutdown
[root@localhost kvm]# virsh list --all
Id Name State
----------------------------------------------------
- virt1 shut off
- virt-clone
# -o, --original: 源虚拟机
# -n ,--name: 新建虚拟机名字
# -f, --file: 指定新建的虚拟机磁盘文件
[root@localhost kvm]# virt-clone -o virt1 -n virt2 -f /home/yx/kvm/virt2.img
WARNING Setting the graphics device port to autoport, in order to avoid conflicting.
Allocating 'virt2.img' | 20 GB 00:00:20
Clone 'virt2' created successfully
[root@localhost kvm]# virsh list --all
Id Name State
----------------------------------------------------
- virt1 shut off
- virt2 shut off
[root@localhost kvm]# virsh start virt2
Domain virt2 started
不同物理机之间的克隆
不同物理机之间的的虚拟机克隆,笔者主要采用virt-manager 图形界面进行.
1.拷贝虚拟机配置和文件
[root@localhost kvm]# scp /home/yx/kvm/virt1.img [email protected]:/home/yx/kvm/
[root@localhost kvm]# scp /etc/libvirt/qemu/virt1.xml [email protected]:/etc/libvirt/qemu/
[root@localhost kvm]#
- virt-manager
进入图形界面
File菜单点击Create a new virtual marchine
进入引导安装页面,选择Import existing disk image
,
根据提示操作即可.
注意,Network selection
选项需要根据实际情况选择,否则网络可能不连通.
附:KVM virsh常用命令
... | ... |
---|---|
virsh start xxx | 启动xxx的非活动虚拟机 |
virsh list --all | 虚拟机列表 |
virsh create xxx.xml | 创建虚拟.,创建后,虚拟机立即执行,成为活动主机 |
virsh suspend xxx | 暂停虚拟机 |
virsh resume xxx | 启动暂停的虚拟机 |
virsh shutdown xxx | 关闭虚拟机 |
virsh destroy xxx | 强制关闭虚拟机 |
virsh undefine xxx | 删除虚拟机 |
virsh domiflist xxx | 虚拟网卡 |
virsh dominfo xxx | 显示虚拟机的基本信息 |
virsh domname 2 | 显示id号为2的虚拟机名 |
virsh domid xxx | 显示虚拟机id号 |
virsh domuuid xxx | 显示虚拟机的uuid |
virsh domstate xxx | 显示虚拟机的当前状态 |
virsh dumpxml xxx | 显示虚拟机的当前配置文件 |
virsh console xxx | 控制台登录xxx |