CentOS 6.9 mini 安装KVM 部署记录

32 篇文章 0 订阅

系统初始化设置

# CentOS6 最小化安装完成后初始化

# 系统联网
vi /etc/udev/rules25.d/70-persistent-net.rules        //查看最后一块网卡编号
vi  /etc/sysconfig/network-scripts/ifcfg-eth0         //修改为最后一块网卡
  
BOOTPROTO=static   #启用静态IP地址
ONBOOT=yes  #开启自动启用网络连接
IPADDR=10.209.180.XXX  #设置IP地址
NETMASK=255.255.255.0  #设置子网掩码
GATEWAY=10.209.180.254   #设置网关
DNS1=202.102.192.68 #设置主DNS
DNS2=8.8.8.8 #设置备DNS
IPV6INIT=no  #禁止IPV6


service network restart 
ip addr                                           //查看ip地址
yum install net-tools                             //安装ipconfig

## 关闭防火墙和SELINUX

/etc/rc.d/init.d/iptables stop
/etc/rc.d/init.d/ip6tables stop
chkconfig iptables off
chkconfig ip6tables off

sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0

## 关闭不必要的服务

/etc/rc.d/init.d/netfs stop
chkconfig netfs off

## 更新系统

yum -y install yum-plugin-fastestmirror
yum -y update

## 添加EPEL源

rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm

## 定时任务设置

yum -y install cronie-noanacron
# yum remove cronie-anacron -y

## 配置vim

yum -y install vim-enhanced lrzsz
#echo " alias vi='vim' " >>  /etc/profile
echo "alias vi='vim' " >> ~/.bashrc
#source /etc/profile
source ~/.bashrc

## 添加用户

useradd yunwei
echo 123456 | passwd --stdin yunwei

# 修改hosts文件

安装依赖包

[root@sh-kvm-1 ~]# yum update -y
[root@sh-kvm-1 ~]# yum -y install qemu-kvm libvirt python-virtinst bridge-utils
[root@kvm-1 ~]# lsmod | grep kvm
kvm_intel              54285  0
kvm                   333172  1 kvm_intel
[root@sh-kvm-1 ~]# /etc/rc.d/init.d/libvirtd start
Starting libvirtd daemon: [ OK ]
[root@sh-kvm-1 ~]# /etc/rc.d/init.d/messagebus start
Starting system message bus: [ OK ]
[root@sh-kvm-1 ~]# chkconfig libvirtd on
[root@sh-kvm-1 ~]# chkconfig messagebus on

配置桥接网络

# 网桥网卡配置
[root@sh-kvm-1 ~]# cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-br0
[root@sh-kvm-1 ~]# vi /etc/sysconfig/network-scripts/ifcfg-br0
DEVICE=br0
TYPE=Bridge
ONBOOT=yes
BOOTPROTO=none
IPADDR=192.168.0.67
NETMASK=255.255.255.0
GATEWAY=192.168.0.1
DNS1=114.114.114.114

# em1网卡配置
[root@sh-kvm-1 ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0
# create new
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
BRIDGE=br0
HWADDR=14:18:77:40:29:D3
UUID=d2e2265d-f891-4574-918f-c76a4dd6f2eb
# 配置网桥这里注意
# bro网卡配置里面不需要em1的mac地址和UUID,em1网卡的mac地址和UUID跟着eth0网卡走。 不然重启网络时会报错,报错在文章末尾。

[root@sh-kvm-1 ~]# /etc/rc.d/init.d/network restart
  • 查看网桥配置状态
[root@sh-kvm-1 ~]# ifconfig
br0       Link encap:Ethernet  HWaddr 14:18:77:40:29:D3
          inet addr:192.168.1.125  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::1618:77ff:fe40:29d3/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:52655 errors:0 dropped:0 overruns:0 frame:0
          TX packets:20216 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:49670413 (47.3 MiB)  TX bytes:1665453 (1.5 MiB)

em1       Link encap:Ethernet  HWaddr 14:18:77:40:29:D3
          inet6 addr: fe80::1618:77ff:fe40:29d3/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:302969 errors:0 dropped:0 overruns:0 frame:0
          TX packets:96324 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:427674107 (407.8 MiB)  TX bytes:7173701 (6.8 MiB)
          Interrupt:41

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:7 errors:0 dropped:0 overruns:0 frame:0
          TX packets:7 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:608 (608.0 b)  TX bytes:608 (608.0 b)

virbr0    Link encap:Ethernet  HWaddr 52:54:00:68:65:A2
          inet addr:192.168.122.1  Bcast:192.168.122.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

vnet0     Link encap:Ethernet  HWaddr FE:54:00:08:94:EC
          inet6 addr: fe80::fc54:ff:fe08:94ec/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:19 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3443 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:500
          RX bytes:1243 (1.2 KiB)  TX bytes:381667 (372.7 KiB)

创建虚拟机

二、Kvm的管理工具webvirtmgr安装和使用

1)安装支持的软件源
[root@openstack ops]#yum -y install http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm

