上一家公司一开始上了许多虚拟机,要找一个工具管理KVM虚拟机,经过对比选择了Webvirtmgr,
Webvirtmgr使用简单也很稳定,在公司上openstack之前一直使用它,在它上面总共跑了400多台虚拟机,后来因为
虚拟机太多和管理成本上了openstack,虚拟机也迁移进了openstack,这里整理了一下当时安装的过程.
一、Kvm 宿主机
1、检查硬件支持
[root@linux-node3 ~]# grep -E "vmx|svm" /proc/cpuinfo
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts nopl xtopology tsc_reliable nonstop_tsc aperfmperf eagerfpu pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm ida arat epb pln pts dtherm tpr_shadow vnmi ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid xsaveopt
2、安装libvirt管理工具
在所有宿主机上操作
yum install qemu-kvm qemu-kvm-tools libvirt virt-manager libvirt-python libguestfs-tools virt-install bridge-utils novnc -y
systemctl start libvirtd
systemctl enable libvirtd
3、创建目录
mkdir -p data/kvm/img
mkdir -p data/kvm/iso
#cd data/kvm/iso/
#dd if=/dev/cdrom of=data/kvm/iso/centos7.iso #上传镜像可选
二、webvirtmgr管理端
- 环境准备
1.1、升级pip到9.0
wget https://bootstrap.pypa.io/get-pip.py
python get-pip.py
1.2、安装依赖包
yum -y install python-pip libvirt-python libxml2-python python-websockify supervisor gcc python-devel openresty
1.3、下载webvirtmgr并配置
mkdir /data/kvm_web/ -p
cd /data/kvm_web/
git clone git://github.com/retspen/webvirtmgr.git
cd webvirtmgr
#查看requirements.txt
cat requirements.txt
django==1.5.5
gunicorn==18.0
# Utility Requirements
# for SECURE_KEY generation
lockfile>=0.9
# Uncoment for support ldap
#django-auth-ldap==1.2.0
sudo pip install -r requirements.txt
./manage.py syncdb
#---配置数据库的账号
./manage.py collectstatic
1.4、创建一个超级用户
cd /data/kvm_web/webvirtmgr
./manage.py createsuperuser #--配置webvirtmgr 登录账号
1.5、编辑nginx配置文件
这里是openresty,如果是nginx则在/etc/nginx/下
cd /usr/local/openresty/nginx/conf/
vi webvirtmgr.conf
添加如下内容
server {
listen 8080;
server_name $hostname;
location /static/ {
root /data/kvm_web/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 $scheme;
proxy_connect_timeout 600;
proxy_read_timeout 600;
proxy_send_timeout 600;
client_max_body_size 1024M;
}
}
启动
chown -R webvirtmgr:webvirtmgr /usr/local/openresty/
systemctl start supervisord.service
systemctl enable supervisord.service
2、配置 Supervisor
vim /etc/supervisord.d/webvirtmgr.ini #这句命令只适用于centos7.x
#centos6 vim /etc/supervisord.conf
[program:webvirtmgr]
command=/usr/bin/python /data/kvm_web/webvirtmgr/manage.py run_gunicorn -c /data/kvm_web/webvirtmgr/conf/gunicorn.conf.py
directory=/data/kvm_web/webvirtmgr
autostart=true
autorestart=true
logfile=/var/log/supervisor/webvirtmgr.log
log_stderr=true
user=webvirtmgr
[program:webvirtmgr-console]
command=/usr/bin/python /data/kvm_web/webvirtmgr/console/webvirtmgr-console
directory=/data/kvm_web/webvirtmgr
autostart=true
autorestart=true
stdout_logfile=/var/log/supervisor/webvirtmgr-console.log
redirect_stderr=true
user=webvirtmgr
#启动
chown -R webvirtmgr:webvirtmgr /data/kvm_web/
systemctl start supervisord.service
systemctl enable supervisord.service
webvirtmgr服务器(服务端)与kvm服务器(客服端)连接配置
3、webvirtmgr与kvm之间使用ssh方式连接管理
3.1在webvirtmgr服务器(服务端)上创建用户
cd /home/
mkdir webvirtmgr
chown webvirtmgr. webvirtmgr webvirtmgr /
chmod 700 webvirtmgr / -R
su - webvirtmgr -s /bin/bash
ssh-keygen ---期间输入yes后直接回车,回车
touch ~/.ssh/config && echo -e "StrictHostKeyChecking=no\nUserKnownHostsFile=/dev/null" >> ~/.ssh/config
chmod 0600 ~/.ssh/config
3.2、在kvm(客服端)服务器上配置webvirt用户
useradd webvirtmgr
echo "" | passwd --stdin webvirtmgr
groupadd libvirt
usermod -G libvirt -a webvirtmgr
3.3 在webvirtmgr服务器(服务端)上,将ssh-key上传到kvm服务器上
su - webvirtmgr -s /bin/bash
ssh-copy-id webvirtmgr@192.168.88.22
3.4在kvm(客服端)服务器上配置 libvirt ssh授权
vim /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
[Remote libvirt SSH access]
Identity=unix-user:webvirtmgr
Action=org.libvirt.unix.manage
ResultAny=yes
ResultInactive=yes
ResultActive=yes
chown -R webvirtmgr.webvirtmgr /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
3.5重启 libvirtd 服务
systemctl restart libvirtd
4、webvirtmgr与kvm之间使用tcp方式连接管理(可选)
4.1Libvirtd服务监听配置
修改/etc/sysconfig/libvirtd文件,去掉下面一行的注释,使Libvirt服务处于监听状态:
vim /etc/sysconfig/libvirtd
LIBVIRTD_ARGS="--listen"
4.2配置Libvirt服务
配置Libvirt服务,允许通过tcp方式通讯,修改vi /etc/libvirt/libvirtd.conf:
#允许tcp监听
listen_tcp = 1
#开放tcp端口
tcp_port = "16509"
#监听地址修改为0.0.0.0
listen_addr = "0.0.0.0"
#配置tcp通过sasl认证
auth_tcp = sasl
#取消CA认证功能
listen_tls = 0
[root@linux-node3 ~]# grep '^[0-Z]' /etc/libvirt/libvirtd.conf
listen_tls = 0
listen_tcp = 1
tcp_port = "16509"
listen_addr = "0.0.0.0"
auth_tcp = "sasl"
4.3启动 saslauthd
systemctl start saslauthd
重启libvirtd服务:
systemctl restart libvirtd
4.4、创建libvirt管理用户
设置TCP认证Setup TCP authorization
注意:账号全名带hostname,如admin@webvirtmgr.net
webvirtmgr新建服务器连接时需要此账号
saslpasswd2 -a libvirt admin
Password: xxxxxx
Again (for verification): xxxxxx
查看新加的账号
sasldblistusers2 -f /etc/libvirt/passwd.db
admin@webvirtmgr.net: userPassword
删除新加的账号
saslpasswd2 -a libvirt -d admin
验证新加的账号
virsh -c qemu+tcp://IP_address/system nodeinfo
Please enter your authentication name: admin@webvirtmgr.net
Please enter your password: xxxxxx
CPU model: x86_64
CPU(s): 2
CPU frequency: 2611 MHz
CPU socket(s): 1
Core(s) per socket: 2
Thread(s) per core: 1
NUMA cell(s): 1
Memory size: 2019260 kB
五、登录webvirtmgr
http://192.168.88.20:8080/servers/
webvirtmgr之前设置管理用户和密码如 admin 123456
选择“SSH链接“,设置Label,IP,用户
Webvirtmgr
注意:Label与IP要相同
5.1查看概览
5.2创建存储池
5.3目录类型卷目录之前创建位置
data/kvm/img 存放虚拟机镜像的位置跟之前创建的目录一样
5.4创建虚拟机硬盘镜像
5.5 宿主机网卡的桥接模式设置
设置桥接需在宿主机提前配置桥接
将以下内容写脚本执行,否则会断网,要做测试
brctl addbr br0
brctl addif br0 eth0
ip addr del dev eth0 192.168.88.11/24
ifconfig br0 192.168.88.11/24 up
route add default gw 192.168.88.2
#还可以修改网络配置文件配置网桥
vi /etc/sysconfig/network-scripts/ifcfg-br0
DEVICE=br0
TYPE=Bridge
BOOTPROTO=static
BROADCAST=10.207.20.255
IPADDR=10.207.20.200
NETMASK=255.255.255.0
NETWORK=10.207.20.0
ONBOOT=yes
vi /etc/sysconfig/network-scripts/ifcfg-eth0
# Broadcom Corporation NetXtreme II BCM5709 Gigabit Ethernet
DEVICE=eth0
BOOTPROTO=none
ONBOOT=yes
BRIDGE=br0