kvm部署
环境说明:
系统类型 | IP |
---|---|
Centos 8 | 192.168.126.155 |
kvm安装
部署前请确保你的CPU虚拟化功能已开启。分为两种情况:
- 虚拟机要关机设置CPU虚拟化
- 物理机要在BIOS里开启CPU虚拟化
//关闭防火墙与SELINUX
[root@localhost ~]# systemctl disable --now firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# setenforce 0
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# reboot
//安装所需的工具
[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
svm
svm
svm
svm
//kvm安装
[root@localhost ~]# yum -y install qemu-kvm qemu-kvm-common qemu-img virt-manager libvirt python3-libvirt libvirt-client virt-install virt-viewer bridge-utils libguestfs-tools
做桥接网卡,用br桥接ens33
[root@localhost network-scripts]# ls
ifcfg-ens33
[root@localhost network-scripts]# cp ifcfg-ens33 ifcfg-br0
[root@localhost network-scripts]# cat ifcfg-br0
TYPE="Bridge"
BOOTPROTO="static"
NAME="br0"
DEVICE="br0"
ONBOOT="yes"
IPADDR=192.168.126.155
GATEWAY=192.168.126.2
NETMASK=255.255.255.0
DNS1=192.168.126.2
DNS2=114.114.114.114
[root@localhost network-scripts]# cat ifcfg-ens33
TYPE="Ethernet"
BOOTPROTO="static"
NAME="ens33"
DEVICE="ens33"
ONBOOT="yes"
BRIDGE="br0"
//重启网络
[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 fq_codel state UP group default qlen 1000
link/ether 00:0c:29:ff:f7:14 brd ff:ff:ff:ff:ff:ff
inet 192.168.126.155/24 brd 192.168.126.255 scope global dynamic noprefixroute ens33
valid_lft 1696sec preferred_lft 1696sec
inet6 fe80::9217:c560:45c6:19e/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
link/ether 52:54:00:dc:a2:ac 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
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc fq_codel master virbr0 state DOWN group default qlen 1000
link/ether 52:54:00:dc:a2:ac brd ff:ff:ff:ff:ff:ff
5: br0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
link/ether 9a:c3:71:7d:99:2a brd ff:ff:ff:ff:ff:ff
inet 192.168.126.155/24 brd 192.168.126.255 scope global noprefixroute br0
valid_lft forever preferred_lft forever
//启动服务
[root@localhost network-scripts]# systemctl start libvirtd
[root@localhost network-scripts]# systemctl enable libvirtd
[root@localhost network-scripts]# systemctl status libvirtd
● libvirtd.service - Virtualization daemon
Loaded: loaded (/usr/lib/systemd/system/libvirtd.service; enabled; vendor preset: ena>
Active: active (running) since Thu 2021-10-21 10:40:46 CST; 28s ago
Docs: man:libvirtd(8)
https://libvirt.org
Main PID: 60357 (libvirtd)
Tasks: 19 (limit: 32768)
Memory: 41.7M
CGroup: /system.slice/libvirtd.service
├─ 1680 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf ->
├─ 1683 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf ->
└─60357 /usr/sbin/libvirtd --timeout 120
10月 21 10:40:46 localhost.localdomain systemd[1]: Starting Virtualization daemon...
10月 21 10:40:46 localhost.localdomain systemd[1]: Started Virtualization daemon.
10月 21 10:40:46 localhost.localdomain dnsmasq[1680]: read /etc/hosts - 2 addresses
10月 21 10:40:46 localhost.localdomain dnsmasq[1680]: read /var/lib/libvirt/dnsmasq/defa>
10月 21 10:40:46 localhost.localdomain dnsmasq-dhcp[1680]: read /var/lib/libvirt/dnsmasq>
//验证安装结果
[root@localhost network-scripts]# lsmod | grep kvm
kvm_amd 110592 0
ccp 98304 1 kvm_amd
kvm 839680 1 kvm_amd
irqbypass 16384 1 kvm
//测试并验证安装结果
[root@localhost network-scripts]# virsh -c qemu:///system list
Id 名称 状态
-------------------
[root@localhost network-scripts]# virsh --version
6.0.0
[root@localhost network-scripts]# virt-install --version
2.2.1
[root@localhost network-scripts]# ln -s /usr/libexec/qemu-kvm /usr/bin/qemu-kvm
[root@localhost network-scripts]# ll /usr/bin/qemu-kvm
lrwxrwxrwx. 1 root root 21 10月 21 10:43 /usr/bin/qemu-kvm -> /usr/libexec/qemu-kvm
[root@localhost network-scripts]# lsmod |grep kvm
kvm_amd 110592 0
ccp 98304 1 kvm_amd
kvm 839680 1 kvm_amd
irqbypass 16384 1 kvm
查看网桥信息
[root@localhost network-scripts]# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.000000000000 no
virbr0 8000.525400dca2ac yes virbr0-nic
kvm web界面安装
环境说明:
系统类型 | IP |
---|---|
Centos 7 | 192.168.126.154 |
安装依赖包
[root@kvm ~]# [root@localhost ~]# dnf -y install git python2-pip python3-libvirt python3-libxml2 python3-websocket-client supervisor nginx python2-devel
从github上下载webvirtmgr代码
[root@localhost ~]# cd /usr/local/src/
[root@localhost 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 5614
接收对象中: 100% (5614/5614), 2.97 MiB | 826.00 KiB/s, done.
处理 delta 中: 100% (3606/3606), done.
//安装webvirtmgr
[root@localhost src]# cd webvirtmgr/
[root@localhost webvirtmgr]# ls
conf dev-requirements.txt interfaces networks serverlog templates
console hostdetail locale README.rst servers Vagrantfile
create images manage.py requirements.txt setup.py vrtManager
deploy instance MANIFEST.in secrets storages webvirtmgr
[root@localhost webvirtmgr]# pip2 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)
100% |████████████████████████████████| 8.1MB 147kB/s
Collecting gunicorn==19.5.0 (from -r requirements.txt (line 2))
Downloading https://files.pythonhosted.org/packages/f9/4e/f4076a1a57fc1e75edc0828db365cfa9005f9f6b4a51b489ae39a91eb4be/gunicorn-19.5.0-py2.py3-none-any.whl (113kB)
100% |████████████████████████████████| 122kB 11.0MB/s
Collecting lockfile>=0.9 (from -r requirements.txt (line 5))
Downloading https://files.pythonhosted.org/packages/c8/22/9460e311f340cb62d26a38c419b1381b8593b0bb6b5d1f056938b086d362/lockfile-0.12.2-py2.py3-none-any.whl
Installing collected packages: django, gunicorn, lockfile
Found existing installation: Django 1.11.27
Uninstalling Django-1.11.27:
Successfully uninstalled Django-1.11.27
Running setup.py install for django ... done
Successfully installed django-1.5.5 gunicorn-19.5.0 lockfile-0.12.2
You are using pip version 8.1.2, however version 21.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command
//检查sqlite3是否安装
[root@localhost 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@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'): admin
Email address: 512153770@qq.com
Password:
Password (again):
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 6 object(s) from 1 fixture(s)
//拷贝web网页至指定目录
[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):
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:9rHAQ5UTATnvoySkQqB0v4y7gGRnz176VY7zo+9jyAg root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
| .o++ |
|.. . o.o |
|o.. . .o . |
|. . ..o . |
| + ooo. S... |
|+ +.+E...+*o |
|.. ..o.+o=+o |
| ... o.ooo+ |
| ..o.. .=+o |
+----[SHA256]-----+
[root@localhost ~]# ssh-copy-id 192.168.126.155
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.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
root@192.168.126.155's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '192.168.126.155'"
and check to make sure that only the key(s) you wanted were added.
//配置端口转发
[root@localhost ~]# ssh 192.168.126.155 -L localhost:8000:localhost:8000 -L localhost:6080:localhost:60
Activate the web console with: systemctl enable --now cockpit.socket
Last login: Thu Oct 21 10:30:16 2021 from 192.168.126.1
//配置nginx
[root@localhost ~]# cd /etc/nginx/
[root@localhost nginx]# ls
conf.d fastcgi_params.default nginx.conf uwsgi_params.default
default.d koi-utf nginx.conf.default win-utf
fastcgi.conf koi-win scgi_params
fastcgi.conf.default mime.types scgi_params.default
fastcgi_params mime.types.default uwsgi_params
[root@localhost nginx]# > nginx.conf
[root@localhost nginx]# vi nginx.conf
[root@localhost nginx]# cat 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]# cd conf.d/
[root@localhost conf.d]# ls
[root@localhost conf.d]# vi /etc/nginx/conf.d/webvirtmgr.conf
[root@localhost conf.d]# 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;
}
}
//确保bind绑定的是本机的8000端口
[root@localhost conf]# vi /var/www/webvirtmgr/conf/gunicorn.conf.py
23 bind = '0.0.0.0:8000' //确保此处绑定的是本机的8000端口,这个在nginx配置中定义了,被代理的端口
//重启nginx
[root@localhost conf.d]# systemctl restart nginx
//文件末尾添加以下内容
[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
启动supervisor并设置开机自启
[root@localhost ~]# vim /etc/supervisord.conf
[root@localhost ~]# systemctl start supervisord
[root@localhost ~]# systemctl enable supervisord
Created symlink /etc/systemd/system/multi-user.target.wants/supervisord.service → /usr/lib/systemd/system/supervisord.service.
[root@localhost ~]# systemctl status supervisord
● supervisord.service - Process Monitoring and Control Daemon
Loaded: loaded (/usr/lib/systemd/system/supervisord.service; enabled; vendor preset: >
Active: active (running) since Thu 2021-10-21 11:56:08 CST; 19s ago
Main PID: 116794 (supervisord)
Tasks: 1 (limit: 23354)
Memory: 18.5M
CGroup: /system.slice/supervisord.service
└─116794 /usr/bin/python3.6 /usr/bin/supervisord -c /etc/supervisord.conf
10月 21 11:56:08 localhost.localdomain systemd[1]: Starting Process Monitoring and Contr>
10月 21 11:56:08 localhost.localdomain systemd[1]: Started Process Monitoring and
[root@localhost ~]# ss -anlt
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 32 192.168.122.1:53 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 5 127.0.0.1:631 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 5 [::1]:631 [::]:*
LISTEN 0 128 [::1]:6080 [::]:*
LISTEN 0 128 [::1]:8000 [::]:*
配置nginx用户
[root@localhost ~]# su - nginx -s /bin/bash
[nginx@localhost ~]$ 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:Lyfe2ITrIj8DZAkdHnOu4495ilHzEED8ZMRVqYQSd4A nginx@localhost.localdomain
The key's randomart image is:
+---[RSA 3072]----+
| o+*B=+... |
| E+**. . |
| *oo.. |
| =o. |
| o* S |
| o.= o |
| . ... + + |
| o.+=. X |
| . +=o== o |
+----[SHA256]-----
[nginx@localhost ~]$ touch ~/.ssh/config && echo -e "StrictHostKeyChecking=no\nUserKnownHostsFile=/dev/null" >> ~/.ssh/config
[nginx@localhost ~]$ cat ~/.ssh/config
StrictHostKeyChecking=no
UserKnownHostsFile=/dev/null
[nginx@localhost ~]$ chmod 0600 ~/.ssh/config
[nginx@localhost ~]$ ssh-copy-id root@192.168.126.155
/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.126.155' (ECDSA) to the list of known hosts.
root@192.168.126.155's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.126.155'"
and check to make sure that only the key(s) you wanted were added.
[root@localhost ~]# cat /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 ~]# systemctl restart nginx
[root@localhost ~]# systemctl restart libvirtd