目录
一.虚拟化介绍
虚拟化是一种将计算机资源进行抽象化的技术,可以将一台物理计算机划分为多个虚拟计算机,每个虚拟计算机都具有独立的操作系统和应用程序。通过虚拟化,可以将计算机的处理能力、存储空间和网络带宽等资源进行有效的利用和管理。
虚拟化技术有多种形式,包括服务器虚拟化、存储虚拟化和网络虚拟化等。其中,服务器虚拟化是最常见的形式,它可以将一台物理服务器划分为多个虚拟服务器,每个虚拟服务器都可以独立运行不同的操作系统和应用程序。这样可以提高服务器的利用率,减少硬件成本,并且方便管理和维护。
虚拟化技术的好处包括:
资源利用率提高:通过虚拟化,可以将计算机的资源进行合理的分配和利用,提高资源的利用率,减少资源的浪费。
灵活性增强:虚拟化可以将计算机的资源进行灵活的配置和管理,可以根据需求动态地分配资源,提高系统的灵活性和可扩展性。
管理简化:虚拟化可以将多个物理计算机集中管理,简化了系统的管理和维护工作,提高了管理效率。
高可用性:通过虚拟化,可以实现虚拟机的快速迁移和容错,提高系统的可用性和容灾能力。
虚拟化技术在云计算、数据中心和企业IT环境中得到广泛应用,为用户提供了更加灵活、可靠和高效的计算资源。
二.全虚拟化和半虚拟化
全虚拟化和半虚拟化是虚拟化技术的两种主要形式。
全虚拟化是指在虚拟机中运行的操作系统不需要进行任何修改,可以直接运行在虚拟化层之上。在全虚拟化中,虚拟机管理器(Hypervisor)负责将物理计算机的资源虚拟化,并提供给虚拟机使用。虚拟机管理器需要模拟硬件设备,如处理器、内存、硬盘和网络等,以便虚拟机可以在虚拟化环境中运行。全虚拟化提供了更高的隔离性和安全性,但由于需要模拟硬件设备,性能开销较大。
半虚拟化是指在虚拟机中运行的操作系统需要进行修改,以便与虚拟化层进行通信和协作。在半虚拟化中,虚拟机管理器与虚拟机之间可以直接通信,虚拟机可以主动向虚拟机管理器发出请求,并获取物理计算机的资源。由于虚拟机操作系统与虚拟化层的协作,半虚拟化可以减少虚拟化的性能开销,提高系统的性能和效率。然而,半虚拟化要求操作系统进行修改,因此不适用于所有操作系统。
总的来说,全虚拟化提供了更高的隔离性和安全性,适用于各种操作系统,但性能开销较大。而半虚拟化可以提供更好的性能和效率,但需要操作系统进行修改。选择全虚拟化还是半虚拟化取决于具体的应用场景和需求。
三.kvm介绍
KVM(Kernel-based Virtual Machine)是一种开源的虚拟化技术,它是在Linux内核中实现的一种全虚拟化解决方案。KVM利用虚拟化技术,可以在一台物理主机上同时运行多个虚拟机,每个虚拟机都可以运行不同的操作系统和应用程序,实现服务器资源的最大化利用和灵活性。
KVM的优点包括:
高性能:KVM利用Linux内核的虚拟化技术,避免了传统虚拟化技术中的性能瓶颈,虚拟机的性能接近于物理机。
安全性:KVM的虚拟机之间是完全隔离的,可以提供更高的安全性。
灵活性:KVM支持多种操作系统和应用程序,可以在同一台主机上运行不同的操作系统和应用程序。
可扩展性:KVM可以通过集群的方式进行扩展,提高服务器的可用性和容错性。
开源:KVM是一种开源技术,拥有庞大的社区支持和开发者参与,可以获得更多的技术支持和更新。
总之,KVM是一种高性能、安全、灵活和可扩展的虚拟化技术,适用于大规模的服务器虚拟化和云计算环境。
四.kvm部署准备工作
部署前请确保你的CPU虚拟化功能已开启。分为两种情况:
- 虚拟机要关机设置CPU虚拟化
- 物理机要在BIOS里开启CPU虚拟化
主机说明 | 主机名 | ip | 系统 |
kvm服务机 | c71.example.com | 192.168.187.150 | centos7 |
kvm web机 | c72.example.com | 192.168.187.160 | centos8 |
五.部署kvm
# 1.配置yum源,这里使用阿里云官方镜像源
# 备份原先的本地yum源
[root@c71 ~]# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
# 下载yum源
[root@c71 yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
# 下载epel源
[root@c71 yum.repos.d]# wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
# 重新加载缓存
[root@c71 yum.repos.d]# yum makecache
# 非阿里云ECS用户会出现 Couldn't resolve host 'mirrors.cloud.aliyuncs.com' 信息,不影响使用。用户也可自行修改相关配置:
[root@c71 yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
# 2.关闭防火墙与selinux
[root@c71 ~]# systemctl disable --now firewalld
[root@c71 ~]# setenforce 0
[root@c71 ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
# 3.验证CPU是否支持KVM;如果结果中有vmx(Intel)或svm(AMD)字样,就说明CPU的支持的
[root@c71 ~]# egrep -o 'vmx|svm' /proc/cpuinfo
vmx
vmx
vmx
vmx
# 4.kvm安装
[root@c71 ~]# 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
因为虚拟机中网络,我们一般都是和公司的其他服务器是同一个网段,所以我们需要把 \
KVM服务器的网卡配置成桥接模式。这样的话KVM的虚拟机就可以通过该桥接网卡和公司内部 \
其他服务器处于同一网段
此处我的网卡是ens33,所以用br0来桥接ens33网卡
[root@c71 ~]# cd /etc/sysconfig/network-scripts/
[root@c71 network-scripts]# ls
ifcfg-ens33 ifdown-isdn ifup ifup-plip ifup-tunnel
ifcfg-lo ifdown-post ifup-aliases ifup-plusb ifup-wireless
.....省略
[root@c71 network-scripts]# cp ifcfg-ens33 ifcfg-br0
[root@c71 network-scripts]# cat ifcfg-br0
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="static"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
NAME="ens33"
UUID="dbe2987c-4321-4daa-8553-a359499fda58"
DEVICE="ens33"
ONBOOT="yes"
IPADDR=192.168.187.150
NETMASK=255.255.255.0
GATEWAY=192.168.187.2
DNS1=8.8.8.8
DNS2=114.114.114.114
# 修改成这样
[root@c71 network-scripts]# cat ifcfg-br0
TYPE="Bridge"
NM_CONTROLLED="no"
BOOTPROTO="static"
DEFROUTE="yes"
NAME="br0"
DEVICE="br0"
ONBOOT="yes"
IPADDR=192.168.187.150
NETMASK=255.255.255.0
GATEWAY=192.168.187.2
DNS1=8.8.8.8
DNS2=114.114.114.114
[root@c71 network-scripts]# cat ifcfg-ens33
OTPROTO="static"
NAME="ens33"
DEVICE="ens33"
ONBOOT="yes"
BRIDGE="br0"
NM_CONTROLLED="no"
DNSn="114.114.114.114"
# 5.重启网卡
[root@c71 network-scripts]# systemctl restart network
[root@c71 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:ab:97:ea brd ff:ff:ff:ff:ff:ff
inet6 fe80::20c:29ff:feab:97ea/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:ab:97:ea brd ff:ff:ff:ff:ff:ff
inet 192.168.187.150/24 brd 192.168.187.255 scope global br0
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:feab:97ea/64 scope link
valid_lft forever preferred_lft forever
# 6.启动服务并设置开机自启
[root@c71 ~]# systemctl enable --now libvirtd
# 7.验证安装结果
[root@c71 ~]# lsmod|grep kvm
kvm_intel 174841 0
kvm 578518 1 kvm_intel
irqbypass 13503 1 kvm
# 8.测试并验证安装结果
[root@c71 ~]# lsmod|grep kvm
kvm_intel 174841 0
kvm 578518 1 kvm_intel
irqbypass 13503 1 kvm
[root@c71 ~]# virsh -c qemu:///system list
Id Name State
----------------------------------------------------
[root@c71 ~]# virsh --version
4.5.0
[root@c71 ~]# virt-install --version
1.5.0
[root@c71 ~]# ln -s /usr/libexec/qemu-kvm /usr/bin/qemu-kvm
[root@c71 ~]# ll /usr/bin/qemu-kvm
lrwxrwxrwx. 1 root root 21 Oct 11 03:02 /usr/bin/qemu-kvm -> /usr/libexec/qemu-kvm
[root@c71 ~]# lsmod |grep kvm
kvm_intel 174841 0
kvm 578518 1 kvm_intel
irqbypass 13503 1 kvm
[root@c71 ~]# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.000c29ab97ea no ens33
virbr0 8000.525400dd4828 yes virbr0-nic
六.部署kvm web管理界面
kvm 的 web 管理界面是由 webvirtmgr 程序提供的。
# 1.安装依赖包
[root@c72 ~]# yum -y install git python2-pip python3-pip libvirt-python libxml2-python python-websockify supervisor nginx python-devel
# 2.从github上下载webvirtmgr的zip压缩包
https://github.com/retspen/webvirtmgr
# 3.将压缩包导入
[root@c72 ~]# cd /usr/local/src
[root@c72 src]# ls
webvirtmgr-master.zip
# 4.解压缩,没有unzip直接yum装一个
[root@c72 src]# unzip webvirtmgr-master.zip
[root@c72 src]# ls
webvirtmgr-master webvirtmgr-master.zip
# 5.安装webvirtmgr
# 这里需要注意的是用pip2安装不了,提示版本过低,得用pip3
# 但是下面初始化账号的时候,python3的命令用不了
# 所以我前面将python2/3-pip都装了
[root@c72 webvirtmgr-master]# pip3 install -r requirements.txt
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 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 265kB/s
......省略
Successfully installed django-1.5.5 gunicorn-19.5.0 lockfile-0.12.2
# 6.检查sqlite3是否安装
[root@c72 webvirtmgr-master]# python
Python 2.7.5 (default, Jun 20 2023, 11:36:40)
[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@c72 webvirtmgr-master]#
# 7.初始化帐号信息
[root@c72 webvirtmgr-master]# 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 ---- 输入yes
Username (leave blank to use 'root'): admin ---- 设定web端超级管理员账号名称
Email address: 2664218545@qq.com ---- 设定超级管理员邮箱
Password: -------- 设定密码
Password (again): -------- 再次输入密码
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 6 object(s) from 1 fixture(s)
# 8.拷贝web网页至指定目录
[root@c72 webvirtmgr-master]# mkdir /var/www
[root@c72 webvirtmgr-master]# cp -r /usr/local/src/webvirtmgr-master /var/www/
[root@c72 webvirtmgr-master]# chown -R nginx.nginx /var/www/webvirtmgr-master/
# 9.生成密钥
[root@c72 ~]# 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:o5/YwKF46hLYKokjmaM+GIAVubYpf31e713L5PSmMtw root@c71
The key's randomart image is:
+---[RSA 2048]----+
| .o |
| o |
|.. . |
|o o |
|oo o . S |
|= +. o o . |
|oX. o.+ .. . o.|
|&..o. .=.o .+.E.+|
|**+. .o= .ooo*.|
+----[SHA256]-----+
# 10.将公钥发送给kvm服务端
[root@c72 ~]# ssh-copy-id root@192.168.187.150
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.187.150 (192.168.187.150)' can't be established.
ECDSA key fingerprint is SHA256:xSPn7EEGoK3LPc4knUoZIawIVAK8O6irC2h8Ctqxp0k.
ECDSA key fingerprint is MD5:34:1e:c8:0e:55:5a:3d:37:22:26:40:d0:d3:a6:2f:b9.
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.187.160's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.187.150'"
and check to make sure that only the key(s) you wanted were added.
# 11.kvm web端上远程连接kvm服务端配置端口转发
[root@c72 ~]# ssh 192.168.187.150 -L localhost:8000:localhost:8000 -L localhost:6080:localhost:60
Last login: Wed Oct 11 02:44:46 2023 from 192.168.187.1
# 查看端口
# 如果端口暂时没有出来,等待一会先继续操作就行
[root@c72 webvirtmgr]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:8000 *:*
LISTEN 0 100 *:6080 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
# 12.配置nginx
# 直接将文件中的内容替换为下面的内容
[root@c72 ~]# 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@c72 ~]# 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@c72 ~]# vim /var/www/webvirtmgr/conf/gunicorn.conf.py
.....此处省略N行
bind = '0.0.0.0:8000' -- 确保此处绑定的是本机的8000端口,这个在nginx配置中定义了,被代理的端口
backlog = 2048
.....此处省略N行
# 重启nginx
[root@c72 ~]# systemctl restart nginx
# 13.设置supervisor
[root@c72 ~]# 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@c72 ~]# systemctl enable --now supervisor
[root@c72 webvirtmgr]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:8000 *:*
LISTEN 0 100 *:6080 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
# 14.进入nginx用户
[root@c72 ~]# 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:koXs18v3dnqxUkUo+1x0Ictn9q8kpczOW8PQxDnHoM8 nginx@c72
The key's randomart image is:
+---[RSA 2048]----+
| ..o.|
| . . oo+++|
| o . .==*+|
| . o . .=+o+|
| + S . .oE.o|
| o . + =oo.|
| o * * +|
| + *.+o|
| +o=+ |
+----[SHA256]-----+
-bash-4.2$ touch ~/.ssh/config && echo -e "StrictHostKeyChecking=no\nUserKnownHostsFile=/dev/null" >> ~/.ssh/config
-bash-4.2$ chmod 0600 ~/.ssh/config
# 将公钥发送到kvm web端超级管理员用户所在的~/.ssh/authorized_keys文件中
-bash-4.2$ ssh-copy-id root@192.168.187.160
/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.187.160' (ECDSA) to the list of known hosts.
root@192.168.187.160's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.187.160'"
and check to make sure that only the key(s) you wanted were added.
-bash-4.2$ exit
logout
# 这时nginx用户就没了,切换不进去了
[root@c72 ~]# su - nginx
Last login: Wed Oct 11 04:24:26 EDT 2023 on pts/0
This account is currently not available.
# 15.配置文件
[root@c72 ~]# 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
# 16.设置属主属组,重启kvm服务端libvirtd服务,重启kvm web端nginx服务
[root@c72 ~]# chown -R root.root /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
[root@c72 ~]# systemctl restart nginx
[root@c71 ~]# systemctl restart libvirtd
# 这时可以用ip访问kvm web管理页面了
七.kvm web界面管理
kvm连接管理
这是主页
创建ssh连接
点击右上角的绿色添加按钮
kvm存储管理
通过远程连接软件上传ISO镜像文件至存储目录/var/lib/libvirt/images/
[root@c71 ~]# cd /var/lib/libvirt/images/
[root@c71 images]# ls
CentOS-Stream-8-20230626.1-x86_64-dvd1.iso
在 web 界面查看ISO镜像是否存在
创建系统安装镜像
点击添加镜像
添加成功如下图所示
kvm网络管理
实例管理
虚拟机插入光盘
设置密码
启动虚拟机
跟正常安装虚拟机一样安装
最后效果图
能够连接shell,ping通百度