KVM虚拟化介绍和安装使用方法

一、KVM虚拟化介绍

虚拟化:在一台计算机上虚拟出多个逻辑的计算机,而且每个逻辑计算机它可以是不同操作系统

虚拟化技术:可以扩大硬件容量,单个cpu模拟出多个cpu并行,允许一个平台上同时运行多个操作系统,应用程序都可以在相互独立的空间内运行,而且互不影响。

为什么企业使用虚拟化技术

1)节约成本

2)提高效率,物理机我们一般称为宿主机(Host),宿主机上面的虚拟机称为客户机(Guest)

那么 Host 是如何将自己的硬件资源虚拟化,并提供给 Guest 使用的呢?

这个主要是通过一个叫做Hypervisor的程序实现的。

Hypervisor:一种运行在物理服务器硬件与操作系统之间的中间软件层,可允许多个操作系统和应用来共享硬件资源。

根据 Hypervisor 的实现方式和所处的位置,虚拟化又分为两种:

1)完全虚拟化:直接在物理机上部署虚拟化,且不需要修改操作系统内核。

2)半虚拟化:需要修改操作系统内核,使其支持虚拟化驱动来实现虚拟化技术。

1、完全虚拟化

Hypervisor 直接安装在物理机上,多个虚拟机在 Hypervisor 上运行。Hypervisor 实现方式一般是一个特殊定制的 Linux 系统。Xen 和 VMWare 的 ESXi 都属于这个类型。

 

2、半虚拟化

理论上讲:

完全虚拟化一般对硬件虚拟化功能进行了特别优化,性能上比半虚拟化要高

半虚拟化因为基于普通的操作系统,会比较灵活,比如支持虚拟机嵌套。嵌套意味着可以在KVM虚拟机中再运行KVM。

二、kvm介绍

kVM 全称是 Kernel-Based Virtual Machine。也就是说 KVM 是基于 Linux 内核实现的。

KVM有一个内核模块叫 kvm.ko,只用于管理虚拟 CPU 和内存。

那 IO 的虚拟化,比如存储和网络设备则是由 Linux 内核与Qemu来实现

Qemu 是纯软件实现的虚拟化模拟器,几乎可以模拟任何硬件设备,我们最熟悉的就是能够模拟一台能够独立运行操作系统的虚拟机,虚拟机认为自己和硬件打交道,但其实是和 Qemu 模拟出来的硬件打交道,Qemu 将这些指令转译给真正的硬件。

正因为 Qemu 是纯软件实现的,所有的指令都要经 Qemu 过一手,性能非常低,所以,在生产环境中,大多数的做法都是配合 KVM 来完成虚拟化工作,因为 KVM 是硬件辅助的虚拟化技术,主要负责 比较繁琐的 CPU 和内存虚拟化,而 Qemu 则负责 I/O 虚拟化,两者合作各自发挥自身的优势,相得益彰。

作为一个 Hypervisor,KVM 本身只关注虚拟机调度和内存管理这两个方面。IO 外设的任务交给 Linux 内核和 Qemu。

Libvirt 就是 KVM 的管理工具。管理虚拟机和虚拟化功能的软件。

其实,Libvirt 除了能管理 KVM 这种 Hypervisor,还能管理 Xen,VirtualBox 等。

Libvirt 包含 3 个东西:后台 daemon 程序 libvirtd、API 库和命令行工具 virsh。

1、libvirtd是服务程序,接收和处理 API 请求;

2、API 库使得其他人可以开发基于 Libvirt 的高级工具,比如 virt-manager,这是个图形化的 KVM 管理工具

3、virsh 是我们经常要用的 KVM 命令行工具

三、KVM部署(基于CentOS7)

环境说明:

系统:CentOS7

IP:192.168.100.100

1、CPU虚拟化功能

部署前请确保你的CPU虚拟化功能已开启,分为两种情况:

(1)虚拟机要关机设置CPU虚拟化

(2)物理机要在BIOS里开启CPU虚拟化

