KVM
环境说明:
角色 | IP | 应用和系统版本 | 备注 |
---|---|---|---|
kvm-web | 192.168.100.3 | uos8 | kvm-web页面 |
kvm | 192.168.100.4,192.168.100.11(桥接) | uos8 | 虚拟化 |
均关闭了防火墙和selinux
服务器端
前期准备
//查看kvm主机是否支持虚拟化,(确认支持)
[root@kvm ~]# egrep -o 'vmx|svm' /proc/cpuinfo
svm
svm
svm
svm
svm
svm
svm
svm
[root@kvm ~]#
//安装依赖包
[root@kvm ~]# yum -y install qemu-kvm qemu-img virt-manager libvirt libvirt-client virt-install virt-viewer bridge-utils libguestfs-tools
网卡
//配置桥接网卡
[root@kvm ~]# cd /etc/sysconfig/network-scripts/
[root@kvm network-scripts]# ls
ifcfg-ens33
[root@kvm network-scripts]# cp ifcfg-ens33 ifcfg-br0
[root@kvm network-scripts]# vim ifcfg-br0
[root@kvm network-scripts]# cat ifcfg-br0
TYPE=bridge
BOOTPROTO=static
NAME=br0
DEVICE=br0
ONBOOT=yes
IPADDR=192.168.100.11
NETMASK=255.255.255.0
GATEWAY=192.168.100.254
DNS1=114.114.114.114
//在ens33里面添加桥接网卡 BRIDGE=br0
在网卡里面添加桥接网卡
[root@kvm network-scripts]# vim ifcfg-ens33
[root@kvm network-scripts]# cat ifcfg-ens33
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=default
NAME=ens33
UUID=fa5edff2-d705-439d-a334-4c8d1050beb9
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.100.4
PREFIX=24
GATEWAY=192.168.100.254
DNS1=192.168.100.254
BRIDGE=br0
[root@kvm network-scripts]#
//重启网卡
[root@kvm network-scripts]# 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 fq_codel state UP group default qlen 1000
link/ether 00:0c:29:89:6b:97 brd ff:ff:ff:ff:ff:ff
inet 192.168.100.4/24 brd 192.168.100.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet6 fe80::f089:4fbb:4bd8:10a3/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: br0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
link/ether da:6c:c1:ad:bc:9f brd ff:ff:ff:ff:ff:ff
inet 192.168.100.11/24 brd 192.168.100.255 scope global noprefixroute br0
valid_lft forever preferred_lft forever
//禁用网卡和启动网卡
[root@kvm network-scripts]# yum -y install NetworkManager-initscripts-updown
[root@kvm network-scripts]# ifdown ens33;ifup ens33
//xshell 重新连接,禁用重启网卡后ens33的ip就相当于没了,xshell就自动断开了
用桥接网卡br0的ip连接 192.168.100.11
[root@kvm ~]# 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:89:6b:97 brd ff:ff:ff:ff:ff:ff
3: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 00:0c:29:89:6b:97 brd ff:ff:ff:ff:ff:ff
inet 192.168.100.11/24 brd 192.168.100.255 scope global noprefixroute br0
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe89:6b97/64 scope link
valid_lft forever preferred_lft forever
[root@kvm ~]#
//启动libvirt
[root@kvm ~]# systemctl enable --now libvirtd
Created symlink /etc/systemd/system/multi-user.target.wants/libvirtd.service → /usr/lib/systemd/system/libvirtd.service.
Created symlink /etc/systemd/system/sockets.target.wants/virtlockd.socket → /usr/lib/systemd/system/virtlockd.socket.
Created symlink /etc/systemd/system/sockets.target.wants/libvirtd.socket → /usr/lib/systemd/system/libvirtd.socket.
Created symlink /etc/systemd/system/sockets.target.wants/libvirtd-ro.socket → /usr/lib/systemd/system/libvirtd-ro.socket.
[root@kvm ~]#
验证
[root@kvm ~]# lsmod|grep kvm
kvm_amd 126976 0
ccp 69632 1 kvm_amd
kvm 774144 1 kvm_amd
irqbypass 20480 1 kvm
//测试并验证安装结果
[root@kvm ~]# virsh -c qemu:///system list
Id 名称 状态
-------------------
[root@kvm ~]# virsh version
根据库编译:libvirt 8.0.0
使用库:libvirt 8.0.0
使用的 API: QEMU 8.0.0
运行管理程序: QEMU 6.2.0
//查看版本
[root@kvm ~]# virt-install --version
3.2.0
//创建软连接使/usr/libexec/qemu-kvm命令可以使用
[root@kvm ~]# ln -s /usr/libexec/qemu-kvm /usr/bin/
[root@kvm ~]# which qemu-kvm
/usr/bin/qemu-kvm
//查看网桥信息
[root@kvm ~]# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.000c29896b97 no ens33
virbr0 8000.5254004799e8 yes
[root@kvm ~]#
web管理端
包
//下载路径,需要下载
可能在虚拟机里面下载不了
可以在浏览器下载在使用xftp放到/root/下
libvirt-python
wget http://mirror.centos.org/centos/7/os/x86_64/Packages/libvirt-python-4.5.0-
1.el7.x86_64.rpm
libxml2-python
wget http://mirror.centos.org/centos/7/os/x86_64/Packages/libxml2-python-2.9.1-
6.el7.5.x86_64.rpm
webvirtmgr
python-websockify
wget https://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/p/pythonwebsockify-0.6.0-5.el7.noarch.rpm
python-websockify
wget https://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/p/pythonwebsockify-0.6.0-5.el7.noarch.rpm
[root@kvm-web ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg Pictures
Desktop libvirt-python-4.5.0-1.el7.x86_64.rpm python-websockify-0.6.0-5.el7.noarch.rpm
Documents libxml2-python-2.9.1-6.el7.5.x86_64.rpm Videos
Downloads Music
[root@kvm-web ~]#
//安装包
[root@kvm-web ~]# yum -y install libvirt-python-4.5.0-1.el7.x86_64.rpm
[root@kvm-web ~]# rpm -ivh --nodeps libxml2-python-2.9.1-6.el7.5.x86_64.rpm
[root@kvm-web ~]# rpm -ivh --nodeps python-websockify-0.6.0-5.el7.noarch.rpm
[root@kvm-web ~]# yum -y install git python2-pip supervisor nginx python2-devel libvirt
//安装webvirtmgr
//xftp导入webvirtmgr-master.zip到/usr/local/src/
[root@kvm-web src]# ls
webvirtmgr-master.zip
[root@kvm-web src]# unzip webvirtmgr-master.zip
Archive: webvirtmgr-master.zip
[root@kvm-web src]# mv webvirtmgr-master webvirtmgr
[root@kvm-web src]# ls
webvirtmgr webvirtmgr-master.zip
[root@kvm-web src]#
[root@kvm-web src]# cd webvirtmgr/
[root@kvm-web webvirtmgr]# pip2 install -r requirements.txt
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip2 install --user` instead.
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 49kB/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 82kB/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
Running setup.py install for django ... done
Successfully installed django-1.5.5 gunicorn-19.5.0 lockfile-0.12.2
[root@kvm-web webvirtmgr-master]#
//检查sqlite3是否安装
[root@kvm-web webvirtmgr]# python2
Python 2.7.18 (default, Apr 12 2023, 18:54:18)
[GCC 8.5.0 20210514 (UnionTech 8.5.0-10.0.3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
>>> exit()
账号
//生成超级管理员
账号admin
密码admin
邮箱随便
[root@kvm-web 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'): 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-web webvirtmgr-master]#
//拷贝网页到指定目录
[root@kvm-web ~]# cp -a /usr/local/src/webvirtmgr /var/www/
[root@kvm-web ~]# chown -R nginx.nginx /var/www/webvirtmgr/
[root@kvm-web ~]# ll /var/www/
总用量 164
drwxr-xr-x 19 nginx nginx 4096 4月 15 09:54 webvirtmgr
[root@kvm-web ~]#
免密
// 配置免密登录
如果有多台kvm服务端,那就要免密多台
[root@kvm-web ~]# 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:FmUK5BGFczYlR0z9sjG6dJI4Cxl4TssULrLhTjOxPKw root@kvm-web
The key's randomart image is:
+---[RSA 3072]----+
| .=+o=B. |
| .+o+*. . |
| o.=o. . |
| + o * . + . |
| + * B +S. o = |
| @ *.o = o |
| + + . + + |
|E . . . |
| |
+----[SHA256]-----+
[root@kvm-web ~]# ssh-copy-id root@192.168.100.11
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.100.11 (192.168.100.11)' can't be established.
ECDSA key fingerprint is SHA256:mw7UCJ/FZpZ+DPm6aX03XcaaRsh/uI4DZy/SeWQxSRE.
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
UOS Server 20 1060a
root@192.168.100.11's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.100.11'"
and check to make sure that only the key(s) you wanted were added.
[root@kvm-web ~]#
// 端口转发
如果有多台kvm服务端,那就要配置多个端口转发
[root@kvm-web ~]# ssh 192.168.100.11 -L localhost:8000:localhost:8000 -L localhost:6080:localhost:60
UOS Server 20 1060a
Welcome to UOS Server 20
Activate the web console with: systemctl enable --now cockpit.socket
Upgradable packages: 283
Upgrade command line: yum upgrade
Last login: Mon Apr 15 09:29:19 2024 from 192.168.100.1
Welcome to 4.19.0-91.82.152.uelc20.x86_64
[root@kvm ~]#
配置nginx
[root@kvm ~]# cp /etc/nginx/nginx.conf /tmp/
[root@kvm ~]# 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@kvm ~]# vim /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@kvm ~]# vim /var/www/webvirtmgr/conf/gunicorn.conf.py
bind = '0.0.0.0:8000'
backlog = 2048
//启动nginx
[root@kvm-web ~]# systemctl enable --now nginx
设置superisor
[root@kvm-web ~]# 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-web ~]# systemctl enable --now supervisord
Created symlink /etc/systemd/system/multi-user.target.wants/supervisord.service → /usr/lib/systemd/system/supervisord.service.
[root@kvm-web ~]#
配置nginx用户
[root@kvm-web ~]# su - nginx -s /bin/bash
Welcome to 4.19.0-91.82.152.uelc20.x86_64
[nginx@kvm-web ~]$ 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:3Axwn5A/tP0Xi/tchHl0LiriQB2Jh+Z//shHrM1zdRY nginx@kvm-web
The key's randomart image is:
+---[RSA 3072]----+
| . o. |
| =.+.. |
| + =ooo o|
| o + =+ . E.|
| o S oo .= B|
| . . oo.==|
| . o o=. .+o|
| o =.o=.o .|
| . +o.o.o |
+----[SHA256]-----+
[nginx@kvm-web ~]$ touch ~/.ssh/config && echo -e "StrictHostKeyChecking=no\nUserKnownHostsFile=/dev/null" >> ~/.ssh/config
[nginx@kvm-web ~]$ cat ~/.ssh/config
StrictHostKeyChecking=no
UserKnownHostsFile=/dev/null
[nginx@kvm-web ~]$ chmod 0600 ~/.ssh/config
// 有几个kvm服务端就免密几个
[nginx@kvm-web ~]$ ssh-copy-id root@192.168.100.11
/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.100.11' (ECDSA) to the list of known hosts.
UOS Server 20 1060a
root@192.168.100.11's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.100.11'"
and check to make sure that only the key(s) you wanted were added.
[nginx@kvm-web ~]$ ssh root@192.168.100.11
Warning: Permanently added '192.168.100.11' (ECDSA) to the list of known hosts.
UOS Server 20 1060a
Welcome to UOS Server 20
Activate the web console with: systemctl enable --now cockpit.socket
Upgradable packages: 283
Upgrade command line: yum upgrade
Last failed login: Mon Apr 15 11:01:26 CST 2024 from 192.168.100.3 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Mon Apr 15 10:32:56 2024 from 192.168.100.3
Welcome to 4.19.0-91.82.152.uelc20.x86_64
[root@kvm ~]# exit
注销
Connection to 192.168.100.11 closed.
[nginx@kvm-web ~]$ exit
注销
[root@kvm-web ~]#
配置
[root@kvm-web ~]# vim /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
[root@kvm-web ~]# chown -R root.root /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
[root@kvm-web ~]# 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@kvm-web ~]# systemctl restart nginx.service
[root@kvm-web ~]# systemctl restart libvirtd
验证
//如果没有6080,8080建议重启
[root@kvm-web ~]# 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 511 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 2048 0.0.0.0:8000 0.0.0.0:*
LISTEN 0 100 0.0.0.0:6080 0.0.0.0:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 5 [::1]:631 [::]:*
[root@kvm-web ~]#
界面管理
登录
创建ssh连接
创建存储
添加镜像
[root@kvm ~]# cd /var/lib/libvirt/images/
[root@kvm images]# ls
uos8.img
[root@kvm images]# ls
uos8.img uos-server-20-1060a-amd64.iso
[root@kvm images]#
添加网络
创建实例
设置虚拟机密码
虚拟机插入光盘
启动
//已经成功获取到ip