虚拟化介绍
1、虚拟化,是指通过虚拟化技术将一台计算机虚拟为多台逻辑计算机。在一台计算机上同时运行多个逻辑计算机,每个逻辑计算机可运行不同的操作系统,并且应用程序都可以在相互独立的空间内运行而互相不影响,从而显著提高计算机的工作效率。
2、虚拟化层次种类:
(1) 完全虚拟化 — 最流行的虚拟化方法使用名为 hypervisor的一种软件,在虚拟服务器和底层硬件之间建立一个抽象层。 VMware 和微软的VirtualPC 是代表该方法的两个商用产品,而基于核心的虚拟机 (KVM) 是面向 Linux 系统的开源产品hypervisor 可以捕获 CPU 指令,为指令访问硬件控制器和外设充当中介。因而,完全虚拟化技术几乎能让任何一款操作系统不用改动就能安装到虚拟服务器上,而它们不知道自己运行在虚拟化环境下。主要缺点是, hypervisor 给处理器带来开销
(2)准虚拟化 — 完全虚拟化是处理器密集型技术,因为它要求 hypervisor管理各个虚拟服务器,并让它们彼此独立。减轻这种负担的一种方法就是,改动客户端操作系统,让它以为自己运行在虚拟环境下,能够与hypervisor 协同工作。这种方法就叫准虚拟化 (para-virtualization)Xen 是开源准虚拟化技术的一个例子。操作系统作为虚拟服务器在 Xen hypervisor 上运行之前,它必须在核心层面进行某些改变。因此, Xen 适用于 BSD 、 Linux 、 Solaris 及其他开源操作系统,但不适合对像Windows 这些专有的操作系统进行虚拟化处理,因为它们无法改动。准虚拟化技术的优点是性能高。经过准虚拟化处理的服务器可与hypervisor 协同工作,其响应能力几乎不亚于未经过虚拟化处理的服务器。准虚拟化与完全虚拟化相比优点明显,以至于微软和 VMware 都在开发这项技术,以完善各自的产品。
(3)系统虚拟化 — 就操作系统层的虚拟化而言,没有独立的hypervisor 层。相反,主机操作系统本身就负责在多个虚拟服务器之间分配硬件资源,并且让这些服务器彼此独立。一个明显的区别是,如果使用操作系统层虚拟化,所有虚拟服务器必须运行同一操作系统 ( 不过每个实例有各自的应用程序和用户。
账户 ) 。虽然操作系统层虚拟化的灵活性比较差,但本机速度性能比较高。此外,由于架构在所有虚拟服务器上使用单一、标准的操作系统,管理起来比异构环境要容易。
(4)桌面虚拟化 — 服务器虚拟化主要针对服务器而言,而虚拟化最接近用户的还是要算的上桌面虚拟化了,桌面虚拟化主要功能是将分散的桌面环境集中保存并管理起来,包括桌面环境的集中下发,集中更新,集中管理。桌面虚拟化使得桌面管理变得简单,不用每台终端单独进行维护,每台终端进行更新。终端数据可以集中存储在中心机房里,安全性相对传统桌面应用要高很多。桌面虚拟化可以使得一个人拥有多个桌面环境,也可以把一个桌面环境供多人使用。
3、虚拟化架构分类
(1)1型虚拟化
Hypervisor 直接安装在物理机上,多个虚拟机在 Hypervisor 上运行。Hypervisor 实现方式一般是一个特殊定制的 Linux 系统。Xen 和 VMWare 的 ESXi 都属于这个类型。
(2)2型虚拟化
物理机上首先安装常规的操作系统,比如 Redhat、Ubuntu 和 Windows。Hypervisor 作为 OS 上的一个程序模块运行,并对管理虚拟机进行管理。KVM、VirtualBox 和 VMWare Workstation 都属于这个类型。
2 、KVM — 全称是基于内核的虚拟机(Kernel-based Virtual Machine)(vm guest ———— 虚拟机)
Host ————宿主机
是一个开源软件,基于内核的虚拟化技术,实际是嵌入系统的一个虚拟化模块,通过优化内核来使用虚拟技术,该内核模块使得 Linux 变成了一个Hypervisor,虚拟机使用 Linux 自身的调度器进行管理。
KVM 是基于虚拟化扩展(Intel VT 或者 AMD-V)的 X86 硬件的开源的 Linux 原生的全虚拟化解决方案。KVM 中,虚拟机被实现为常规的 Linux 进程,由标准 Linux 调度程序进行调度;虚机的每个虚拟 CPU 被实现为一个常规的 Linux 进程。这使得 KMV 能够使用 Linux 内核的已有功能。但是,KVM 本身不执行任何硬件模拟,需要客户空间程序通过 /dev/kvm 接口设置一个客户机虚拟服务器的地址空间,向它提供模拟的 I/O,并将它的视频显示映射回宿主的显示屏。目前这个应用程序是 QEMU。
3、Linux 上的用户空间、内核空间、虚机:
Guest:客户机系统,包括CPU(vCPU)、内存、驱动(Console、网卡、I/O 设备驱动等),被 KVM 置于一种受限制的 CPU 模式下运行。
KVM:运行在内核空间,提供CPU 和内存的虚级化,以及客户机的 I/O 拦截。Guest 的 I/O (硬盘,网卡,主机)被 KVM 拦截后,交给 QEMU 处理。
QEMU:修改过的为 KVM 虚机使用的 QEMU 代码,运行在用户空间,提供硬件 I/O 虚拟化,通过IOCTL /dev/kvm 设备和 KVM 交互。
4、KVM有一个内核模块叫 kvm.ko,只用于管理虚拟 CPU 和内存。IO 的虚拟化,就交给 Linux 内核和qemu来实现。
Libvirt:是 KVM 的管理工具。Libvirt 除了能管理 KVM 这种 Hypervisor,还能管理 Xen,VirtualBox 等。OpenStack 底层也使用 Libvirt。
Libvirt 包含 3 个东西:后台 daemon 程序 libvirtd、API 库和命令行工具 virsh
(1)libvirtd是服务程序,接收和处理 API 请求;
(2)API 库使得其他人可以开发基于 Libvirt 的高级工具,比如 virt-manager,这是个图形化的 KVM 管理工具,后面我们也会介绍;
(3)virsh 是我们经常要用的 KVM 命令行工具,后面会有使用的示例。作为 KVM 和 OpenStack 的实施人员,virsh 和 virt-manager 是一定要会用的。
kvm部署
环境说明:
系统类型 | IP |
---|---|
centos7 | 192.168.201.140 |
部署前请确保你的CPU虚拟化功能已开启。分为两种情况:
- 虚拟机要关机设置CPU虚拟化
- 物理机要在BIOS里开启CPU虚拟化
//关闭防火墙与SELINUX
[root@wnz ~]# systemctl stop firewalld
[root@wnz ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@wnz ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@wnz ~]# setenforce 0
[root@wnz ~]# reboot
//配置网络源
[root@wnz ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
[root@wnz ~]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS-Base.repo
[root@wnz ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@wnz ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
//安装软件包
[root@wnz ~]# yum -y install net-tools unzip zip gcc gcc-c++
//验证CPU是否支持KVM;如果结果中有vmx(Intel)或svm(AMD)字样,就说明CPU的支持的
[root@wnz ~]# egrep -o 'vmx|svm' /proc/cpuinfo
vmx
vmx
vmx
vmx
//kvm安装
[root@wnz ~]# yum -y install qemu-kvm qemu-kvm-tools qemu-img virt-manager libvirt libvirt-python libvirt-client virt-install virt-viewer bridge-utils libguestfs-tools
//因为虚拟机中网络,我们一般都是和公司的其他服务器是同一个网段,所以我们需要把 \
KVM服务器的网卡配置成桥接模式。这样的话KVM的虚拟机就可以通过该桥接网卡和公司内部 \
其他服务器处于同一网段
//此处我的网卡是ens33,所以用br0来桥接ens33网卡
[root@wnz ~]# cd /etc/sysconfig/network-scripts/
[root@wnz network-scripts]# cp ifcfg-ens33 ifcfg-br0
[root@wnz network-scripts]# cat ifcfg-br0
TYPE=Bridge
DEVICE=br0
NM_CONTROLLED=no
BOOTPROTO=static
NAME=br0
ONBOOT=yes
IPADDR=192.168.201.140
NETMASK=255.255.255.0
GATEWAY=192.168.201.1
DNS1=114.114.114.114
DNS2=8.8.8.8
[root@wnz network-scripts]# cat ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
NAME=ens33
DEVICE=ens33
ONBOOT=yes
BRIDGE=br0
NM_CONTROLLED=no
//重启网络
[root@wnz ~]# systemctl restart network
[root@wnz ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> 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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master br0 state UP group default qlen 1000
link/ether 00:0c:29:56:da:10 brd ff:ff:ff:ff:ff:ff
inet6 fe80::20c:29ff:fe56:da10/64 scope link
valid_lft forever preferred_lft forever
3: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 00:0c:29:56:da:10 brd ff:ff:ff:ff:ff:ff
inet 192.168.201.140/24 brd 192.168.201.255 scope global br0
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe56:da10/64 scope link
valid_lft forever preferred_lft forever
//启动服务
[root@wnz ~]# systemctl start libvirtd
[root@wnz ~]# systemctl enable libvirtd
//验证安装结果
[root@wnz ~]# lsmod|grep kvm
kvm_intel 188688 0
kvm 636969 1 kvm_intel
irqbypass 13503 1 kvm
//测试并验证安装结果
[root@wnz ~]# virsh -c qemu:///system list
Id Name State
----------------------------------------------------
[root@wnz ~]# virsh --version
4.5.0
[root@wnz ~]# virt-install --version
1.5.0
[root@wnz ~]# ln -s /usr/libexec/qemu-kvm /usr/bin/qemu-kvm
[root@wnz ~]# ll /usr/bin/qemu-kvm
lrwxrwxrwx 1 root root 21 Aug 3 11:08 /usr/bin/qemu-kvm -> /usr/libexec/qemu-kvm
[root@wnz ~]# lsmod |grep kvm
kvm_intel 188688 0
kvm 636969 1 kvm_intel
irqbypass 13503 1 kvm
//查看网桥信息
[root@wnz ~]# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.000c2956da10 no ens33
virbr0 8000.5254004701ac yes virbr0-nic
添加光盘挂载并格式化
[root@wnz ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part
├─centos-root 253:0 0 17G 0 lvm /
└─centos-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 50G 0 disk //此处是我加的光盘
sr0 11:0 1 4.5G 0 rom
[root@wnz ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x0478aeec.
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-104857599, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-104857599, default 104857599):
Using default value 104857599
Partition 1 of type Linux and of size 50 GiB is set
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@wnz ~]# partprobe //刷新
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
[root@wnz ~]# mkfs.xfs /dev/sdb1 //格式化
meta-data=/dev/sdb1 isize=512 agcount=4, agsize=3276736 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=13106944, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=6399, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@wnz ~]# blkid
/dev/sda1: UUID="8e3db3b4-8763-49e7-a4e5-a360ab27724a" TYPE="xfs"
/dev/sda2: UUID="Vskah6-PgKn-cAgo-1f2i-aFv0-UfGe-vALkRC" TYPE="LVM2_member"
/dev/sdb1: UUID="6a6dcf39-75d1-4010-89b3-5445972ac46f" TYPE="xfs"
/dev/sr0: UUID="2020-04-22-00-54-00-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos"
/dev/mapper/centos-root: UUID="612547dd-a1eb-47ba-bf98-c1ede8f81918" TYPE="xfs"
/dev/mapper/centos-swap: UUID="289184f4-b55a-45a9-b512-e43b8b7826c6" TYPE="swap"
[root@wnz ~]# vim /etc/fstab
//添加一行
UUID="6a6dcf39-75d1-4010-89b3-5445972ac46f" /storage xfs defaults 0 0
[root@wnz ~]# mkdir /storage
[root@wnz ~]# mount -a
[root@wnz ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 12M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/centos-root 17G 2.0G 16G 12% /
/dev/sda1 1014M 152M 863M 15% /boot
/dev/sdb1 50G 33M 50G 1% /storage
tmpfs 378M 0 378M 0% /run/user/0
kvm web管理界面安装
kvm 的 web 管理界面是由 webvirtmgr 程序提供的。
//安装依赖包
[root@wnz ~]# yum -y install git python-pip libvirt-python libxml2-python python-websockify supervisor nginx python-devel
//升级pip
[root@wnz ~]# pip install --upgrade pip
Collecting pip
Downloading https://files.pythonhosted.org/packages/36/74/38c2410d688ac7b48afa07d413674afc1f903c1c1f854de51dc8eb2367a5/pip-20.2-py2.py3-none-any.whl (1.5MB)
100% |████████████████████████████████| 1.5MB 162kB/s
Installing collected packages: pip
Found existing installation: pip 8.1.2
Uninstalling pip-8.1.2:
Successfully uninstalled pip-8.1.2
Successfully installed pip-20.2
//从github上下载webvirtmgr代码
[root@wnz ~]# cd /usr/local/src/
[root@wnz src]# git clone git://github.com/retspen/webvirtmgr.git
Cloning into 'webvirtmgr'...
remote: Enumerating objects: 5614, done.
remote: Total 5614 (delta 0), reused 0 (delta 0), pack-reused 5614
Receiving objects: 100% (5614/5614), 2.98 MiB | 73.00 KiB/s, done.
Resolving deltas: 100% (3602/3602), done.
//安装webvirtmgr
[root@wnz src]# cd webvirtmgr/
[root@wnz webvirtmgr]# pip install -r requirements.txt
Collecting django==1.5.5
Downloading Django-1.5.5.tar.gz (8.1 MB)
|████████████████████████████████| 8.1 MB 164 kB/s
Collecting gunicorn==19.5.0
Downloading gunicorn-19.5.0-py2.py3-none-any.whl (113 kB)
|████████████████████████████████| 113 kB 175 kB/s
Collecting lockfile>=0.9
Downloading lockfile-0.12.2-py2.py3-none-any.whl (13 kB)
Using legacy 'setup.py install' for django, since package 'wheel' is not installed.
Installing collected packages: django, gunicorn, lockfile
Running setup.py install for django ... done
Successfully installed django-1.5.5 gunicorn-19.5.0 lockfile-0.12.2
//检查sqlite3是否安装
[root@wnz webvirtmgr]# python
Python 2.7.5 (default, Apr 2 2020, 13:16:51)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
>>> exit()
//初始化帐号信息
[root@wnz webvirtmgr]# python manage.py syncdb
WARNING:root:No local_settings file found.
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_groups
Creating table auth_user_user_permissions
Creating table auth_user
Creating table django_content_type
Creating table django_session
Creating table django_site
Creating table servers_compute
Creating table instance_instance
Creating table create_flavor
You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): yes //问你是否创建超级管理员帐号
Username (leave blank to use 'root'): //指定超级管理员帐号用户名,默认留空为root
Email address: 272867565@qq.com //设置超级管理员邮箱
Password: //设置超级管理员密码
Password (again): //再次输入超级管理员密码
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 6 object(s) from 1 fixture(s)
//拷贝web网页至指定目录
[root@wnz ~]# mkdir /var/www
[root@wnz ~]# cp -r /usr/local/src/webvirtmgr /var/www/
[root@wnz ~]# chown -R nginx.nginx /var/www/webvirtmgr/
//生成密钥
[root@wnz ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:Ese/b+2lbwCWSmjsfcsdtrtSQnYnH0wQT2P/yiI6xQE root@wnz
The key's randomart image is:
+---[RSA 2048]----+
| oo+ |
| .E +.o|
| ..oo .o..|
| o+.o * ooo|
| .oS+.* o +o|
| .. =.o.=..|
| .oo.Bo+.|
| .. o=.+o.|
| .. ..o==.|
+----[SHA256]-----+
//由于这里webvirtmgr和kvm服务部署在同一台机器,所以这里本地信任。如果kvm部署在其他机器,那么这个是它的ip
[root@wnz ~]# ssh-copy-id 192.168.201.140
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.201.140 (192.168.201.140)' can't be established.
ECDSA key fingerprint is SHA256:rrRgRX5XRCl7L8jnOpjUWu+XwX3iPXcH/wzbcLXfKM0.
ECDSA key fingerprint is MD5:e1:00:92:05:30:dd:45:de:5b:d4:64:f1:02:04:e8:fb.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.201.140's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '192.168.201.140'"
and check to make sure that only the key(s) you wanted were added.
//配置端口转发
[root@wnz ~]# ssh 192.168.201.140 -L localhost:8000:localhost:8000 -L localhost:6080:localhost:60
Last login: Mon Aug 3 12:12:12 2020 from 192.168.201.1
[root@wnz ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:111 *:*
LISTEN 0 5 192.168.122.1:53 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 127.0.0.1:6080 *:*
LISTEN 0 128 127.0.0.1:8000 *:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 100 [::1]:25 [::]:*
LISTEN 0 128 [::1]:6080 [::]:*
LISTEN 0 128 [::1]:8000 [::]:*
//配置nginx
[root@wnz ~]# vim /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name localhost;
include /etc/nginx/default.d/*.conf;
location / {
root html;
index index.html index.htm;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
[root@wnz ~]# vim /etc/nginx/conf.d/webvirtmgr.conf
server {
listen 80 default_server;
server_name $hostname;
#access_log /var/log/nginx/webvirtmgr_access_log;
location /static/ {
root /var/www/webvirtmgr/webvirtmgr;
expires max;
}
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-for $proxy_add_x_forwarded_for;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Forwarded-Proto $remote_addr;
proxy_connect_timeout 600;
proxy_read_timeout 600;
proxy_send_timeout 600;
client_max_body_size 1024M;
}
}
//确保bind绑定的是本机的8000端口
[root@wnz ~]# vim /var/www/webvirtmgr/conf/gunicorn.conf.py
.....省略内容
bind = '0.0.0.0:8000' //确保此处绑定的是本机的8000端口,这个在nginx配置中定义了,被代理的端口
backlog = 2048
.....省略内容
//重启nginx
[root@wnz ~]# systemctl restart nginx
//设置supervisor
[root@wnz ~]# vim /etc/supervisord.conf
.....此处省略上面的内容,在文件最后加上以下内容
[program:webvirtmgr]
command=/usr/bin/python2 /var/www/webvirtmgr/manage.py run_gunicorn -c /var/www/webvirtmgr/conf/gunicorn.conf.py
directory=/var/www/webvirtmgr
autostart=true
autorestart=true
logfile=/var/log/supervisor/webvirtmgr.log
log_stderr=true
user=nginx
[program:webvirtmgr-console]
command=/usr/bin/python2 /var/www/webvirtmgr/console/webvirtmgr-console
directory=/var/www/webvirtmgr
autostart=true
autorestart=true
stdout_logfile=/var/log/supervisor/webvirtmgr-console.log
redirect_stderr=true
user=nginx
//启动supervisor并设置开机自启
[root@wnz ~]# systemctl start supervisord
[root@wnz ~]# systemctl enable supervisord
Created symlink from /etc/systemd/system/multi-user.target.wants/supervisord.service to /usr/lib/systemd/system/supervisord.service.
[root@wnz ~]# systemctl status supervisord
● supervisord.service - Process Monitoring and Control Daemon
Loaded: loaded (/usr/lib/systemd/system/supervisord.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2020-08-04 02:45:54 EDT; 28s ago
Main PID: 23226 (supervisord)
CGroup: /system.slice/supervisord.service
├─23226 /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf
└─23285 /usr/bin/python2 /var/www/webvirtmgr/manage.py run_gunicorn -c /var/www/...
Aug 04 02:45:53 wnz systemd[1]: Starting Process Monitoring and Control Daemon...
Aug 04 02:45:54 wnz systemd[1]: Started Process Monitoring and Control Daemon.
//配置nginx用户
[root@wnz ~]# su - nginx -s /bin/bash
-bash-4.2$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/var/lib/nginx/.ssh/id_rsa):
Created directory '/var/lib/nginx/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /var/lib/nginx/.ssh/id_rsa.
Your public key has been saved in /var/lib/nginx/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:kEnD39oNFduLV6o7PL57ceti1fUuvRSDclARC/95v3Y nginx@wnz
The key's randomart image is:
+---[RSA 2048]----+
| .o ..=o |
| ..+ =o. |
| +. . o.o. .|
| .. o ..o+o|
| So +.o+==|
| . . +o..O|
| .. .*o|
| +++oE|
| .**+=o|
+----[SHA256]-----+
-bash-4.2$ touch ~/.ssh/config && echo -e "StrictHostKeyChecking=no\nUserKnownHostsFile=/dev/null" >> ~/.ssh/config
-bash-4.2$ chmod 0600 ~/.ssh/config
-bash-4.2$ ssh-copy-id root@192.168.201.140
/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/var/lib/nginx/.ssh/id_rsa.pub"
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
Warning: Permanently added '192.168.201.140' (ECDSA) to the list of known hosts.
root@192.168.201.140's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.201.140'"
and check to make sure that only the key(s) you wanted were added.
-bash-4.2$ exit
logout
[root@wnz ~]# vim /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
[Remote libvirt SSH access]
Identity=unix-user:root
Action=org.libvirt.unix.manage
ResultAny=yes
ResultInactive=yes
ResultActive=yes
[root@wnz ~]# chown -R root.root /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
[root@wnz ~]# systemctl restart nginx
[root@wnz ~]# systemctl restart libvirtd
访问时报错解决方案
第一次通过web访问kvm时可能会一直访问不了,一直转圈,而命令行界面一直报错(too many open files)
//此时需要对nginx进行配置
[root@wnz ~]# vim /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
worker_rlimit_nofile 655350; //添加此行配置
....此处省略N行
[root@wnz ~]# systemctl restart nginx
//然后对系统参数进行设置
[root@wnz ~]# vim /etc/security/limits.conf
....此处省略N行
# End of file
* soft nofile 655350
* hard nofile 655350
通过远程连接软件上传ISO镜像文件至存储目录/storage
[root@wnz ~]# cd /storage/
[root@wnz storage]# ls
CentOS-7-x86_64-DVD-2003.iso
在 web 界面查看ISO镜像是否存在
创建系统安装镜像
添加桥接网络
实例(虚拟机)创建
设置在 web 上访问虚拟机的密码
启动虚拟机
启动时报错,解决方法:
[root@wnz ~]# ll /etc/rc.local
lrwxrwxrwx. 1 root root 13 Jul 11 16:39 /etc/rc.local -> rc.d/rc.local
[root@wnz ~]# ll /etc/rc.d/rc.local
-rw-r--r--. 1 root root 473 Mar 31 21:30 /etc/rc.d/rc.local
[root@wnz ~]# chmod +x /etc/rc.d/rc.local
[root@wnz ~]# ll /etc/rc.d/rc.local
-rwxr-xr-x. 1 root root 473 Mar 31 21:30 /etc/rc.d/rc.local
[root@wnz ~]# vim /etc/rc.d/rc.local
......此处省略N行
# that this script will be executed during boot.
touch /var/lock/subsys/local
nohup novnc_server 192.168.201.140:5920 &
[root@wnz ~]# source /etc/rc.d/rc.local
//做完以上操作后再次访问即可正常访问