2、虚拟机设置(内存:8G 磁盘:200G 虚拟化功能:开启)

//设置虚拟机内存,添加一块200G的硬盘

//开启虚拟化

//新建分区,将硬盘所有大小都给这个分区

注意要关闭防火墙和selinux

[root@RS1 ~]# lsblk 
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   50G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   49G  0 part 
  ├─centos-root 253:0    0   44G  0 lvm  /
  └─centos-swap 253:1    0    5G  0 lvm  [SWAP]
sdb               8:16   0  200G  0 disk 
sr0              11:0    1  4.2G  0 rom  
[root@RS1 ~]# parted /dev/sdb 
GNU Parted 3.1
使用 /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel                                                          
新的磁盘标签类型? 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  标志

(parted) mkpart                                                           
分区类型?  primary/主分区/extended/扩展分区? primary                     
文件系统类型?  [ext2]? xfs                                               
起始点? 10MiB                                                            
结束点? 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  标志
 1      10.0MiB  204790MiB  204780MiB  primary

(parted) q                                                                
信息: You may need to update /etc/fstab.

//格式化
[root@RS1 ~]# udevadm settle
[root@RS1 ~]# mkfs.xfs /dev/sdb
sdb   sdb1  
[root@RS1 ~]# 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@RS1 ~]# blkid /dev/sdb1 
/dev/sdb1: UUID="0df1b1eb-b450-4761-a2a7-38b7ef24d599" BLOCK_SIZE="512" TYPE="xfs" PARTUUID="e9ad57d5-01"
[root@RS1 ~]# 

//挂载
[root@RS1 ~]# mkdir /kvmdata
[root@RS1 ~]# vim /etc/fstab
#最下面添加
UUID="0df1b1eb-b450-4761-a2a7-38b7ef24d599" /kvmdata xfs defaults 0 0
[root@RS1 ~]# mount -a
[root@RS1 ~]# df -Th
Filesystem          Type      Size  Used Avail Use% Mounted on
devtmpfs            devtmpfs  3.8G     0  3.8G   0% /dev
tmpfs               tmpfs     3.8G     0  3.8G   0% /dev/shm
tmpfs               tmpfs     3.8G  9.0M  3.8G   1% /run
tmpfs               tmpfs     3.8G     0  3.8G   0% /sys/fs/cgroup
/dev/mapper/cs-root xfs        44G  2.1G   42G   5% /
/dev/sr0            iso9660   9.0G  9.0G     0 100% /mnt/cdrom
/dev/sda1           xfs      1014M  195M  820M  20% /boot
tmpfs               tmpfs     775M     0  775M   0% /run/user/0
/dev/sdb1           xfs       200G  1.5G  199G   1% /kvmdata

//安装所需软件包
[root@RS1 ~]# yum -y install epel-release vim wget net-tools unzip zip gcc gcc-c++

//验证cpu是否支持kvm,vmx是intel的  svm是AMD的
[root@RS1 ~]# egrep -o 'vmx|svm' /proc/cpuinfo 
svm
svm

//安装kvm
[root@RS1 ~]# 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@RS1 ~]# cd /etc/sysconfig/network-scripts/
[root@RS1 network-scripts]# ls
ifcfg-eth0
[root@RS1 network-scripts]# cp ifcfg-eth0 ifcfg-br0
[root@RS1 network-scripts]# vim ifcfg-br0 
TYPE=Ethernet
BOOTPROTO=none
DEVICE=ens33
ONBOOT=yes
BRIDGE=br0

[root@RS1 network-scripts]# vim ifcfg-br0
TYPE=Bridge
BOOTPROTO=none
NAME=br0
DEVICE=br0
ONBOOT=yes
IPADDR=192.168.141.145
PREFIX=24
GATEWAY=192.168.141.2
DNS1=8.8.8.8


[root@RS1 network-scripts]# systemctl restart NetworkManager
[root@RS1 network-scripts]# ifdown ens33
Error: '/etc/sysconfig/network-scripts/ifcfg-eth0' is not an active connection.
Error: no active connection provided.

