雲計算概述和KVM虛擬化

前言:

近些年一直聽着 虛擬化、雲計算、公有云、私有云、混合雲這些個概念,一直想着....這些概念要用什麼技術實現?html

 

1、雲計算的概念

1.傳統IDC機房面都會臨什麼問題?node

 

任何新事物都是由需求催生的,以前網站上線基本上會有如下幾種選擇:python

 

IDC託管:買臺服務託管到IDC機房---》安裝系統----》部署應用---》域名備案------》上線linux

IDC租用:租用IDC機房提供的服務器算法

VPS:虛擬專用主機vim

 

以上傳統的網站上線的模式有什麼重大缺陷呢?centos

A.成本高 (IDC託管、租用費用費用很高)api

B.自動化能力差 事多、麻煩(買了/租了服務器還要裝系統、部署代碼,是否會被網絡攻擊)安全

 

 

2.雲計算是什麼?性能優化

把總多服務器的資源經過虛擬化的技術手段熔化成一個資源池 (雲)

用戶以網絡的方式 按需獲取資源池中的服務器資源,服務商按用戶使用量公平收費;  (計算)

是一種資源整合、按需消費、按需收費的商業模式;  讓企業使用服務器資源就像 居民平時用水、用電同樣簡單;

 

3.雲計算的層次

根據雲提供的服務分爲如下幾個層次

Infrastructure as a service        iaas
Platforn as a service              paas
Software as a service              saas

 PS:也許你會據說過vaas(Video-as-a-service) 是創建在這3層之上的典型表明有樂視雲;

 

4.雲計算的分類

根據雲面向的人羣分爲如下幾類:

公有云:面向公衆提供虛擬機、開發環境、服務(阿里雲)       優勢:技術安全可靠      缺陷:數據不安全

私有用:僅對一部分人/本身/公司內部員工使用(阿根雲)    缺陷:技術問題本身解決    優勢:數據安全

混合雲:既使用公有云  也有本身的私有云(咱們公司的)       (綜合私有云、公有云的優缺點)

 

 

2、虛擬化的概念

1.什麼是虛擬化?

首先聲 明    雲計算 != 虛擬化,雲計算的實現使用到了虛擬化這種技術手段;

虛擬化是指經過軟件技術 把1個計算機的CPU、硬件 虛擬出不少虛擬機,這些虛擬機寄生在宿主機之上,功能和宿主機的功能無異, 可是彼此之間是隔離的;

 

 

2.虛擬化類別

 

3、KVM

KVM:須要硬件支持的虛擬化技術,從硬件層面實現隔離;(openstack默認使用的虛擬化技術就是KVM)

QEMU:QEMU和KVM同樣是一種虛擬化軟件;

QUEMU和KVM的區別:因爲KVM是內核級虛擬化軟件(能虛擬化出CPU和內存),磁盤、網卡虛擬不出來,因而KVM就是結合了QEMU實現了用戶態的虛擬化;

 

1.虛擬環境準備

[root@localhost ~]# grep -E '(vmx|svm)' /proc/cpuinfo 
flags        : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch arat tpr_shadow vnmi ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid mpx rdseed adx smap clflushopt xsaveopt xsavec
查看是否支持虛擬化
[root@localhost ~]# yum list|grep kvm 
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
libvirt-daemon-kvm.x86_64               2.0.0-10.el7                   @anaconda
qemu-kvm.x86_64                         10:1.5.3-126.el7               @anaconda
qemu-kvm-common.x86_64                  10:1.5.3-126.el7               @anaconda
libvirt-daemon-kvm.x86_64               3.2.0-14.el7_4.5               updates  
oci-kvm-hook.x86_64                     0.2-2.el7                      epel     
pcp-pmda-kvm.x86_64                     3.11.8-7.el7                   base     
qemu-kvm.x86_64                         10:1.5.3-141.el7_4.4           updates  
qemu-kvm-common.x86_64                  10:1.5.3-141.el7_4.4           updates  
qemu-kvm-tools.x86_64                   10:1.5.3-141.el7_4.4           updates  
yum list|grep kvm 查看能夠安裝得KVM包

 

