KVM虚拟化部署

首先准备一台contos7左服务器,配置尽量要高,虚拟机CPU虚拟化设置打开。

[root@192 ~]# systemctl disable --now firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@192 ~]# vim /etc/selinux/config 
[root@192 ~]# setenforce 0
//首先关闭防火墙selinux

[root@192 yum.repos.d]# yum -y install epel-release
//配置epel源
[root@localhost ~]# egrep -o 'vmx|svm' /proc/cpuinfo
svm
svm
svm
svm
svm
svm
svm
svm
//验证CPU是否支持KVM;如果结果中有vmx(Intel)或svm(AMD)字样,就说明CPU的支持的


[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
//安装依赖包
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# ls
ifcfg-ens33  ifdown-isdn      ifup          ifup-plip      ifup-tunnel
ifcfg-lo     ifdown-post      ifup-aliases  ifup-plusb     ifup-wireless
ifdown       ifdown-ppp       ifup-bnep     ifup-post      init.ipv6-global
ifdown-bnep  ifdown-routes    ifup-eth      ifup-ppp       network-functions
ifdown-eth   ifdown-sit       ifup-ib       ifup-routes    network-functions-ipv6
ifdown-ib    ifdown-Team      ifup-ippp     ifup-sit
ifdown-ippp  ifdown-TeamPort  ifup-ipv6     ifup-Team
ifdown-ipv6  ifdown-tunnel    ifup-isdn     ifup-TeamPort
[root@localhost network-scripts]# cp ifcfg-ens33 ifcfg-br0
[root@localhost network-scripts]# cat ifcfg-br0 
TYPE=Bridge
BOOTPROTO=static
NM_CONTROLLED=no
NAME=br0
DEVICE=br0
ONBOOT=yes
IPADDR=192.168.80.10
NETMASK=255.255.255.0
GATEWAY=192.168.80.2
DNS1=114.114.114.114
//因为虚拟机中网络,我们一般都是和公司的其他服务器是同一个网段,所以我们需要把 \
KVM服务器的网卡配置成桥接模式。这样的话KVM的虚拟机就可以通过该桥接网卡和公司内部 \
其他服务器处于同一网段
//此处我的网卡是ens33,所以用br0来桥接ens33网卡
[root@localhost network-scripts]# vim ifcfg-ens33 

TYPE=Ethernet
BOOTPROTO=static
NAME=ens33
DEVICE=ens33
ONBOOT=yes
BRIDGE=br0
NM_CONTROLLED=no


[root@localhost network-scripts]# systemctl restart network
[root@localhost 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 pfifo_fast master br0 state UP group default qlen 1000
    link/ether 00:0c:29:bd:ba:a4 brd ff:ff:ff:ff:ff:ff
    inet6 fe80::20c:29ff:febd:baa4/64 scope link 
       valid_lft forever preferred_lft forever
3: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 00:0c:29:bd:ba:a4 brd ff:ff:ff:ff:ff:ff
    inet 192.168.80.10/24 brd 192.168.80.255 scope global br0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:febd:baa4/64 scope link 
       valid_lft forever preferred_lft forever

[root@localhost ~]#  systemctl enable libvirtd
[root@localhost ~]#  systemctl status libvirtd
● libvirtd.service - Virtualization daemon
   Loaded: loaded (/usr/lib/systemd/system/libvirtd.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2022-08-21 20:21:14 CST; 12s ago
     Docs: man:libvirtd(8)
           https://libvirt.org
 Main PID: 2924 (libvirtd)
   CGroup: /system.slice/libvirtd.service
           ├─2924 /usr/sbin/libvirtd
           ├─3035 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf ...
           └─3036 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf ...

Aug 21 20:21:15 localhost.localdomain dnsmasq[3029]: listening on virbr0(#4): 192.168...1
Aug 21 20:21:15 localhost.localdomain dnsmasq[3035]: started, version 2.76 cachesize 150
Aug 21 20:21:15 localhost.localdomain dnsmasq[3035]: compile time options: IPv6 GNU-g...y
Aug 21 20:21:15 localhost.localdomain dnsmasq-dhcp[3035]: DHCP, IP range 192.168.122....h
Aug 21 20:21:15 localhost.localdomain dnsmasq-dhcp[3035]: DHCP, sockets bound exclusi...0
Aug 21 20:21:15 localhost.localdomain dnsmasq[3035]: reading /etc/resolv.conf
Aug 21 20:21:15 localhost.localdomain dnsmasq[3035]: using nameserver 114.114.114.114#53
Aug 21 20:21:15 localhost.localdomain dnsmasq[3035]: read /etc/hosts - 2 addresses
Aug 21 20:21:15 localhost.localdomain dnsmasq[3035]: read /var/lib/libvirt/dnsmasq/de...s
Aug 21 20:21:15 localhost.localdomain dnsmasq-dhcp[3035]: read /var/lib/libvirt/dnsma...e
Hint: Some lines were ellipsized, use -l to show in full.
//验证安装结果
[root@localhost ~]# lsmod|grep kvm
kvm_amd              2177304  0 
kvm                   637289  1 kvm_amd
irqbypass              13503  1 kvm
[root@localhost ~]# virsh -c qemu:///system list
 Id    Name                           State
----------------------------------------------------

[root@localhost ~]# virsh --version
4.5.0
[root@localhost ~]# virt-install --version
1.5.0
[root@localhost ~]# ln -s /usr/libexec/qemu-kvm /usr/bin/qemu-kvm
[root@localhost ~]# ll /usr/bin/qemu-kvm
lrwxrwxrwx 1 root root 21 Aug 21 20:23 /usr/bin/qemu-kvm -> /usr/libexec/qemu-kvm
[root@localhost ~]#  brctl show
bridge name	bridge id		STP enabled	interfaces
br0		8000.000c29bdbaa4	no		ens33
virbr0		8000.5254002c6bca	yes		virbr0-nic
//查看网桥信息

  kvm web管理界面安装

yum -y install git python-pip libvirt-python libxml2-python python-websockify supervisor nginx python-devel
//安装依赖包

[root@localhost ~]# ls
anaconda-ks.cfg  webvirtmgr-master.zip
//可以提前去网上把这个包下载下来
[root@localhost ~]# ls
anaconda-ks.cfg  webvirtmgr-master  webvirtmgr-master.zip
[root@localhost ~]# mv webvirtmgr-master /usr/local/src/webvirtmgr
[root@localhost src]# ls
webvirtmgr
//放到这个目录

[root@localhost webvirtmgr]# [root@localhost webvirtmgr]# pip install -i https://pypi.douban.com/simple -r requirements.txt

//安装webvirtmgr

[root@localhost webvirtmgr]# python
Python 2.7.5 (default, Jun 28 2022, 15:30:04) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
>>> exit()
//检查sqlite3是否安装
//初始化账号信息
[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: mingzi540@126.com
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 ~]# 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:xLVw/S3n0Eee+cNtm+N72C7R6REgp/xlaXjwmW+0zgI root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
|        . o.     |
|       . + o.+  .|
|        o o +.==*|
|       .   o .+&B|
|        S   . *BO|
|           E ..*O|
|            . +*=|
|             .oBo|
|              o==|
+----[SHA256]-----+
//由于这里webvirtmgr和kvm服务部署在同一台机器,所以这里本地信任。如果kvm部署在其他机器,那么这个是它的ip

[root@localhost ~]#  ssh-copy-id 192.168.80.10
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.80.10 (192.168.80.10)' can't be established.
ECDSA key fingerprint is SHA256:s+jLuiJTBe5G/3BKKZ12kxFk7FjXsddZA6aIQmGvVeY.
ECDSA key fingerprint is MD5:b6:8c:7f:bd:f4:bb:5c:a9:9f:db:77:23:7e:09:dc:0c.
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.80.10's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '192.168.80.10'"
and check to make sure that only the key(s) you wanted were added.
//配置端口转发
root@localhost ~]# ssh 192.168.80.10 -L localhost:8000:localhost:8000 -L localhost:6080:localhost:60
Last login: Sun Aug 21 20:51:27 2022 from 192.168.80.1

[root@localhost ~]# ss -antl
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port              
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      5      192.168.122.1:53                           *:*                  
LISTEN      0      128              *:22                           *:*                  
LISTEN      0      100      127.0.0.1:25                           *:*                  
LISTEN      0      128          [::1]:6010                      [::]:*                  
LISTEN      0      128          [::1]:6080                      [::]:*                  
LISTEN      0      128          [::1]:8000                      [::]:*                  
LISTEN      0      128           [::]:22                        [::]:*                  
LISTEN      0      100          [::1]:25                        [::]:*  

配置nginx

[root@localhost ~]# vim /etc/nginx/nginx.conf
[root@localhost ~]# vi /etc/nginx/nginx.conf
[root@localhost ~]# 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 ~]# vi  /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://192.168.80.10: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 ~]# vi /var/www/webvirtmgr/conf/gunicorn.conf.py

bind = '192.168.80.10:8000'
backlog = 2048
//绑定本机的

[root@localhost ~]# systemctl restart nginx
[root@localhost ~]# ss -antl
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port              
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              *:80                           *:*                  
LISTEN      0      5      192.168.122.1:53                           *:*                  
LISTEN      0      128              *:22                           *:*                  
LISTEN      0      100      127.0.0.1:25                           *:*                  
LISTEN      0      128          [::1]:6010                      [::]:*                  
LISTEN      0      128          [::1]:6080                      [::]:*                  
LISTEN      0      128          [::1]:8000                      [::]:*                  
LISTEN      0      128           [::]:22                        [::]:*                  
LISTEN      0      100          [::1]:25                        [::]:*     

设置supervisor

[root@localhost ~]# vi /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@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 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:
SHA256:CUz/xJcmzYTcxscki5YnzwW3dxpuZ3NJXhrUcV78Y4o nginx@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
|      .  . ++o+++|
|     o . .o*+Boo=|
|      o . O.O.=.*|
|       . = O ooO=|
|        S . + B+=|
|           E o oo|
|                 |
|                 |
|                 |
+----[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 root@192.168.80.10
/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/var/lib/nginx/.ssh/id_rsa.pub"
/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
Warning: Permanently added '192.168.80.10' (ECDSA) to the list of known hosts.
root@192.168.80.10's password: 

Number of key(s) added: 1

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

-bash-4.2$ exit
logout
[root@localhost ~]# cat /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
bvirt SSH access]
Identity=unix-user:root
Action=org.libvirt.unix.manage
ResultAny=yes
ResultInactive=yes
ResultActive=yes
[root@localhost ~]# chown -R root.root /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
[root@localhost ~]# systemctl restart nginx
[root@localhost ~]#  systemctl restart libvirtd
[root@localhost ~]# ss -antl
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN      0      128      127.0.0.1:6010                         *:*                  
LISTEN      0      128    192.168.80.10:8000                         *:*                  
LISTEN      0      128      127.0.0.1:6080                         *:*                  
LISTEN      0      128      127.0.0.1:8000                         *:*                  
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          [::1]:6010                      [::]:*                  
LISTEN      0      128          [::1]:6080                      [::]:*                  
LISTEN      0      128          [::1]:8000                      [::]:*                  
LISTEN      0      128           [::]:22                        [::]:*                  
LISTEN      0      100          [::1]:25                        [::]:*  

用ip访问

点击这里

 

 通过远程连接软件上传ISO镜像文件至存储目录/var/lib/libvirt/images/

[root@localhost ~]# cd /var/lib/libvirt/images/
[root@localhost images]# ls
CentOS-Stream-8-x86_64-20220228-dvd1.iso

 

 刷新一下

创建网络

 

创建新的虚拟机

定制虚拟机

设置密码

接下来启动

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值