实验需求:
1、部署kvm
2、使用WebVirtMgr的WEB界面管理
3、在该WEB界面中安装一台Linux操作系统
环境说明:
系统:CentOS7
IP:192.168.253.145
1. CPU虚拟化功能
部署前请确保你的CPU虚拟化功能已开启,分为两种情况:
(1)虚拟机要关机设置CPU虚拟化
(2)物理机要在BIOS里开启CPU虚拟化

2、虚拟机设置(内存:8G 磁盘:200G 虚拟化功能:开启)
设置虚拟机内存,添加一块200G的硬盘

新建分区,将硬盘所有大小都给这个分区
[root@localhost ~]# parted /dev/sdb
GNU Parted 3.2
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel
New disk label type? msdos
(parted) unit
Unit? [compact]? MIB
(parted) p
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 204800MiB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: Number Start End Size Type File system Flags(parted) mkpart
Partition type? primary/extended? primary
File system type? [ext2]? xfs
Start? 10MiB
End? 204790MiB
(parted) p
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 204800MiB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: Number Start End Size Type File system Flags1 10.0MiB 204790MiB 204780MiB primary xfs lba(parted) q
Information: You may need to update /etc/fstab.
[root@localhost ~]# udevadm settle
格式化挂载
[root@localhost ~]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1 isize=512 agcount=4, agsize=13105920 blks= sectsz=512 attr=2, projid32bit=1= crc=1 finobt=1, sparse=1, rmapbt=0= reflink=1
data = bsize=4096 blocks=52423680, imaxpct=25= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=25597, version=2= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@localhost ~]# blkid /dev/sdb1
/dev/sdb1: UUID="72ab5f05-368b-450d-bc4f-65ead8a953f3" BLOCK_SIZE="512" TYPE="xfs" PARTUUID="c06f042f-01"
[root@localhost ~]# mkdir /kvmdata
[root@localhost ~]# vim /etc/fstab
UUID="72ab5f05-368b-450d-bc4f-65ead8a953f3" /kvmdata xfs defaults 0 0
[root@localhost ~]# mount -a
[root@localhost ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 865M 0 865M 0% /dev
tmpfs tmpfs 895M 0 895M 0% /dev/shm
tmpfs tmpfs 895M 9.7M 886M 2% /run
tmpfs tmpfs 895M 0 895M 0% /sys/fs/cgroup
/dev/mapper/cs-root xfs 66G 4.7G 61G 8% /
/dev/mapper/cs-home xfs 32G 275M 32G 1% /home
/dev/sda1 xfs 1014M 242M 773M 24% /boot
tmpfs tmpfs 179M 1.2M 178M 1% /run/user/42
tmpfs tmpfs 179M 4.6M 175M 3% /run/user/0
/dev/sr0 iso9660 9.0G 9.0G 0 100% /run/media/root/CentOS-Stream-8-x86_64-dvd
/dev/sdb1 xfs 200G 1.5G 199G 1% /kvmdata
KVM安装
关闭防火墙和selinux
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# vim /etc/sysconfig/selinux
SELINUX=disabled
//重启reboot
部署yum源,安装所需软件包
[root@localhost ~]# cd /etc/yum.repos.d
[root@localhost yum.repos.d]# ls
CentOS-Stream-AppStream.repo CentOS-Stream-HighAvailability.repo
CentOS-Stream-BaseOS.repo CentOS-Stream-Media.repo
CentOS-Stream-Debuginfo.repo CentOS-Stream-PowerTools.repo
CentOS-Stream-Extras.repo CentOS-Stream-RealTime.repo
[root@localhost yum.repos.d]# rm -rf *
[root@localhost yum.repos.d]# ls
[root@localhost yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
[root@localhost ~]# yum -y install epel-release vim wget net-tools unzip zip gcc gcc-c++
验证cpu是否支持kvm,vmx是intel的 svm是AMD的
[root@localhost ~]# egrep -o 'vmx|svm' /proc/cpuinfo
vmx
vmx
安装kvm
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的网卡配置成桥接模式
[root@localhost network-scripts]# cat ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=none
NAME=ens33
DEVICE=ens33
ONBOOT=yes
BRIDGE=br0
[root@localhost network-scripts]# cat ifcfg-br0
TYPE=Bridge
BOOTPROTO=ststic
NAME=br0
DEVICE=br0
ONBOOT=yes
IPADDR=192.168.253.145
PREFIX=24
GATEWAY=192.168.253.2
DNS1=8.8.8.8
重启网卡服务
[root@localhost network-scripts]# systemctl restart NetworkManager
[root@localhost network-scripts]# ifdown ens33
Error: '/etc/sysconfig/network-scripts/ifcfg-ens33' is not an active connection.
Error: no active connection provided.
[root@localhost network-scripts]# ifup ens33
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/4)

重启libvirtd服务,并设置下次启动生效
[root@localhost ~]# systemctl restart libvirtd
[root@localhost ~]# systemctl enable libvirtd
查看kvm模块是否加载,验证安装结果
[root@localhost webvirtmgr]# virsh -c qemu:///system listId Name State
----------------------------------------------------[root@localhost webvirtmgr]# virsh --version
4.5.0
[root@localhost webvirtmgr]# virt-install --version
1.5.0
将qemu-kvm这个命令做一个软链接到/usr/bin/qemu-kvm
[root@localhost ~]# ln -s /usr/libexec/qemu-kvm /usr/bin/qemu-kvm
[root@localhost ~]# ll /usr/bin/qemu-kvm
lrwxrwxrwx 1 root root 21 Oct 6 04:15 /usr/bin/qemu-kvm -> /usr/libexec/qemu-kvm
查看网桥信息
[root@localhost ~]# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.000c295f6b7d no ens33
virbr0 8000.52540084a097 yes
有问题:
[root@localhost ~]# brctl show
bash: brctl: command not found...解决:
yum install -y bridge-utils
KVM管理界面安装
Kvm的web界面是由webvirtmgr程序提供的
安装依赖包
[root@localhost network-scripts]# yum -y install git python-pip libvirt-python libxml2-python python-websockify supervisor nginx python-devel
从github上下载webvirtmgr代码
[root@localhost yum.repos.d]# cd /usr/local/src/
[root@localhost src]# git clone http://github.com/retspen/webvirtmgr.git
Cloning into 'webvirtmgr'...
warning: redirecting to https://github.com/retspen/webvirtmgr.git/
remote: Enumerating objects: 5614, done.
remote: Total 5614 (delta 0), reused 0 (delta 0), pack-reused 5614
Receiving objects: 100% (5614/5614), 2.97 MiB | 1.63 MiB/s, done.
Resolving deltas: 100% (3606/3606), done.
安装webvirtmgr
[root@localhost src]#cd webvirtmgr/
[root@localhost webvirtmgr]# pip3 install -r requirements.txt
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.
Collecting django==1.5.5 (from -r requirements.txt (line 1))Downloading https://files.pythonhosted.org/packages/38/49/93511c5d3367b6b21fc2995a0e53399721afc15e4cd6eb57be879ae13ad4/Django-1.5.5.tar.gz (8.1MB)100% |████████████████████████████████| 8.1MB 29kB/s
Collecting gunicorn==19.5.0 (from -r requirements.txt (line 2))Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='pypi.org', port=443): Read timed out. (read timeout=15)",)': /simple/gunicorn/Downloading https://files.pythonhosted.org/packages/f9/4e/f4076a1a57fc1e75edc0828db365cfa9005f9f6b4a51b489ae39a91eb4be/gunicorn-19.5.0-py2.py3-none-any.whl (113kB)100% |████████████████████████████████| 122kB 21kB/s
Collecting lockfile>=0.9 (from -r requirements.txt (line 5))Downloading https://files.pythonhosted.org/packages/c8/22/9460e311f340cb62d26a38c419b1381b8593b0bb6b5d1f056938b086d362/lockfile-0.12.2-py2.py3-none-any.whl
Installing collected packages: django, gunicorn, lockfileRunning setup.py install for django ... done
Successfully installed django-1.5.5 gunicorn-19.5.0 lockfile-0.12.2
检查sqlite3是否安装
[root@localhost webvirtmgr]# python
Python 2.7.5 (default, Jun 28 2022, 15:30:04)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
>>> exit()
初始化账号信息
[root@localhost 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_flavorYou 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: m13720252783@163.com //设置超级管理员邮箱
Password:
Password (again): //再次输入确认超级管理员密码
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 6 object(s) from 1 fixture(s)
拷贝web网页到指定目录
[root@localhost webvirtmgr]# mkdir /var/www
[root@localhost webvirtmgr]# cp -r /usr/local/src/webvirtmgr/ /var/www/
[root@localhost webvirtmgr]# chown -R nginx.nginx /var/www/webvirtmgr/
生成一对公钥与私钥,由于这里webvirtmgr和kvm服务部署在同一台主机中,所以这里本地信任。如果kvm部署在其他机器上的时候,那么就需要把公钥发送到kvm主机中
[root@localhost webvirtmgr]# ssh-keygen
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:OJqLh1i19LUcRd46kI/io5tc54L5P7D1uz7sbvlwghw root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
| .. |
| o.. |
| o.. . |
| o .o+ . |
| o o+oEo+ |
| . .+o++o . |
| o .oo+++oo.. |
|. .o+=o+. *+ |
| ..*o.oo**=. |
+----[SHA256]-----+
[root@localhost webvirtmgr]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.253.145
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.253.145 (192.168.253.145)' can't be established.
ECDSA key fingerprint is SHA256:tFwOucKjKiuxswyt0IReTBn44SsMOUG7/+2hBBdiggI.
ECDSA key fingerprint is MD5:14:04:2e:76:01:25:77:3a:a6:2f:e2:31:85:2d:58:3d.
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.253.145's password: Number of key(s) added: 1Now try logging into the machine, with: "ssh 'root@192.168.253.145'"
and check to make sure that only the key(s) you wanted were added.
配置端口转发
[root@localhost webvirtmgr]# ssh 192.168.253.145 -L localhost:8000:localhost:8000 -L localhost:6080:localhost:60
Last login: Thu Oct 6 21:36:07 2022 from 192.168.253.1
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.1:6080 *:*
LISTEN 0 128 127.0.0.1:8000 *:*
LISTEN 0 128 *:111 *:*
LISTEN 0 5 192.168.122.1:53 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 127.0.0.1:6011 *:*
LISTEN 0 128 ::1:6080 :::*
LISTEN 0 128 ::1:8000 :::*
LISTEN 0 128 :::111 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 128 ::1:6011 :::*
配置nginx

[root@localhost ~]# cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
[root@localhost ~]# vim /etc/nginx/nginx.conf
删除listen [::]:80;行
参数server_name行改成server_name localhost;
删除root /usr/share/nginx/html;行
在include /etc/nginx/default.d/*.conf;行下添加
location / {root html;index index.html index.html;}
配置nginx虚拟主机
[root@localhost ~]# 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端口
vim /var/www/webvirtmgr/conf/gunicorn.conf.py
//省略多行
bind = '127.0.0.1:8000' //确保此处绑定的是本机的8000端口,这个在nginx配置中定义了,被代理的端口
backlog = 2048
重启nginx服务,查看端口是否开启,设置supervisor
[root@localhost ~]# systemctl restart nginx
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.1:6080 *:*
LISTEN 0 128 127.0.0.1:8000 *:*
LISTEN 0 128 *:111 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 5 192.168.122.1:53 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 127.0.0.1:6011 *:*
LISTEN 0 128 ::1:6080 :::*
LISTEN 0 128 ::1:8000 :::*
LISTEN 0 128 :::111 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 128 ::1:6011 :::* [root@localhost ~]# 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
[root@localhost ~]# systemctl restart supervisord.service
[root@localhost ~]# systemctl enable supervisord.service
Created symlink from /etc/systemd/system/multi-user.target.wants/supervisord.service to /usr/lib/systemd/system/supervisord.service.
配置nginx用户,验证基于密钥认证是否成功
[root@localhost ~]# su - nginx -s /bin/bash
-bash-4.2$ ssh-keygen
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:BFngzSn72l5yKvveGd8GAoNK1WaWW5Row5S+784xl9g nginx@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
| oBo=.. |
| .o+%.o |
| .oB=+ |
| . .+= |
| . .. S+ |
| . .. .o.. |
| o.B.E. |
| .o B.B .. |
| o**o* ... |
+----[SHA256]-----+
-bash-4.2$
-bash-4.2$ touch ~/.ssh/config
-bash-4.2$ echo -e "StrictHostKeyChecking=no\nUserKnownHostsFile=/dev/null" >> ~/.ssh/config
-bash-4.2$ chmod 0600 ~/.ssh/config
-bash-4.2$ ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.253.145
/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.253.145' (ECDSA) to the list of known hosts.
root@192.168.253.145's password: Number of key(s) added: 1Now try logging into the machine, with: "ssh 'root@192.168.253.145'"
and check to make sure that only the key(s) you wanted were added.-bash-4.2$ ssh root@192.168.253.145
Warning: Permanently added '192.168.253.145' (ECDSA) to the list of known hosts.
Last login: Thu Oct 6 22:45:13 2022 from 192.168.253.145
[root@localhost ~]# exit
logout
Connection to 192.168.253.145 closed.
-bash-4.2$ exit
logout
[root@localhost ~]# [root@localhost ~]# 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@localhost ~]# chown -R root.root /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
[root@localhost ~]# systemctl restart nginx
[root@localhost ~]# systemctl restart libvirtd
KVM web界面管理
使用浏览器访问192.168.253.145,服务器出现了accept:Too many open files

解决方法:
[root@localhost ~]# 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; //添加此行//对系统参数进行设置
[root@localhost ~]# vim /etc/security/limits.conf
//在文件最末尾写入
* soft nofile 655350
* hard nofile 655350
//重启服务,重读文件
[root@localhost ~]# sysctl -p
[root@localhost ~]# systemctl restart nginx如果出现了连接超时,则做如下步骤,如果没有出现,则不管
解决方法:
//安装novnc,并通过novnc_server启动一个vnc 下载之前先检查你有没有下载epel,如果没有就去下载一个
[root@localhost yum.repos.d]# wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
[root@localhost ~]# yum install -y tigervnc-server
[root@localhost ~]# yum -y install novnc
[root@localhost ~]# chmod +x /etc/rc.d/rc.local
[root@localhost ~]#vim /etc/rc.d/rc.local
//在最末尾加入如下行
nohup novnc_server 192.168.253.145:5920 &
[root@localhost ~]# . /etc/rc.d/rc.local



创建虚拟机
1、新建存储

2、上传镜像,将镜像文件上传到服务器的/kvmdata目录下存放


创建镜像


实例管理,现在去创建一个虚拟机






如果出现在安装了novnc的情况下启动超时,建议试下
. /etc/rc.d/rc.local
如果出现nohup: ignoring input and appending output to ‘nohup.out’ 这不是报错并用管它
在浏览器上强制关机,再重新启动


















