kvm虚拟化

部署kvm

添加硬盘

安装kvm

[root@kvm ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@kvm ~]# setenforce 0
[root@kvm ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@kvm ~]# reboot

//部署yum源
[root@kvm ~]# cd /etc/yum.repos.d/
[root@kvm yum.repos.d]# rm -rf *
[root@kvm yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2495  100  2495    0     0   8372      0 --:--:-- --:--:-- --:--:--  8372
[root@kvm yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo


//安装所需软件包
[root@kvm ~]# yum -y install epel-release vim wget net-tools unzip zip gcc gcc-c++ qemu-kvm  qemu-img virt-manager libvirt libvirt-client virt-install virt-viewer  libguestfs-tools


//验证cpu是否支持kvm,vmx是intel的  svm是AMD的
[root@kvm ~]# egrep -o 'vmx|svm' /proc/cpuinfo
vmx

//安装kvm
[root@kvm ~]# yum -y install qemu-kvm \
> qemu-kvm \
> qemu-img \
>  virt-manager \
>  libvirt \
>  libvirt-python3 \
>  libvirt-client \
>  virt-install \
>  virt-viewer \
>  bridge-utils \
>  libguestfs-tools

[root@kvm ~]# cd /etc/sysconfig/network-scripts/
[root@kvm network-scripts]# cp ifcfg-ens33 ifcfg-br0
[root@kvm network-scripts]# ls
ifcfg-br0  ifcfg-ens33
[root@kvm network-scripts]# vim ifcfg-br0 
//将网卡配置修改为如下内容
TYPE=Bridge
BOOTPROTO=none
NAME=br0
DEVICE=br0
ONBOOT=yes
IPADDR=192.168.253.130
PREFIX=24
GATEWAY=192.168.253.2
DNS1=8.8.8.8

[root@kvm network-scripts]# vim ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=none
NAME=ens33
DEVICE=ens33
ONBOOT=yes
BRIDGE=eth0

//重启网卡
[root@kvm network-scripts]# nmcli connection reload
[root@kvm network-scripts]# nmcli connection up ens33
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/4)
[root@kvm network-scripts]# nmcli connection up eth0
Connection successfully activated (master waiting for slaves) (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/5)

[root@kvm network-scripts]# 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 fq_codel master br0 state UP group default qlen 1000
    link/ether 00:0c:29:7b:a9:19 brd ff:ff:ff:ff:ff:ff
3: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 00:0c:29:7b:a9:19 brd ff:ff:ff:ff:ff:ff
    inet 192.168.253.130/24 brd 192.168.253.255 scope global noprefixroute br0
       valid_lft forever preferred_lft forever

启动

//启动libvirtd服务
[root@kvm ~]# systemctl enable --now libvirtd

//查看kvm模块是否加载
[root@kvm ~]# lsmod | grep kvm
kvm_intel             323584  0
kvm                   905216  1 kvm_intel
irqbypass              16384  1 kvm

//将qemu-kvm这个命令做一个软链接到/usr/bin/qemu-kvm
[root@kvm ~]# ln -s /usr/libexec/qemu-kvm /usr/bin/qemu-kvm
[root@kvm ~]# ll /usr/bin/qemu-kvm
lrwxrwxrwx. 1 root root 21 Oct  4 22:32 /usr/bin/qemu-kvm -> /usr/libexec/qemu-kvm
[root@kvm ~]# 

//查看网桥信息
[root@kvm ~]# brctl show
bridge name     bridge id               STP enabled     interfaces
eth0             8000.000c297ba919       no              ens33
virbr0          8000.52540038282c       yes             virbr0-nic

管理界面安装

//安装依赖包
[root@kvm ~]# yum -y install git python2-pip supervisor nginx python2-devel
[root@kvm ~]# wget http://mirror.centos.org/centos/7/os/x86_64/Packages/libxml2-python-2.9.1-6.el7.5.x86_64.rpm
--2022-10-04 22:40:47--  http://mirror.centos.org/centos/7/os/x86_64/Packages/libxml2-python-2.9.1-6.el7.5.x86_64.rpm
Resolving mirror.centos.org (mirror.centos.org)... 13.233.140.107, 2401:78c0::e00e
Connecting to mirror.centos.org (mirror.centos.org)|13.233.140.107|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 253008 (247K) [application/x-rpm]
Saving to: 'libxml2-python-2.9.1-6.el7.5.x86_64.rpm'

libxml2-python-2.9. 100%[===================>] 247.08K  65.7KB/s    in 3.8s    

2022-10-04 22:40:52 (65.7 KB/s) - 'libxml2-python-2.9.1-6.el7.5.x86_64.rpm' saved [253008/253008]

//下载webvirtmgr
[root@kvm ~]# wget https://download-ib01.fedoraproject.org/pub/epel/7/x86_64/Packages/p/python-websockify-0.6.0-2.el7.noarch.rpm
[root@kvm ~]# rpm -ivh --nodeps libxml2-python-2.9.1-6.el7.5.x86_64.rpm
[root@kvm ~]# rpm -ivh --nodeps python-websockify-0.6.0-2.el7.noarch.rpm

//升级pip
[root@kvm ~]# pip2 install --upgrade pip
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip2 install --user` instead.
Collecting pip
  Downloading https://files.pythonhosted.org/packages/27/79/8a850fe3496446ff0d584327ae44e7500daf6764ca1a382d2d02789accf7/pip-20.3.4-py2.py3-none-any.whl (1.5MB)
    100% |################################| 1.5MB 17kB/s 
Installing collected packages: pip
  Found existing installation: pip 9.0.3
    Uninstalling pip-9.0.3:
      Successfully uninstalled pip-9.0.3
Successfully installed pip-20.3.4
You are using pip version 20.3.4, however version 22.2.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
[root@kvm ~]# pip -V
pip 20.3.4 from /usr/lib/python2.7/site-packages/pip (python 2.7)

//github拉取webvirtmgr
[root@kvm ~]# cd /usr/local/src/
[root@kvm src]# git clone http://github.com/retspen/webvirtmgr.git
Cloning into 'webvirtmgr'...
warning: redirecting to https://github.com/retspen/webvirtmgr.git/
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 | 1.98 MiB/s, done.
Resolving deltas: 100% (3606/3606), done.
[root@kvm src]# cd webvirtmgr/
[root@kvm webvirtmgr]# ls
MANIFEST.in  create                instance    requirements.txt  storages
README.rst   deploy                interfaces  secrets           templates
Vagrantfile  dev-requirements.txt  locale      serverlog         vrtManager
conf         hostdetail            manage.py   servers           webvirtmgr
console      images                networks    setup.py

//安装webvirtmgr
[root@kvm webvirtmgr]# pip install -r requirements.txt

//检查sqlite3是否安装
[root@kvm webvirtmgr]# python3
Python 3.6.8 (default, Jan 19 2022, 23:28:49) 
[GCC 8.5.0 20210514 (Red Hat 8.5.0-7)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
>>> exit()
[root@kvm webvirtmgr]# 

//初始化账号信息
[root@kvm webvirtmgr]# python2 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		/指定超级管理员账号用户名,默认留空为root
Email address: 123@qq.com		//设置超级管理员邮箱
Password: 		//设置超级管理员密码
Password (again): 		//确认密码
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 6 object(s) from 1 fixture(s)
[root@kvm webvirtmgr]# 

//拷贝web网页到指定目录
[root@kvm webvirtmgr]# mkdir /var/www/
[root@kvm webvirtmgr]# cp -r /usr/local/src/webvirtmgr/ /var/www/
[root@kvm webvirtmgr]# chown -R nginx.nginx /var/www/webvirtmgr/

//生成一对公钥与私钥,由于这里webvirtmgr和kvm服务部署在同一台主机中,所以这里本地信任。如果kvm部署在其他机器上的时候,那么就需要把公钥发送到kvm主机中
[root@kvm webvirtmgr]# ssh-keygen
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:JS78xohDOCq2A6FXAPKnNfClTcf8tydqzsxCrRAotOo root@kvm
The key's randomart image is:
+---[RSA 3072]----+
|o..   oo.        |
|..oo = .o        |
| ..o*.. ...      |
|. o=oo.. o. .    |
|.o+o. o.S. . .   |
|+..o ..=. . o .  |
|=o  o .o+. . o   |
|oE.  . .o+o      |
| ..      +=      |
+----[SHA256]-----+
[root@kvm webvirtmgr]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.253.130
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.253.130 (192.168.253.130) can't be established.
ECDSA key fingerprint is SHA256:g406IIpvR6wRO/BO9gWbfG8O9r+eVF9PuwyLN6XtqyI.
Are you sure you want to continue connecting (yes/no/[fingerprint])? 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.183.138's password: 

Number of key(s) added: 1

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

[root@kvm webvirtmgr]# 

//配置端口转发
[root@kvm webvirtmgr]# ssh 192.168.253.130 -L localhost:8000:localhost:8000 -L localhost:6080:localhost:60
Last login: Tue Oct  4 22:00:55 2022 from 192.168.183.1

//配置nginx
[root@kvm ~]# cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
[root@kvm ~]# vim /etc/nginx/nginx.conf
//在server参数中进行修改
删除listen       [::]:80;行
参数server_name行改成server_name  localhost;
删除root         /usr/share/nginx/html;行
在include /etc/nginx/default.d/*.conf;行下添加
location / {
       root    html;
       index   index.html index.htm;
      }


server {
        listen       80 default_server;
        server_name  localhost;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
        location / {
                root    html;
                index   index.html index.htm;
        }       


//配置nginx虚拟主机
[root@kvm ~]# 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@kvm ~]# 

//确保bind绑定本机的8000端口
[root@kvm ~]# grep "bind"  /var/www/webvirtmgr/conf/gunicorn.conf.py
# bind - The socket to bind.
bind = '127.0.0.1:8000'

//重启nginx服务,查看端口
[root@kvm ~]# systemctl restart nginx
[root@kvm ~]# ss -antl
State      Recv-Q     Send-Q           Local Address:Port           Peer Address:Port     Process     
LISTEN     0          128                    0.0.0.0:111                 0.0.0.0:*                    
LISTEN     0          128                    0.0.0.0:80                  0.0.0.0:*                    
LISTEN     0          128                    0.0.0.0:22                  0.0.0.0:*                    
LISTEN     0          128                  127.0.0.1:6080                0.0.0.0:*                    
LISTEN     0          128                  127.0.0.1:8000                0.0.0.0:*                    
LISTEN     0          128                       [::]:111                    [::]:*                    
LISTEN     0          128                       [::]:22                     [::]:*                    
LISTEN     0          128                      [::1]:6080                   [::]:*                    
LISTEN     0          128                      [::1]:8000                   [::]:*                    
[root@kvm ~]# 

//设置supervisor
[root@kvm ~]# 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

//启动并设置开机自启
[root@kvm ~]# systemctl enable --now supervisord.service
Created symlink /etc/systemd/system/multi-user.target.wants/supervisord.service → /usr/lib/systemd/system/supervisord.service.

//配置nginx用户
[root@kvm ~]# su - nginx -s /bin/bash
[nginx@kvm ~]$ 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:eAzzREPxYbX+6oJ4SBgPLPQtjyd3nQ8di4GNPeZVxLE nginx@kvm
The key's randomart image is:
+---[RSA 3072]----+
|       .=.o..oo. |
|  .    . + . .o. |
| . o .o .=. ..E  |
|  . * .Bo *.o    |
|   . O. S+ B.o   |
|    + *.. * o.   |
|     = + . o  .  |
|      o o . ..   |
|       .   oo    |
+----[SHA256]-----+
[nginx@kvm ~]$ echo -e "StrictHostKeyChecking=no\nUserKnownHostsFile=/dev/null" > ~/.ssh/config
[nginx@kvm ~]$ cat .ssh/config 
StrictHostKeyChecking=no
UserKnownHostsFile=/dev/null
[nginx@kvm ~]$ chmod 600 .ssh/config
[nginx@kvm ~]$ ssh-copy-id root@192.168.253.130
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/var/lib/nginx/.ssh/id_rsa.pub"
/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
Warning: Permanently added '192.168.253.130' (ECDSA) to the list of known hosts.
root@192.168.253.130's password: 

Number of key(s) added: 1

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

[nginx@kvm ~]$ exit
logout
[root@kvm ~]# 
[root@kvm ~]# 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@kvm ~]# systemctl restart nginx
[root@kvm ~]# systemctl restart libvirtd
[root@kvm ~]# ss -antl
State    Recv-Q   Send-Q     Local Address:Port     Peer Address:Port  Process  
LISTEN   0        128            127.0.0.1:8000          0.0.0.0:*              
LISTEN   0        100              0.0.0.0:6080          0.0.0.0:*              
LISTEN   0        128              0.0.0.0:111           0.0.0.0:*              
LISTEN   0        128              0.0.0.0:80            0.0.0.0:*              
LISTEN   0        32         192.168.122.1:53            0.0.0.0:*              
LISTEN   0        128              0.0.0.0:22            0.0.0.0:*              
LISTEN   0        128                 [::]:111              [::]:*              
LISTEN   0        128                 [::]:22               [::]:*  

在这里插入图片描述
上传镜像
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值