目錄
KVM虛擬化
創(chuàng)建虛擬機(jī)
報(bào)錯(cuò)解決
虛擬化介紹
kvm介紹
KVM部署(基于CentOS7)
KVM管理界面安裝
KVM web界面管理
虛擬化介紹
虛擬化:在一臺(tái)計(jì)算機(jī)上虛擬出多個(gè)邏輯的計(jì)算機(jī),而且每個(gè)邏輯計(jì)算機(jī)
它可以是不同操作系統(tǒng)
虛擬化技術(shù):可以擴(kuò)大硬件容量,單個(gè)cpu模擬出多個(gè)cpu并行,
允許一個(gè)平臺(tái)上同時(shí)運(yùn)行多個(gè)操作系統(tǒng),應(yīng)用程序都可以在相互獨(dú)立
的空間內(nèi)運(yùn)行,而且互不影響。
為什么企業(yè)使用虛擬化技術(shù)
1、節(jié)約成本
2、提高效率,物理機(jī)我們一般稱(chēng)為宿主機(jī)(Host),宿主機(jī)上面的虛擬機(jī)稱(chēng)為客戶機(jī)(Guest)。
那么 Host 是如何將自己的硬件資源虛擬化,并提供給 Guest 使用的呢?
這個(gè)主要是通過(guò)一個(gè)叫做 Hypervisor 的程序?qū)崿F(xiàn)的。
Hypervisor:一種運(yùn)行在物理服務(wù)器硬件與操作系統(tǒng)之間的中間軟件層
可允許多個(gè)操作系統(tǒng)和應(yīng)用來(lái)共享硬件資源
根據(jù) Hypervisor 的實(shí)現(xiàn)方式和所處的位置,虛擬化又分為兩種:
完全虛擬化:直接在物理機(jī)上部署虛擬化,且不需要修改操作系統(tǒng)內(nèi)核
半虛擬化:需要修改操作系統(tǒng)內(nèi)核,使其支持虛擬化驅(qū)動(dòng)來(lái)實(shí)現(xiàn)虛擬化技術(shù)
1、完全虛擬化
Hypervisor 直接安裝在物理機(jī)上,多個(gè)虛擬機(jī)在 Hypervisor 上運(yùn)行。Hypervisor 實(shí)現(xiàn)方式一般是一個(gè)特殊定制的 Linux 系統(tǒng)。Xen 和 VMWare 的 ESXi 都屬于這個(gè)類(lèi)型

2、半虛擬化