2.安裝KVM

[root@localhost ~]# yum -y install qemu-kvm qemu-kvm-tools libvirt virt-install

 

3.啓動librbd服務

libvirtd是管理KVM虛擬機的工具

[root@localhost ~]# yum update librbd1
[root@localhost ~]# systemctl start libvirtd
[root@localhost ~]# systemctl enable libvirtd

 

 當libvertd服務啓動以後

系統會自動建立一個名爲virbr0的橋接網卡,ip地址默認爲192.168.122.1;

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:31:bf:a8  txqueuelen 1000  (Ethernet)
        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
ifconfig 查看新建立的橋接網卡

系統 自動啓動dnsmasq進程該進程有DNS和DHCP的功能,給虛擬機動態分配IP地址並提供域名解析服務;

[root@localhost zhanggen]# ps -aux | grep dnsmasq 
nobody     1433  0.0  0.0  49532  1072 ?        S    23:12   0:00 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshelper
root       1436  0.0  0.0  49504   348 ?        S    23:12   0:00 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshelper
root       3727  0.0  0.0 112664   972 pts/0    R+   23:19   0:00 grep --color=auto dnsmasq
[root@localhost zhanggen]# 
ps -aux | grep dnsmasq

 

4.建立虛擬機

使用qemu-img給KVM虛擬機建立虛擬鏡像文件

[root@localhost kvm_img]# qemu-img create -f raw /kvm_img/Centos-7-x86.raw 10G
Formatting '/kvm_img/Centos-7-x86.raw', fmt=raw size=10737418240 

 新建了虛擬鏡像文件就能夠安裝虛擬機了

virt-install --virt-type kvm --name Centos-7-x86_64 --ram 2048 --cdrom=/centos7iso/CentOS-7-x86_64-DVD-1611.iso --disk path=/kvm_img/Centos-7-x86.raw --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole

 而後使用TightVNC鏈接到服務器上給kvm虛擬機安裝系統

 

在宿主機查看當前KVM實例列表

[root@localhost centos7iso]# virsh list --all
 Id    名稱                         狀態
----------------------------------------------------
 1     Centos-7-x86_64                running

經過宿主機啓動1個 kvm實例

[root@localhost centos7iso]# virsh start Centos-7-x86_64 
域 Centos-7-x86_64 已開始

kvm虛擬機啓動以後,其實就是宿主機中的一個進程

[root@localhost zhanggen]# ps -ef | grep kvm
root        482      2  0 3月15 ?       00:00:00 [kvm-irqfd-clean]
qemu       8000      1  7 01:27 ?        00:01:53 /usr/libexec/qemu-kvm -name Centos-7-x86_64 -S -machine pc-i440fx-rhel7.0.0,accel=kvm,usb=off,dump-guest-core=off -cpu Broadwell,-hle,-rtm -m 2048 -realtime mlock=off -smp 1,sockets=1,cores=1,threads=1 -uuid fc152f09-9b09-4890-a316-3c2a5afb0e68 -no-user-config -nodefaults -chardev socket,id=charmonitor,path=/var/lib/libvirt/qemu/domain-2-Centos-7-x86_64/monitor.sock,server,nowait -mon chardev=charmonitor,id=monitor,mode=control -rtc base=utc,driftfix=slew -global kvm-pit.lost_tick_policy=delay -no-hpet -no-shutdown -global PIIX4_PM.disable_s3=1 -global PIIX4_PM.disable_s4=1 -boot strict=on -device ich9-usb-ehci1,id=usb,bus=pci.0,addr=0x4.0x7 -device ich9-usb-uhci1,masterbus=usb.0,firstport=0,bus=pci.0,multifunction=on,addr=0x4 -device ich9-usb-uhci2,masterbus=usb.0,firstport=2,bus=pci.0,addr=0x4.0x1 -device ich9-usb-uhci3,masterbus=usb.0,firstport=4,bus=pci.0,addr=0x4.0x2 -device virtio-serial-pci,id=virtio-serial0,bus=pci.0,addr=0x5 -drive file=/kvm_img/Centos-7-x86.raw,format=raw,if=none,id=drive-virtio-disk0 -device virtio-blk-pci,scsi=off,bus=pci.0,addr=0x6,drive=drive-virtio-disk0,id=virtio-disk0,bootindex=1 -drive if=none,id=drive-ide0-0-0,readonly=on -device ide-cd,bus=ide.0,unit=0,drive=drive-ide0-0-0,id=ide0-0-0 -netdev tap,fd=26,id=hostnet0,vhost=on,vhostfd=28 -device virtio-net-pci,netdev=hostnet0,id=net0,mac=52:54:00:ca:24:4f,bus=pci.0,addr=0x3 -chardev pty,id=charserial0 -device isa-serial,chardev=charserial0,id=serial0 -chardev socket,id=charchannel0,path=/var/lib/libvirt/qemu/channel/target/domain-2-Centos-7-x86_64/org.qemu.guest_agent.0,server,nowait -device virtserialport,bus=virtio-serial0.0,nr=1,chardev=charchannel0,id=channel0,name=org.qemu.guest_agent.0 -device usb-tablet,id=input0,bus=usb.0,port=1 -vnc 0.0.0.0:0 -vga cirrus -device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x7 -msg timestamp=on
ps -ef | grep kvm

 

