確保kvm已經(jīng)安裝成功,見(jiàn) centos7安裝kvm
本文基于docker安裝,見(jiàn) centos7安裝docker
假設(shè)kvm主機(jī)為10.88.36.101
下載鏡像
地址:https://hub.docker.com/r/unws/webvirtmgr/
docker pull primiano/docker-webvirtmgr
創(chuàng)建webvirtmgr用戶和組
sudo groupadd -g 1010 webvirtmgr
sudo useradd -u 1010 -g webvirtmgr -s /sbin/nologin -d /data/vm webvirtmgr
sudo chown -R webvirtmgr:webvirtmgr /data/vm
其中:
組ID必須是1010株婴,和鏡像中組對(duì)應(yīng)
目錄/data/vm用于存放鏡像中的sqlite數(shù)據(jù)文件
啟動(dòng)鏡像
docker run -d -p 8080:8080 -p 6080:6080 --name webvirtmgr -v /data/vm:/data/vm primiano/docker-webvirtmgr
配置libvirtd
在kvm主機(jī)上公黑,對(duì)libvirtd進(jìn)行配置
cat /etc/default/libvirt-bin
start_libvirtd="yes"
libvirtd_opts="-d -l"
cat /etc/libvirt/libvirtd.conf
listen_tls = 0
listen_tcp = 1
listen_addr = "0.0.0.0" ## Address of docker0 veth on the host
unix_sock_group = "libvirt"
unix_sock_ro_perms = "0777"
unix_sock_rw_perms = "0770"
auth_unix_ro = "none"
auth_unix_rw = "none"
auth_tcp = "none"
auth_tls = "none"
其中:
listen_addr推薦0.0.0.0,監(jiān)聽(tīng)所有網(wǎng)卡
unix_sock_group為kvm主機(jī)的libvirt/libvirtd組割岛,可用cat /etc/group查看
cat /etc/libvirt/qemu.conf
# This is obsolete. Listen addr specified in VM xml.
# vnc_listen = "0.0.0.0"
vnc_tls = 0
# vnc_password = ""
重啟libvirtd服務(wù)
systemctl restart libvirtd
配置webvirtmgr
使用docker命令登錄到webvirtmgr容器中,修改admin用戶密碼
cd /webvirtmgr
python manage.py changepassword admin
也可以創(chuàng)建新超級(jí)用戶
cd /webvirtmgr
python manage.py createsuperuser
切換至webvirtmgr用戶
su - webvirtmgr -s /bin/bash
在webvirtmgr用戶下,創(chuàng)建到kvm主機(jī)的免登錄
ssh-keygen
touch ~/.ssh/config && echo -e "StrictHostKeyChecking=no\nUserKnownHostsFile=/dev/null" >> ~/.ssh/config
chmod 0600 ~/.ssh/config
然后copy到kvm主機(jī)
ssh-copy-id root@10.88.36.101
登錄webvirtmgr
打開(kāi)瀏覽器舶衬,訪問(wèn)http://10.88.36.101:8080
輸入用戶密碼即可
添加connection時(shí),要保證已經(jīng)配置容器中webvirtmgr用戶到目標(biāo)機(jī)用戶的ssh免登錄
問(wèn)題
1.如果遇到虛擬機(jī)啟動(dòng)時(shí)報(bào)錯(cuò):
qemu-kvm: Failed to start VNC server on `172.17.42.1:0': Failed to bind socket: Cannot assign requested addres
請(qǐng)將對(duì)應(yīng)虛擬機(jī)XML配置中VNC部分改為:
<graphics type='vnc' port='-1' autoport='yes' listen='
0.0.0.0
'>
<listen type='address' address='0.0.0.0
'/>
</graphics>
2.如果遇到虛擬機(jī)啟動(dòng)時(shí)報(bào)錯(cuò):
Unable to add port vnet0 to OVS bridge br0
請(qǐng)將對(duì)應(yīng)虛擬機(jī)XML配置中網(wǎng)絡(luò)部分改為:
<interface type='
bridge
'>
<mac address='52:54:00:fc:50:3e'/>
<sourcebridge
='br0
'/>
<model type='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
3.如果遇到虛擬機(jī)無(wú)法根據(jù)dhcp獲取ip赎离,請(qǐng)檢查kvm主機(jī)的iptables設(shè)置逛犹,必要時(shí)可以清空iptables規(guī)則