2)安装相关软件
---------------------------------------------------------------------------------------------------------------------
如果报错说没有下面安装的某些软件,则下载http://pan.baidu.com/s/1bX3vkE (密码:6ucs) 
将下载的yum.tar.gz解压后的文件替换到/etc/yum.repos.d目录下,然后执行yum clean all && yum makecache
---------------------------------------------------------------------------------------------------------------------
[root@openstack ops]#yum -y install git python-pip libvirt-python libxml2-python python-websockify supervisor nginx       (若是centos7系统,需要先执行"yum -y install epel-release")

3)从git-hub中下载相关的webvirtmgr代码
[root@openstack ops]# cd /usr/local/src/
[root@openstack src]# git clone git://github.com/retspen/webvirtmgr.git    (下载地址:https://pan.baidu.com/s/1pLS3kCj      获取密码:8efm

4)安装webvirtmgr
[root@openstack src]# cd webvirtmgr/
[root@openstack webvirtmgr]# pip install -r requirements.txt

5)安装数据库
[root@openstack webvirtmgr]# yum install python-sqlite2          //默认情况下,centos系统一般会自带sqlite软件(执行sqlite3 命令,不报错就说明已经安装了)

6)对django进行环境配置
[root@openstack webvirtmgr]#pwd
/usr/local/src/webvirtmgr
[root@openstack webvirtmgr]#./manage.py syncdb           //默认是python执行,如下报错,换用其他版本的python
-------------------------------------------------- ---------------------------- 
注意此处用默认的python执行上面命令,一般会报错,如下:
ImportError: No module named django.core.management

这个一般是由于python版本引起的,因为系统自带有好几个版本的python
[root@openstack webvirtmgr]# python       //按Tab键自查找
python python2.6 
python2 python2.6-config python-config
[root@openstack webvirtmgr]# python -V
Python 2.6.6

由此可看出,系统默认的Python版本是2.6.6
说明上面命令默认是python2.6执行的

既然使用python2.6执行上面的命令报错,那就换用其他版本python2执行(如果当前是python3.3.0,那么就将下面的/usr/bin/python2换成/usr/bin/python2.6)
[root@openstack webvirtmgr]# /usr/bin/python2 manage.py syncdb     //最终发现使用python2执行这个命令就不报错了 
............
............ 
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
Username (leave blank to use 'root'): admin
Email address: wangshibo@163.com
Password:*********
Password (again):*********
--------------------- --------------------- --------------------- --------------------- 
[root@openstack webvirtmgr]#/usr/bin/python2 manage.py collectstatic          //生成配置文件(同样使用python2版本执行,不要使用默认的python执行)
WARNING:root:No local_settings file found.

You have requested to collect static files at the destination
location as specified in your settings.

This will overwrite existing files!
Are you sure you want to do this?

Type 'yes' to continue, or 'no' to cancel: yes 
..........
..........

[root@openstack webvirtmgr]#/usr/bin/python2 manage.py createsuperuser  //添加管理员账号(同样使用python2版本执行,不要使用默认的python执行)
WARNING:root:No local_settings file found.
Username: ops                                                   //这个是管理员账号,用上面的admin和这个管理员账号都可以登陆webvirtmgr的web界面管理平台
Email address: wangshibo@163.com
Password: 
Password (again): 
Superuser created successfully.

