Kvm虚拟化部署

需求说明

使用Kvm部署一台主机

环境说明

确保CPU虚拟化功能已开启
物理机要在BIOS里开启CPU虚拟化(现在一般默认开启)
虚拟机要关机设置CPU虚拟化
①.关闭防火墙

[[email protected] ~]# systemctl stop firewalld.service 

②.关闭selinux

[[email protected] ~]# setenforce 0
[[email protected] ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config 

③.配置网络源和epel源

[[email protected] ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[[email protected] ~]# sed -i 's/$releasever/7/g' /etc/yum.repos.d/CentOS-Base.repo 

操作步骤

①.验证CPU是否支持KVM,支持就安装kvm

[[email protected] ~]# egrep -o 'vmx|svm' /proc/cpuinfo 
vmx    //出现vmx或者svm既表示支持虚拟化
[[email protected] ~]# yum -y install qemu-kvm qemu-kvm-tools qemu-img virt-manager libvirt libvirt-python libvirt-client virt-install virt-viewer bridge-utils libguestfs-tools

②.使用br0来桥接本机网卡

[[email protected] ~]# cd /etc/sysconfig/network-scripts/
[[email protected] network-scripts]# cp ifcfg-eth0 ifcfg-br0
[[email protected] network-scripts]# vim ifcfg-br0 
TYPE=Bridge
DEVICE=br0
NM_CONTROLLED=no
BOOTPROTO=static
NAME=br0
ONBOOT=yes
IPADDR=172.16.11.100
NETMASK=255.255.255.0
GATEWAY=172.16.11.1
DNS1=114.114.114.114
DNS2=8.8.8.8
[[email protected] network-scripts]# vim ifcfg-eth0 
TYPE=Ethernet
BOOTPROTO=static
NAME=eth0
DEVICE=eth0
ONBOOT=yes
BRIDGE=br0
NM_CONTROLLED=no
[[email protected] ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master br0 state UP qlen 1000
    link/ether 00:0c:29:35:dd:b8 brd ff:ff:ff:ff:ff:ff
    inet6 fe80::20c:29ff:fe35:ddb8/64 scope link 
       valid_lft forever preferred_lft forever
3: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP qlen 1000
    link/ether 00:0c:29:35:dd:b8 brd ff:ff:ff:ff:ff:ff
    inet 172.16.11.100/24 brd 172.16.11.255 scope global br0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe35:ddb8/64 scope link 
       valid_lft forever preferred_lft forever

③.测试并验证安装结果

[[email protected] ~]# systemctl start libvirtd
[[email protected] ~]# systemctl enable libvirtd
[[email protected] ~]# virsh -c qemu:///system list
 Id    Name                           State
----------------------------------------------------

[[email protected] ~]# virsh -v 
4.5.0
[[email protected] ~]# virt-install --version
1.5.0
[[email protected] ~]# ln -s /usr/libexec/qemu-kvm /usr/bin/    
[[email protected] ~]# lsmod | grep kvm
kvm_intel             170086  0 
kvm                   566340  1 kvm_intel
irqbypass              13503  1 kvm
[[email protected] ~]# brctl show      //查看网桥信息
bridge name	bridge id		STP enabled	interfaces
br0		8000.000c2935ddb8	no		eth0
virbr0		8000.525400afae89	yes		virbr0-nic

④.kvm的web管理界面安装

[[email protected] ~]# yum -y install git python-pip libvirt-python libxml2-python python-websockify supervisor nginx python-devel     //安装依赖包
[[email protected] ~]# pip install --upgrade pip     //升级pip
Collecting pip
  Downloading https://files.pythonhosted.org/packages/c2/d7/90f34cb0d83a6c5631cf71dfe64cc1054598c843a92b400e55675cc2ac37/pip-18.1-py2.py3-none-any.whl (1.3MB)
    100% |████████████████████████████████| 1.3MB 17kB/s 
Installing collected packages: pip
  Found existing installation: pip 8.1.2
    Uninstalling pip-8.1.2:
      Successfully uninstalled pip-8.1.2
Successfully installed pip-18.1

⑤.从github上下载webvirtmgr代码并安装

[[email protected] ~]# cd /usr/local/src
[[email protected] src]# git clone git://github.com/retspen/webvirtmgr.git       //下载webvirtmgr
Cloning into 'webvirtmgr'...
remote: Enumerating objects: 5614, done.
remote: Total 5614 (delta 0), reused 0 (delta 0), pack-reused 5614
Receiving objects: 100% (5614/5614), 2.98 MiB | 248.00 KiB/s, done.
Resolving deltas: 100% (3603/3603), done.
[[email protected] src]# cd webvirtmgr/
[[email protected] webvirtmgr]# pip install -r requirements.txt        //安装webviregmr
Collecting django==1.5.5 (from -r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/38/49/93511c5d3367b6b21fc2995a0e53399721afc15e4cd6eb57be879ae13ad4/Django-1.5.5.tar.gz (8.1MB)
    100% |████████████████████████████████| 8.1MB 127kB/s 
Collecting gunicorn==19.5.0 (from -r requirements.txt (line 2))
  Downloading https://files.pythonhosted.org/packages/f9/4e/f4076a1a57fc1e75edc0828db365cfa9005f9f6b4a51b489ae39a91eb4be/gunicorn-19.5.0-py2.py3-none-any.whl (113kB)
    100% |████████████████████████████████| 122kB 123kB/s 
Collecting lockfile>=0.9 (from -r requirements.txt (line 5))
  Downloading https://files.pythonhosted.org/packages/c8/22/9460e311f340cb62d26a38c419b1381b8593b0bb6b5d1f056938b086d362/lockfile-0.12.2-py2.py3-none-any.whl
Installing collected packages: django, gunicorn, lockfile
  Running setup.py install for django ... done
Successfully installed django-1.5.5 gunicorn-19.5.0 lockfile-0.12.2
[[email protected] webvirtmgr]# python      //检查sqlite3模块是否安装
Python 2.7.5 (default, Oct 30 2018, 23:45:53) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3       //这里不报错即表示安装
>>> exit()

⑥.初始化账号信息,拷贝网页到指定目录

[[email protected] webvirtmgr]# python manage.py syncdb
WARNING:root:No local_settings file found.
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_groups
Creating table auth_user_user_permissions
Creating table auth_user
Creating table django_content_type
Creating table django_session
Creating table django_site
Creating table servers_compute
Creating table instance_instance
Creating table create_flavor

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'):        //指定用户名,默认root
Email address: [email protected]     //邮箱地址
Password:       //管理员密码
Password (again): 
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 6 object(s) from 1 fixture(s)
[[email protected] webvirtmgr]# mkdir /var/www
[[email protected] webvirtmgr]# cp -r /usr/local/src/webvirtmgr/ /var/www/
[[email protected] webvirtmgr]# chown -R nginx.nginx /var/www/webvirtmgr/

⑦.生成**并拷贝到kvm服务器

由于这里webvirtmgr和kvm服务部署在同一台机器,所以这里本地信任。如果kvm部署在其他机器,那么就要把公钥拷贝到部署kvm的服务器上面去

[[email protected] ~]# ssh-****** -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:iMvPwgisiUrNyT+fjp4GrjNyc79dUgwNaXubaRBKdKc [email protected]
The key's randomart image is:
+---[RSA 2048]----+
|     .. o..      |
|      ..+=       |
|     . oEo.      |
|     ...oo.      |
|.   . . Soo+     |
|..+o..   .=      |
|o+.B+   ...      |
|*o+.==o..o       |
|+o+ooBO=.        |
+----[SHA256]-----+
[[email protected] ~]# ssh-copy-id 172.16.11.100

[[email protected] ~]# ssh 172.16.11.100  -L localhost:8000:localhost:8000 -L localhost:6080:localhost:60      //配置端口转发
Last login: Sat Dec 15 22:45:57 2018 from 172.16.11.1
[[email protected] ~]# ss -antl
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN      0      128              *:111                          *:*                  
LISTEN      0      5      192.168.122.1:53                           *:*                  
LISTEN      0      128              *:22                           *:*                  
LISTEN      0      100      127.0.0.1:25                           *:*                  
LISTEN      0      128      127.0.0.1:6080                         *:*                  
LISTEN      0      128      127.0.0.1:8000                         *:*                  
LISTEN      0      128             :::111                         :::*                  
LISTEN      0      128             :::22                          :::*                  
LISTEN      0      100            ::1:25                          :::*                  
LISTEN      0      128            ::1:6080                        :::*                  
LISTEN      0      128            ::1:8000                        :::*  

⑧.配置nginx

[[email protected] ~]# vim /etc/nginx/nginx.conf
......
    server {
        listen       80;      //删除后面的defaults
        server_name  _;
        root         /usr/share/nginx/html;
......
[[email protected] ~]# 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;
        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;
    }
}

[[email protected] ~]# vim /var/www/webvirtmgr/conf/gunicorn.conf.py 
bind = '0.0.0.0:8000'     //确保此处绑定的是本机的8000端口,这个在nginx配置中定义了,被代理的端口
backlog = 2048
[[email protected] ~]# systemctl restart nginx

[[email protected] ~]# vim /etc/supervisord.conf      //设置supervisor,在配置文件的最后添加
.......
[program:webvirtmgr]
command=/usr/bin/python2 /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/python2 /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

[[email protected] ~]# systemctl start supervisord.service 
[[email protected] ~]# systemctl enable supervisord.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/supervisord.service to /usr/lib/systemd/system/supervisord.service.

⑨.配置nginx用户

[[email protected] ~]# mkdir /home/nginx
[[email protected] ~]# chown -R nginx.nginx /home/nginx/
[[email protected] ~]# chmod -R 700 /home/nginx/
[[email protected] ~]# su - nginx -s /bin/bash      //因为nginx用户默认使用的shell是nologbin,这里使用bash来登陆nginx
-bash-4.2$ ssh-****** -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/var/lib/nginx/.ssh/id_rsa): 
Created directory '/var/lib/nginx/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /var/lib/nginx/.ssh/id_rsa.
Your public key has been saved in /var/lib/nginx/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:PQqLHtYD89vtuseW7Oasoo2JRZVxfq87ZlqCO55cdyY [email protected]
The key's randomart image is:
+---[RSA 2048]----+
|      . .        |
|       =         |
|      o . .      |
|     .   o .     |
|    + . S o .    |
|   . * + . o     |
|    = * +oE.o    |
|   = *oB *@=     |
|  . =oBo*#B.     |
+----[SHA256]-----+
-bash-4.2$ touch ~/.ssh/config && echo -e "StrictHostKeyChecking=no\nUserKnownHostsFile=/dev/null" >> ~/.ssh/config
-bash-4.2$ chmod 0600 ~/.ssh/config 
-bash-4.2$ ssh-copy-id [email protected]

[[email protected] ~]# vim /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
[Remote libvirt SSH access]
Identity=unix-user:root
Action=org.libvirt.unix.manage
ResultAny=yes
ResultInactive=yes
ResultActive=yes

[[email protected] ~]# systemctl restart nginx
[[email protected] ~]# systemctl restart libvirtd


到这里接配置完成了,可以使用web界面添加虚拟机了
Kvm虚拟化部署

登陆进去以后就开始添加ssh主机链接
Kvm虚拟化部署

Kvm虚拟化部署

Kvm虚拟化部署

添加存储池
Kvm虚拟化部署

Kvm虚拟化部署

Kvm虚拟化部署

Kvm虚拟化部署

通过远程链接将ISO镜像上传至这个目录即可

[[email protected] ~]# cd /var/lib/libvirt/images/
[[email protected] images]# 
[[email protected] images]# ls
rhel-server-7.4-x86_64-dvd.iso

Kvm虚拟化部署

创建系统安装镜像
Kvm虚拟化部署

Kvm虚拟化部署

添加桥接网络
Kvm虚拟化部署

Kvm虚拟化部署

添加虚拟机实例
Kvm虚拟化部署

Kvm虚拟化部署

Kvm虚拟化部署

虚拟机插入光盘
Kvm虚拟化部署

设置web*问虚拟机的密码
Kvm虚拟化部署

开启虚拟机,打开控制台
Kvm虚拟化部署