理論上講:
完全虛擬化一般對(duì)硬件虛擬化功能進(jìn)行了特別優(yōu)化,性能上比半虛擬化要高;
半虛擬化因?yàn)榛谄胀ǖ牟僮飨到y(tǒng),會(huì)比較靈活,比如支持虛擬機(jī)嵌套。嵌套意味著可以在KVM虛擬機(jī)中再運(yùn)行KVM。
kvm介紹
kVM 全稱(chēng)是 Kernel-Based Virtual Machine。也就是說(shuō) KVM 是基于 Linux 內(nèi)核實(shí)現(xiàn)的。
KVM有一個(gè)內(nèi)核模塊叫 kvm.ko,只用于管理虛擬 CPU 和內(nèi)存。
那 IO 的虛擬化,比如存儲(chǔ)和網(wǎng)絡(luò)設(shè)備則是由 Linux 內(nèi)核與Qemu來(lái)實(shí)現(xiàn)。
Qemu 是純軟件實(shí)現(xiàn)的虛擬化模擬器,幾乎可以模擬任何硬件設(shè)備,我們最熟悉的就是能夠模擬一臺(tái)能夠獨(dú)立運(yùn)行操作系統(tǒng)的虛擬機(jī),虛擬機(jī)認(rèn)為自己和硬件打交道,但其實(shí)是和 Qemu 模擬出來(lái)的硬件打交道,Qemu 將這些指令轉(zhuǎn)譯給真正的硬件。
正因?yàn)?Qemu 是純軟件實(shí)現(xiàn)的,所有的指令都要經(jīng) Qemu 過(guò)一手,性能非常低,所以,在生產(chǎn)環(huán)境中,大多數(shù)的做法都是配合 KVM 來(lái)完成虛擬化工作,因?yàn)?KVM 是硬件輔助的虛擬化技術(shù),主要負(fù)責(zé) 比較繁瑣的 CPU 和內(nèi)存虛擬化,而 Qemu 則負(fù)責(zé) I/O 虛擬化,兩者合作各自發(fā)揮自身的優(yōu)勢(shì),相得益彰。
作為一個(gè) Hypervisor,KVM 本身只關(guān)注虛擬機(jī)調(diào)度和內(nèi)存管理這兩個(gè)方面。IO 外設(shè)的任務(wù)交給 Linux 內(nèi)核和 Qemu。
Libvirt 就是 KVM 的管理工具。管理虛擬機(jī)和虛擬化功能的軟件
其實(shí),Libvirt 除了能管理 KVM 這種 Hypervisor,還能管理 Xen,VirtualBox 等。
Libvirt 包含 3 個(gè)東西:后臺(tái) daemon 程序 libvirtd、API 庫(kù)和命令行工具 virsh
1、libvirtd是服務(wù)程序,接收和處理 API 請(qǐng)求;
2、API 庫(kù)使得其他人可以開(kāi)發(fā)基于 Libvirt 的高級(jí)工具,比如 virt-manager,這是個(gè)圖形化的 KVM 管理工具;
3、virsh 是我們經(jīng)常要用的 KVM 命令行工具
KVM部署(基于CentOS7)
虛擬機(jī)的安裝步驟可以查看我之前centos8.5的安裝,變化倒是不大修改一下對(duì)應(yīng)參數(shù)就可以了
環(huán)境說(shuō)明:
系統(tǒng):CentOS7
ip :192.168.222.140
CPU虛擬化功能
部署前請(qǐng)確保你的CPU虛擬化功能已開(kāi)啟,分為兩種情況:
(1)虛擬機(jī)要關(guān)機(jī)設(shè)置CPU虛擬化
(2)物理機(jī)要在BIOS里開(kāi)啟CPU虛擬化
虛擬機(jī)設(shè)置(內(nèi)存:8G 磁盤(pán):80G(自定義可以大一點(diǎn)) 虛擬化功能:開(kāi)啟)
//設(shè)置虛擬機(jī)內(nèi)存,添加一塊80G的硬盤(pán)

開(kāi)啟虛擬化

