KVM虚拟化(web控制)

一、虚拟化介绍

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

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

二、为什么使用虚拟化技术

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

那么 Host 是如何将自己的硬件资源虚拟化,并提供给 Guest 使用的呢?
这个主要是通过一个叫做 Hypervisor 的程序实现的。
Hypervisor:一种运行在物理服务器硬件与操作系统之间的中间软件层
可允许多个操作系统和应用来共享硬件资源
根据 Hypervisor 的实现方式和所处的位置,虚拟化又分为两种:
完全虚拟化:直接在物理机上部署虚拟化,且不需要修改操作系统内核
半虚拟化:需要修改操作系统内核,使其支持虚拟化驱动来实现虚拟化技术

1、完全虚拟化

Hypervisor 直接安装在物理机上,多个虚拟机在 Hypervisor 上运行。Hypervisor 实现方式一般是一个特殊定制的 Linux 系统。Xen 和 VMWare 的 ESXi 都属于这个类型。
是一种全面模拟硬件的虚拟化方式,它允许多个虚拟机在同一台物理机上运行不同的操作系统,每个虚拟机都可以独立运行,仿佛在独立的物理服务器上运行一样。在全虚拟化中,虚拟机的操作系统并不知道它是在虚拟机上运行的,因为虚拟机的硬件和操作系统看起来和物理机的硬件和操作系统没有区别。全虚拟化可以在不对真实硬件进行任何更改的情况下运行,并且能够支持传统的操作系统,例如 ‌Windows。但是由于需要进行硬件仿真,全虚拟化通常会导致性能下降。

2、半虚拟化

是一种要求操作系统修改以支持虚拟硬件的虚拟化方式。在半虚拟化中,虚拟机能够通过访问虚拟化层提供的接口来与物理硬件交互,而无需通过硬件仿真的方式进行。由于不需要进行硬件仿真,因此半虚拟化通常比全虚拟化的性能更高。但是,要求虚拟机操作系统修改,可能并不适用于某些操作系统,例如 Windows。半虚拟化可以减少开销并提高系统性能

两者区别:
在这里插入图片描述

理论上讲:
完全虚拟化一般对硬件虚拟化功能进行了特别优化,性能上比半虚拟化要高;
半虚拟化因为基于普通的操作系统,会比较灵活,比如支持虚拟机嵌套。嵌套意味着可以在KVM虚拟机中再运行KVM。

全虚拟化:
优点:不需要修改操作系统内核,支持广泛的操作系统,包括未经修改的Windows系统。
缺点:性能较低,因为需要进行硬件仿真,资源消耗较大。‌12
半虚拟化:
优点:性能较高,不需要进行硬件仿真,资源消耗较少。
缺点:需要修改操作系统内核,可能不适用于所有操作系统。

三、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 ;在虚拟机中新增一块200G的硬盘
在虚拟机设置中找到处理器,将虚拟化 intel VT-x/EPT选项勾选上

1、配置yum仓库

[root@kvm ~]# cd /etc/yum.repos.d/
[root@kvm yum.repos.d]# ls
CentOS-Base.repo
[root@kvm yum.repos.d]# rm -rf *
//这里我们使用阿里的yum仓库
[root@kvm yum.repos.d]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
//安装epel源
[root@kvm yum.repos.d]# yum -y install epel-release
[root@kvm yum.repos.d]# ls
CentOS-Base.repo  epel.repo  epel-testing.repo 

//确保firewalld和selinux都处于关闭状态

2、格式化新硬盘

[root@kvm ~]# 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 MiB                                                         
(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) quit                                                             
信息: You may need to update /etc/fstab.

[root@kvm ~]# udevadm settle     //同步
[root@kvm ~]# mkfs.xfs /dev/sdb1     //格式化xfs格式
meta-data=/dev/sdb1              isize=512    agcount=4, agsize=13105920 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
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@kvm ~]# blkid /dev/sdb1
/dev/sdb1: UUID="77f5254b-64b5-405b-989c-ea94e46d51d6" TYPE="xfs" 
[root@kvm ~]# mkdir /kvmdata                //创建挂载点
[root@kvm ~]# vim /etc/fstab     //写入fstab实现永久挂载
UUID=77f5254b-64b5-405b-989c-ea94e46d51d6  /kvmdtat   xfs defaults        0 0

