一 安裝操作系統(tǒng)
● 重裝操作系統(tǒng)為centos7.2 ,將原來的數(shù)據(jù)全部格掉
● 最小化安裝
● 使用lvm將所有容量放到根目錄下
二 安裝kvm和libvirt和webvirtmgr
創(chuàng)建虛擬化環(huán)境
yum install kvm libvirt
安裝webvirtmgr
安裝基礎(chǔ)環(huán)境
yum -y install git python-pip libvirt-python libxml2-python python-websockify supervisor nginx
yum -y install gcc python-devel
pip install numpy
安裝python環(huán)境和Django環(huán)境
git clone git://github.com/retspen/webvirtmgr.git
cd webvirtmgr
pip install -r requirements.txt
./manage.py syncdb
./manage.py collectstatic
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 (Put: yes)
Username (Leave blank to use 'admin'): admin (Put: your username or login)
E-mail address: username@domain.local (Put: your email)
Password: QQzjUUw* (Put: your password)
Password (again): QQzjUUw* (Put: confirm password)
Superuser created successfully.
./manage.py createsuperuser
安裝nginx
cd ..
mv webvirtmgr /var/www/
在/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
}
}
打開/etc/nginx/nginx.conf
將server域的內(nèi)容全部注釋掉
# server {
# listen 80 default_server;
# server_name localhost;
# root /usr/share/nginx/html;
#
# #charset koi8-r;
#
# #access_log /var/log/nginx/host.access.log main;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# # redirect server error pages to the static page /40x.html
# #
# error_page 404 /404.html;
# location = /40x.html {
# }
# # redirect server error pages to the static page /50x.html
# #
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
同時檢查下有沒有include /etc/nginx/conf.d/*.conf這一項
重啟nginx
service nginx restart
關(guān)閉selinux和iptables
下載supervisord來管理進程
Yum install supervisor
設(shè)置為開機啟動
Chkconfig supervisord on
安裝設(shè)置supervisor
Chown –R nginx:nginx /var/www/webvirtmgr
創(chuàng)建文件/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
啟動supervisord
Supervisord –c /etc/supervisord.conf
看看可以不可以訪問web和能否通過web端訪問虛擬機
使用tcp方式添加遠程node
在所有宿主機上修改
修改/etc/sysconfig/libvirtd,開啟兩個配置項
LIBVIRTD_CONFIG=/etc/libvirt/libvirtd.conf
LIBVIRTD_ARGS="—listen
修改libvirt的配置文件
vim /etc/libvirt/libvirtd.conf
listen_tls = 0
listen_tcp = 1
tcp_port = "16509"
listen_addr = "0.0.0.0"
auth_tcp = "sasl"
重啟libvirtd并查看監(jiān)聽的端口
service libvirtd restart
netstat -anltp | grep 16509
添加libvirt的用戶zl
saslpasswd2 -a libvirt zl
查看存在的用戶
sasldblistusers2 -f /etc/libvirt/passwd.db
刪除存在的用戶
saslpasswd2 -a libvirt -d zl
此時從另一臺機器上訪問就可以啦
virsh -c qemu+tcp://IP_address/system nodeinfo/list
從webvirtmgr端添加就行啦嗤谚。
三 node端的操作
安裝kvm和libvirt
設(shè)置網(wǎng)橋
四 網(wǎng)卡設(shè)置
每臺node上有六塊物理網(wǎng)卡呼猪,現(xiàn)將其中五塊設(shè)為網(wǎng)橋。剩余一塊用來進行通信。
添加網(wǎng)橋
五 虛擬機的導入
virt-install --name=ceshi --ram=8192 --vcpu=4 --disk path=/image/mobileTest,bus=virtio --network bridge=br1 --vnc --vncport=5905 --vnclisten=0.0.0.0 --connect qemu:///system --import –noautoconsole
動態(tài)添加網(wǎng)卡(添加完畢后重啟網(wǎng)卡會消失,需要改配置文件)
virsh attach-interface vmname --type bridge --source br1
查看新增網(wǎng)卡
# virsh domiflist vm01
更新XML文件
# virsh dumpxml vm01 > /etc/libvirt/qemu/vm01.xml
動態(tài)添加硬盤
kvm libvirt qemu實踐系列(四)-kvm虛擬機在線調(diào)整配置
● centos5.x版本不支持動態(tài)調(diào)整內(nèi)存,CPU祭埂,以下是在centos6.x上測試
● 在線調(diào)整配置應(yīng)該保證虛擬機重啟之后依然生效,即調(diào)整的同時需要更改xml文件
● centos6.x平臺下兵钮,cpu core數(shù)只能在線增加蛆橡,不能在線減小
查看虛擬機信息
shell> virsh dumpxml cos_v1 | head -n 10
<domain type='kvm'id='9' xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0'>
<name>cos_v1</name>
<uuid>d39efd06-6629-aa4a-7241-b36400eade2d</uuid>
<memory unit='KiB'>4194304</memory>
<currentMemory unit='KiB'>2097152</currentMemory>
<vcpu placement='static'current='2'>8</vcpu>
<os>
<type arch='x86_64'machine='rhel6.5.0'>hvm</type>
<boot dev='hd'/>
<boot dev='cdrom'/>
根據(jù)上面配置,首先需要明確
<memory unit='KiB'>4194304</memory>
<currentMemory unit='KiB'>2097152</currentMemory>
#為此虛擬機最大分配4G內(nèi)存,目前使用2G掘譬,因此下文內(nèi)存在線調(diào)整區(qū)間`1G-4G`
<vcpu placement='static'current='2'>8</vcpu>
#為虛擬機分配最多8個core,目前使用2個core泰演,因此下文cpu在線增加最多到8個core
內(nèi)存,cpu調(diào)整
在線調(diào)整虛擬機內(nèi)存
#調(diào)整為4G
virsh setmem cos_v1 4G
#減小為2G
virsh setmem cos_v1 2G
#調(diào)整的同時更改xml文件
virsh setmem cos_v1 2G --config --live
#使用virsh setmem --help 查看在線調(diào)整的同時更改xml文件葱轩,以保證調(diào)整永久生效
能夠在線調(diào)整的最大內(nèi)存不能超過為虛擬機分配的最大內(nèi)存(上面xml文件中設(shè)置<memory unit='KiB'>4194304</memory>最大為4G)調(diào)整范圍1G-4G
在線調(diào)整虛擬機CPU(只能增大睦焕,不能減小)
virsh setvcpus cos_v1 4
virsh setvcpus cos_v1 8
同樣,能夠動態(tài)調(diào)整的最大VCPU個數(shù)也不能超過為虛擬機設(shè)置的最大VCPU數(shù)量
在線添加,移除硬盤
添加qcow2格式硬盤(建議動態(tài)添加此格式硬盤)
#創(chuàng)建qcow2格式的新磁盤,大小為40G
qemu-img create -f qcow2 /data/vhosts/test/cos_v1-add1.disk 40G
virsh attach-disk cos_v1 /data/vhosts/test/cos_v1-add1.disk vdb --cache none --subdriver qcow2 --config --persistent
#虛擬機根磁盤為vda靴拱,因此這里使用vdb表示新添加磁盤
#--config 參數(shù)同時更新虛擬機xml文件垃喊,確保重啟后依然生效
添加完畢后在服務(wù)器端加到lvm里即可
添加raw格式硬盤
#創(chuàng)建raw格式的新磁盤,大小為40G
qemu-img create -f raw /data/vhosts/test/cos_v1-add2.disk 40G
virsh attach-disk lnmptest-107 /data/vhosts/test/cos_v1-add2.disk vdc --cache none --subdriver raw --config --persistent
在線移除硬盤
#可以查看虛擬機所有磁盤
virsh domblklist cos_v1
virsh detach-disk cos_v1 vdb
網(wǎng)卡,CD-ROM添加
在線添加網(wǎng)卡
virsh attach-interface --domain cos_v1 --type network --source default --model virtio --config
#可以查看新添加的網(wǎng)卡
virsh domiflist cos_v1
在線添加光盤
virsh attach-disk centosbase /data_lij/iso/CentOS-6.4-x86_64-bin-DVD1.iso vdd
其他注意事項:
關(guān)閉networkmanager服務(wù)
chkconfig NetworkManager off
service NetworkManager stop
大多數(shù)時候我們虛擬機使用的是 bridge(網(wǎng)橋)直接連到局域網(wǎng)里袜炕,所以這個 virbr0 不是必須的(注:不要搞混淆了本谜,bridge 和這里的 virbr0 bridge 是互不相干的)。如何關(guān)掉這個 virbr0 呢偎窘?先 net-destroy 然后 net-undefine乌助,最后別忘了重啟 libvirtd 讓設(shè)置生效:
# virsh net-list
Name State Autostart
-----------------------------------------
default active yes
# virsh net-destroy default
Network default destroyed
# virsh net-undefine default
Network default has been undefined
# service libvirtd restart
Stopping libvirtd daemon: [ OK ]
Starting libvirtd daemon: [ OK ]