5.管理KVM虛擬機

在宿主機裏面建立好了kvm實例,接下來就是對它們進行管理了,Red Hat引入了libvirt庫做爲管理平臺虛擬化(hypervisor和VM)的一個API,openstack就是調用libvirt的API來管理KVM虛擬機的,

virsh是調用libvirt的客戶端。

看到這裏我有點興奮,其實我用Python去調用libvert的API,也能夠寫一套相似openstack的虛擬機管理工具;

PS:openstack掛了 kvm虛擬機也會掛嗎?你的老闆掛了你會掛嗎?

 

 

5.1.KVM虛擬機是經過什麼定義的?

[root@localhost qemu]# cat Centos-7-x86_64.xml 
<!--
WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:
  virsh edit Centos-7-x86_64
or other application using the libvirt API.
-->

<domain type='kvm'>
  <name>Centos-7-x86_64</name>
  <uuid>fc152f09-9b09-4890-a316-3c2a5afb0e68</uuid>
  <memory unit='KiB'>2097152</memory>
  <currentMemory unit='KiB'>2097152</currentMemory>
  <vcpu placement='static'>1</vcpu>
  <os>
    <type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
    <boot dev='hd'/>
  </os>
  <features>
    <acpi/>
    <apic/>
  </features>
  <cpu mode='custom' match='exact' check='partial'>
    <model fallback='allow'>Broadwell-noTSX</model>
  </cpu>
  <clock offset='utc'>
    <timer name='rtc' tickpolicy='catchup'/>
    <timer name='pit' tickpolicy='delay'/>
    <timer name='hpet' present='no'/>
  </clock>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>destroy</on_crash>
  <pm>
    <suspend-to-mem enabled='no'/>
    <suspend-to-disk enabled='no'/>
  </pm>
  <devices>
    <emulator>/usr/libexec/qemu-kvm</emulator>
    <disk type='file' device='disk'>
      <driver name='qemu' type='raw'/>
      <source file='/kvm_img/Centos-7-x86.raw'/>
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
    </disk>
    <disk type='file' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <target dev='hda' bus='ide'/>
      <readonly/>
      <address type='drive' controller='0' bus='0' target='0' unit='0'/>
    </disk>
    <controller type='usb' index='0' model='ich9-ehci1'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x7'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci1'>
      <master startport='0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0' multifunction='on'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci2'>
      <master startport='2'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x1'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci3'>
      <master startport='4'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x2'/>
    </controller>
    <controller type='pci' index='0' model='pci-root'/>
    <controller type='ide' index='0'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
    </controller>
    <controller type='virtio-serial' index='0'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
    </controller>
    <interface type='network'>
      <mac address='52:54:00:ca:24:4f'/>
      <source network='default'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    </interface>
    <serial type='pty'>
      <target type='isa-serial' port='0'>
        <model name='isa-serial'/>
      </target>
    </serial>
    <console type='pty'>
      <target type='serial' port='0'/>
    </console>
    <channel type='unix'>
      <target type='virtio' name='org.qemu.guest_agent.0'/>
      <address type='virtio-serial' controller='0' bus='0' port='1'/>
    </channel>
    <input type='tablet' bus='usb'>
      <address type='usb' bus='0' port='1'/>
    </input>
    <input type='mouse' bus='ps2'/>
    <input type='keyboard' bus='ps2'/>
    <graphics type='vnc' port='-1' autoport='yes' listen='0.0.0.0'>
      <listen type='address' address='0.0.0.0'/>
    </graphics>
    <video>
      <model type='cirrus' vram='16384' heads='1' primary='yes'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
    </video>
    <memballoon model='virtio'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
    </memballoon>
  </devices>