[root@kvm ~]# mount -a   //重新挂载
[root@kvm ~]# df -Th        //查看挂载信息
文件系统                类型      容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root xfs        36G  5.0G   31G   15% /
devtmpfs                devtmpfs  1.9G     0  1.9G    0% /dev
tmpfs                   tmpfs     1.9G     0  1.9G    0% /dev/shm
tmpfs                   tmpfs     1.9G  9.1M  1.9G    1% /run
tmpfs                   tmpfs     1.9G     0  1.9G    0% /sys/fs/cgroup
/dev/sda1               xfs      1014M  179M  836M   18% /boot
tmpfs                   tmpfs     378M   12K  378M    1% /run/user/42
tmpfs                   tmpfs     378M     0  378M    0% /run/user/0
/dev/sdb1               xfs       200G   33M  200G    1% /kvmdata

3、KVM管理界面安装

1.)安装依赖包
[root@kvm ~]# cat /proc/cpuinfo | grep svm   //查看cpu是否支持虚拟化,vmx是intel的  svm是AMD的
flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 
syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc art rep_good nopl tsc_reliable nonstop_tsc extd_apicid 
eagerfpu pni pclmulqdq ssse3 fma cx16 sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand hypervisor 
lahf_lm *svm* extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw topoext fsgsbase bmi1 avx2 smep bmi2 
erms invpcid rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 arat npt sv_lock nrip_save 
vmcb_clean flushbyasid decodeassists overflow_recov succor


[root@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
2.)修改网卡配置文件
[root@kvm src]# cd /etc/sysconfig/network-scripts/
[root@kvm network-scripts]# ls
ifcfg-ens33  ifdown-eth   ifdown-isdn    ifdown-sit       ifup          ifup-ib    ifup-plip   ifup-routes    ifup-tunnel        network-functions-ipv6
ifcfg-lo     ifdown-ib    ifdown-post    ifdown-Team      ifup-aliases  ifup-ippp  ifup-plusb  ifup-sit       ifup-wireless
ifdown       ifdown-ippp  ifdown-ppp     ifdown-TeamPort  ifup-bnep     ifup-ipv6  ifup-post   ifup-Team      init.ipv6-global
ifdown-bnep  ifdown-ipv6  ifdown-routes  ifdown-tunnel    ifup-eth      ifup-isdn  ifup-ppp    ifup-TeamPort  network-functions
[root@kvm network-scripts]# cp ifcfg-ens33 ifcfg-br0
[root@kvm network-scripts]# vim ifcfg-ens33 
TYPE="Ethernet"
BOOTPROTO="none"
NAME="ens33"
DEVICE="ens33"
ONBOOT="yes"   //除了这些都删除,最后再添加一行信息
BRIDGE="br0"

[root@kvm network-scripts]# vim ifcfg-br0
TYPE="Bridge"                          //这里修改为桥接
BOOTPROTO="none"
NAME="br0"
DEVICE="br0"
ONBOOT="yes"
IPADDR="192.168.100.10"
PREFIX="24"
GATEWAY="192.168.100.254"
DNS1="8.8.8.8"

[root@kvm ~]# systemctl restart network
[root@kvm ~]# systemctl restart NetworkManager
[root@kvm ~]# cd /etc/sysconfig/network-scripts/
[root@kvm network-scripts]# ifdown ens33
[root@kvm network-scripts]# ifup ens33
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/4)

3.)验证安装结果
[root@kvm ~]# systemctl restart libvirtd     //重启libvirtd服务,并enable
[root@kvm ~]# systemctl enable libvirtd
[root@kvm ~]# lsmod | grep kvm   //查看kvm进程信息
kvm_amd                69849  0 
kvm                   566340  1 kvm_amd
irqbypass              13503  1 kvm
[root@kvm ~]# virsh -c qemu:///system list
 Id    名称                         状态
----------------------------------------------------
//出现这些就表明没有问题安装成功

//查看版本信息
[root@kvm ~]# virsh --version
4.5.0
[root@kvm ~]# virt-install --version
1.5.0

[root@kvm ~]# ln -s /usr/libexec/qemu-kvm /usr/bin/qemu-kvm     //做一个软连接,否则qemu-kvm该命令指执行tab不出来
[root@kvm ~]# ll /usr/bin/qemu-kvm
lrwxrwxrwx 1 root root 21 7月  28 19:27 /usr/bin/qemu-kvm -> /usr/libexec/qemu-kvm

//查看桥接网络信息
[root@kvm ~]# brctl show
bridge name	bridge id		STP enabled	interfaces
br0		8000.000c29aafd37	no		ens33
virbr0		8000.525400b818f4	yes		virbr0-nic

4.)KVM管理界面安装

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

[root@kvm ~]# yum -y install git python-pip libvirt-python libxml2-python python-websockify supervisor nginx python-devel
[root@kvm src]# cd /usr/local/src/
[root@kvm src]# git clone http://github.com/retspen/webvirtmgr.git
[root@kvm src]# ls 
webvirtmgr

