KVM虛擬化 虛擬化介紹 虛擬化:在一臺電腦上虛擬出多個邏輯的電腦,而且每個邏輯電腦 它可以是不同操作系統 虛擬化技術:可以擴大硬體容量,單個cpu模擬出多個cpu並行, 允許一個平臺上同時運行多個操作系統,應用程式都可以在相互獨立 的空間內運行,而且互不影響。 為什麼企業使用虛擬化技術 1、 ...
KVM虛擬化
目錄
虛擬化介紹
虛擬化:在一臺電腦上虛擬出多個邏輯的電腦,而且每個邏輯電腦
它可以是不同操作系統
虛擬化技術:可以擴大硬體容量,單個cpu模擬出多個cpu並行,
允許一個平臺上同時運行多個操作系統,應用程式都可以在相互獨立
的空間內運行,而且互不影響。
為什麼企業使用虛擬化技術
1、節約成本
2、提高效率,物理機我們一般稱為宿主機(Host),宿主機上面的虛擬機稱為客戶機(Guest)。
那麼 Host 是如何將自己的硬體資源虛擬化,並提供給 Guest 使用的呢?
這個主要是通過一個叫做 Hypervisor 的程式實現的。
Hypervisor:一種運行在物理伺服器硬體與操作系統之間的中間軟體層
可允許多個操作系統和應用來共用硬體資源
根據 Hypervisor 的實現方式和所處的位置,虛擬化又分為兩種:
完全虛擬化:直接在物理機上部署虛擬化,且不需要修改操作系統內核
半虛擬化:需要修改操作系統內核,使其支持虛擬化驅動來實現虛擬化技術
1、完全虛擬化
Hypervisor 直接安裝在物理機上,多個虛擬機在 Hypervisor 上運行。Hypervisor 實現方式一般是一個特殊定製的 Linux 系統。Xen 和 VMWare 的 ESXi 都屬於這個類型
2、半虛擬化
理論上講:
完全虛擬化一般對硬體虛擬化功能進行了特別優化,性能上比半虛擬化要高;
半虛擬化因為基於普通的操作系統,會比較靈活,比如支持虛擬機嵌套。嵌套意味著可以在KVM虛擬機中再運行KVM。
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 虛擬化,兩者合作各自發揮自身的優勢,相得益彰.
作為一個 Hypervisor,KVM 本身只關註虛擬機調度和記憶體管理這兩個方面。IO 外設的任務交給 Linux 內核和 Qemu。
Libvirt 就是 KVM 的管理工具。管理虛擬機和虛擬化功能的軟體
其實,Libvirt 除了能管理 KVM 這種 Hypervisor,還能管理 Xen,VirtualBox 等。
Libvirt 包含 3 個東西:後臺 daemon 程式 libvirtd、API 庫和命令行工具 virsh
1、libvirtd是服務程式,接收和處理 API 請求;
2、API 庫使得其他人可以開發基於 Libvirt 的高級工具,比如 virt-manager,這是個圖形化的 KVM 管理工具;
3、virsh 是我們經常要用的 KVM 命令行工具
KVM部署(基於CentOS7)
虛擬機的安裝步驟可以查看我之前centos8.5的安裝,變化倒是不大修改一下對應參數就可以了
環境說明:
系統:CentOS7
ip :192.168.222.140
- CPU虛擬化功能
部署前請確保你的CPU虛擬化功能已開啟,分為兩種情況:
(1)虛擬機要關機設置CPU虛擬化
(2)物理機要在BIOS里開啟CPU虛擬化 - 虛擬機設置(記憶體:8G 磁碟:80G(自定義可以大一點) 虛擬化功能:開啟)
//設置虛擬機記憶體,添加一塊80G的硬碟
開啟虛擬化
新建分區,將硬碟所有大小都給這個分區
[root@localhost ~]# lsblk //查看一下分區
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 10G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 9G 0 part
├─centos-root 253:0 0 8G 0 lvm /
└─centos-swap 253:1 0 1G 0 lvm [SWAP]
sdb 8:16 0 80G 0 disk
sr0 11:0 1 4.2G 0 rom
[root@localhost ~]# parted /dev/sdb
GNU Parted 3.1
使用 /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) //Tab兩下查看
align-check help mktable quit select unit
disk_set mklabel name rescue set version
disk_toggle mkpart print rm toggle
(parted) mklabel
新的磁碟標簽類型? //Tab兩下查看
aix amiga bsd dvh gpt loop mac msdos pc98 sun
新的磁碟標簽類型? msdos
(parted) //Tab兩下查看
align-check help mktable quit select unit
disk_set mklabel name rescue set version
disk_toggle mkpart print rm toggle
(parted) unit
Unit? [compact]? //Tab兩下查看
% chs cyl GiB kiB MiB TB
B compact GB kB MB s TiB
Unit? [compact]? MiB
(parted) p
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 81920MiB //創建磁碟的大小
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system 標誌
(parted) //Tab兩下查看
align-check help mktable quit select unit
disk_set mklabel name rescue set version
disk_toggle mkpart print rm toggle
(parted) mkpart
分區類型? primary/主分區/extended/擴展分區? primary
文件系統類型? [ext2]? //Tab兩下查看
affs0 amufs apfs2 hfs linux-swap(v0)
affs1 amufs0 asfs hfs+ linux-swap(v1)
affs2 amufs1 btrfs hfsx nilfs2
affs3 amufs2 ext2 hp-ufs ntfs
affs4 amufs3 ext3 jfs reiserfs
affs5 amufs4 ext4 linux-swap sun-ufs
affs6 amufs5 fat16 linux-swap(new) swsusp
affs7 apfs1 fat32 linux-swap(old) xfs
文件系統類型? [ext2]? xfs
起始點? 10MiB
結束點? 81910MiB //比剛剛創建的小10MiB就可以了
(parted) p
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 81920MiB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system 標誌
1 10.0MiB 81910MiB 81900MiB primary
(parted) q
信息: You may need to update /etc/fstab.
[root@localhost ~]# udevadm settle
格式化
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 10G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 9G 0 part
├─centos-root 253:0 0 8G 0 lvm /
└─centos-swap 253:1 0 1G 0 lvm [SWAP]
sdb 8:16 0 80G 0 disk
└─sdb1 8:17 0 80G 0 part
sr0 11:0 1 4.2G 0 rom
[root@localhost ~]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1 isize=512 agcount=4, agsize=5241600 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=20966400, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=10237, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@localhost ~]# blkid /dev/sdb1
/dev/sdb1: UUID="09888ad8-a1ef-42fb-9c36-937e071bf897" TYPE="xfs"
創建掛載點,併進行永久掛載
[root@localhost ~]# yum -y install vim
[root@localhost ~]# mkdir /kvmdata
[root@localhost ~]# vim /etc/fstab
UUID="09888ad8-a1ef-42fb-9c36-937e071bf897" /kvmdata xfs defaults 0 0 //添加這一行
[root@localhost ~]# mount -a
[root@localhost ~]# df -Th
文件系統 類型 容量 已用 可用 已用% 掛載點
/dev/mapper/centos-root xfs 8.0G 1.2G 6.9G 15% /
devtmpfs devtmpfs 3.9G 0 3.9G 0% /dev
tmpfs tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs tmpfs 3.9G 8.7M 3.9G 1% /run
tmpfs tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/sda1 xfs 1014M 143M 872M 15% /boot
tmpfs tmpfs 781M 0 781M 0% /run/user/0
/dev/sdb1 xfs 80G 33M 80G 1% /kvmdata
KVM安裝
關閉防火牆和selinux
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# vim /etc/selinux/config
SELINUX=disabled
[root@localhost ~]# setenforce 0
[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 ~]# reboot
部署yum源
可以配置阿裡雲的網路源
阿裡雲官網
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# ls
CentOS-Base.repo CentOS-Debuginfo.repo CentOS-Media.repo CentOS-Vault.repo epel-testing.repo
CentOS-CR.repo CentOS-fasttrack.repo CentOS-Sources.repo epel.repo
[root@localhost yum.repos.d]# rm -rf *
[root@localhost yum.repos.d]# ls
[root@localhost 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
100 2523 100 2523 0 0 9299 0 --:--:-- --:--:-- --:--:-- 9309
[root@localhost yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@localhost yum.repos.d]# ls
CentOS-Base.repo
安裝所需軟體包
[root@localhost yum.repos.d]# cd
[root@localhost ~]# yum -y install epel-release wget net-tools unzip zip gcc gcc-c++
驗證cpu是否支持kvm,vmx是intel的 svm是AMD的
[root@localhost ~]# egrep -o 'vmx|svm' /proc/cpuinfo
vmx
安裝kvm
[root@localhost ~]# 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@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# ls
ifcfg-ens33 ifdown-ipv6 ifdown-TeamPort ifup-ippp ifup-routes network-functions
ifcfg-lo ifdown-isdn ifdown-tunnel ifup-ipv6 ifup-sit network-functions-ipv6
ifdown ifdown-post ifup ifup-isdn ifup-Team
ifdown-bnep ifdown-ppp ifup-aliases ifup-plip ifup-TeamPort
ifdown-eth ifdown-routes ifup-bnep ifup-plusb ifup-tunnel
ifdown-ib ifdown-sit ifup-eth ifup-post ifup-wireless
ifdown-ippp ifdown-Team ifup-ib ifup-ppp init.ipv6-global
[root@localhost network-scripts]# cp ifcfg-ens33 ifcfg-br0
[root@localhost network-scripts]# vim ifcfg-br0
[root@localhost network-scripts]# cat ifcfg-br0
TYPE=bridge
BOOTPROTO=none
NAME=br0
DEVICE=br0
ONBOOT=yes
IPADDR=192.168.222.140
PREFIX=24
GATEWAY=192.168.222.2
DNS1=192.168.222.2
[root@localhost network-scripts]# vim ifcfg-ens33
[root@localhost network-scripts]# cat ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=none
NAME=ens33
DEVICE=ens33
ONBOOT=yes
BRIDGE=br0
[root@localhost network-scripts]# systemctl restart NetworkManager
[root@localhost network-scripts]# ifdown ens33;ifup ens33
成功斷開設備 'ens33'。
連接已成功激活(D-Bus 活動路徑:/org/freedesktop/NetworkManager/ActiveConnection/3)
[root@localhost network-scripts]# cd
[root@localhost ~]# ifconfig
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.222.140 netmask 255.255.255.0 broadcast 192.168.222.255
inet6 fe80::1c12:5bff:fea2:ba71 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:5a:f0:0d txqueuelen 1000 (Ethernet)
RX packets 494 bytes 34964 (34.1 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 813 bytes 143186 (139.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether 00:0c:29:5a:f0:0d txqueuelen 1000 (Ethernet)
RX packets 201496 bytes 194698973 (185.6 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 108153 bytes 10993998 (10.4 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
重啟libvirtd服務,並設置開機自啟
[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 三 2022-10-05 02:57:06 CST; 10s ago
Docs: man:libvirtd(8)
https://libvirt.org
Main PID: 106910 (libvirtd)
Tasks: 19 (limit: 32768)
CGroup: /system.slice/libvirtd.service
├─106910 /usr/sbin/libvirtd
├─107058 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefil...
└─107060 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefil...
10月 05 02:57:07 localhost.example.com dnsmasq[107052]: listening on virbr0(#4): 192.168.122.1
10月 05 02:57:07 localhost.example.com dnsmasq[107058]: started, version 2.76 cachesize 150
10月 05 02:57:07 localhost.example.com dnsmasq[107058]: compile time options: IPv6 GNU-getopt D...fy
10月 05 02:57:07 localhost.example.com dnsmasq-dhcp[107058]: DHCP, IP range 192.168.122.2 -- 192...h
10月 05 02:57:07 localhost.example.com dnsmasq-dhcp[107058]: DHCP, sockets bound exclusively to ...0
10月 05 02:57:07 localhost.example.com dnsmasq[107058]: reading /etc/resolv.conf
10月 05 02:57:07 localhost.example.com dnsmasq[107058]: using nameserver 192.168.222.2#53
10月 05 02:57:07 localhost.example.com dnsmasq[107058]: read /etc/hosts - 2 addresses
10月 05 02:57:07 localhost.example.com dnsmasq[107058]: read /var/lib/libvirt/dnsmasq/default.a...es
10月 05 02:57:07 localhost.example.com dnsmasq-dhcp[107058]: read /var/lib/libvirt/dnsmasq/defau...e
Hint: Some lines were ellipsized, use -l to show in full.
查看kvm模塊是否載入
[root@localhost ~]# lsmod |grep kvm
kvm_intel 170086 0
kvm 566340 1 kvm_intel
irqbypass 13503 1 kvm
驗證安裝結果
[root@localhost ~]# virsh -c qemu:///system list
Id 名稱 狀態
----------------------------------------------------
[root@localhost ~]# virsh --version
4.5.0
[root@localhost ~]# virt-install --version
1.5.0
將qemu-kvm這個命令做一個軟鏈接到/usr/bin/qemu-kvm
[root@localhost ~]# ln -s /usr/libexec/qemu-kvm /usr/bin/qemu-kvm
[root@localhost ~]# ll /usr/bin/qemu-kvm
lrwxrwxrwx 1 root root 21 10月 5 03:01 /usr/bin/qemu-kvm -> /usr/libexec/qemu-kvm
查看網橋信息
[root@localhost ~]# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.000c295af00d no ens33
virbr0 8000.525400e562ac yes virbr0-nic
KVM管理界面安裝
Kvm的web界面是由webvirtmgr程式提供的
安裝依賴包
[root@localhost ~]# yum -y install git python-pip libvirt-python libxml2-python python-websockify supervisor nginx python-devel
.....
已安裝:
git.x86_64 0:1.8.3.1-23.el7_8 nginx.x86_64 1:1.20.1-9.el7
python-devel.x86_64 0:2.7.5-92.el7_9 python-websockify.noarch 0:0.6.0-2.el7
python2-pip.noarch 0:8.1.2-14.el7 supervisor.noarch 0:3.4.0-1.el7
作為依賴被安裝:
centos-indexhtml.noarch 0:7-9.el7.centos nginx-filesystem.noarch 1:1.20.1-9.el7
openssl11-libs.x86_64 1:1.1.1k-4.el7 perl-Error.noarch 1:0.17020-2.el7
perl-Git.noarch 0:1.8.3.1-23.el7_8 perl-TermReadKey.x86_64 0:2.30-20.el7
python-meld3.x86_64 0:0.6.10-1.el7 python-rpm-macros.noarch 0:3-34.el7
python-setuptools.noarch 0:0.9.8-7.el7 python-srpm-macros.noarch 0:3-34.el7
python2-rpm-macros.noarch 0:3-34.el7 rsync.x86_64 0:3.1.2-11.el7_9
作為依賴被升級:
python.x86_64 0:2.7.5-92.el7_9 python-libs.x86_64 0:2.7.5-92.el7_9
完畢!
可以提前做一個映射,防止訪問不了github
[root@localhost ~]# vim /etc/hosts
[root@localhost ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
20.205.243.166 github.com //添加
從github上下載webvirtmgr代碼
[root@localhost ~]# 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 | 689.00 KiB/s, done.
處理 delta 中: 100% (3606/3606), done.
安裝webvirtmgr
[root@localhost src]# ls
webvirtmgr
[root@localhost src]# cd webvirtmgr/
[root@localhost webvirtmgr]# pip 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 42kB/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 104kB/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
You are using pip version 8.1.2, however version 22.2.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
檢查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
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'): //指定超級管理員賬號用戶名,預設留空為root
Email address: [email protected] //設置超級管理員郵箱
Password: //設置超級管理員密碼
Password (again): //再次輸入確認超級管理員密碼
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 6 object(s) from 1 fixture(s)
拷貝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:GM4Wtczgi6KVA8VnhunqFApm06RP221yf9hwbhE63Ig [email protected]
The key's randomart image is:
+---[RSA 2048]----+
| ..o . . |
| .+.+. = . |
|..++ + + |
|.B.+ + = . |
|=.X + B S + . |
|o+ = + E * + |
|+ + . B . |
| . o = |
| o |
+----[SHA256]-----+
[root@localhost webvirtmgr]# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.222.140 (192.168.222.140)' can't be established.
ECDSA key fingerprint is SHA256:FkmNy5vPRVhOZGwZpvDsgPmo4WjilEinC9gpRbRiZQQ.
ECDSA key fingerprint is MD5:57:cc:00:bc:17:10:5d:1c:ed:ac:19:bf:7b:66:a3:62.
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
[email protected]'s password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.
配置埠轉發
可以使用ss -antl查看埠情況
[root@localhost webvirtmgr]# ssh 192.168.222.140 -L localhost:8000:localhost:8000 -L localhost:6080:localhost:60
Last login: Wed Oct 5 17:08:52 2022 from 192.168.222.1
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 5 192.168.122.1:53 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 127.0.0.1:6080 *:*
LISTEN 0 128 127.0.0.1:8000 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 128 ::1:6080 :::*
LISTEN 0 128 ::1:8000 :::*
配置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 ~]# vim /etc/nginx/conf.d/webvirtmgr.conf
[root@localhost ~]# 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 ~]# vim /var/www/webvirtmgr/conf/gunicorn.conf.py
bind = '127.0.0.1:8000' //預設
backlog = 2048 //預設
[root@localhost ~]# grep bind /var/www/webvirtmgr/conf/gunicorn.conf.py
# bind - The socket to bind.
bind = '127.0.0.1:8000'
//確保此處綁定的是本機的8000埠,這個在nginx配置中定義了,被代理的埠
重啟nginx服務設置開機自啟,查看埠是否開啟
[root@localhost ~]# systemctl enable --now nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:80 *:*
LISTEN 0 5 192.168.122.1:53 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 127.0.0.1:6080 *:*
LISTEN 0 128 127.0.0.1:8000 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 128 ::1:6080 :::*
LISTEN 0 128 ::1:8000 :::*
設置supervisor
[root@localhost ~]# vim /etc/supervisord.conf
//在文件最後添加如下信息
#這裡command是一行
[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 ~]# systemctl enable --now 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:U+al0qUqjCY2+jySrqJYJ/+mc9fkaVtgPovqsa9+qCE [email protected]
The key's randomart image is:
+---[RSA 2048]----+
| |
| |
| o o |
| = = |
| S B |
| o *.. |
| .E = +..+o.. |
|==.O..++o.=+ |
|X+o.+XB*o.o. |
+----[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 [email protected]
/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.222.140' (ECDSA) to the list of known hosts.
[email protected]'s password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.
-bash-4.2$ ssh [email protected]
Warning: Permanently added '192.168.222.140' (ECDSA) to the list of known hosts.
Last login: Wed Oct 5 17:34:44 2022 from 192.168.222.140
[root@localhost ~]# exit
logout
Connection to 192.168.222.140 closed.
-bash-4.2$ exit
logout
[root@localhost ~]#
root@localhost ~]# vim /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
[root@localhost ~]# cat /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
bvirt 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 enable --now nginx
[root@localhost ~]# systemctl enable --now libvirtd
KVM web界面管理
修改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 enable --now nginx
安裝novnc,並通過novnc_server啟動一個vnc
[root@localhost ~]# yum -y install novnc
[root@localhost ~]# chmod +x /etc/rc.d/rc.local
[root@localhost ~]# vim /etc/rc.d/rc.local
//在最末尾加入如下行
nohup novnc_server 192.168.222.140:5920 &
[root@localhost ~]# . /etc/rc.d/rc.local
[root@localhost ~]# nohup: 忽略輸入並把輸出追加到"nohup.out"
這裡會一直停在這裡
使用瀏覽器訪問
登錄用戶是之前設置的預設root,密碼也是之前設置的
添加連接
創建虛擬機
1、新建存儲
2、上傳鏡像,使用FinalShell,xftp或者xshell,再或者其他工具,將鏡像文件上傳到伺服器的/kvmdata目錄下存放
再開一個終端:
[root@localhost ~]# cd /kvmdata/
[root@localhost kvmdata]# ls
CentOS-7.4-x86_64-DVD-1708.iso
刷新一下網頁界面
添加網路
實例管理,現在去創建一個虛擬機
報錯解決
報錯:
fatal: unable to access 'http://github.com/retspen/webvirtmgr.git/': Failed connect to github.com:443; Connection refused
fatal: unable to access 'http://github.com/retspen/webvirtmgr.git/': TCP connection reset by peer
解決:
[root@localhost src]# git config --global -l
fatal: unable to read config file '/root/.gitconfig': ?????????
[root@localhost src]# env|grep -i proxy