kvm虚拟化安装

关闭防火墙与selinux

[root@ c7-43 ~]# systemctl stop firewalld
[root@ c7-43 ~]# systemctl disable firewalld
[root@ c7-43 ~]# setenforce 0
[root@ c7-43 ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

安装依赖环境

[root@ localhost ~]# yum -y install epel-release vim wget net-tools unzip zip gcc gcc-c++

配置环境修改网站访问并发量

#末尾添加  星号代表全局, soft为软件,hard为硬件,nofile为这里指可打开文件数。
[root@ localhost ~]# vim /etc/security/limits.conf
[root@ localhost ~]# cat /etc/security/limits.conf |grep -w "*" | grep -v "^#"
* soft nofile 65535
* hard nofile 65535

#末尾添加
[root@ localhost ~]# vim /etc/pam.d/login
[root@ localhost ~]# cat /etc/pam.d/login |sed -n "19p"
session    required     /lib/security/pam_limits.so

#末尾添加
[root@ localhost ~]# vim /etc/profile
[root@ localhost webvirtmgr]# cat /etc/profile | grep -w "ulimit"
ulimit -n 65535

[root@ localhost ~]# source /etc/profile
[root@ localhost ~]# ulimit -n
65535

验证CPU是否支持KVM;如果结果中有vmx(Intel)或svm(AMD)字样,就说明CPU的支持的

先关机

[root@ localhost ~]# shutdown -h

关机之后编辑虚拟机,选打钩的
kvm虚拟化安装_第1张图片

[root@ localhost ~]# egrep -o 'vmx|svm' /proc/cpuinfo
vmx
vmx

kvm安装

[root@ localhost ~]# 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
#文件大,需要等几分钟

启动服务,验证安装结果

[root@ localhost ~]# systemctl start libvirtd
[root@ localhost ~]# systemctl enable libvirtd
[root@ localhost ~]# lsmod|grep kvm
kvm_intel             188644  0
kvm                   621480  1 kvm_intel
irqbypass              13503  1 kvm
[root@ localhost ~]# virsh -c qemu:///system list
 Id    Name                           State
----------------------------------------------------

[root@ localhost ~]# ln -s /usr/libexec/qemu-kvm /usr/bin/qemu-kvm
[root@ localhost ~]# ll /usr/bin/qemu-kvm
lrwxrwxrwx 1 root root 21 May  9 21:59 /usr/bin/qemu-kvm -> /usr/libexec/qemu-kvm

kvm web管理界面安装
kvm 的 web 管理界面是由 webvirtmgr 程序提供的

安装依赖包

[root@ localhost ~]# yum -y install git python-pip libvirt-python libxml2-python python-websockify supervisr nginx python-devel
#等待下载完毕之后,升级pip
[root@ localhost ~]# pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple
Collecting pip
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/54/2e/df11ea7e23e7e761d484ed3740285a34e38548cf2badbed3dd5768ec8b9/pip-20.1-py2.py3-none-any.whl (1.5MB)
    100% |████████████████████████████████| 1.5MB 475kB/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.1

pip 20.0.2文档 https://pip.pypa.io/en/stable/user_guide/#config-file
pip是一个很好用的第三方库安装方式,但是默认的源没法连接,就算有时候可以成功率也很低,所以换成国内镜像源比较方便。
将pip源更换到国内镜像

用pip管理工具安装库文件时,默认使用国外的源文件,因此在国内的下载速度会比较慢,可能只有50KB/s。幸好,国内的一些顶级科研机构已经给我们准备好了各种镜像,下载速度可达2MB/s。

临时方法

阿里云 https://mirrors.aliyun.com/pypi/simple/
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
豆瓣(douban) https://pypi.douban.com/simple/
清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/
中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple/
华中理工大学:http://pypi.hustunique.com/
山东理工大学:http://pypi.sdutlinux.org/

从github上下载webvirtmgr代码

[root@ localhost ~]#  cd /usr/local/src/
[root@ localhost src]# git clone git://github.com/retspen/webvirtmgr.git
#如果有安装包可以直接上传安装包操作,因为下载比较慢

[root@ localhost src]# rz
[root@ localhost src]# ls
webvirtmgr.tar
[root@ localhost src]# gzip webvirtmgr.tar
[root@ localhost src]# ls
webvirtmgr.tar.gz
[root@ localhost src]# tar -zxf webvirtmgr.tar.gz
[root@ localhost src]# ls
webvirtmgr  webvirtmgr.tar.gz

安装webvirtmgr

[root@ localhost webvirtmgr]# pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Pyton 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details abot Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#pytho-2-support
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting django==1.5.5
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/38/49/93511c5d3367b6b21fc2995a0e53399721afc15e4cd6b57be879ae13ad4/Django-1.5.5.tar.gz (8.1 MB)
     |████████████████████████████████| 8.1 MB 329 kB/s
Collecting gunicorn==19.5.0
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/f9/4e/f4076a1a57fc1e75edc0828db365cfa9005f9f6b4a51489ae39a91eb4be/gunicorn-19.5.0-py2.py3-none-any.whl (113 kB)
     |████████████████████████████████| 113 kB 4.6 MB/s
Collecting lockfile>=0.9
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/c8/22/9460e311f340cb62d26a38c419b1381b8593b0bb6b5df056938b086d362/lockfile-0.12.2-py2.py3-none-any.whl (13 kB)
Could not build wheels 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@ localhost 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@ 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_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: [email protected]
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/

生成密钥 首次生成直接回车,不是则Overwrite (y/n)? y 选择覆盖

[root@ localhost ~]# 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:sKh3T3c+zHPupfd/uszLOdg8CGUXyaU5aL6vdQqcZW4 [email protected]
The key's randomart image is:
+---[RSA 2048]----+
|             . o.|
|             .+o |
|      .     o +. |
|     . o   oo .. |
|    . . S  o..o  |
|   .      .. *   |
|  . . . . .+*=E o|
|   . . o . oBO**o|
|        .   oB#B*|
+----[SHA256]-----+
[root@ localhost ~]# cd /root/.ssh/
[root@ localhost .ssh]# ls
id_rsa  id_rsa.pub

由于这里webvirtmgr和kvm服务部署在同一台机器,所以这里本地信任。如果kvm部署在其他机器,则需要更换ip

[root@ localhost .ssh]# cd /usr/local/src/webvirtmgr/
[root@ localhost webvirtmgr]# ssh-copy-id 10.0.0.43
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '10.0.0.43 (10.0.0.43)' can't be established.
ECDSA key fingerprint is SHA256:azg36VNWOuua+J1VKdb0Cy20RuWDGqdwQIjSSA4FZqE.
ECDSA key fingerprint is MD5:24:06:df:b1:5c:3b:6f:79:9a:74:56:0a:32:fb:b6:be.
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 intalled
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install thenew keys
[email protected]'s password:

Number of key(s) added: 1

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

端口转发,查看

[root@ localhost webvirtmgr]# ss -antl
State      Recv-Q Send-Q        Local Address:Port                       Peer Address:Port
LISTEN     0      100               127.0.0.1:25                                    *:*
LISTEN     0      128               127.0.0.1:8000                                  *:*
LISTEN     0      100                       *:6080                                  *:*
LISTEN     0      1                         *:5900                                  *:*
LISTEN     0      128                       *:80                                    *:*
LISTEN     0      5                172.16.0.1:53                                    *:*
LISTEN     0      5             192.168.122.1:53                                    *:*
LISTEN     0      128                       *:22                                    *:*
LISTEN     0      100                   [::1]:25                                 [::]:*
LISTEN     0      128                    [::]:22                                 [::]:*

配置nginx

[root@ localhost webvirtmgr]# vim /etc/nginx/nginx.conf
[root@ localhost webvirtmgr]# cat /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 {
        }
    }
}

