KVM虚拟化
虚拟化介绍
虚拟化:在一台计算机上虚拟出多个逻辑的计算机,而且每个逻辑计算机
它可以是不同操作系统
虚拟化技术:可以扩大硬件容量,单个cpu模拟出多个cpu并行,
允许一个平台上同时运行多个操作系统,应用程序都可以在相互独立
的空间内运行,而且互不影响。
kvm介绍
kVM 全称是 Kernel-Based Virtual Machine。也就是说 KVM 是基于 Linux 内核实现的。
KVM有一个内核模块叫 kvm.ko,只用于管理虚拟 CPU 和内存。
那 IO 的虚拟化,比如存储和网络设备则是由 Linux 内核与Qemu来实现。
Qemu 是纯软件实现的虚拟化模拟器,几乎可以模拟任何硬件设备,我们最熟悉的就是能够模拟一台能够独立运行操作系统的虚拟机,虚拟机认为自己和硬件打交道,但其实是和 Qemu 模拟出来的硬件打交道,Qemu 将这些指令转译给真正的硬件。
正因为 Qemu 是纯软件实现的,所有的指令都要经 Qemu 过一手,性能非常低,所以,在生产环境中,大多数的做法都是配合 KVM 来完成虚拟化工作,因为 KVM 是硬件辅助的虚拟化技术,主要负责 比较繁琐的 CPU 和内存虚拟化,而 Qemu 则负责 I/O 虚拟化,两者合作各自发挥自身的优势,相得益彰.
KVM部署
虚拟机设置
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-I24xkGUz-1665149273587)(C:\Users\superman\AppData\Roaming\Typora\typora-user-images\image-20221003153102828.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Lm0ESGTl-1665149273589)(C:\Users\superman\AppData\Roaming\Typora\typora-user-images\image-20221003153150581.png)]
//关闭防火墙和selinux
[root@loaclhost ~]# systemctl stop firewalld
[root@loaclhost ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@loaclhost ~]# setenforce 0
[root@loaclhost ~]# vim /etc/selinux/config
SELINUX=disabled
//新建分区,将硬盘所有大小都给这个分区
[root@loaclhost ~]# parted /dev/sdb
GNU Parted 3.1
使用 /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel
新的磁盘标签类型? msdos
(parted) unit
Unit? [compact]? MIB
(parted) p
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 204800MiB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system 标志
(parted) mkpart
分区类型? primary/主分区/extended/扩展分区? primary
文件系统类型? [ext2]? xfs
起始点? 10MIB
结束点? 204790MIB
(parted) p
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 204800MiB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system 标志
1 10.0MiB 204790MiB 204780MiB primary
(parted) q
信息: You may need to update /etc/fstab.
[root@loaclhost ~]# udevadm settle
//格式化
[root@loaclhost ~]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1 isize=512 agcount=4, agsize=13105920 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=52423680, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=25597, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@loaclhost ~]# blkid /dev/sdb1
/dev/sdb1: UUID="f9e6d60a-38f0-4048-966b-3c6466369e94" TYPE="xfs"
//挂载
[root@loaclhost ~]# mkdir /kvmdata
[root@loaclhost ~]# vim /etc/fstab
UUID=a74cbc28-c106-49b8-bf47-d02ed3267bda /kvmdata xfs defaults 0 0
[root@loaclhost ~]# mount -a
[root@loaclhost ~]# df -Th
文件系统 类型 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root xfs 50G 1.3G 49G 3% /
devtmpfs devtmpfs 3.9G 0 3.9G 0% /dev
tmpfs tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs tmpfs 3.9G 12M 3.9G 1% /run
tmpfs tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/mapper/centos-home xfs 42G 33M 42G 1% /home
/dev/sda1 xfs 1014M 130M 885M 13% /boot
tmpfs tmpfs 797M 0 797M 0% /run/user/0
/dev/sdb1 xfs 200G 33M 200G 1% /kvmdata
//部署yum源
[root@loaclhost ~]# cd /etc/yum.repos.d/
[root@loaclhost yum.repos.d]# ls
CentOS-Base.repo CentOS-fasttrack.repo CentOS-Vault.repo
CentOS-CR.repo CentOS-Media.repo
CentOS-Debuginfo.repo CentOS-Sources.repo
[root@loaclhost yum.repos.d]# rm -rf *
[root@loaclhost yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 100 2523 100 2523 0 0 11790 0 --:--:-- --:--:-- --:--:-- 11845
//安装所需软件包
[root@loaclhost ~]# yum -y install epel-release vim wget net-tools unzip zip gcc gcc-c++
//验证cpu是否支持kvm,vmx是intel的 svm是AMD的
[root@loaclhost ~]# egrep -o 'vmx|svm' /proc/cpuinfo
vmx
//安装kvm
[root@loaclhost ~]# 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的网卡配置成桥接模式
[root@loaclhost ~]# cd /etc/sysconfig/network-scripts/
[root@loaclhost network-scripts]# cp ifcfg-ens33 ifcfg-br0
[root@loaclhost network-scripts]# cat ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=none
NAME=ens33
DEVICE=ens33
ONBOOT=yes
BRIDGE=br0
[root@loaclhost network-scripts]# vim ifcfg-br0
TYPE=Bridge
BOOTPROTO=none
NAME=br0
DEVICE=br0
ONBOOT=yes
IPADDR=192.168.205.160
PREFIX=24
GATEWAY=192.168.205.2
DNS1=8.8.8.8
//重启网卡服务
[root@loaclhost network-scripts]# systemctl restart network
[root@loaclhost network-scripts]# systemctl restart NetworkManager
[root@loaclhost network-scripts]# ifdown ens33
[root@loaclhost network-scripts]# ifup ens33
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-J73RLiVx-1665149273589)(C:\Users\superman\AppData\Roaming\Typora\typora-user-images\image-20221003170933731.png)]
//重启libvirtd服务,并设置下次启动生效
[root@loaclhost ~]# systemctl restart libvirtd
[root@loaclhost ~]# systemctl enable libvirtd
//查看kvm模块是否加载
[root@loaclhost ~]# lsmod | grep kvm
kvm_intel 174841 0
kvm 578518 1 kvm_intel
irqbypass 13503 1 kvm
//验证安装结果
[root@loaclhost ~]# virsh -c qemu:///system list
Id 名称 状态
----------------------------------------------------
[root@loaclhost ~]# virsh --version
4.5.0
[root@loaclhost ~]# virt-install --version
1.5.0
//将qemu-kvm这个命令做一个软链接
[root@loaclhost ~]# ln -s /usr/libexec/qemu-kvm /usr/bin/qemu-kvm
[root@loaclhost ~]# ll /usr/bin/qemu-kvm
lrwxrwxrwx. 1 root root 21 10月 3 05:14 /usr/bin/qemu-kvm -> /usr/libexec/qemu-kvm
//查看网桥信息
[root@loaclhost ~]# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.000c29437d30 no ens33
virbr0 8000.525400bb72cd yes virbr0-nic
KVM管理界面安装
Kvm的web界面是由webvirtmgr程序提供的
//安装依赖包
[root@loaclhost ~]# yum -y install git python-pip libvirt-python libxml2-python python-websockify supervisor nginx python-devel
//从github上下载webvirtmgr代码
[root@loaclhost ~]# cd /usr/local/src/
[root@localhost src]# git clone http://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 | 459.00 KiB/s, done.
处理 delta 中: 100% (3606/3606), done.
[root@localhost src]# cd webvirtmgr/
[root@localhost webvirtmgr]# pip install -r requirements.txt
//安装webvirtmgr
[root@localhost src]# cd webvirtmgr/
[root@localhost webvirtmgr]# pip --default-timeout=1000 install -r requirements.txt
//检查sqlite3是否安装
[root@localhost ~]# python
Python 2.7.5 (default, Jun 28 2022, 15:30:04)
[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 ~]# cd /usr/local/src/webvirtmgr/
[root@localhost webvirtmgr]# python manage.py syncdb
//拷贝web网页到指定目录
[root@localhost webvirtmgr]# mkdir /var/www
[root@localhost webvirtmgr]# cp -r /usr/local/src/webvirtmgr/ /var/www/
[root@localhost webvirtmgr]# chown -R nginx.nginx /var/www/webvirtmgr/
//生成一对公钥与私钥,由于这里webvirtmgr和kvm服务部署在同一台主机中,所以这里本地信任。如果kvm部署在其他机器上的时候,那么就需要把公钥发送到kvm主机中
[root@localhost webvirtmgr]# ssh-keygen
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:ejniZRaY3qdCNQPG42IRS78Rk0BU3wVEXp1QgrAtQ7I root@localhost.example.com
The key's randomart image is:
+---[RSA 2048]----+
| o*==.o++o=+.. |
| ..o*B =.o .o |
| .+Eo= + |
| o .=+o |
| . .+.So |
| ..o o |
| .+ O . |
| ..* + |
| ... |
+----[SHA256]-----+
[root@localhost webvirtmgr]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.205.160
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.205.160 (192.168.205.160)' can't be established.
ECDSA key fingerprint is SHA256:5amwsa82XgGUK+f2GeERriJ0n/E3JsBZKaSxxhXfk78.
ECDSA key fingerprint is MD5:04:2a:9a:de:3a:3b:ad:66:ec:c5:04:c5:4b:22:f2:4a.
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.205.160's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.205.160'"
and check to make sure that only the key(s) you wanted were added.
//配置端口转发
[root@localhost webvirtmgr]# ssh 192.168.205.160 -L localhost:8000:localhost:8000 -L localhost:6080:localhost:60
[root@localhost webvirtmgr]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.1:6080 *:*
LISTEN 0 128 127.0.0.1:8000 *:*
LISTEN 0 128 *:111 *:*
LISTEN 0 5 192.168.122.1:53 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 127.0.0.1:631 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 ::1:6080 :::*
LISTEN 0 128 ::1:8000 :::*
LISTEN 0 128 :::111 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 128 ::1:631 :::*
LISTEN 0 100 ::1:25 :::*
//配置nginx
[root@localhost ~]# cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
[root@localhost ~]# vim /etc/nginx/nginx.conf
//在server参数中进行修改
删除listen [::]:80;行
参数server_name行改成server_name localhost;
删除root /usr/share/nginx/html;行
在include /etc/nginx/default.d/*.conf;行下添加
location / {
root html;
index index.html index.htm;
}
//配置nginx虚拟主机
[root@localhost ~]# cd /etc/nginx/conf.d/
[root@localhost conf.d]# ll
总用量 0
[root@localhost conf.d]# vim 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 ~]# vim /var/www/webvirtmgr/conf/gunicorn.conf.py
bind = '127.0.0.1:8000'
backlog = 2048
//重启nginx服务,查看端口是否开启
[root@localhost ~]# systemctl restart nginx
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.1:6080 *:*
LISTEN 0 128 127.0.0.1:8000 *:*
LISTEN 0 128 *:111 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 5 192.168.122.1:53 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 127.0.0.1:631 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 ::1:6080 :::*
LISTEN 0 128 ::1:8000 :::*
LISTEN 0 128 :::111 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 128 ::1:631 :::*
LISTEN 0 100 ::1:25 :::*
//设置supervisor
[root@localhost ~]# vim /etc/supervisord.conf
#在文件最后添加如下信息
[program:webvirtmgr]
#这里command是一行
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 ~]# systemctl restart supervisord.service
[root@localhost ~]# systemctl enable supervisord.service
Created symlink from /etc/systemd/system/multi-user.target.wants/supervisord.service to /usr/lib/systemd/system/supervisord.service.
//配置nginx用户
[root@localhost ~]# su - nginx -s /bin/bash
-bash-4.2$ ssh-keygen
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:/gJAbWkv4NuoZcXxPaphumRsUPTkL/g5ZLG5yaVRYt8 nginx@localhost.example.com
The key's randomart image is:
+---[RSA 2048]----+
| .... |
| .o+* |
| o.=*+.. |
| .oo+Oo.o |
| . .*B.S.E. |
| o===X. |
| +=oO+. |
| .+. .... |
| .. .. |
+----[SHA256]-----+
-bash-4.2$ touch ~/.ssh/config
-bash-4.2$ echo -e "StrictHostKeyChecking=no\nUserKnownHostsFile=/dev/null" >> ~/.ssh/config
-bash-4.2$ chmod 0600 ~/.ssh/config
-bash-4.2$ ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.205.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.205.160' (ECDSA) to the list of known hosts.
root@192.168.205.160's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.205.160'"
and check to make sure that only the key(s) you wanted were added.
//验证基于密钥认证是否成功
-bash-4.2$ ssh root@192.168.205.160
Warning: Permanently added '192.168.205.160' (ECDSA) to the list of known hosts.
Last login: Fri Oct 7 19:57:15 2022 from localhost.example.com
[root@localhost ~]# exit
登出
Connection to 192.168.205.160 closed.
-bash-4.2$ exit
登出
[root@localhost ~]# 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@localhost ~]# chown -R root.root /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
[root@localhost ~]# systemctl restart nginx
[root@localhost ~]# systemctl restart libvirtd
//修改nginx配置文件
[root@localhost ~]# 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@localhost ~]# vim /etc/security/limits.conf
在文件最末尾写入
* soft nofile 655350
* hard nofile 655350
//重启服务,重读文件
[root@localhost ~]# sysctl -p
[root@localhost ~]# systemctl restart nginx
使用浏览器访问
添加连接
创建虚拟机
1、新建存储
2、上传镜像
添加网络
开机测试