</domain>
/etc/libvirt/qemu
全部的虛擬機都是由這個目錄下的 xml文件定義的 其中包含:CPU、內存、硬盤...參數,對它的修改也就是對KVM虛擬機的動態調整

 

5.2.virsh管理命令

如上圖所示 vish 是虛擬機管理服務(libvert)的客戶端,那麼如何使用virsh來對KVM虛擬機進行管理呢?

virsh list        #查看虛擬機列表

 qemu-img convert -f raw -O qcow2 /var/lib/libvirt/images/xuegod63-kvm2.img /var/lib/libvirt/images/xuegod63-kvm2.qcow2 #轉換格式(將raw格式轉換爲qcow2格式):

virsh dumpxml Centos-7-x86_64 > back.xml  #備份虛擬機定義文件
virsh defined back.xml #經過備份的.xml文件還原虛擬機
virsh reboot Centos-7-x86_64             #重啓虛擬機
virsh shutdown Centos-7-x86_64 #關閉虛擬機
virsh start Centos-7-x86_64 #開啓虛擬機
virsh suspend Centos-7-x86_64            #掛起虛擬機
virsh resume Centos-7-x86_64             #恢復虛擬機
virsh snapshot-list Centos-7-x86_64            #查看本虛擬機的快照
virsh snapshot-create-as Centos-7-x86_64 mysnapshot   #建立快照
virsh snapshot-revert Centos-7-x86_64 mysnapshot #恢復快照
virsh snapshot-delete Centos-7-x86_64 mysnapshot #刪除快照

 

5.3.virsh edit命令

該命名能夠對 /etc/libvirt/qemu下的xml虛擬機定義文件進行修改,進而對虛擬機的硬件參數進行在線調整;

 

調整CPU個數

 <vcpu placement='auto' current='1'>4</vcpu>
virsh edit Centos-7-x86_64

virsh setvcpus Centos-7-x86_64 2 --live #在線修改成2個CPU

 

調整內存大小

virsh qemu-monitor-command Centos-7-x86_64 --hmp --cmd balloon 1024     #設置內存大小

  virsh qemu-monitor-command Centos-7-x86_64 --hmp --cmd info balloon     #查看內存大小

 

硬盤相關

QUME使用的鏡像文件分爲:qcow2和raw格式

qcow2格式:支持快照功能、性能慢、動態佔用

raw格式:不支持快照功能、性能快、指定多多空間佔用多大,能夠轉換成其餘格式

qemu-img info Centos-7-x86.raw                       #查看KVM鏡像文件格式
qemu-img convert -f raw -O qcow2 Centos-7-x86.raw test.qcow2 #轉換鏡像文件的格式

 

KVM網絡架構調整(把全部KVM虛假 橋接到虛擬網橋br0上)

KVM虛假默認是經過NAT轉換的方式上網的,可是NAT轉換有網絡平靜,全部我可在宿主機上建立 一些虛擬網橋,讓一堆堆的虛擬機橋接到宿主機的各個網橋上,達到網絡負載均衡的效果;

brctl show       #查看網橋:經過virbr0網橋作NAT IP地址轉換 分配給虛擬機

 

stepA:宿主機新增虛擬網橋(以避免增長網橋的操做過程當中宿主機網絡中斷,因此必定經過腳本的形式進行操做。) 

