kvm虚拟化
kvm部署
环境:
系统类型 | ip |
---|---|
centos7 | 192.168.147.55(kvm) |
centos7 | 192.168.147.66(web) |
开启虚拟化功能
kvm安装
部署前请确保你的CPU虚拟化功能已开启。分为两种情况:
- 虚拟机要关机设置CPU虚拟化
- 物理机要在BIOS里开启CPU虚拟化
//关闭防火墙与SELINUX
[root@kvm ~]# systemctl stop firewalld
[root@kvm ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@kvm ~]# setenforce 0
[root@localhost ~]# reboot
//验证CPU是否支持
[root@kvm ~]# egrep -o 'vmx|svm' /proc/cpuinfo
vmx
vmx
vmx
vmx
//kvm安装
[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
//安装工具
yum -y install epel-release vim wget net-tools unzip zip gcc gcc-c++
//配置网络
[root@kvm ~]# cd /etc/sysconfig/network-scripts/
[root@kvm network-scripts]# cp ifcfg-ens33 ifcfg-br0
[root@kvm network-scripts]# vim ifcfg-br0
[root@kvm ~]# cat /etc/sysconfig/network-scripts/ifcfg-br0
TYPE=Bridge
BOOTPROTO=static
NM_CONTROLLED=no
NAME=br0
DEVICE=br0
ONBOOT=yes
IPADDR=192.168.147.55
NETMASK=255.255.255.0
GATEWAY=192.168.147.2
DNS1=114.114.114.114
[root@kvm ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
NAME=ens33
DEVICE=ens33
ONBOOT=yes
BRIDGE=br0
NM_CONTROLLED=no
[root@kvm network-scripts]# systemctl restart network
//启动服务
[root@kvm ~]# systemctl enable --now libvirtd
[root@kvm ~]# systemctl status libvirtd
● libvirtd.service - Virtualization daemon
Loaded: loaded (/usr/lib/systemd/system/libvirtd.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2021-06-20 09:45:13 EDT; 5s ago
Docs: man:libvirtd(8)
https://libvirt.org
Main PID: 2760 (libvirtd)
Tasks: 19 (limit: 32768)
CGroup: /system.slice/libvirtd.service
├─2760 /usr/sbin/libvirtd
├─2880 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshelper
└─2881 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshelper
//验证
[root@kvm ~]# lsmod|grep kvm
kvm_intel 188654 0
kvm 63245 1 kvm_intel
irqbypass 13423 1 kvm
[root@kvm ~]# virsh -c qemu:///system list
Id Name State
----------------------------------------------------
[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
[root@kvm ~]# ll /usr/bin/qemu-kvm
lrwxrwxrwx 1 root root 21 Jun 20 09:45 /usr/bin/qemu-kvm -> /usr/libexec/qemu-kvm
//查看网桥信息
[root@kvm ~]# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.000c297b512e no ens33
virbr0 8000.5254009b92d8 yes virbr0-nic
kvm web 页面安装
//安装依赖包
[root@web ~]# yum -y install epel-release
[root@web ~]# yum -y install git python-pip libvirt-python libxml2-python python-websockify supervisor nginx python-deve
//在github上下载webvirtmgr代码
[root@web ~]# git clone git://github.com/retspen/webvirtmgr.git
Cloning into 'webvirtmgr'...
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 | 610.00 KiB/s, done.
Resolving deltas: 100% (3606/3606), done.
//安装webvirtmgr
[root@web ~]# ls
anaconda-ks.cfg webvirtmgr
[root@web ~]# cd webvirtmgr/
[root@web webvirtmgr]# pip install -r requirements.txt
//检查sqlite3是否安装
[root@web webvirtmgr]# python
Python 2.7.5 (default, Aug 7 2019, 00:51:29)
[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@web webvirtmgr]# python manage.py syncdb
……
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: 15442114@qq.com #邮箱
Password:
Password (again):
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 6 object(s) from 1 fixture(s)
//将网站移动到指定目录并修改属组、属主
[root@web webvirtmgr]# mkdir -p /var/www
[root@web webvirtmgr]# cp -r /root/webvirtmgr/ /var/www/
[root@web webvirtmgr]# chown -R nginx.nginx /var/www/webvirtmgr/
//免密登录
[root@web webvirtmgr]# 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:uvnoeyte8tuq458jdISUT54YESghy4YeY554YT3 root@web
The key's randomart image is:
+---[RSA 2048]----+
| . ooo. |
| . E .o. |
| o.. ... |
| oo.+=. . |
|.o=o*+* S |
|++O*.X. |
|.*=O*. |
|.+ooo |
|..=o |
+----[SHA256]-----+
[root@web webvirtmgr]# ssh-copy-id root@192.168.147.66
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.147.66 (192.168.147.66)' can't be established.
ECDSA key fingerprint is SHA256:qScIS0D/MHXJczgQLJo7DCUkeuEpYBKCPV0DtR3rNCA.
ECDSA key fingerprint is MD5:cf:52:9c:f5:74:1f:54:25:52:a9:ce:50:47:33:16:ad.
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.147.66's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.147.66'"
and check to make sure that only the key(s) you wanted were added.
[root@web webvirtmgr]# ssh 192.168.147.66
Last login: Sun Jun 20 04:48:00 2021 from 192.168.147.1
配置nginx
[root@web ~]# cd /etc/nginx/
[root@web nginx]# mv nginx.conf nginx.conf-bak
[root@web nginx]# vim /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 {
}
}
}
[root@web nginx]# vi /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@web nginx]# vi /var/www/webvirtmgr/conf/gunicorn.conf.py
bind = '0.0.0.0:8000'
backlog = 2048
//修改supervisord.conf文件
[root@web nginx]# cat >> /etc/supervisord.conf <<EOF
[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
EOF
//启动supervisord
[root@web nginx]# systemctl enable --now supervisord
Created symlink from /etc/systemd/system/multi-user.target.wants/supervisord.service to /usr/lib/systemd/system/supervisord.service.
[root@web nginx]# systemctl restart nginx
[root@web nginx]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 100 *:6080 *:*
LISTEN 0 128 *:8000 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 [::1]:25 [::]:*
LISTEN 0 128 [::]:22
//配置nginx账户,做免密登录
[root@web nginx]# su - nginx -s /bin/bash
-bash-4.2$ 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:sset85954ug8uenguih5hv9v5633y2hfwq84y84y6h nginx@web
The key's randomart image is:
+---[RSA 2048]----+
| ... oo.=+o+OO|
| + o o * .o==*|
| o + . . + o.E+|
| . . . = . |
|. . . . S. B o |
| o o . o oO o |
| . . .o.. |
| |
| |
+----[SHA256]-----+
-bash-4.2$ ssh-copy-id root@192.168.147.55
/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/var/lib/nginx/.ssh/id_rsa.pub"
The authenticity of host '192.168.147.55 (192.168.147.55)' can't be established.
ECDSA key fingerprint is SHA256:qScIS0D/MHXJczgQLJo7DCUkeuEpYBKCPV0DtR3rNCA.
ECDSA key fingerprint is MD5:cf:52:9c:f5:74:1f:54:25:52:a9:ce:50:47:33:16:ad.
Are you sure you want to continue connecting (yes/no)? yes
/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
root@192.168.147.55's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.147.55'"
and check to make sure that only the key(s) you wanted were added.
-bash-4.2$ exit
logout
[root@web nginx]# vi /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@web nginx]# systemctl restart nginx
[root@kvm ~]# systemctl restart libvirtd
//关闭防火墙
[root@web nginx]# systemctl disable --now firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@web nginx]# setenforce 0
[root@web nginx]# vim /etc/selinux/config
SELINUX=disabled
web页面配置
故障案例
案例1
web界面配置完成后可能会出现以下错误界面
解决方法是安装novnc并通过novnc_server启动一个vnc
[root@localhost ~]# ll /etc/rc.local
lrwxrwxrwx. 1 root root 13 Aug 6 2018 /etc/rc.local -> rc.d/rc.local
[root@localhost ~]# ll /etc/rc.d/rc.local
-rw-r--r-- 1 root root 513 Mar 11 22:35 /etc/rc.d/rc.local
[root@localhost ~]# chmod +x /etc/rc.d/rc.local
[root@localhost ~]# ll /etc/rc.d/rc.local
-rwxr-xr-x 1 root root 513 Mar 11 22:35 /etc/rc.d/rc.local
[root@localhost ~]# vim /etc/rc.d/rc.local
......此处省略N行
# that this script will be executed during boot.
touch /var/lock/subsys/local
nohup novnc_server 172.16.12.128:5920 &
[root@localhost ~]# . /etc/rc.d/rc.local
做完以上操作后再次访问即可正常访问
案例2
第一次通过web访问kvm时可能会一直访问不了,一直转圈,而命令行界面一直报错(too many open files)
此时需要对nginx进行配置
[root@localhost ~]# vim /etc/nginx/nginx.conf
....此处省略N行
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
worker_rlimit_nofile 655350; //添加此行配置
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
....此处省略N行
[root@localhost ~]# systemctl restart nginx
然后对系统参数进行设置
[root@localhost ~]# vim /etc/security/limits.conf
....此处省略N行
# End of file
* soft nofile 655350
* hard nofile 655350