[root@RS1 network-scripts]# ifup ens33
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/4)

[root@rs1 ~]# 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:b2:01:c8 brd ff:ff:ff:ff:ff:ff
3: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 00:0c:29:b2:01:c8 brd ff:ff:ff:ff:ff:ff
    inet 192.168.141.145/24 brd 192.168.141.255 scope global noprefixroute br0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:feb2:1c8/64 scope link 
       valid_lft forever preferred_lft forever
[root@rs1 ~]# 


//重启libvirtd服务,并设置下次启动生效
[root@RS1 ~]# systemctl restart libvirtd
[root@RS1 ~]# systemctl enable libvirtd

//查看kvm模块是否加载
[root@rs1 ~]# lsmod | grep kvm
kvm_amd              2177304  0 
kvm                   637515  1 kvm_amd
irqbypass              13503  1 kvm
[root@rs1 ~]# 


//验证安装结果
[root@rs1 ~]# virsh -c qemu:///system list
 Id    名称                         状态
----------------------------------------------------
[root@rs1 ~]# virsh --version
4.5.0
[root@rs1 ~]# virt-install --version
1.5.0
[root@rs1 ~]# 


//将qemu-kvm这个命令做一个软链接到/usr/bin/qemu-kvm
[root@rs1 ~]# ln -s /usr/libexec/qemu-kvm /usr/bin/qemu-kvm
[root@rs1 ~]# ll /usr/bin/qemu-kvm
lrwxrwxrwx. 1 root root 21 9月  30 23:02 /usr/bin/qemu-kvm -> /usr/libexec/qemu-kvm
[root@rs1 ~]# 

//查看网桥信息
[root@rs1 ~]# brctl show
bridge name	bridge id		STP enabled	interfaces
br0		8000.000c29b201c8	no		ens33
virbr0		8000.5254009a3e79	yes		virbr0-nic
[root@rs1 ~]# 

KVM管理界面安装

Kvm的web界面是由webvirtmgr程序提供的

//安装依赖包
[root@RS1 ~]# yum -y install git python-pip libvirt-python libxml2-python python-websockify supervisor nginx python-devel

//从github上下载webvirtmgr代码
[root@rs1 ~]# cd /usr/local/src/
[root@rs1 src]# git clone http://github.com/retspen/webvirtmgr.git
正克隆到 'webvirtmgr'...
remote: Enumerating objects: 5614, done.
remote: Total 5614 (delta 0), reused 0 (delta 0), pack-reused 5614
接收对象中: 100% (5614/5614), 2.97 MiB | 1.29 MiB/s, done.
处理 delta 中: 100% (3606/3606), done.
[root@rs1 src]# 


//安装webvirtmgr这个因为版本不太pip用TAB一下查看有哪些安装方式
[root@rs1 src]# cd webvirtmgr/
[root@rs1 webvirtmgr]# pip install -r requirements.txt
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 61kB/s 
Collecting gunicorn==19.5.0 (from -r requirements.txt (line 2))
  Downloading https://files.pythonhosted.org/packages/f9/4e/f4076a1a57fc1e75edc0828db365cfa9005f9f6b4a51b489ae39a91eb4be/gunicorn-19.5.0-py2.py3-none-any.whl (113kB)
    100% |████████████████████████████████| 122kB 122kB/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, lockfile
  Running setup.py install for django ... done
Successfully installed django-1.5.5 gunicorn-19.5.0 lockfile-0.12.2
[root@rs1 webvirtmgr]# 


//检查sqlite3是否安装
[root@rs1 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@rs1 webvirtmgr]# 

