部署环境
系统类型 | ip | 网卡模式 | 主机名 |
---|---|---|---|
CentOS7 | 192.168.11.120 | nat | localhost |
CentOS7 | 192.168.11.122 | nat | localhost2 |
配置网络
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp ifcfg-ens33 ifcfg-br0
[root@localhost network-scripts]# vim ifcfg-br0
[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-br0
TYPE=Bridge
BOOTPROTO=static
NM_CONTROLLED=no
NAME=br0
DEVICE=br0
ONBOOT=yes
IPADDR=192.168.11.120
NETMASK=255.255.255.0
GATEWAY=192.168.11.2
DNS1=114.114.114.114
[root@localhost ~]# cat /etc/sysconfig/network-scripts/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 ~]# 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@localhost ~]# setenforce 0
[root@localhost ~]# vim /etc/selinux/config
SELINUX=disabled
[root@localhost ~]# reboot
安装工具
[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
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 ~]# systemctl enable --now 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 2021-06-20 09:22:13 EDT; 5s ago
Docs: man:libvirtd(8)
https://libvirt.org
Main PID: 2760 (libvirtd)
Tasks: 19 (limit: 32768)
CGroup: /system.slice/libvirtd.service
├─2760 /usr/sbin/libvirtd
├─2880 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshelper
└─2881 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshelper
验证安装结果
[root@localhost ~]# lsmod|grep kvm
kvm_intel 188644 0
kvm 621480 1 kvm_intel
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 Jun 20 09:44 /usr/bin/qemu-kvm -> /usr/libexec/qemu-kvm
查看网桥信息
[root@localhost ~]# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.000c297b512e no ens33
virbr0 8000.5254009b92d8 yes virbr0-nic
kvm web 页面安装
安装依赖包
[root@localhost2 ~]# yum -y install epel-release
[root@localhost ~]# yum -y install git python-pip libvirt-python libxml2-python python-websockify supervisor nginx python-devel
从github上下载webvirtmgr代码
[root@localhost2 ~]#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 | 610.00 KiB/s, done.
Resolving deltas: 100% (3606/3606), done.
安装webvirtmgr
[root@localhost2 ~]# ls anaconda-ks.cfg webvirtmgr
[root@localhost2 ~]# cd webvirtmgr/
[root@localhost2 webvirtmgr]# pip install -r requirements.txt
检查sqlite3是否安装
[root@localhost2 webvirtmgr]# python
Python 2.7.5 (default, Aug 7 2019, 00:51:29)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
>>> exit()
初始化账号信息
[root@localhost2 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'):
Email address:h210401@163.com
Password:
Password (again):
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 6 object(s) from 1 fixture(s)
将网站移动到指定目录并设置属组和属主
[root@localhost2 webvirtmgr]# mkdir -p /var/www
[root@localhost2 webvirtmgr]# cp -r /root/webvirtmgr/ /var/www/
[root@localhost2 webvirtmgr]# chown -R nginx.nginx /var/www/webvirtmgr/
做免密登录
[root@localhost2 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:EVPBojL6JBp8TXoZM24prkYxLzsP5NnMBk2/asVCYXg root@web
The key's randomart image is:
+---[RSA 2048]----+
| . ooo. |
| . E .o. |
| o.. ... |
| oo.+=. . |
|.o=o*+* S |
|++O*.X. |
|.*=O*. |
|.+ooo |
|..=o |
+----[SHA256]-----+
[root@localhost2 webvirtmgr]# ssh-copy-id root@192.168.11.120
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.11.120 (192.168.11.120)' can't be established.
ECDSA key fingerprint is SHA256:qScIS0D/MHXJczgQLJo7DCUkeuEpYBKCPV0DtR3rNCA.
ECDSA key fingerprint is MD5:cf:52:9c:f5:74:1f:54:25:52:a9:ce:50:47:33:16:ad.
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.11.120's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.11.120'"
and check to make sure that only the key(s) you wanted were added.
[root@localhost2 webvirtmgr]# ssh 192.168.11.120
Last login: Sun Jun 21 01:22:00 2021 from 192.168.11.1
配置nginx
[root@localhost2 ~]# cd /etc/nginx/
[root@localhost2 nginx]# mv nginx.conf nginx.conf-bak
[root@localhost2 nginx]# vim /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@localhost2 nginx]# 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://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@localhost2 nginx]# vi /var/www/webvirtmgr/conf/gunicorn.conf.py
bind = '0.0.0.0:8000'
backlog = 2048
修改supervisord.conf文件
[root@localhost2 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@localhost2 nginx]# systemctl enable --now supervisord
Created symlink from /etc/systemd/system/multi-user.target.wants/supervisord.service to /usr/lib/systemd/system/supervisord.service.
[root@localhost2 nginx]# systemctl restart nginx
[root@localhost2 nginx]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 100 *:6080 *:*
LISTEN 0 128 *:8000 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 [::1]:25 [::]:*
LISTEN 0 128 [::]:22
配置nginx账户,做免密登录
[root@localhost2 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:Dw5K4lBt2FFe1oWUli/lp5iWbHmUFfDNhQVSXU4Xm+Q nginx@web
The key's randomart image is:
+---[RSA 2048]----+
| ... oo.=+o+OO|
| + o o * .o==*|
| o + . . + o.E+|
| . . . = . |
|. . . . S. B o |
| o o . o oO o |
| . . .o.. |
| |
| |
+----[SHA256]-----+
-bash-4.2$ ssh-copy-id root@192.168.11.120
/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.11.120 (192.168.11.120)' can't be established.
ECDSA key fingerprint is SHA256:qScIS0D/MHXJczgQLJo7DCUkeuEpYBKCPV0DtR3rNCA.
ECDSA key fingerprint is MD5:cf:52:9c:f5:74:1f:54:25:52:a9:ce:50:47:33:16:ad.
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.11.120's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.11.120'"
and check to make sure that only the key(s) you wanted were added.
-bash-4.2$ exit
logout
[root@localhost2 nginx]# vi /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@localhost2 nginx]# systemctl restart nginx
[root@localhost ~]# systemctl restart libvirtd
关闭防火墙
[root@localhost2 nginx]# systemctl disable --now firewalld
[root@localhost2 symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
[root@localhost2 symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost2 nginx]# setenforce 0
[root@localhost2 nginx]# vim /etc/selinux/config
SELINUX=disabled
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 12M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/centos-root 38G 1.9G 36G 5% /
/dev/sda1 1014M 151M 864M 15% /boot
/dev/mapper/centos-home 19G 33M 19G 1% /home
tmpfs 378M 0 378M 0% /run/user/0
/dev/sr0 4.4G 4.4G 0 100% /mnt