kvm虚拟化部署

本文详细指导了KVM虚拟化环境的安装与配置过程,包括内核模块、QEMU设备模拟、开启虚拟化功能、安装所需工具、检查CPU支持、搭建网络、部署webvirtmgr等关键步骤。适合深入了解KVM技术的开发者和运维人员。
摘要由CSDN通过智能技术生成

kvm虚拟化部署


KVM虚拟化有两个核心模块:

**1)KVM内核模块:**主要包括KVM虚拟化核心模块KVM.ko,以及硬件相关的KVM_intel或KVM_AMD模块;负责CPU与内存虚拟化,包括VM创建,内存分配与管理、vCPU执行模式切换等。

**2)QEMU设备模拟:**实现IO虚拟化与各设备模拟(磁盘、网卡、显卡、声卡等),通过IOCTL系统调用与KVM内核交互。KVM仅支持基于硬件辅助的虚拟化(如Intel-VT与AMD-V),在内核加载时,KVM先初始化内部数据结构,打开CPU控制寄存器CR4里面的虚拟化模式开关,执行VMXON指令将Host OS设置为root模式,并创建的特殊设备文件/dev/kvm等待来自用户空间的命令,然后由KVM内核与QEMU相互配合实现VM的管理。KVM会复用部分Linux内核的能力,如进程管理调度、设备驱动,内存管理等。

物理服务器上通常配置2个物理pCPU(Socket),每个CPU有多个核(core);开启超线程Hyper-Threading技术后,每个core有2个线程(Thread);在虚拟化环境中一个Thread对应一个vCPU。在KVM中每一个VM就是一个用户空间的QEMU进程,分配给Guest的vCPU就是该进程派生的一个线程Thread,由Linux内核动态调度到基于时分复用的物理pCPU上运行。KVM支持设置CPU亲和性,将vCPU绑定到特定物理pCPU,如通过libvirt驱动指定从NUMA节点为Guest分配vCPU与内存。KVM支持vCPU超分(over-commit)使得分配给Guest的vCPU数量超过物理CPU线程总量。

开启虚拟化功能image-20210620202100377

关闭防火墙和selinux

[root@localhost ~]# systemctl disable --now firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# vi /etc/selinux/config 
SELINUX=disabled

安装工具

[root@localhost ~]# yum -y install epel-release vim wget net-tools unzip zip gcc gcc-c++

检查CPU是否支持KVM

[root@localhost ~]# egrep -o 'vmx|svm' /proc/cpuinfo
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

使用br0来桥接ens33网卡

