KVM虚拟化
KVM虚拟化
- 虚拟化介绍
- kvm介绍
- 1.KVM安装
- 2.kvm web管理界面安装
- 3.添加虚拟机
- 3.1登录
- A.案例1
- 3.2.添加虚拟机
- 4.配置虚拟机
- 4.1创建存储池
- 4.2添加镜像
- 4.3添加网络池
- 4.4.配置虚拟机
- 5.启动
- B.案例2
- 6.完成
#
#
#
虚拟化介绍
虚拟化是云计算的基础。简单的说,虚拟化使得在一台物理的服务器上可以跑多台虚拟机,虚拟机共享物理机的 CPU、内存、IO 硬件资源,但逻辑上虚拟机之间是相互隔离的。
物理机我们一般称为宿主机(Host),宿主机上面的虚拟机称为客户机(Guest)。
宿主机(Host)是通过一个叫做 Hypervisor 的程序实现硬件资源虚拟化,并提供给客户机(Guest) 使用的
根据 Hypervisor 的实现方式和所处的位置,虚拟化又分为两种:
全虚拟化
半虚拟化
全虚拟化:
Hypervisor 直接安装在物理机上,多个虚拟机在 Hypervisor 上运行。Hypervisor 实现方式一般是一个特殊定制的 Linux 系统。Xen 和 VMWare 的 ESXi 都属于这个类型
半虚拟化:
物理机上首先安装常规的操作系统,比如 Redhat、Ubuntu 和 Windows。Hypervisor 作为 OS 上的一个程序模块运行,并对管理虚拟机进行管理。KVM、VirtualBox 和 VMWare Workstation 都属于这个类型
kvm介绍
kVM 全称是 Kernel-Based Virtual Machine。也就是说 KVM 是基于 Linux 内核实现的。
KVM有一个内核模块叫 kvm.ko,只用于管理虚拟 CPU 和内存。
那 IO 的虚拟化,比如存储和网络设备则是由 Linux 内核与Qemu来实现。
作为一个 Hypervisor,KVM 本身只关注虚拟机调度和内存管理这两个方面。IO 外设的任务交给 Linux 内核和 Qemu。
大家在网上看 KVM 相关文章的时候肯定经常会看到 Libvirt 这个东西,Libvirt 就是 KVM 的管理工具。
其实,Libvirt 除了能管理 KVM 这种 Hypervisor,还能管理 Xen,VirtualBox 等。
Libvirt 包含 3 个东西:后台 daemon 程序 libvirtd、API 库和命令行工具 virsh
libvirtd是服务程序,接收和处理 API 请求;
API 库使得其他人可以开发基于Libvirt的高级工具,比如 virt-manager,这是个图形化的KVM管理工具
virsh 是我们经常要用的 KVM 命令行工具
1.KVM安装
关闭防火墙和selinux
[root@zyy180 ~]# systemctl stop firewalld
[root@zyy180 ~]# 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@zyy180 ~]# setenforce 0
[root@zyy180 ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@zyy180 ~]# reboot
下载网络源
[root@zyy180 ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
[root@zyy180 ~]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/Centos-7.repo
[root@zyy180 ~]# sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/Centos-7.repo
建立缓存
[root@zyy180 ~]# yum makecache fast
下载所需软件
[root@zyy180 ~]# yum -y install epel-release vim wget net-tools unzip zip gcc gcc-c++
验证CPU是否支持KVM;如果结果中有vmx(Intel)或svm(AMD)字样,就说明CPU的支持的
[root@zyy180 ~]# egrep -o 'vmx|svm' /proc/cpuinfo
vmx
vmx
vmx
vmx
1.kvm安装
[root@zyy180 ~]# 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
2.这里一般是要用桥接网卡,保证虚拟机和公司网络在同一网段
root@zyy180 ~]# cd /etc/sysconfig/network-scripts/
[root@zyy180 network-scripts]# cp ifcfg-ens33 ifcfg-br0
TYPE="Bridge"
NM_CONTROLLED=no
BOOTPROTO="static"
DEFROUTE="yes"
NAME="br0"
DEVICE="br0"
ONBOOT="yes"
IPADDR=192.168.30.150
NETMASK=255.255.255.0
GATEWAY=192.168.30.2
DNS1=114.114.114.114
[root@zyy180 network-scripts]# vim ifcfg-ens33
TYPE="Ethernet"
BOOTPROTO="static"
NAME="ens33"
DEVICE="ens33"
ONBOOT="yes"
BRIDGE=br0
NM_CONTROLLED=no
查看网卡信息
[root@zyy180 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
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 qlen 1000
link/ether 00:0c:29:50:dc:de brd ff:ff:ff:ff:ff:ff
inet6 fe80::20c:29ff:fe50:dcde/64 scope link
valid_lft forever preferred_lft forever
3: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP qlen 1000
link/ether 00:0c:29:50:dc:de brd ff:ff:ff:ff:ff:ff
inet 192.168.30.150/24 brd 192.168.30.255 scope global br0
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe50:dcde/64 scope link
valid_lft forever preferred_lft forever
[root@zyy180 ~]# systemctl restart network
重启libvirtd服务
[root@zyy180 ~]# systemctl start libvirtd
[root@zyy180 ~]# systemctl enable libvirtd
验证安装结果
[root@zyy180 ~]# lsmod|grep kvm
kvm_intel 170086 0
kvm 566340 1 kvm_intel
irqbypass 13503 1 kvm
/测试并验证安装结果
[root@zyy180 ~]# virsh -c qemu:///system list
Id 名称 状态
----------------------------------------------------
[root@zyy180 ~]# virsh --version
4.5.0
[root@zyy180 ~]# virt-install --version
1.5.0
3.做个软连接,保证系统能找到qemu-kvm
[root@zyy180 ~]# ln -s /usr/libexec/qemu-kvm /usr/bin/qemu-kvm
查看网桥信息
[root@zyy180 ~]# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.000c2950dcde no ens33
virbr0 8000.525400597859 yes virbr0-nic
2.kvm web管理界面安装
1.安装依赖包
[root@zyy180 ~]# yum -y install git python-pip libvirt-python libxml2-python python-websockify supervisor nginx python-devel
2.升级pip
[root@zyy180 ~]# pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple/
Collecting pip
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/36/74/38c2410d688ac7b48afa07d413674afc1f903c1c1f854de51dc8eb2367a5/pip-20.2-py2.py3-none-any.whl (1.5MB)
100% |████████████████████████████████| 1.5MB 593kB/s
Installing collected packages: pip
Found existing installation: pip 8.1.2
Uninstalling pip-8.1.2:
Successfully uninstalled pip-8.1.2
Successfully installed pip-20.2
2.从github上下载webvirtmgr代码
[root@zyy180 ~]# cd /usr/local/src/
[root@zyy180 src]# git clone git://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.98 MiB | 10.00 KiB/s, done.
处理 delta 中: 100% (3602/3602), done.
3.安装webvirtmgr
[root@zyy180 webvirtmgr]# pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple/
检查sqlite3是否安装
[root@zyy180 ~]# python
Python 2.7.5 (default, Apr 2 2020, 13:16:51)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
>>> exit()
4.初始化帐号信息
[root@zyy180 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'):
Email address: 1412@qq.com
Password:
Password (again):
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 6 object(s) from 1 fixture(s)
5.拷贝web网页至指定目录
[root@zyy180 webvirtmgr]# mkdir /var/www
[root@zyy180 webvirtmgr]# cp -r /usr/local/src/webvirtmgr/ /var/www/
[root@zyy180 webvirtmgr]# chown -R nginx.nginx /var/www/webvirtmgr/
6.生成密钥
[root@zyy180 ~]# ssh-keygen -t rsa
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:jhkhq3fzTKROVjeyfATLMeBuxA6V+3QQYwkEtlj6l6k root@zyy180
The key's randomart image is:
+---[RSA 2048]----+
| +o=o+o |
| = =.oo. |
| o + =.+. |
| . B.=.=. |
| o OoS.+ |
| . + X.= . |
| . E O = . |
| . = = . |
| . o |
+----[SHA256]-----+
7.做互信,
这里webvirtmgr和kvm服务部署在同一台主机上,所以地址为本地地址
如果kvm部署在其他主机 地址改为kvm主机
[root@zyy180 ~]# ssh-copy-id 192.168.30.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.30.150 (192.168.30.150)' can't be established.
ECDSA key fingerprint is SHA256:bGJka92FBGDxlsCxrm6FWR/nryT8gK/uWC/jwUe37Uw.
ECDSA key fingerprint is MD5:46:4e:e1:0e:e5:0e:b4:8f:b3:a8:cd:5d:7d:5b:22:2f.
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.30.150's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '192.168.30.150'"
and check to make sure that only the key(s) you wanted were added.
8.配置端口转发
[root@zyy180 ~]# ssh 192.168.30.150 -L localhost:8000:localhost:8000 -L localhost:6080:localhost:60
Last login: Mon Aug 3 19:57:21 2020 from 192.168.30.1
[root@zyy180 ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:111 *:*
LISTEN 0 5 192.168.122.1:53 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 *:8000 *:*
LISTEN 0 100 *:6080 *:*
LISTEN 0 128 :::111 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
9.配置nginx
[root@zyy180 ~]# 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@zyy180 ~]# 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@zyy180 ~]# vim /var/www/webvirtmgr/conf/gunicorn.conf.py
bind = '0.0.0.0:8000'
10.重启nginx
[root@zyy180 ~]# systemctl restart nginx
[root@zyy180 ~]# ss -anlt
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 :::*
11.设置supervisor
[root@zyy180 ~]# 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
12.启动supervisor并设置开机自启
[root@zyy180 ~]# systemctl enable supervisord
[root@zyy180 ~]# systemctl start supervisord
[root@zyy180 ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:111 *:*
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 *:8000 *:*
LISTEN 0 100 *:6080 *:*
LISTEN 0 128 :::111 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
13.配置nginx用户
[root@zyy180 ~]# 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:ohczhDrvonTF5b6Q2+8Zqij2eIu4C7yQRT9QnNynF/k nginx@zyy180
The key's randomart image is:
+---[RSA 2048]----+
| o.o . |
| .+.. + |
| o . .+ o |
| . +..+ . E |
| + oo=oS |
|.o o.oo= |
|+o .oo.. . |
|=o*o..+ o o |
|*B+=oo.+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 root@192.168.30.150
/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.30.150' (ECDSA) to the list of known hosts.
root@192.168.30.150's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.30.150'"
and check to make sure that only the key(s) you wanted were added.
-bash-4.2$ exit
登出
[root@zyy180 ~]# 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@zyy180 ~]# chown -R root.root /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
[root@zyy180 ~]# systemctl restart nginx libvirtd
[root@zyy180 ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 1 *:5900 *:*
LISTEN 0 128 *:111 *:*
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 *:8000 *:*
LISTEN 0 100 *:6080 *:*
LISTEN 0 128 :::111 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
3.添加虚拟机
3.1登录
http://192.168.30.150
A.案例1
第一次可能会出现这种情况
[root@zyy180 ~]# 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@zyy180 ~]# vim /etc/security/limits.conf
* soft nofile 655350
* hard nofile 655350
重启服务
[root@zyy180 ~]# systemctl restart nginx
再次登录
3.2.添加虚拟机
连接宿主机
点击网址
4.配置虚拟机
4.1创建存储池
路径是镜像所在目录
将镜像传送到该目录下
[root@zyy180 zyy]# ls
rhel-server-7.4-x86_64-dvd.iso
4.2添加镜像
4.3添加网络池
网络类型选BRIDGE
网络池配置完成
4.4.配置虚拟机
磁盘镜像选择刚创建的vm1
网络池选择刚创建的br0
!注意 虚拟cpu的数量要与宿主机的cpu数量一致,否则在安装时会报错
将镜像开启
(已断开)是开启状态
网页登录密码(可做可不做)
5.启动
B.案例2
[root@zyy180 zyy]# yum -y install novnc
[root@zyy180 zyy]# chmod +x /etc/rc.d/rc.local
[root@zyy180 zyy]# vim /etc/rc.d/rc.local
touch /var/lock/subsys/local
nohup novnc_server 192.168.30.150:5920 & ##添加此行
[root@zyy180 zyy]# source /etc/rc.d/rc.local
6.完成
还没有评论,来说两句吧...