添加webvirtmgr.conf

[root@ localhost webvirtmgr]# vim /etc/nginx/conf.d/webvirtmgr.conf
[root@ localhost webvirtmgr]# cat /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@ localhost webvirtmgr]# cat /var/www/webvirtmgr/conf/gunicorn.conf.py | sed -n "23,24p"
bind = '127.0.0.1:8000'
backlog = 2048

启动nginx

[root@ localhost webvirtmgr]# systemctl start nginx
[root@ localhost webvirtmgr]# systemctl status nginx

设置supervisor

[root@ localhost webvirtmgr]# cat /etc/supervisord.conf	#末尾添加,如下
[root@ localhost webvirtmgr]# cat /etc/supervisord.conf | sed -n "130,146p"
[program:webvirtmgr]
command=/usr/bin/python2 /var/www/webvirtmgr/manage.py run_gunicorn -c /var/www/webvirtmgr/conf/gunicorn.cof.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@ localhost webvirtmgr]# systemctl start supervisord
[root@ localhost webvirtmgr]# systemctl enable supervisord
Created symlink from /etc/systemd/system/multi-user.target.wants/supervisord.service to /usr/lib/systemd/sytem/supervisord.service.
[root@ localhost webvirtmgr]# 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 Sat 2020-05-09 22:52:33 CST; 1h 25min ago
  Process: 21141 ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf (code=exited, status=0/SUCCESS)
 Main PID: 21145 (supervisord)
    Tasks: 21
   CGroup: /system.slice/supervisord.service
           ├─21145 /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf
           ├─21146 /usr/bin/python2 /var/www/webvirtmgr/console/webvirtmgr-console
           ├─21147 /usr/bin/python2 /var/www/webvirtmgr/manage.py run_gunicorn -c /var/www/webvirtmgr/c...
           ├─21153 /usr/bin/python2 /var/www/webvirtmgr/manage.py run_gunicorn -c /var/www/webvirtmgr/c...
           ├─21154 /usr/bin/python2 /var/www/webvirtmgr/manage.py run_gunicorn -c /var/www/webvirtmgr/c...
           ├─21155 /usr/bin/python2 /var/www/webvirtmgr/manage.py run_gunicorn -c /var/www/webvirtmgr/c...
           ├─21156 /usr/bin/python2 /var/www/webvirtmgr/manage.py run_gunicorn -c /var/www/webvirtmgr/c...
           ├─21157 /usr/bin/python2 /var/www/webvirtmgr/manage.py run_gunicorn -c /var/www/webvirtmgr/c...
           ├─21338 ssh -l root -- 10.0.0.43 sh -c 'if 'nc' -q 2>&1 | grep "requires an argument" >/dev/...
           ├─21351 ssh -l root -- 10.0.0.43 sh -c 'if 'nc' -q 2>&1 | grep "requires an argument" >/dev/...
           ├─21369 ssh -l root -- 10.0.0.43 sh -c 'if 'nc' -q 2>&1 | grep "requires an argument" >/dev/...
           ├─21382 ssh -l root -- 10.0.0.43 sh -c 'if 'nc' -q 2>&1 | grep "requires an argument" >/dev/...
           ├─21497 ssh -l root -- 10.0.0.43 sh -c 'if 'nc' -q 2>&1 | grep "requires an argument" >/dev/...
           ├─21578 /usr/bin/python2 /var/www/webvirtmgr/console/webvirtmgr-console
           └─21579 ssh -l root -- 10.0.0.43 sh -c 'if 'nc' -q 2>&1 | grep "requires an argument" >/dev/...

