KVM部署

系统 ip
centos7 192.168.94.137

网卡设置nat模式,开启cpu虚拟化,防火墙等安全服务关闭。

systemctl disable --now firewalld

vi /etc/selinux/config 
...
SELINUX=disabled

桥接网络,br0桥接本地网卡(我是ens33)

//安装基本工具
[root@kvm ~]# yum -y install epel-release vim wget net-tools unzip zip gcc gcc-c++
//安装kvm相关包
[root@kvm ~]# 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@kvm network-scripts]# cat ifcfg-ens33 
TYPE="Ethernet"
BOOTPROTO=static
NAME=ens33
DEVICE=ens33
ONBOOT=yes
BRIDGE=br0
NM_CONTROLLED=no

[root@kvm network-scripts]# cat ifcfg-br0 
TYPE=Bridge
DEVICE=br0
NM_CONTROLLED=no
BOOTPROTO=static
NAME=br0
ONBOOT=yes
IPADDR=192.168.94.137
NETMASK=255.255.255.0
GATEWAY=192.168.94.2
DNS=192.168.94.1

[root@kvm ~]# systemctl restart NetworkManager
[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 pfifo_fast master br0 state UP group default qlen 1000
    link/ether 00:0c:29:8c:c6:c0 brd ff:ff:ff:ff:ff:ff
    inet6 fe80::20c:29ff:fe8c:c6c0/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:8c:c6:c0 brd ff:ff:ff:ff:ff:ff
    inet 192.168.94.137/24 brd 192.168.94.255 scope global br0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe8c:c6c0/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:16:d8 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:16:d8 brd ff:ff:ff:ff:ff:ff

//启动libvirt守护进程
[root@kvm network-scripts]# systemctl enable --now libvirtd
[root@kvm network-scripts]# lsmod |grep kvm
kvm_amd              2177304  0 
kvm                   637289  1 kvm_amd
irqbypass              13503  1 kvm
[root@kvm network-scripts]# virsh --version
4.5.0
[root@kvm network-scripts]# virt-install --version
1.5.0
[root@kvm network-scripts]# ln -s /usr/libexec/qemu-kvm /usr/bin/qemu-kvm
[root@kvm network-scripts]# ll /usr/bin/qemu-kvm
lrwxrwxrwx 1 root root 21 5月  23 17:21 /usr/bin/qemu-kvm -> /usr/libexec/qemu-kvm
//查看桥接状态
[root@kvm network-scripts]# brctl show
bridge name	bridge id		STP enabled	interfaces
br0		8000.000c298cc6c0	no		ens33
virbr0		8000.525400f716d8	yes		virbr0-nic
//拉取web项目代码
[root@kvm network-scripts]# cd /usr/local/src/
[root@kvm src]# git clone git://github.com/retspen/webvirtmgr.git
正克隆到 'webvirtmgr'...
remote: Enumerating objects: 5614, done.
remote: Total 5614 (delta 0), reused 0 (delta 0), pack-reused 561
接收对象中: 100% (5614/5614), 2.97 MiB | 968.00 KiB/s, done.
处理 delta 中: 100% (3606/3606), done.
//安装项目
[root@kvm src]# cd webvirtmgr/
[root@kvm webvirtmgr]# pip install -r requirements.txt 
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)
//导入sqlite3
[root@kvm webvirtmgr]# python
Python 2.7.5 (default, Nov 16 2020, 22:23:17) 
[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()

//初始化账户
[root@kvm 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'): admin
Email address: 1@2.com
Password: 
Password (again): 
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 6 object(s) from 1 fixture(s)

//生成网页存放目录
[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/
//生成公钥
[root@kvm 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:y4fZP0uAt95N0lhcvrGWEvpzHQ3MdUZl4qee4fx/qjo root@kvm
The key's randomart image is:
+---[RSA 2048]----+
|              ..=|
|             . o+|
|             o.o+|
|         .   .+= |
|        S o  .=oo|
|       . * o.B.+*|
|        = +.+.B=o|
|         oE+o=ooo|
|          oo===.=|
+----[SHA256]-----+
//由于都在一台主机,复制到本地
[root@kvm webvirtmgr]# ssh-copy-id 192.168.94.137
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.94.137 (192.168.94.137)' can't be established.
ECDSA key fingerprint is SHA256:/dNOsfSJwlmEpd2e4FMRVsKNyHB2UfpEyZiHwJFcMpo.
ECDSA key fingerprint is MD5:2c:b9:e4:60:da:bd:71:a7:1e:48:1f:7e:82:e1:47:56.
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.94.137's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '192.168.94.137'"
and check to make sure that only the key(s) you wanted were added.
//开启地址转发
[root@kvm webvirtmgr]#  ssh 192.168.94.137 -L localhost:8000:localhost:8000 -L localhost:6080:localhost:60
Last login: Sun May 23 17:12:07 2021 from 192.168.94.1
[root@kvm ~]# 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:6010                *:*                  
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]:6010                 [::]:*                  
LISTEN     0      128     [::1]:6080                 [::]:*                  
LISTEN     0      128     [::1]:8000                 [::]:*      

nginx配置

//备份配置文件
[root@kvm ~]# cp   /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
//写入新配置
[root@kvm ~]# vim /etc/nginx/nginx.conf
[root@kvm ~]# 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 {
        }
    }
}
//web相关配置(没有则创建)
[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;
    }
}

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 from /etc/systemd/system/multi-user.target.wants/supervisord.service to /usr/lib/systemd/system/supervisord.service.
[root@kvm ~]# ss -antl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
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    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]:6010                  [::]:*                  
LISTEN     0      128      [::1]:6080                  [::]:*                  
LISTEN     0      128      [::1]:8000                  [::]:*  