[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# vim ifcfg-br0
[root@localhost network-scripts]# cat ifcfg-br0 
TYPE=Bridge
DEVICE=br0
NM_CONTROLLED=no
BOOTPROTO=static
NAME=br0
ONBOOT=yes
IPADDR=192.168.0.40
NETMASK=255.255.255.0
GATEWAY=192.168.0.2
DNS1=114.114.114.114
DNS2=8.8.8.8
[root@localhost network-scripts]# cat ifcfg-ens33 
TYPE=Ethernet
BOOTPROTO=static
NAME=ens33
DEVICE=ens33
ONBOOT=yes
BRIDGE=br0
NM_CONTROLLED=no


重启网卡

[root@localhost ~]# systemctl restart network

启动服务

[root@localhost network-scripts]# systemctl enable --now libvirtd
[root@localhost network-scripts]# systemctl status libvirtd
● libvirtd.service - Virtualization daemon
   Loaded: loaded (/usr/lib/systemd/system/libvirtd.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2021-06-20 20:53:17 CST; 38min ago
     Docs: man:libvirtd(8)
           https://libvirt.org
 Main PID: 2181 (libvirtd)
    Tasks: 19 (limit: 32768)
   CGroup: /system.slice/libvirtd.service
           ├─1364 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-r...
           ├─1367 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-r...
           └─2181 /usr/sbin/libvirtd

Jun 20 21:01:19 localhost.localdomain dnsmasq[1364]: reading /etc/resolv.conf
Jun 20 21:01:19 localhost.localdomain dnsmasq[1364]: using nameserver 114.114.114.114#53
Jun 20 21:01:19 localhost.localdomain dnsmasq[1364]: using nameserver 8.8.8.8#53
Jun 20 21:01:24 localhost.localdomain dnsmasq[1364]: reading /etc/resolv.conf
Jun 20 21:01:24 localhost.localdomain dnsmasq[1364]: using nameserver 114.114.114.114#53
Jun 20 21:01:24 localhost.localdomain dnsmasq[1364]: using nameserver 8.8.8.8#53
Jun 20 21:25:28 localhost.localdomain dnsmasq[1364]: reading /etc/resolv.conf
Jun 20 21:25:28 localhost.localdomain dnsmasq[1364]: using nameserver 192.168.0.1#53
Jun 20 21:25:30 localhost.localdomain dnsmasq[1364]: reading /etc/resolv.conf
Jun 20 21:25:30 localhost.localdomain dnsmasq[1364]: using nameserver 192.168.0.1#53

验证

[root@localhost network-scripts]# lsmod|grep kvm
kvm_intel             188740  0 
kvm                   637289  1 kvm_intel
irqbypass              13503  1 kvm
[root@localhost network-scripts]# ln -s /usr/libexec/qemu-kvm /usr/bin/qemu-kvm


kvm web 页面安装

安装依赖包

[root@localhost ~]# yum -y install git python-pip libvirt-python libxml2-python python-websockify supervisor nginx python-devel

从github上下载webvirtmgr代码

[root@localhost ~]# 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.97 MiB | 28.00 KiB/s, done.
Resolving deltas: 100% (3606/3606), done.
[root@localhost ~]# ls
anaconda-ks.cfg  webvirtmgr

安装webvirtmgr

[root@localhost ~]# cd webvirtmgr/
[root@localhost webvirtmgr]# cat requirements.txt 
django==1.5.5
gunicorn==19.5.0
# Utility Requirements
# for SECURE_KEY generation
lockfile>=0.9
# Uncoment for support ldap
#django-auth-ldap==1.2.0
[root@localhost webvirtmgr]# pip install -r requirements.txt 

初始化账号信息

[root@localhost webvirtmgr]# python manage.py syncdb

移动网站目录并设置属组和属主

[root@localhost webvirtmgr]# mkdir -p /var/www
[root@localhost webvirtmgr]# cp -r /root/webvirtmgr/ /var/www/
[root@localhost webvirtmgr]# chown -R nginx.nginx /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:
SHA256:N+sdmfjKqtQTxdftfwCOe6gzOLEbHC+3BfHPg8Ff5sbUU root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
|                 |
|                 |
|.        .     E |
|.+    . ...   .  |
|+.* ..o.So+    . |
|.*.. ++Booo.. .  |
|.ooo. =*= o+ o   |
| oB+o+. .o. .    |
|. +B=+           |
+----[SHA256]-----+
[root@localhost webvirtmgr]# ssh-copy-id 192.168.207.140
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.247.50 (192.168.247.140)' can't be established.
ECDSA key fingerprint is SHA256:w6Kbwkn18/Aj5eoWDcNRGLWpWsdEa6r4gYvyX8ZBKeQ.
ECDSA key fingerprint is MD5:e8:7f:6c:96:23:f1:e8:e3:4e:cd:1e:7f:50:d7:a9:38.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.207.140's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '192.168.247.50'"
and check to make sure that only the key(s) you wanted were added.

[root@localhost webvirtmgr]# ssh 192.168.207.140
Last login: Sat Jun 19 03:08:24 2021 from 192.168.207.2
[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master br0 state UP group default qlen 1000
    link/ether 00:0c:29:d7:8d:e3 brd ff:ff:ff:ff:ff:ff
3: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 00:0c:29:d7:8d:e3 brd ff:ff:ff:ff:ff:ff
    inet 192.168.207.140/24 brd 192.168.207.255 scope global br0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fed7:8de3/64 scope link 
       valid_lft forever preferred_lft forever
4: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether 52:54:00:f7:62:63 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
5: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN group default qlen 1000
    link/ether 52:54:00:f7:62:63 brd ff:ff:ff:ff:ff:ff
[root@localhost ~]# exit
logout
Connection to 192.168.207.140 closed.

配置nginx

[root@localhost webvirtmgr]# cd /etc/nginx/
[root@localhost nginx]# mv nginx.conf nginx.conf-bak
[root@localhost nginx]# vim /etc/nginx/nginx.conf
[root@localhost nginx]# cat /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80;
        server_name  localhost;

        include /etc/nginx/default.d/*.conf;

        location / {
            root html;
            index index.html index.htm;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}
[root@localhost nginx]# vim /etc/nginx/conf.d/webvirtmgr.conf
[root@localhost nginx]# cat /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;
    }
}

修改绑定端口

[root@localhost nginx]# vim /var/www/webvirtmgr/conf/gunicorn.conf.py 
bind = '0.0.0.0:8000'
backlog = 2048

对supervisord.conf文件进行修改

[root@localhost nginx]# cat >> /etc/supervisord.conf <<EOF
> [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
> EOF

启动supervisord

[root@localhost nginx]# systemctl enable --now supervisord
[root@localhost nginx]# systemctl restart nginx
[root@localhost nginx]# ss -anlt
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN      0      128       *:8000                  *:*                  
LISTEN      0      100       *:6080                  *:*                  
LISTEN      0      128       *:111                   *:*                  
LISTEN      0      128       *:80                    *:*                  
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:6010                  *:*                  
LISTEN      0      128    [::]:111                [::]:*                  
LISTEN      0      128    [::]:22                 [::]:*                  
LISTEN      0      100       [::1]:25                 [::]:*                  
LISTEN      0      128       [::1]:6010               [::]:*    

配置nginx

[root@localhost nginx]# 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:
SHA256:effZyqsdMBdsKY5sdIIHysQVAasvRcAtBnI2Ln3FkONUg nginx@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
|=*=++Eoo         |
|+.==*= ..        |
|oo=*+.* .        |
|.  + B + +   . o |
|    + o S * o o .|
|     . o o = . . |
|            . +  |
|             o . |
|            .    |
+----[SHA256]-----+
-bash-4.2$ ssh-copy-id root@192.168.207.140
/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/var/lib/nginx/.ssh/id_rsa.pub"
The authenticity of host '192.168.207.140 (192.168.207.140)' can't be established.
ECDSA key fingerprint is SHA256:w6Kbwkn18/Aj5eoWDcNRGLWpWsdEa6r4gYvyX8ZBKeQ.
ECDSA key fingerprint is MD5:e8:7f:6c:96:23:f1:e8:e3:4e:cd:1e:7f:50:d7:a9:38.
Are you sure you want to continue connecting (yes/no)? yes
/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: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.247.50's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@192.168.207.140'"
and check to make sure that only the key(s) you wanted were added.

-bash-4.2$ exit
logout

[root@localhost nginx]# 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 nginx]# systemctl restart nginx
[root@localhost ~]# systemctl restart libvirtd     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值