May 09 22:52:33 localhost.localdomain systemd[1]: Stopped Process Monitoring and Control Daemon.
May 09 22:52:33 localhost.localdomain systemd[1]: Starting Process Monitoring and Control Daemon...
May 09 22:52:33 localhost.localdomain systemd[1]: Started Process Monitoring and Control Daemon.

配置nginx用户 未创建nginx用户,所以用su命令赋予它交互式登录的权限

[root@ localhost webvirtmgr]# su - nginx -s /bin/bash
[nginx@ localhost ~]$ 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:Fs88rvaU+Gu8+to99Qu74pCnyOvyFm5/9jUsUvU0OFg [email protected]
The key's randomart image is:
+---[RSA 2048]----+
|             E   |
|            o .  |
|        .  . o o.|
|         =    o.o|
|        S =  .  .|
|       o o.o. o  |
|      . o++o + = |
|     ..+o=*=o = o|
|      *B*O@++=...|
  ---[SHA256]-----+
[nginx@ localhost ~]$  touch ~/.ssh/config && echo -e "StrictHostKeyChecking=no\nUserKnownHostsFile=/dev/nul" >> ~/.ssh/config
[nginx@ localhost ~]$ chmod 0600 ~/.ssh/config
[nginx@ localhost ~]$
[nginx@ localhost ~]$ ssh-copy-id [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 instaled
/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the newkeys
Warning: Permanently added '10.0.0.43' (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.

[nginx@ localhost ~]$ exit
logout
[root@ localhost webvirtmgr]# 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 webvirtmgr]# chown -R root.root /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-ccess.pkla
[root@ localhost webvirtmgr]# systemctl restart supervisord
[root@ localhost webvirtmgr]# systemctl restart libvirtd

kvm web界面管理

浏览器访问:http://10.0.0.43
kvm虚拟化安装_第2张图片
kvm虚拟化安装_第3张图片
kvm虚拟化安装_第4张图片
kvm虚拟化安装_第5张图片
kvm虚拟化安装_第6张图片
kvm虚拟化安装_第7张图片

通过远程连接软件上传ISO镜像文件至存储目录/var/lib/libvirt/images/

#上传镜像在这里插入图片描述

在web界面查看ISO镜像文件是否存在

kvm虚拟化安装_第8张图片

创建系统安装镜像

添加镜像
kvm虚拟化安装_第9张图片
kvm虚拟化安装_第10张图片
kvm虚拟化安装_第11张图片

kvm网络管理

kvm虚拟化安装_第12张图片
kvm虚拟化安装_第13张图片

实例管理 实例(虚拟机的创建)

kvm虚拟化安装_第14张图片
kvm虚拟化安装_第15张图片

虚拟机插入光盘

kvm虚拟化安装_第16张图片

设置在web上访问虚拟机的密码

kvm虚拟化安装_第17张图片

启动虚拟机

kvm虚拟化安装_第18张图片

虚拟机安装

kvm虚拟化安装_第19张图片
手动选上第一个 Install CentOS7

kvm虚拟化安装_第20张图片
跟平常安装虚拟机一样,然后登陆
kvm虚拟化安装_第21张图片

可能遇到的问题

1、web页面刷新不出来
虚拟机报错:命令行界面一直报错(too many open files)
解决方法:https://blog.51cto.com/inbank2012/1097939

2、控制台连接失败

如果出现上图报错或者连接超时
解决方法:

安装novnc并通过novnc_server启动一个vnc
[root@ c7-43 images]# yum -y install novnc
#在末尾添加
[root@ c7-43 ~]# cat /etc/rc.d/rc.local | grep “nohup”
nohup novnc_server 172.16.12.128:5920 &
[root@ c7-43 images]# . /etc/rc.d/rc.local
[root@ c7-43 images]# nohup: ignoring input and appending output to ‘nohup.out’

你可能感兴趣的:(linux)