//初始化账号信息
[root@rs1 webvirtmgr]# cd /usr/local/src/webvirtmgr/
[root@rs1 webvirtmgr]# python manage.py syncdb
[root@rs1 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'):   //指定超级管理员账号用户名,默认留空
Email address: [email protected]  //设置超级管理员邮箱
Password:  //设置超级管理员密码此处我设置为1也可以
Password (again):   //再次输入确认超级管理员密码
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 6 object(s) from 1 fixture(s)
[root@rs1 webvirtmgr]# 

//拷贝web网页到指定目录
[root@rs1 webvirtmgr]# mkdir /var/www
[root@rs1 webvirtmgr]# cp -r /usr/local/src/webvirtmgr/ /var/www/
[root@rs1 webvirtmgr]# chown -R nginx.nginx /var/www/webvirtmgr/
[root@rs1 webvirtmgr]# ll /var/www/ | grep webvirtmgr
drwxr-xr-x. 20 nginx nginx 4096 9月  30 23:14 webvirtmgr
[root@rs1 webvirtmgr]# 

//生成一对公钥与私钥,由于这里webvirtmgr和kvm服务部署在同一台主机中,所以这里本地信任。如果kvm部署在其他机器上的时候,那么就需要把公钥发送到kvm主机中
[root@rs1 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:K+VHRSthU39R8lg1POPy0Q/2kXpfDozxU5b7XS3puA0 root@rs1
The key's randomart image is:
+---[RSA 2048]----+
|          +.o oo*|
|         . + o Oo|
|          . o + O|
|           o..oB+|
|        S .  *+**|
|       o o  ..B+*|
|      . o . Eo.=*|
|       . .  .o. =|
|            ...  |
+----[SHA256]-----+
#自己发给自己
[root@rs1 webvirtmgr]# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.141.145 (192.168.141.145)' can't be established.
ECDSA key fingerprint is SHA256:pte5EPGaqvv5cFY581zFekKgzYvvwd62Gq9OKY9y+/o.
ECDSA key fingerprint is MD5:75:66:ea:1b:6b:b3:0c:5d:80:a3:d8:bf:d6:0c:71:14.
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
[email protected]'s password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.

//配置端口转发
[root@rs1 webvirtmgr]# ss -anlt
State      Recv-Q Send-Q     Local Address:Port                    Peer Address:Port              
LISTEN     0      5          192.168.122.1:53                                 *:*                  
LISTEN     0      128                    *:22                                 *:*                  
LISTEN     0      100            127.0.0.1:25                                 *:*                  
LISTEN     0      128                 [::]:22                              [::]:*                  
LISTEN     0      100                [::1]:25                              [::]:*                  
[root@rs1 webvirtmgr]# ssh 192.168.141.145 -L localhost:8000:localhost:8000 -L localhost:6080:localhost:60
Last login: Fri Sep 30 23:01:06 2022 from 192.168.141.1
[root@rs1 ~]# ss -anlt
State      Recv-Q Send-Q     Local Address:Port                    Peer Address:Port              
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                 [::]:22                              [::]:*                  
LISTEN     0      100                [::1]:25                              [::]:*                  
LISTEN     0      128                [::1]:6080                            [::]:*                  
LISTEN     0      128                [::1]:8000                            [::]:*                  
[root@rs1 ~]# 

//配置nginx
[root@rs1 ~]# cd /etc/nginx/
[root@rs1 nginx]# cp nginx.conf nginx.conf.bak
[root@rs1 nginx]# vim /etc/nginx/nginx.conf
//修改38-48的内容下面是修改后的内容
 38     server {
 39         listen       80;
 40         server_name  localhost;
 41         
 42         # Load configuration files for the default server block.
 43         include /etc/nginx/default.d/*.conf;
 44         
 45         location / {
 46                 root    html;
 47                 index   index.html index.htm;
 48         }

//配置nginx虚拟主机
[root@rs1 nginx]# cd /etc/nginx/conf.d/
[root@rs1 conf.d]# ls
[root@rs1 conf.d]# vim webvirtmgr.conf
[root@rs1 conf.d]# cat 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@rs1 conf.d]# vim /var/www/webvirtmgr/conf/gunicorn.conf.py
 22 
 23 bind = '127.0.0.1:8000'
 24 backlog = 2048
#查看是否有8000端口
[root@rs1 conf.d]# systemctl restart nginx
[root@rs1 conf.d]# ss -anlt
State      Recv-Q Send-Q     Local Address:Port                    Peer Address:Port              
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                 [::]:22                              [::]:*                  
LISTEN     0      100                [::1]:25                              [::]:*                  
LISTEN     0      128                [::1]:6080                            [::]:*                  
LISTEN     0      128                [::1]:8000                            [::]:*                  
[root@rs1 conf.d]# 

//设置supervisor在最底层写入
[root@rs1 ~]# vim /etc/supervisord.conf
files = supervisord.d/*.ini #在这个下写

[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@rs1 ~]# systemctl restart supervisord.service 
[root@rs1 ~]# systemctl enable supervisord.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/supervisord.service to /usr/lib/systemd/system/supervisord.service.
[root@rs1 ~]# 

//配置nginx用户
[root@rs1 ~]# 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:FFffKx6el+eoq/PyyBtAMW1ZfXn4+DcKQg/kmyKcJ2k nginx@rs1
The key's randomart image is:
+---[RSA 2048]----+
|        +..+o. ..|
|         *+  .ooo|
|        =.    .+o|
|       o +    . o|
|    . o S =  o o |
|     E o = oo +.+|
|    . + . o .+.o+|
|         .oo ..o.|
|          =B=o. .|
+----[SHA256]-----+
-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 [email protected]
/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.141.145' (ECDSA) to the list of known hosts.
[email protected]'s password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.

//验证基于密钥认证是否成功
-bash-4.2$ ssh [email protected]
Warning: Permanently added '192.168.141.145' (ECDSA) to the list of known hosts.
Last login: Fri Sep 30 23:20:39 2022 from rs1
[root@rs1 ~]# exit
登出
Connection to 192.168.141.145 closed.
-bash-4.2$ exit
登出
[root@rs1 ~]# 

[root@rs1 ~]# vim /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
[root@rs1 ~]# cat /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@rs1 ~]# chown -R nginx.nginx /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
[root@rs1 ~]# systemctl restart nginx
[root@rs1 ~]# systemctl restart libvirtd
[root@rs1 ~]# 

KVM web界面管理

使用浏览器访问192.168.141.145,服务器出现了accept:Too many open files

解决方法:

//修改nginx配置文件
[root@rs1 ~]# 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@rs1 ~]# vim /etc/security/limits.conf
//在文件最末尾# End of file写入
# End of file
* soft nofile 655350
* hard nofile 655350

//重启服务,重读文件
[root@rs1 ~]# sysctl -p
[root@rs1 ~]# systemctl restart nginx
[root@rs1 ~]# 

再次使用浏览器访问

登录成功,但无了解把虚拟机添加进来

创建虚拟机

1、新建存储

上传镜像

#因为镜像文件很打安装第三方工具帮助

工具下载地址https://www.filezilla.cn/download/client

刷新浏览器

添加网络

因为是桥接网络而且没有配置DNS所以网络类型是BRIDGE

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

选择镜像和网络

解决连接超时方法

[root@rs1 ~]# yum -y install novnc
[root@rs1 ~]# vim /etc/rc.d/rc.local
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
touch /var/lock/subsys/local
nohup novnc_server 192.168.141.145:5920 & 
#5920这个端口任意但不能是在用的端口
[root@rs1 ~]# . /etc/rc.d/rc.local
[root@rs1 ~]# nohup: 忽略输入并把输出追加到"nohup.out"
#回车即可没有影响
[root@rs1 ~]# 

再次访问在浏览器上创建的虚拟机


访问成功开始安装

安装成功

可以在概况观看CPU和内存占用情况

在虚拟机实列可以有效操作创建虚拟机

转自:KVM虚拟化介绍和安装使用方法_kvm虚拟机_KUILING的博客-CSDN博客

猜你喜欢

转载自blog.csdn.net/fuhanghang/article/details/131594902