7)拷贝web到 相关目录
[root@openstack ops]# mkdir -pv /var/www
[root@openstack ops]# cp -Rv /usr/local/src/webvirtmgr /var/www/webvirtmgr

8)设置ssh (可以忽略)
[root@openstack ops]# ssh-keygen -t rsa             //产生公私钥
[root@openstack ops]# ssh-copy-id 192.168.1.17        //由于这里webvirtmgr和kvm服务部署在同一台机器,所以这里本地信任。如果kvm部署在其他机器,那么这个是它的ip
[root@openstack ops]# ssh 192.168.1.17 -L localhost:8000:localhost:8000 -L localhost:6080:localhost:60

9)编辑nginx配置文件
提前确保/etc/nginx/nginx.conf文件里开启了“include /etc/nginx/conf.d/*.conf;”

[root@openstack ops]#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; # 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 $remote_addr;
proxy_connect_timeout 600;
proxy_read_timeout 600;
proxy_send_timeout 600;
client_max_body_size 1024M; # Set higher depending on your needs
}
}

[root@openstack ops]# mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.bak

10)启动nginx
[root@openstack ops]#/ etc/init.d/nginx restart

11)修改防火墙规则
[root@ops ~]# vim /etc/sysconfig/selinux 
......
SELINUX=disabled

[root@ops ~]# setenforce 0
setenforce: SELinux is disabled
[root@ops ~]# getenforce 
Disabled

[root@openstack ops]#/usr/sbin/setsebool httpd_can_network_connect true

12)设置 supervisor (如果iptables防火墙开启的话,就必须要开通80、8000、6080端口访问)
[root@openstack ops]# chown -R nginx:nginx /var/www/webvirtmgr

[root@openstack ops]# vim /etc/supervisord.conf     //在文件末尾添加,注意将默认的python改为python2,因为上面只有用这个版本执行才不报错!
[program:webvirtmgr]
command=/usr/bin/python2 /var/www/webvirtmgr/manage.py run_gunicorn -c /var/www/webvirtmgr/conf/gunicorn.conf.py                     //启动8000端口
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                               //启动6080端口(这是控制台vnc端口)
directory=/var/www/webvirtmgr
autostart=true
autorestart=true
stdout_logfile=/var/log/supervisor/webvirtmgr-console.log
redirect_stderr=true
user=nginx
[root@openstack ops]#vim /var/www/webvirtmgr/conf/gunicorn.conf.py    //确保下面bind绑定的是本机的8000端口,这个在nginx配置中定义了,被代理的端口
bind = '127.0.0.1:8000'
13)设置开机启动
[root@openstack ops]#chkconfig supervisord on
[root@openstack ops]#vim /etc/rc.local
/usr/sbin/setsebool httpd_can_network_connect true

14)启动进程
[root@openstack ops]#/etc/init.d/supervisord restart
15)查看进程
[root@openstack ops]#netstat -lnpt    //即可以看到6080和8000已经启动
[root@openstack ops]# lsof -i:6080
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
python2 53476 nginx 3u IPv4 364124 0t0 TCP *:6080 (LISTEN)
[root@openstack ops]#lsof -i:8000

------------------------------------------------------------------------------
一般来说,只要上面的supervisord服务启动了,8000和6080端口就起来了。
因为在/etc/supervisord.conf文件里配置了这两个端口的启动设置

如果supervisord服务启动了,8000和6080端口还是没起来!
那么就需要手动启动/etc/supervisord.conf文件里配置的这两个端口的启动命令,即:
[root@openstack ops]# /usr/bin/python2 /var/www/webvirtmgr/manage.py run_gunicorn -c /var/www/webvirtmgr/conf/gunicorn.conf.py       //这个命令会一直在操作
[root@openstack ops]# /usr/bin/python2 /var/www/webvirtmgr/console/webvirtmgr-console                        //这个命令会一直在操作
[root@openstack ops]# /etc/init.d/supervisord restart
[root@openstack ops]# lsof -i:8000
[root@openstack ops]# lsof -i:6080
------------------------------------------------------------------------------

16)web访问
http://192.168.1.17/login/

这里用超级管理员登陆,只有超级管理员登陆后才能看到“基础构架”窗口

普通用户登陆后,只能看到“WebVirtMgr”一个窗口

使用tcp进行对远程libvirtd进行连接访问的配置如下:

1)修改文件/etc/sysconfig/libvirtd,用来启用tcp的端口
[root@openstack ops]# cat /etc/sysconfig/libvirtd
........
LLIBVIRTD_CONFIG=/etc/libvirt/libvirtd.conf
LIBVIRTD_ARGS="--listen"

2)修改文件/etc/libvirt/libvirtd.conf
[root@openstack ops]#vim /etc/libvirt/libvirtd.conf
listen_tls = 0
listen_tcp = 1
tcp_port = "16509"
listen_addr = "0.0.0.0"
auth_tcp = "none"

3)运行 libvirtd
[root@openstack ops]#service libvirtd restart

如果没起效果(我的就没有生效,那么使用命令行.如果生效了,执行下面命令就会报错
[root@openstack ops]#libvirtd --daemon --listen --config /etc/libvirt/libvirtd.conf

4)查看运行进程
[root@openstack ops]# ps aux | grep libvirtd
root 16563 1.5 0.1 925880 7056 ? Sl 16:01 0:28 libvirtd -d -l --config /etc/libvirt/libvirtd.conf

5)查看端口
[root@openstack ops]# lsof -i:16509

6)在source host连接dest host远程libvirtd查看信息(也可以用公网ip)
[root@openstack ops]# virsh -c qemu+tcp://192.168.1.17/system
Welcome to virsh, the virtualization interactive terminal.

Type: 'help' for help with commands
'quit' to quit

virsh #

成功使用tcp去访问libvirtd。

注意:

在使用tcp方式连接后,会出现连接终端的情况!

[root@openstack .ssh]# virsh -c qemu+tcp://192.168.1.17/system
error: Cannot recv data: Connection reset by peer
error: failed to connect to the hypervisor

连接断开,重新连接便可。

[root@openstack ops]# ps aux | grep libvirtd
root 59619 0.6 0.0 1008128 22048 ? Sl 19:17 0:06 libvirtd --daemon --config /etc/libvirt/libvirtd.conf --listen
root 61081 0.0 0.0 103316 1004 pts/2 S+ 19:33 0:00 grep --color libvirtd
[root@openstack ops]# kill -9 59619
[root@openstack ops]# ps aux | grep libvirtd
root 61083 0.0 0.0 103312 904 pts/2 S+ 19:33 0:00 grep --color libvirtd
[root@openstack ops]# libvirtd --daemon --listen --config /etc/libvirt/libvirtd.conf
[root@openstack ops]# ps aux | grep libvirtd
root 61086 13.5 0.0 418240 6576 ? Sl 19:33 0:00 libvirtd --daemon --listen --config /etc/libvirt/libvirtd.conf
root 61176 0.0 0.0 103312 908 pts/2 S+ 19:33 0:00 grep --color libvirtd
[root@openstack ops]# virsh -c qemu+tcp://192.168.1.17/system
Welcome to virsh, the virtualization interactive terminal.

Type: 'help' for help with commands
'quit' to quit

virsh #

后续发现,webvirtmgr连上后,过一会儿就会断开!

针对这个情况,可以可以写个定时脚本,如下:

[root@openstack ops]# cat /usr/local/src/libvirtd.sh

#!/bin/bash
ps -ef | grep "libvirtd --daemon --listen"|grep -v grep|awk -F" " '{print $2}'|xargs kill -9
/usr/sbin/libvirtd --daemon --listen --config /etc/libvirt/libvirtd.conf

[root@openstack ops]# crontab -l
* * * * * /bin/bash -x /usr/local/src/libvirtd.sh > /dev/null 2>&1
* * * * * sleep 10;/bin/bash -x /usr/local/src/libvirtd.sh > /dev/null 2>&1
* * * * * sleep 20;/bin/bash -x /usr/local/src/libvirtd.sh > /dev/null 2>&1
* * * * * sleep 30;/bin/bash -x /usr/local/src/libvirtd.sh > /dev/null 2>&1
* * * * * sleep 40;/bin/bash -x /usr/local/src/libvirtd.sh > /dev/null 2>&1
* * * * * sleep 50;/bin/bash -x /usr/local/src/libvirtd.sh > /dev/null 2>&1

*********************************************************************************

一般如上配置后,webvirtmgr里的控制台是可以正常连接虚拟机的。

但如果webvirtmgr里通过控制台页面(vnc)连接虚拟机失败,可以按照下面的操作方法尝试解决:

kvm原来的安装方式是客户端需要安装vncviewer,才能看到安装页面,而webvirtmgr使用了novnc,页面通过websocket进行通信。

1)首先需要安装novnc
[root@openstack ops]# yum install -y novnc

2)防火墙打开vnc的6080端口
[root@openstack ops]# vim /etc/sysconfig/iptables
.......
-A INPUT -p tcp -m state --state NEW -m tcp --dport 6080 -j ACCEPT
.......

[root@openstack ops]# /etc/init.d/iptables restart

3)由上面可知,webvirtmgr进程通过supervisor管理。这里需要重启supervisor进程
[root@openstack ops]# /etc/init.d/supervisord restart

这样,再次打开Webvirtmgr的控制台,发现就能连上虚拟机了!

******************
可以在webvirtmgr服务器上通过命令行尝试下连接:
[root@openstack ops]# novnc_server --help
Usage: novnc_server [--listen PORT] [--vnc VNC_HOST:PORT] [--cert CERT]

Starts the WebSockets proxy and a mini-webserver and 
provides a cut-and-paste URL to go to.

--listen PORT Port for proxy/webserver to listen on
Default: 6080
--vnc VNC_HOST:PORT VNC server host:port proxy target
Default: localhost:5900
--cert CERT Path to combined cert/key file
Default: self.pem
--web WEB Path to web files (e.g. vnc.html)
Default: ./

[root@openstack ops]# novnc_server --listen 192.168.1.17:6086           #端口在6080后的都可以用
Warning: could not find self.pem
Starting webserver and WebSockets proxy on port 192.168.1.17:6086
WARNING: no 'numpy' module, HyBi protocol will be slower
WebSocket server settings:
- Listen on 192.168.1.17:6086
- Flash security policy server
- Web server. Web root: /usr/share/novnc
- No SSL/TLS support (no cert file)
- proxying from 192.168.1.17:6086 to localhost:5900


Navigate to this URL:

http://kvm-server:192.168.1.17:6086/vnc.html?host=kvm-server&port=192.168.1.17:6086

Press Ctrl-C to exit

*********************************************************************************

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
上面是在centos6下的操作记录。下面简单介绍下在centos7版本下的操作:
 
1)yum源的配置及安装所需软件包
# yum -y install http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
# yum -y install Git Python-pip libvirt-python libxml2-python python-websockify supervisor nginx
# yum -y install gcc python-devel
# pip install numpy   
 
2)下载webvirtmgr.git软件
# cd /usr/local/src/
# git clone git://github.com/retspen/webvirtmgr.git 
# git clone https://github.com/retspen/webvirtmgr.git         
 
3)数据库安装
# cd /usr/local/src/
# wget http://www.sqlite.org/sqlite-3.5.6.tar.gz
# cd sqlite-3.5.6/
# ./configure --disable-tcl 
# # make
make  install
 
4)webvirtmgr安装
# cd /usr/local/src/webvirtmgr
# pip install -r requirements.txt
............
 
后面跟在centos6版本系统下的安装操作步骤一样。


由于KVM管理虚拟机的硬盘和网卡需要virtio驱动,linux系统默认支持virtio驱动,所以安装linux系统虚拟机的时候只需要加载iso镜像即可。

但是windows系统默认不支持virtio驱动,需要另外下载virtio驱动,在安装windowss虚拟机过程中,需要选择virtio驱动路径,继而加载驱动,最后才可以识别到驱动。

wxp: winxp 磁盘驱动
wnet: win2003磁盘驱动 
win7:win7磁盘驱动
wlh: win2008磁盘驱动 
xp: win xp/2003 网卡驱动
vista: win vista/7/2008 网卡驱动

下面提供一个下载地址:
已经集成VirtIO驱动的各版本Windows的ISO镜像

http://www.hostloc.com/thread-242194-1-1.html

________________________________________________________________________

重新启动服务器:

cd / 
etc/init.d/nginx restart

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值