新建分區(qū),將硬盤(pán)所有大小都給這個(gè)分區(qū)
[root@localhost ~]# lsblk //查看一下分區(qū) 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 新的磁盤(pán)標(biāo)簽類(lèi)型? //Tab兩下查看 aix amiga bsd dvh gpt loop mac msdos pc98 sun 新的磁盤(pán)標(biāo)簽類(lèi)型? 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 //創(chuàng)建磁盤(pán)的大小 Sector size (logical/physical): 512B/512B Partition Table: msdos Disk Flags: Number Start End Size Type File system 標(biāo)志 (parted) //Tab兩下查看 align-check help mktable quit select unit disk_set mklabel name rescue set version disk_toggle mkpart print rm toggle (parted) mkpart 分區(qū)類(lèi)型? primary/主分區(qū)/extended/擴(kuò)展分區(qū)? primary 文件系統(tǒng)類(lèi)型? [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 文件系統(tǒng)類(lèi)型? [ext2]? xfs 起始點(diǎn)? 10MiB 結(jié)束點(diǎn)? 81910MiB //比剛剛創(chuàng)建的小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 標(biāo)志 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"
創(chuàng)建掛載點(diǎn),并進(jìn)行永久掛載
[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 文件系統(tǒng) 類(lèi)型 容量 已用 可用 已用% 掛載點(diǎn) /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安裝
關(guān)閉防火墻和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源
可以配置阿里云的網(wǎng)絡(luò)源
阿里云官網(wǎng)
[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++
驗(yàn)證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
配置網(wǎng)絡(luò),因?yàn)樘摂M機(jī)中的網(wǎng)絡(luò),我們一般是都和公司服務(wù)器處在同一網(wǎng)段的,所以我們需要把kvm的網(wǎng)卡配置成橋接模式
[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 成功斷開(kāi)設(shè)備 'ens33'。 連接已成功激活(D-Bus 活動(dòng)路徑:/org/freedesktop/NetworkManager/ActiveConnection/3) [root@localhost network-scripts]# cd [root@localhost ~]# ifconfig br0: flags=4163mtu 1500 inet 192.168.222.140 netmask 255.255.255.0 broadcast 192.168.222.255 inet6 fe80:5bffba71 prefixlen 64 scopeid 0x20 ether 0029f0: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 mtu 1500 ether 0029f0: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 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10 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服務(wù),并設(shè)置開(kāi)機(jī)自啟
[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
驗(yàn)證安裝結(jié)果
[root@localhost ~]# virsh -c qemu:///system list Id 名稱(chēng) 狀態(tài) ---------------------------------------------------- [root@localhost ~]# virsh --version 4.5.0 [root@localhost ~]# virt-install --version 1.5.0
將qemu-kvm這個(gè)命令做一個(gè)軟鏈接到/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
查看網(wǎng)橋信息
[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程序提供的
安裝依賴(lài)包
[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 作為依賴(lài)被安裝: 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 作為依賴(lài)被升級(jí): python.x86_64 0:2.7.5-92.el7_9 python-libs.x86_64 0:2.7.5-92.el7_9 完畢!
可以提前做一個(gè)映射,防止訪問(wèn)不了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 接收對(duì)象中: 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()
初始化賬號(hào)信息
[root@localhost ~]# cd /usr/local/src/webvirtmgr/ [root@localhost webvirtmgr]# python manage.py syncdb WARNINGNo 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 //是否創(chuàng)建超級(jí)管理員賬號(hào) Username (leave blank to use 'root'): //指定超級(jí)管理員賬號(hào)用戶名,默認(rèn)留空為root Email address: lnh136@78.com //設(shè)置超級(jí)管理員郵箱 Password: //設(shè)置超級(jí)管理員密碼 Password (again): //再次輸入確認(rèn)超級(jí)管理員密碼 Superuser created successfully. Installing custom SQL ... Installing indexes ... Installed 6 object(s) from 1 fixture(s)
拷貝web網(wǎng)頁(yè)到指定目錄
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/
生成一對(duì)公鑰與私鑰,由于這里webvirtmgr和kvm服務(wù)部署在同一臺(tái)主機(jī)中,所以這里本地信任。如果kvm部署在其他機(jī)器上的時(shí)候,那么就需要把公鑰發(fā)送到kvm主機(jī)中
[root@localhost webvirtmgr]# ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): //直接回車(chē) Created directory '/root/.ssh'. Enter passphrase (empty for no passphrase): /直接回車(chē) Enter same passphrase again: /直接回車(chē) 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 root@localhost.example.com 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 root@192.168.222.140 /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:570017:10:5d:1cac:197b:6662. 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.222.140's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'root@192.168.222.140'" and check to make sure that only the key(s) you wanted were added.
配置端口轉(zhuǎn)發(fā)
可以使用ss -antl查看端口情況
[root@localhost webvirtmgr]# ssh 192.168.222.140 -L localhostlocalhost:8000 -L localhostlocalhost: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參數(shù)中進(jìn)行修改
刪除listen [::]:80;行
參數(shù)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虛擬主機(jī)
[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綁定本機(jī)的8000端口
[root@localhost ~]# vim /var/www/webvirtmgr/conf/gunicorn.conf.py bind = '127.0.0.1:8000' //默認(rèn) backlog = 2048 //默認(rèn) [root@localhost ~]# grep bind /var/www/webvirtmgr/conf/gunicorn.conf.py # bind - The socket to bind. bind = '127.0.0.1:8000' //確保此處綁定的是本機(jī)的8000端口,這個(gè)在nginx配置中定義了,被代理的端口
重啟nginx服務(wù)設(shè)置開(kāi)機(jī)自啟,查看端口是否開(kāi)啟
[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 :::*
設(shè)置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
啟動(dòng)supervisor并設(shè)置開(kāi)機(jī)自啟
[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 nginx@localhost.example.com 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 UserKnownHostsFile=/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.222.140 /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. root@192.168.222.140's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'root@192.168.222.140'" and check to make sure that only the key(s) you wanted were added. -bash-4.2$ ssh root@192.168.222.140 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; //添加此行
對(duì)系統(tǒng)參數(shù)進(jìn)行設(shè)置
[root@localhost ~]# vim /etc/security/limits.conf //在文件最末尾寫(xiě)入 * soft nofile 655350 * hard nofile 655350
重啟服務(wù),重讀文件
[root@localhost ~]# sysctl -p [root@localhost ~]# systemctl enable --now nginx
安裝novnc,并通過(guò)novnc_server啟動(dòng)一個(gè)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" 這里會(huì)一直停在這里
使用瀏覽器訪問(wèn)

登錄用戶是之前設(shè)置的默認(rèn)root,密碼也是之前設(shè)置的

添加連接



創(chuàng)建虛擬機(jī)
1、新建存儲(chǔ)


2、上傳鏡像,使用FinalShell,xftp或者xshell,再或者其他工具,將鏡像文件上傳到服務(wù)器的/kvmdata目錄下存放
再開(kāi)一個(gè)終端: [root@localhost ~]# cd /kvmdata/ [root@localhost kvmdata]# ls CentOS-7.4-x86_64-DVD-1708.iso
刷新一下網(wǎng)頁(yè)界面



添加網(wǎng)絡(luò)


實(shí)例管理,現(xiàn)在去創(chuàng)建一個(gè)虛擬機(jī)








報(bào)錯(cuò)解決
報(bào)錯(cuò): 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
-
Web
+關(guān)注
關(guān)注
2文章
1303瀏覽量
73947 -
虛擬化
+關(guān)注
關(guān)注
1文章
403瀏覽量
30882 -
KVM
+關(guān)注
關(guān)注
0文章
203瀏覽量
13613
原文標(biāo)題:深入 KVM 虛擬化:如何構(gòu)建高效、可擴(kuò)展的虛擬化環(huán)境
文章出處:【微信號(hào):magedu-Linux,微信公眾號(hào):馬哥Linux運(yùn)維】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
虛擬儀器技術(shù)
【Android安全】ARM平臺(tái)代碼保護(hù)之虛擬化
WAN中的虛擬化
深入了解AArch64虛擬化
關(guān)于QorIQ SoC的虛擬性能介紹(二)
Linux桌面虛擬化技術(shù)KVM的詳細(xì)資料說(shuō)明
虛擬化概述,Hypervisor簡(jiǎn)介
KVM虛擬機(jī)管理和基本使用
虹科案例|虛擬化數(shù)據(jù)中心 N_Port_ID 虛擬化 (NPIV)
用于高度虛擬化云環(huán)境的可擴(kuò)展企業(yè)級(jí)SAN交換機(jī)
KVM矩陣:數(shù)據(jù)中心虛擬化管理的關(guān)鍵組件
RAID 5 在虛擬化環(huán)境中的應(yīng)用
hyper v 虛擬化,Hyper-V 虛擬化:Hyper-V虛擬化的最佳實(shí)踐
深入KVM虛擬化之構(gòu)建高效、可擴(kuò)展的虛擬化環(huán)境
評(píng)論