KVM 虚拟化及配置
- 全称: Kernel-based Virtual Machine(基于内核的虚拟机)。
- 核心原理: 它不是一個像 VirtualBox 或 VMware Workstation 那样的独立应用程序。KVM 是 Linux 内核本身的一个模块。它通过将 Linux 内核转变为一个裸机管理程序(Hypervisor) 来工作。
- 运作方式:
- Linux 内核加载
kvm
模块后,本身就直接成为了 Hypervisor。 - 每个虚拟机(Guest)都是一个普通的 Linux 进程(如
qemu-system-x86_64
),由标准 Linux 调度程序进行调度。 - 虚拟机的硬件(如 CPU、内存)由 KVM 内核模块直接管理,提供了极高的性能。
- 用户空间的工具(主要是 QEMU)则负责模拟虚拟机的 I/O 设备(如网卡、磁盘、显卡等)。
- Linux 内核加载
简单来说:KVM 负责 CPU 和内存的虚拟化,提供性能;QEMU 负责 I/O 设备的模拟,提供兼容性。 两者结合,构成了一个完整的虚拟化解决方案。
KVM 的主要特点和优势
- 高性能 & 低开销:
- 由于是内核原生支持,并且需要 CPU 硬件虚拟化扩展(Intel VT-x 或 AMD-V),其性能非常接近物理机,开销极小。
- 安全性:
- 虚拟机作为 Linux 进程运行,可以利用 Linux 内核的所有安全特性(如 SELinux)进行隔离和保护。
- 开源:
- 作为 Linux 内核的一部分,它是完全开源的,无需支付任何许可费用。
- 与 Linux 生态无缝集成:
- 管理工具(如
libvirt
、virt-manager
)非常成熟。 - 可以轻松使用 Linux 上的所有功能,如cgroups(资源控制)、网络桥接等。
- 管理工具(如
- 支持多种操作系统:
- 可以运行各种 Linux 发行版、Windows、BSD 等客户机操作系统。
- 高级功能:
- 支持实时迁移(Live Migration):在不中断服务的情况下将运行中的虚拟机从一台物理主机迁移到另一台。
- 支持超配(Overcommitment):可以分配超过物理实际数量的虚拟 CPU 和内存。
- 支持硬件直通(PCI Passthrough):将物理硬件(如网卡、GPU)直接分配给虚拟机使用,获得近乎原生的性能。
- 底层: QEMU/KVM 是实际创建和运行虚拟机的引擎,但它本身比较复杂,直接使用不便。
- 中间层: Libvirt 作为抽象层和管理 API,统一了不同 Hypervisor(KVM, Xen, VMware 等)的管理方式。绝大多数工具都基于 Libvirt。
- 应用层: 我们实际使用的各种命令行和图形化工具,它们通过调用 Libvirt API 来管理虚拟机。
kvm 部署
开始前确保
1.centos7部署
2.cpu虚拟化功能开启
3.内存8G,磁盘空间200G
4.关闭防火墙和selinux
创建200G分区,然后挂载(略)
使用网络yum源(curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo)
[root@xieyuhui3 yum.repos.d]# yum -y install epel-release
安装所需软件包
[root@xieyuhui3 ~]# yum -y install vim wget net-tools unzip zip gcc gcc-c++
查看cpu是否支持kvm,vmx是intel的 svm是AMD的
[root@xieyuhui3 ~]# egrep -o 'vmx|svm' /proc/cpuinfo
安装kvm
[root@xieyuhui3 ~] 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@xieyuhui3 ~]# cd /etc/sysconfig/network-scripts/
[root@xieyuhui3 network-scripts]# cp ifcfg-ens33 ifcfg-br0
[root@xieyuhui3 network-scripts]# vim ifcfg-br0
TYPE=Bridge
BOOTPROTO=none
NAME=br0
DEVICE=br0
ONBOOT=yes
IPADDR=192.168.100.30
PREFIX=24
GATEWAY=192.168.100.254
DNS1=114.114.114.114
[root@xieyuhui3 network-scripts]# vim ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=none
NAME=ens33
DEVICE=ens33
ONBOOT=yes
BRIDGE=br0
重启网卡服务
[root@xieyuhui3 network-scripts]# systemctl restart NetworkManager
[root@xieyuhui3 network-scripts]# systemctl restart network
重启libvirtd服务
[root@xieyuhui3 ~]# systemctl restart libvirtd
[root@xieyuhui3 ~]# systemctl enable libvirtd
将qemu-kvm这个命令做一个软链接到/usr/bin/qemu-kvm
[root@xieyuhui3 ~]# ln -s /usr/libexec/qemu-kvm /usr/bin/qemu-kvm
安装依赖包
[root@xieyuhui3 ~]# yum -y install git python-pip libvirt-python libxml2-python python-websockify supervisor nginx python-devel
从github上下载webvirtmgr代码
[root@xieyuhui3 ~]# cd /usr/local/src/
[root@xieyuhui3 src]# git clone https://github.com/retspen/webvirtmgr.git
安装webvirtmgr
[root@xieyuhui3 src]# pip --trusted-host pypi.python.org install -r requirements.txt
初始化账号信息
[root@xieyuhui3 src]# cd /usr/local/src/webvirtmgr/
[root@xieyuhui3 webvirtmgr]# python manage.py syncdb
创建超级管护理员,用户名为root,设置管理员邮箱,设置超级管理员密码
拷贝web网页到指定目录
[root@xieyuhui3 webvirtmgr]# mkdir /var/www
[root@xieyuhui3 webvirtmgr]# cp -r /usr/local/src/webvirtmgr/ /var/www/
[root@xieyuhui3 webvirtmgr]# chown -R nginx.nginx /var/www/webvirtmgr/
生成一对公钥与私钥,这里webvirtmgr和kvm服务部署在同一台主机中,所以这里本地信任。如果kvm部署在其他机器上的时候,那么就需要把公钥发送到kvm主机中
[root@xieyuhui3 webvirtmgr]# ssh-keygen
[root@xieyuhui3 webvirtmgr]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.100.30
配置端口转发
[root@xieyuhui3 webvirtmgr]# ssh 192.168.100.30 -L localhost:8000:localhost:8000 -L localhost:6080:localhost:60
配置nginx
[root@xieyuhui3 ~]# cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
[root@xieyuhui3 ~]# vim /etc/nginx/nginx.conf
配置nginx虚拟主机
[root@xieyuhui3 ~]# vim /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@xieyuhui3 ~]# vim /var/www/webvirtmgr/conf/gunicorn.conf.py
重启nginx服务,查看端口是否开启
[root@xieyuhui3 ~]# systemctl restart nginx
[root@xieyuhui3 ~]# ss -antl
设置supervisor
[root@xieyuhui3 ~]# 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
启动supervisor
[root@xieyuhui3 ~]# systemctl restart supervisord
[root@xieyuhui3 ~]# systemctl enable supervisord
配置nginx用户
[root@xieyuhui3 ~]# su - nginx -s /bin/bash
-bash-4.2$ ssh-keygen
-bash-4.2$ touch ~/.ssh/config
-bash-4.2$ vim ~/.ssh/config
-bash-4.2$ chmod 0600 ~/.ssh/config
-bash-4.2$ ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.100.30
退出
-bash-4.2$ exit
[root@xieyuhui3 ~]# vim /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
[root@xieyuhui3 ~]# chown -R root.root /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
[root@xieyuhui3 ~]# systemctl restart nginx
[root@xieyuhui3 ~]# systemctl restart libvirtd
使用浏览器访问后会一直出现 accept:Too many open files
我们通过修改nginx配置文件来解决
[root@xieyuhui3 ~]# vim /etc/nginx/nginx.conf
对系统参数进行设置
[root@xieyuhui3 ~]# vim /etc/security/limits.conf
在文件最后加上
重启服务,重读文件
[root@xieyuhui3 ~]# sysctl -p
[root@xieyuhui3 ~]# systemctl restart nginx
再次使用谷歌浏览器访问