- KVM是构成主流Linux内核一部分的第一个虚拟化解决方案
- 以下内容汲取网上各路大神,各种踩坑后的总结,不拖泥带水,也没有过多的注释,复制粘贴即可成功部署完成。
- kvm和webvirtmgr可部署在同一台服务器上,但是以下操作为kvm和webvirtmgr分2台服务器部署
一、部署kvm服务
安装前准备
cat /proc/cpuinfo |grep vmx #检查服务器是否支持虚拟化
systemctl stop firewalld
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
安装依赖组件
yum install -y wget
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum install epel-release -y
yum makecache
yum install qemu-kvm qemu-key-tools virt-manager libvirt virt-install python-virtinst bridge-utils -y
modprobe kvm #加载kvm模块
modprobe kvm_intel
设置管理libvirt的权限
cat << EOF >> /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
EOF
chown -R root.root /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
saslpasswd2 -a libvirt admin
启动服务
chkconfig libvirtd on
systemctl start libvirtd
ps -ef |grep libvirtd
二、webvirtmgr安装
安装前准备
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
依赖组件安装
yum install -y wget
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum install epel-release -y
yum makecache
yum -y install git python-pip libvirt-python libxml2-python python-websockify supervisor nginx
yum -y install gcc python-devel
pip install --upgrade pip
pip install numpy -i https://mirrors.aliyun.com/pypi/simple/
下载webvirtmgr
git clone git://github.com/retspen/webvirtmgr.git
cd webvirtmgr/
pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple
初始化web登陆账号
./manage.py syncdb #按提示输入依次为:yes 用户名(root) 邮箱 密码
./manage.py collectstatic #生成配置文件,输入yes
cd ..
mkdir /var/www
mv webvirtmgr /var/www/
配置nginx
cd /etc/nginx/conf.d/
cat << EOF >> /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; # or /srv instead of /var
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 $scheme;
proxy_connect_timeout 600;
proxy_read_timeout 600;
proxy_send_timeout 600;
client_max_body_size 1024M; # Set higher depending on your needs
}
}
EOF
vi /etc/nginx/nginx.conf #编辑默认nginx配置文件,从server行开始注释 只留最后一个大括号
chown -R nginx:nginx /var/www/webvirtmgr
systemctl restart nginx
添加配置文件
cat << EOF >> /etc/supervisord.d/webvirtmgr.ini
[program:webvirtmgr]
command=/usr/bin/python /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/python /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
chkconfig supervisord on
systemctl restart supervisord
ss -anlt | egrep --color "(8000|6080|80)" #查看这3个端口是否都在监听状态
给kvm服务器ssh免密授权
su - nginx -s /bin/bash #进入nginx用户bash
#下面命令运行过程中出现提示信息一路回车。生成ssh密钥,复制到kvm服务器。
ssh-keygentouch ~/.ssh/config && echo -e "StrictHostKeyChecking=no\nUserKnownHostsFile=/dev/null" >> ~/.ssh/configchmod 0600 ~/.ssh/configssh-copy-id root@192.168.0.171
exit
systemctl restart nginx
三、浏览器登录WebVirtMgr管理平台
登陆并ssh连接kvm服务器
用户和密码是上面【初始化web登陆账号】步骤所设置的
点击右侧Add Connection,SSH连接 ,Label和FQDN/IP地址一样
如果成功连接上了,那就基本大功告成了
接下来就开始创建虚拟机及其他配置,我就不赘述了