nginx用户配置

[root@kvm ~]# 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:7U586bxMY3qlcqkWvxVrg6NxCFCdGjuWg3xKt8ESvFI nginx@kvm
The key's randomart image is:
+---[RSA 2048]----+
|     .  .. .     |
|      E.. o      |
|     o.= =       |
|    . *.%.       |
|     o *S=.   .  |
|      . .+.. o.o |
|          *oX+=  |
|         oo%*= . |
|         .==*o   |
+----[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.94.137
/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.94.137' (ECDSA) to the list of known hosts.
root@192.168.94.137's password: 

Number of key(s) added: 1

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

bash-4.2$ exit 
exit

[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 ~]# chown -R root.root /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
[root@kvm ~]# systemctl restart nginx
[root@kvm ~]# systemctl restart libvirtd

创建kvm存储池

//分区
[root@kvm ~]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。

更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。

Device does not contain a recognized partition table
使用磁盘标识符 0x1b6ba998 创建新的 DOS 磁盘标签。

命令(输入 m 获取帮助):n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): 
Using default response p
分区号 (1-4,默认 1):
起始 扇区 (2048-83886079,默认为 2048):
将使用默认值 2048
Last 扇区, +扇区 or +size{K,M,G} (2048-83886079,默认为 83886079):
将使用默认值 83886079
分区 1 已设置为 Linux 类型,大小设为 40 GiB

命令(输入 m 获取帮助):w
The partition table has been altered!

Calling ioctl() to re-read partition table.
正在同步磁盘。
[root@kvm ~]# partprobe 
Warning: 无法以读写方式打开 /dev/sr0 (只读文件系统)。/dev/sr0 已按照只读方式打开。
//格式化
[root@kvm ~]# mkfs.xfs /dev/sdb1
[root@kvm ~]# mkdir /kvmstorage
[root@kvm /]# blkid /dev/sdb1
/dev/sdb1: UUID="01e68c4c-bb45-4c41-8b76-ab3d25d651c9" TYPE="xfs" 
[root@kvm /]# vim /etc/fstab 
...
UUID=01e68c4c-bb45-4c41-8b76-ab3d25d651c9 /kvmstorage   xfs defaults    0 0

[root@kvm ~]# mount -a
[root@kvm ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
...
/dev/sdb1                 40G   33M   40G    1% /kvm-storage

访问网页too many open files解决办法

[root@kvm ~]# vim /etc/nginx/nginx.conf

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
worker_rlimit_nofile 655350;	#添加改行
[root@kvm ~]# systemctl restart nginx.service 

//修改系统参数
[root@kvm ~]# vim /etc/security/limits.conf 
# End of file
* soft nofile 655350	#添加以下两行
* hard nofile 655350
[root@kvm ~]# systemctl restart libvirtd

访问

  • 输入之前生成的超级用户名和密码

上传镜像到挂载目录

image-20210523193841177

image-20210523194035019

网络管理

实例创建

之后就跟VMware一样正常安装选项就行

posted on 2021-05-23 20:45  fxx013  阅读(301)  评论(0编辑  收藏  举报

导航