KVM虚拟化配置

版权声明:未经本人允许严禁转载 https://blog.csdn.net/WanJiaBaoBao/article/details/85014585

环境介绍

  • 宿主机的IP地址为192.168.43.25

配置步骤

KVM安装

  • 关闭防火墙、selinux
[root@localhost ~]# systemctl stop firewalld 
[root@localhost ~]# systemctl disable firewalld
rm '/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service'
rm '/etc/systemd/system/basic.target.wants/firewalld.service'
[root@localhost ~]# getenforce 
Enforcing
[root@localhost ~]# setenforce 0
[root@localhost ~]# vi /etc/selinux/config 
······
SELINUX=disabled	##改为disabled
······
  • 配置网络源
[root@localhost ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@localhost ~]# sed -i 's/$releasever/7/g' /etc/yum.repos.d/CentOS-Base.repo
[root@localhost ~]# yum -y install epel-release vim wget net-tools unzip zip gcc gcc-c++
  • 验证CPU是否支持KVM;如果结果中有vmx(Intel)或svm(AMD)字样,就说明CPU的支持的
[root@localhost ~]# egrep -o 'vmx|svm' /proc/cpuinfo
vmx
vmx
vmx
vmx
  • 安装KVM
[root@localhost ~]# 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
  • 将网卡eno33554984桥接为br0
[root@localhost ~]# ip a
3: eno33554984: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:47:34:d7 brd ff:ff:ff:ff:ff:ff
    inet 192.168.43.25/24 brd 192.168.43.255 scope global dynamic eno33554984
       valid_lft 2997sec preferred_lft 2997sec
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@kvm network-scripts]# cp ifcfg-eno33554984 ifcfg-br0
[root@localhost network-scripts]# vim ifcfg-eno33554984
TYPE=Ethernet
BOOTPROTO=dhcp
NAME=eno33554984
DEVICE=eno33554984
ONBOOT=yes
BRIDGE=br0
NM_CONTROLLED=no
[root@localhost network-scripts]# vim ifcfg-br0
TYPE=Bridge
BOOTPROTO=dhcp
NAME=br0
DEVICE=br0
ONBOOT=yes
NM_CONTROLLED=no
[root@localhost network-scripts]# systemctl restart network
[root@localhost network-scripts]# ip a
3: eno33554984: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master br0 state UP qlen 1000
    link/ether 00:0c:29:47:34:d7 brd ff:ff:ff:ff:ff:ff
4: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP 
    link/ether 00:0c:29:47:34:d7 brd ff:ff:ff:ff:ff:ff
    inet 192.168.43.25/24 brd 192.168.43.255 scope global dynamic br0
       valid_lft 2827sec preferred_lft 2827sec
  • 启动服务
[root@localhost network-scripts]# systemctl start libvirtd
[root@localhost network-scripts]# systemctl enable libvirtd
  • 测试安装的结果
[root@localhost network-scripts]# lsmod|grep kvm
kvm_intel             138567  0 
kvm                   441119  1 kvm_intel
[root@localhost network-scripts]#
  • 测试并验证安装结果
[root@localhost network-scripts]# virsh -c qemu:///system list
 Id    Name                           State
----------------------------------------------------

  • 查看信息
[root@localhost network-scripts]# virsh --version
4.5.0
[root@localhost network-scripts]# virt-install --version
1.5.0
## 将执行命令连接到PASH中
[root@localhost network-scripts]# ln -s /usr/libexec/qemu-kvm /usr/bin/
[root@localhost network-scripts]# ll /usr/bin/qemu-kvm
lrwxrwxrwx 1 root root 21 Dec 15 23:44 /usr/bin/qemu-kvm -> /usr/libexec/qemu-kvm
## 查看网卡桥接信息
[root@localhost network-scripts]# brctl show
bridge name	bridge id		STP enabled	interfaces
br0			8000.000c294734d7		no			eno33554984
virbr0		8000.5254007cebb5		yes			virbr0-nic

KVM Web界面安装

注:kvm 的 web 管理界面是由 webvirtmgr 程序提供的,而 webvirtmgr是有第三方提供的

  • 安装web界面所需的安装包
[root@localhost network-scripts]# yum -y install git python-pip libvirt-python libxml2-python python-websockify supervisor nginx python-devel
  • 升级pip,并从github上下载webvirtmgr代码
[root@localhost network-scripts]# pip install --upgrade 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 41kB/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

[root@localhost network-scripts]# cd /usr/local/src/
[root@localhost src]# git clone git://github.com/retspen/webvirtmgr.git
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 | 563.00 KiB/s, done.
Resolving deltas: 100% (3603/3603), done.
  • 安装webvirtmgr
[root@localhost src]# cd webvirtmgr/
[root@localhost webvirtmgr]# pip install -r requirements.txt
  • 检查sqlite3是否安装
[root@localhost webvirtmgr]# python
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()
  • 初始化web登录账号信息
[root@localhost 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)
  • 拷贝web网页至指定目录
[root@localhost webvirtmgr]# mkdir /var/www
[root@localhost webvirtmgr]# cp -r /usr/local/src/webvirtmgr/ /var/www/
[root@localhost webvirtmgr]# chown -R nginx.nginx /var/www/webvirtmgr/
[root@localhost webvirtmgr]# ll /var/www/webvirtmgr/ -d
drwxr-xr-x 20 nginx nginx 4096 Dec 16 00:13 /var/www/webvirtmgr/
  • 生成密钥