//安装webvirtmgr
[root@kvm webvirtmgr]# pip --trusted-host pypi.python.org install -r requirements.txt    //加上--trusted-host pypi.python.org表示信任该网址
Collecting django==1.5.5 (from -r requirements.txt (line 1))
  \xe7\xbd\x91\xe7\xbb\x9c\xe4\xb8\x8d\xe5\x8f\xaf\xe8\xbe\xbe',)': /packages/38/49/93511c5d3367b6b21fc2995a0e53399721afc15e4cd6eb57be879ae13ad4/Django-1.5.5.tar.gz
  Downloading https://files.pythonhosted.org/packages/38/49/93511c5d3367b6b21fc2995a0e53399721afc15e4cd6eb57be879ae13ad4/Django-1.5.5.tar.gz (8.1MB)
    100% |████████████████████████████████| 8.1MB 33kB/s 
Collecting gunicorn==19.5.0 (from -r requirements.txt (line 2))
  Retrying (Retry(total=4, connect=None, read=None, redirect=None)) after connection broken by 'NewConnectionError('<pip._vendor.requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x7f50af8c2b50>: Failed to establish a new connection: [Errno 101] \xe7\xbd\x91\xe7\xbb\x9c\xe4\xb8\x8d\xe5\x8f\xaf\xe8\xbe\xbe',)': /simple/gunicorn/
  Downloading https://files.pythonhosted.org/packages/f9/4e/f4076a1a57fc1e75edc0828db365cfa9005f9f6b4a51b489ae39a91eb4be/gunicorn-19.5.0-py2.py3-none-any.whl (113kB)
    100% |████████████████████████████████| 122kB 55kB/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@kvm webvirtmgr]# python
Python 2.7.5 (default, Nov 14 2023, 16:14:06) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3    //将sqlite33导入python数据库中
>>> exit()

4.) 初始化账号信息
[root@kvm 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: admin@example.com    //邮箱(这个可以随便天)
Password:                               //输入设置密码
Password (again):                 //再次输入密码
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 6 object(s) from 1 fixture(s)

//拷贝web网页到指定目录
[root@kvm webvirtmgr]# mkdir /var/www
[root@kvm webvirtmgr]# cp -r /usr/local/src/webvirtmgr/ /var/www/
[root@kvm webvirtmgr]# chown -R nginx.nginx /var/www                             /webvirtmgr/

//生成一对公钥与私钥,由于这里webvirtmgr和kvm服务部署在同一台主机中,所以这里本地信任。如果kvm部署在其他机器上的时候,那么就需要把公钥发送到kvm主机中
[root@kvm webvirtmgr]# ssh-keygen
[root@kvm webvirtmgr]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.100.10    //将密钥发送到主机
[root@kvm webvirtmgr]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.100.10
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/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.100.10's password: 

Number of key(s) added: 1

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

5.)配置端口转发
[root@kvm webvirtmgr]# ssh 192.168.100.10 -L localhost:8000:localhost:8000 -L localhost:6080:localhost:60
Last login: Sun Jul 28 21:27:47 2024 from 192.168.100.1
//查看段端口信息
[root@kvm ~]# ss -anlt
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      128                                             127.0.0.1:631                                                                 *:*                  
LISTEN      0      100                                             127.0.0.1:25                                                                  *:*                  
LISTEN      0      128                                             127.0.0.1:6010                                                                *:*                  
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      128                                                   ::1:631                                                                :::*                  
LISTEN      0      100                                                   ::1:25                                                                 :::*                  
LISTEN      0      128                                                   ::1:6010                                                               :::*                  
LISTEN      0      128                                                   ::1:6080                                                               :::*                  
LISTEN      0      128                                                   ::1:8000                                                               :::*   
6.)配置nginx
[root@kvm ~]# cd /etc/nginx/
[root@kvm nginx]# ls
conf.d     fastcgi.conf          fastcgi_params          koi-utf  mime.types          nginx.conf          scgi_params          uwsgi_params          win-utf
default.d  fastcgi.conf.default  fastcgi_params.default  koi-win  mime.types.default  nginx.conf.default  scgi_params.default  uwsgi_params.default
[root@kvm nginx]# cp nginx.conf nginx.conf.bak     //给配置文件做个备份
[root@kvm nginx]# vim nginx.conf
删除listen       [::]:80;行
参数server_name行改成server_name  localhost;
删除root         /usr/share/nginx/html;行

    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80;
        server_name  localhost;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
        //添加一部分
        location / {
                root    html;
                index  index.html index.htm;
        }
        error_page 404 /404.html;
        location = /404.html {
        }

//配置nginx虚拟主机
[root@kvm nginx]# cd conf.d/
[root@kvm conf.d]# ls
[root@kvm conf.d]# vim 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;
    }
}

