一、环境配置
1、关闭防火墙和selinux
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
[root@localhost ~]# getenforce ##查看服务器selinux状态
[root@localhost ~]# setenforce 0 ##临时关闭selinux,服务器重启后失效
##永久关闭selinux,修改下述配置后,reboot重启服务器生效
[root@localhost ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
2、安装依赖
[root@localhost ~]# yum -y install epel-release vim wget net-tools unzip zip gcc gcc-c++
3、
[root@localhost ~]# vim /etc/security/limits.conf ##文件最后添加下述配置
* soft nofile 65535
* hard nofile 65535
### *代表全局, soft为软件, hard为硬件, nofile指可打开文件数
[root@localhost ~]# cat /etc/security/limits.conf |grep -w '*'|grep -v '^#' ##查看文件中非注释行配置
如下图返回:
[root@localhost ~]# vim /etc/pam.d/login ##配置文件中最后一行添加下述配置:
session required /lib/security/pam_limits.so
#上述配置重启服务器生效
#下述临时生效
[root@localhost ~]# vim /etc/profile ##配置中最后一行添加下述配置:
ulimit -n 65535
[root@localhost ~]# source /etc/profile ##使上述配置生效
[root@localhost ~]# ulimit -n ##查看上述配置是否生效,如下图返回成功
4、验证CPU是否支持KVM
虚拟机勾选下图配置:
[root@localhost ~]# egrep -o 'vmx|svm' /proc/cpuinfo ###查看是否支持KVM,下述返回有vmx或者svm,表示CPU支持KVM
###如下图,有返回vmx,表示CPU支持KVM
二、KVM安装
1、安装kvm
###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
[root@localhost ~]# systemctl status libvirtd ##查看libvirtd状态
[root@localhost ~]# systemctl start libvirtd ##启动
[root@localhost ~]# systemctl enable libvirtd ##配置开启可自启动
###检查是否有kvm模块
[root@localhost ~]# lsmod |grep kvm
###查看当前系统里面有没有
[root@localhost ~]# virsh -c qemu:///system list
###创建软连接
[root@localhost ~]# ln -s /usr/libexec/qemu-kvm /usr/bin/qemu-kvm
[root@localhost ~]# ll /usr/bin/qemu-kvm
2、KVM对应web管理界面安装webvirtmgr程序
###安装依赖
[root@localhost ~]# yum -y install git python-pip libvirt-python libxml2-python python-websockify supervisor nginx python-devel
##更新升级pip
##此处需要注意,pip默认的安装源地址,有事会访问不通,故可换成国内的源地址
##如下国内源地址可选
阿里云 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/
[root@localhost ~]# pip install --upgrade pip -i https://mirrors.aliyun.com/pypi/simple/
##直接运行可能会报下图错误:
###修改为下述安装命令,升级pip
[root@localhost ~]# pip3 install --upgrade pip -i https://mirrors.aliyun.com/pypi/simple/
###从github上下载webvirtmgr代码包
[root@localhost ~]# cd /usr/local/src/
[root@localhost src]# git clone https://github.com/retspen/webvirtmgr.git
##安装webvirtmgr
[root@localhost ~]# cd /usr/local/src/webvirtmgr
[root@localhost webvirtmgr]# pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/ ##此处源地址建议配置国内地址
3、检查sqlite3是否安装
###验证是否可以导入
[root@localhost webvirtmgr]# python
###如下图看到,可以导入,无报错,表示sqlite3安装成功
4、初始化账号信息
[root@localhost webvirtmgr]# python manage.py syncdb
###如下图
5、复制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/
6、生成秘钥
###直接全回车,生成秘钥
[root@localhost webvirtmgr]# ssh-keygen -t rsa
###将生成的秘钥,传到要新人的服务器,可远程免密登录
##上述在webvirtmgr部署服务器生成秘钥,KVM部署在其他服务器上的话,需要把上述生成的秘钥传到KVM部署服务器上
[root@localhost webvirtmgr]# ssh-copy-id 192.168.170.10 ###IP为KVM部署服务器IP
7、端口转发,查看
###端口转发
[root@localhost ~]# ssh 192.168.170.10 -L localhost:8000:localost:8000 -L localhost:6080:localhost:60
8、配置nginx
###配置nginx.conf配置
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
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 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
listen [::]:80;
server_name localhost;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
===========
##配置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;
}
}
9、确定bind绑定的是本机的8000端口
cat /var/www/webvirtmgr/conf/gunicorn.conf.py |grep -v '^#'
##如下图,可以看到
10、启动nginx
[root@localhost nginx]# systemctl start nginx ##启动
[root@localhost nginx]# systemctl enable nginx ##配置开机自启动
11、设置supervisor
##在文件末尾添加
[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
12、启动supervisor和设置开机自启动
[root@localhost ~]# systemctl status supervisord
[root@localhost ~]# systemctl start supervisord
[root@localhost ~]# systemctl enable supervisord
13、配置nginx用户
未创建nginx用户,所以用su命令赋予它交互式登录权限
[root@localhost ~]# su - nginx -s /bin/bash
-bash-4.2$ ssh-keygen -t rsa
##直接全部回车
-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.170.10
##登出nginx
-bash-4.2$ exit
登出
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
chown -R root.root /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla ##文件赋权
systemctl restart supervisord
systemctl restart libvirtd
三、上述部署完,浏览器web端访问kvm管理界面
1、web登录配置
web地址:http://192.168.170.10/
账号:root
密码:123456 ##上述第8步【python manage.py syncdb】初始化的时候设置的root密码
上述存储池创建好后,后台上传软件ISO镜像到服务器上述存储池配置路径下:
2、创建系统安装镜像:
3、创建虚拟机实例
然后,继续就是常见的虚拟机安装步骤
4、上述虚拟机安装完,给虚拟机配置网路,能访问宿主机和公网
cd /etc/sysconfig/network-scripts
vi ifcfg-eth0 ##修改为下述配置
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=dhcp
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=eth0
UUID=c6a7c3d4-8089-4980-897e-e668b23043ea
DEVICE=ens33
ONBOOT=yes ###此处改为yes
###重启网络服务
systemctl restart network
##如下图可以看到
注:如果上述配置完,还是访问公网不通,可以添加下述配置后重启网络服务
###在ifcfg-eth0最后添加一行配置:
NM_CONTROLLED=no
###然后重启网络服务
systemctl restart network
四、KVM常见部署报错
1、pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple 安装依赖报错
问题原因:https://pypi.tuna.tsinghua.edu.cn/simple 地址访问不通,可换成国内地址:
阿里云 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/
替换为阿里云地址安装,可能会报如上错误:
解决方法:pip替换为pip3安装
2、KVM的web端安装好之后,访问控制台报错
noVNC ready: native WebSockets, canvas rendering
#解决方法:
#安装novnc
yum -y install novnc
重启服务:
systemctl restart supervisord
systemctl restart libvirtd
问题解决