[root@localhost webvirtmgr]# ssh-keygen -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:
14:49:57:08:c2:83:61:a3:01:b9:48:cf:70:2b:99:e2 [email protected]
The key's randomart image is:
+--[ RSA 2048]----+
|.o. ++..+o.o.    |
|.o =..o..o.      |
|o.O .  ..        |
|++ +   .         |
|o .     S        |
| E               |
|                 |
|                 |
|                 |
+-----------------+

## 由于这里webvirtmgr和kvm服务部署在同一台机器,所以这里本地信任。如果kvm部署在其他机器,那么IP是安装KVM的那台服务器
[root@localhost ~]# ssh-copy-id 192.168.43.25
Are you sure you want to continue connecting (yes/no)? yes
[email protected]'s password:
  • 配置端口转发
[root@localhost ~]# ssh 192.168.43.25 -L localhost:8000:localhost:8000 -L localhost:6080:localhost:60
[root@localhost ~]# ss -antl
State      Recv-Q Send-Q               Local Address:Port                 Peer Address:Port 
LISTEN     0      100                      127.0.0.1:25                              *:*     
LISTEN     0      128                      127.0.0.1:6010                            *:*     
LISTEN     0      128                      127.0.0.1:6080                            *:*     
LISTEN     0      128                      127.0.0.1:8000                            *:*     
LISTEN     0      128                              *:111                             *:*     
LISTEN     0      5                    192.168.122.1:53                              *:*     
LISTEN     0      128                              *:22                              *:*     
  • 配置nginx
[root@localhost ~]# vim /etc/nginx/nginx.conf
......
    server {
        listen       80;				##监听80端口
#       listen       [::]:80 default_server;			##注释
        server_name  _;			##可以改为主机名或者公司的域名
#       root         /usr/share/nginx/html;			##注释
......
  • 生成 webvirtmgr.conf 配置文件
[root@localhost ~]# 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;
    }
}
  • 确保bind绑定的是本机的8000端口
[root@localhost ~]# vim /var/www/webvirtmgr/conf/gunicorn.conf.py
......
bind = '0.0.0.0:8000'		##将127.0.0.1修改为0.0.0.0(监听所有主机)
backlog = 2048
......
  • 设置supervisor
[root@localhost ~]# vim /etc/supervisord.conf
......(文末加)
[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
  • 启动supervisor并设置开机自启
[root@localhost ~]# systemctl start supervisord
[root@localhost ~]# systemctl enable supervisord
Created symlink from /etc/systemd/system/multi-user.target.wants/supervisord.service to /usr/lib/systemd/system/supervisord.service.
  • 配置nginx用户
[root@localhost ~]# cd /home
[root@localhost home]# mkdir nginx
[root@localhost home]# chown -R nginx.nginx nginx/
[root@localhost home]# chmod 700 nginx/
[root@localhost home]# su - nginx -s /bin/bash
-bash-4.2$ ssh-keygen -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:
4d:6e:be:b5:32:17:df:c1:9b:8f:c3:eb:0f:6e:1e:bd [email protected]
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|                 |
|          .      |
|         +       |
|        S +   .  |
|         o  .  + |
|          . .+o.=|
|          oo.o=*o|
|          .+.+*E+|
+-----------------+
-bash-4.2$ touch ~/.ssh/config && echo -e "StrictHostKeyChecking=no\nUserKnownHostsFile=/dev/null" >> ~/.ssh/config
-bash-4.2$ ssh-copy-id [email protected]
[email protected]'s password: 
-bash-4.2$ exit
logout

[root@localhost home]# 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

[root@localhost home]# chown -R root.root /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
[root@localhost home]# systemctl start nginx
[root@localhost home]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@localhost home]# systemctl restart libvirtd

KVM web界面设置及管理

  • 通过ip地址在浏览器上访问kvm,例如我这里就是http://192.168.43.25/login
    在这里插入图片描述

  • 创建ssh连接
    在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

  • 创建存储
    在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

  • 通过远程连接软件上传ISO镜像文件至存储目录/var/lib/libvirt/images/(拷贝过程不展示)
    在这里插入图片描述

  • 创建系统镜像
    在这里插入图片描述

在这里插入图片描述

  • 添加桥接网络
    在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

  • 创建实例
    在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

遇到的错误

错误一:

-bash-4.2$ ssh-copy-id [email protected]
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed

/bin/ssh-copy-id: ERROR: Bad owner or permissions on /var/lib/nginx/.ssh/config

解决办法:

-bash-4.2$ chmod 600 .ssh/config

错误二:(一直弹)

[root@localhost home]# accept: Too many open files
accept: Too many open files
accept: Too many open files
accept: Too many open files
······

解决办法:

[root@localhost ~]# vim /etc/security/limits.conf
## 添加一行如下参数 ##
* - nofile 65530

[root@localhost ~]# vim /etc/nginx/nginx.conf
## 同时在nginx.conf配置文件(全局段)中加入如下参数 ##
worker_rlimit_nofile 65530;

## 重新读取nginx配置文件 ##
[root@localhost ~]# nginx -s reload
## 重新启动系统 ##
[root@localhost ~]# reboot

猜你喜欢

转载自blog.csdn.net/WanJiaBaoBao/article/details/85014585