//查看gunicorn.conf.py中的信息是否与上文webvirtmgr.conf中IP一致
[root@kvm conf.d]# cd /var/www/webvirtmgr/
[root@kvm webvirtmgr]# cd conf/
[root@kvm conf]# ls
conf.d  gunicorn.conf.py  init  initd  libvirt-bootstrap.sh  saltstack
[root@kvm conf]# vim gunicorn.conf.py 

bind = '127.0.0.1:8000'
backlog = 2048
//是一致的就保存退出就可以了

//重启nginx服务,并查看端口是否还处于开启状态
[root@kvm conf]# systemctl restart nginx
[root@kvm conf]# ss -anlt
State       Recv-Q Send-Q                                      Local Address:Port                                                     Peer Address:Port              
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:631                                                                 *:*                  
LISTEN      0      100                                             127.0.0.1:25                                                                  *:*                  
LISTEN      0      128                                             127.0.0.1:6010                                                                *:*                  
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      128                                                   ::1:631                                                                :::*                  
LISTEN      0      100                                                   ::1:25                                                                 :::*                  
LISTEN      0      128                                                   ::1:6010                                                               :::*                  
LISTEN      0      128                                                   ::1:6080                                                               :::*                  
LISTEN      0      128                                                   ::1:8000      

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

8.)配置nginx用户
[root@kvm ~]# 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:4sVaYlLBTcczZ4BmYa+Dd8K0mgr3X0ByBb3c+diUxJQ nginx@kvm.example.com
The key's randomart image is:
+---[RSA 2048]----+
|     ..o=*+. o.. |
|      .o=+= o E  |
|      oo+..B o . |
|     . O oo o o  |
|    . = S .  =   |
|     + X =  . o  |
|  . . =   .      |
|   o o   .       |
|    . ...        |
+----[SHA256]-----+
-bash-4.2$ touch ~/.ssh/config
-bash-4.2$ vim ~/.ssh/config     //在文件内写入
StrictHostKeyChecking=no
UserKnownHostsFile=/dev/null

-bash-4.2$ chmod 0600 ~/.ssh/config
-bash-4.2$ ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.100.10
/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.100.10' (ECDSA) to the list of known hosts.
root@192.168.100.10's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@192.168.100.10'"
and check to make sure that only the key(s) you wanted were added.
-bash-4.2$ ssh root@192.168.100.10     //验证密钥认证是否成功
Warning: Permanently added '192.168.100.10' (ECDSA) to the list of known hosts.
Last login: Sun Jul 28 21:37:00 2024 from 192.168.100.10
[root@kvm ~]# 
[root@kvm ~]# exit
登出
Connection to 192.168.100.10 closed.
-bash-4.2$ exit
登出
//验证完成就可以退出来了


//配置libvirt
[root@kvm ~]# 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@kvm ~]# chown -R root.root /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
[root@kvm ~]# systemctl restart nginx.service 
[root@kvm ~]# systemctl enable nginx.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@kvm ~]# systemctl restart libvirtd
[root@kvm ~]# systemctl enable libvirtd

四、访问web页面

当你访问网页时,命令窗口会一直弹出信息

[root@kvm ~]# accept: Too many open files
accept: Too many open files
accept: Too many open files
accept: Too many open files
accept: Too many open files
accept: Too many open files

解决方法:
//修改nginx配置文件
[root@kvm ~]# 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@kvm ~]# vim /etc/security/limits.conf
//在文件最末尾写入
* soft nofile 655350
* hard nofile 655350
//重启服务,重读文件
[root@kvm ~]# sysctl -p
[root@kvm ~]# systemctl restart nginx

在这里插入图片描述
进去之后会显示无连接,点击右上角的绿色的添加连接按钮
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
我这里使用的是FileZilla进行传输的
也可以使用Xshell
[root@kvm ~]# yum -y install lrzsz
[root@kvm ~]# cd /kvmdata/
将进行拖进来

在这里插入图片描述
在这里插入图片描述
//点击左侧网络池选项卡 ,然后defautl上方有个 NEW network,点击它
在这里插入图片描述
在这里插入图片描述
点击自定义实例,创建心得虚拟机
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

如果出现了连接超时,则做如下步骤,如果没有出现,则不管

解决方法:

[root@kvm ~]# yum -y install novnc
[root@kvm ~]# chmod +x /etc/rc.d/rc.local
[root@kvm ~]# vim /etc/rc.d/rc.local    //在最后写入
nohup novnc_server 192.168.100.10:5920 &
[root@kvm ~]# . /etc/rc.d/rc.local
[root@kvm ~]# nohup: 忽略输入并把输出追加到"nohup.out"    //这里回车

在这里插入图片描述
这里正常安装就可以了
在这里插入图片描述

  • 21
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值