#!/usr/bin/bash
ip addr del dev ens33  192.168.226.151/24    #剝奪、刪除ens33的IP地址
brctl addbr br0                              #添加虛擬機網橋 br0
ifconfig br0 192.168.226.151/24 up         #給網橋設置IP地址
brctl addif br0 ens33                      #將br0橋接到ens22物理網卡
route add default gw 192.168.226.2         #添加網關出口
宿主機新增虛擬網橋腳本

 

stepB:設置KVM.xml 讓虛擬機鏈接到宿主機新增的網橋上

<interface type='bridge'>
      <mac address='52:54:00:ca:24:4f'/>
      <source bridge='br0'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    </interface>
修改KVM定義文件(/etc/libvirt/qemu/Centos-7-x86_64.xml )

 

stepC:KVM虛擬機橋接到宿主機以後,重啓KVM(重啓不能用reboot不然不生效!), 而後登錄KVM給KVM配置靜態IP、網關、DNS

KVM網卡配置 (/etc/sysconfig/network-scripts/ifcfg-eth0)

BOOTPROTO=static
ONBOOT=yes
IPADDR=192.168.226.18
NETMASK=255.255.255.0
GATEWAY=192.168.226.2

KVMDNS配置
/etc/resolv.conf 
nameserver 192.168.226.2
KVM配置靜態IP地址

 

 

6.KVM性能優化

6.1:減小CachMiss

每一個kvm虛擬機都對應宿主機中的一個進程,爲了減小CachMiss,能夠把某些運行重要業務的kvm綁定到一顆CPU上;

[root@localhost zhanggen]# taskset -cp 0 8000        #0:CPU 800:進程ID
pid 8000's current affinity list: 0
pid 8000's new affinity list: 0

 

6.2.使用大頁內存,減小內存碎片,加速內存尋址(默認已開啓);

[root@localhost zhanggen]# cat /sys/kernel/mm/transparent_hugepage/enabled 
[always] madvise never

 

6.3.Linux的IO調度算法

 

4、python3調用libvirtd API

若是你想經過python3調用宿主機的libvirtd API 管理KVM虛擬機,必需要   pip 安裝libvirt-python模塊 而後在libvirtd 宿主機端開啓API 端口,就能夠了;

 

1.libvert-python模塊安裝

yum install -y libvirt-devel
yum install python-devel
pip3 install psutil
pip3 install ioctl_opt
下載python-libvirt包#https://pypi.org/project/libvirt-python/3.7.0/

python setup.py build
python setup.py install


效果
[root@cmdb libvirt-python-3.7.0]# python
Python 3.6.1 (default, Feb 22 2019, 15:18:59) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import libvirt
>>> 
python3安裝libvirt模塊

 

2.設置LIBVIRTD_PATH環境變量

export LIBVIRTD_PATH=/usr/sbin/
/etc/profile

 

以上是python端的準備工做,若是python想要鏈接到KVM宿主機的libverd API  就須要配置 libvirtd的開放端口;

 

3.配置libvirtd API 端口

LIBVIRTD_CONFIG=/etc/libvirt/libvirtd.conf
LIBVIRTD_ARGS="--listen"
vim /etc/sysconfig/libvirtd
listen_tls = 0
 
listen_tcp = 1
 
tcp_port = "16509"
 
listen_addr = "0.0.0.0"
 
auth_tcp = "none"
vim /etc/libvirt/libvirtd.conf
LISTEN      0      128             *:16509                       *:*  

  

四、代碼

用了半天時間 搭建libvirtd API 環境 、pip libvert-python模塊 ,要運維幹嗎?............終於能夠用python連進來寫會兒代碼了。

[root@localhost libvirt]# python
Python 2.7.5 (default, Nov  6 2016, 00:28:07) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import libvirt
>>> conn = libvirt.open("qemu+tcp://192.168.226.151/system")
>>> 

 

 

參考:

python3安裝libvirt模塊

python的libvirt模塊使用

https://www.cnblogs.com/psv-fuyang/articles/8598240.html

配置16509鏈接端口

相關文章
相關標籤/搜索