当前位置: > 虚拟化 Virtualization > KVM >

kvm+webvirtmgr在centos7上的部署

时间:2018-04-06 23:34来源:linux.it.net.cn 作者:IT
#!/bin/bash
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++安装配置kvm并创建虚拟机+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#验证cpu是否支持kvm
egrep '(vmx|svm)' /proc/cpuinfo 
 
#然后需要在bios设置中打开cpu virtual technology!!!!!!!
 
#关闭selinux
vi /etc/sysconfig/selinux
selinux=disabled
 
#关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
 
#更换yum源(可选,一般不用换)
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum makecache
 
#安装依赖环境
yum install epel-release net-tools vim unzip zip wget ftp -y
 
#安装kvm及其依赖项
yum install qemu-kvm libvirt virt-install bridge-utils -y
 
#验证安装结果
lsmod | grep kvm 
 
#开启kvm服务,并且设置其开机自动启动
systemctl start libvirtd
systemctl enable libvirtd
#配置网络桥接模式
#手动修改  配置网桥模式,先将 /etc/sysconfig/network-scripts/ 目录下的网卡配置文件备份一份(不要备在当前目录以及子目录下,其他目录随意)
 
#a. 创建 ifcfg-br0 文件,内容如下:
 
BOOTPROTO=static
DEVICE=br0
TYPE=Bridge
NM_CONTROLLED=no
IPADDR=192.168.1.95
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=61.177.7.1
DNS2=8.8.8.8
#b. 移除掉原来的 ifcfg-enp0s25 ,重新创建该文件,内容如下:
 
BOOTPROTO=none
DEVICE=enp0s25
NM_CONTROLLED=no
ONBOOT=yes
BRIDGE=br0
 
#c.重启网络服务
systemctl restart network
 
 
 
#安装虚拟机
 
#准备虚拟机镜像文件,把该文件放到 /home/iso(没有可创建)目录下
 
#创建虚拟机
mkdir -p /home/kvm-bak
 
virt-install -n xiaocanan_media -r 4096 --vcpus=4 --os-type=linux --accelerate -c /home/iso/CentOS-7-x86_64-Minimal-1611.iso --disk path=/home/vmdisk_pool/xiaocanan_media.img,format=qcow2,bus=ide --network bridge=br0 --vnc --vncport=5993 --vnclisten=0.0.0.0 --force --autostart
 
 
#关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
 
 
#关闭selinux
vi /etc/sysconfig/selinux
selinux=disabled
 
#用vnc连接虚拟机,进行虚拟机操作系统的安装
 
#手动启动虚拟机
virsh list --all
virsh start virt-centos
 
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++安装配置webvirtmgr+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
#yum源的配置及所需软件包
yum -y install http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-8.noarch.rpm  #repo源,可选
yum -y install git python-pip libvirt-python libxml2-python python-websockify supervisor nginx
yum -y install gcc python-devel
 
#更换pip源
vi ~/.pip/pip.conf
[global]
index-url = https://pypi.douban.com/simple
 
#安装numpy
pip install numpy
 
#下载webvirtmgr源码 from git
mkdir /application/
cd /application/
git clone git://github.com/retspen/webvirtmgr.git 
 
#sqlite3数据库安装 
cd /application/
wget http://www.sqlite.org/sqlite-3.5.6.tar.gz
cd sqlite-3.5.6/
./configure --disable-tcl  
make
make install 
 
#webvirtmgr安装
cd /application/webvirtmgr
pip install -r requirements.txt
 
./manage.py syncdb  
 
./manage.py collectstatic 
 
./manage.py createsuperuser  #创建super用户  可选
 
#webvirtmgr配置
mkdir -pv /var/www
cp -Rv /application/webvirtmgr /var/www/webvirtmgr
 
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
}
}
 
#确保bind绑定的端口是本机的8000端口
grep '^bind =' /var/www/webvirtmgr/conf/gunicorn.conf.py
#注释掉 /etc/nginx/nginx.conf  server内容
systemctl restart nginx.service
systemctl enable nginx.service
*/9-/*
#kvm被管理端配置
vi /etc/sysconfig/libvirtd  #取消以下字段的注释
LLIBVIRTD_CONFIG=/etc/libvirt/libvirtd.conf
LIBVIRTD_ARGS="--listen"
 
vi /etc/libvirt/libvirtd.conf  #取消以下字段的注释并修改
listen_tls = 0
listen_tcp = 1
tcp_port = "16509"
listen_addr = "0.0.0.0"
auth_tcp = "none"
systemctl restart  libvirtd.service    
 
#设置ssh免密登录
 1. vi /etc/ssh/sshd_config
    RSAAuthentication yes   #新版本ssh可能不需要配置此项
  PubkeyAuthentication yes
  AuthorizedKeysFile      .ssh/authorized_keys
 2.重启sshd服务:systemctl restart sshd
 3.生成证书公私钥
    ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa
    cat ~/.ssh/id_dsa.pub >> ~/.ssh/authorized_keys
 4.chmod 600 ~/.ssh/authorized_keys
 
 #免密登录远程服务器
  cat ~/.ssh/id_rsa.pub | ssh 远程用户名@远程服务器ip 'cat - >> ~/.ssh/authorized_keys'
 
  
 
#启动webvirtmgr server
nohup /var/www/webvirtmgr/manage.py runserver 0:8000
nohup /var/www/webvirtmgr/console/webvirtmgr-console
 
#设置webvirtmgr和webvirtmgr-console开机自启动脚本
nohup /var/www/webvirtmgr/manage.py runserver 0:8000 &
nohup /var/www/webvirtmgr/console/webvirtmgr-console &
 
#切记!!!! webvirtmgr第一次一定要手动运行,不然会提示密码验证失败
#切记!!!! 使用webvirtmgr创建windows虚拟机时不能选virtio,因为没有相应驱动的话,硬盘和网卡都会无法识别
 
#webvirtmgr web地址 http://本机ip:8000
 
 

 
(责任编辑:IT)
------分隔线----------------------------