云计算概述:
什么是云计算:资源使用和交付模式。
虚拟化:一种具体的技术,用来将物理机虚拟成为多个相互独立的虚拟机。
云计算不等于虚拟化。使用了虚拟化的技术做支撑。
IAAS 基础设施即服务
PAAS
SAAS
私有云 公有云 混合云
KVM。 服务器虚拟化 桌面虚拟化 应用虚拟化
硬件虚拟化。Intel vt-x/EPT AMD AMD-v /RVI
libvirt libvrit API库 + libvirtd datemon + 用户工具(virsh)
xen kvm esxi qemu virtualbox
virsh shutdown start undefine list edit
libvrit xml
#####################################################################################################################################################################33
[[email protected] ~]# cat /etc/redhat-release
CentOS Linux release 7.1.1503 (Core)
[[email protected] ~]# grep -E ‘(vmx|svm)‘ /proc/cpuinfo ----查看是否支持虚拟化
[[email protected] ~]# yum install qemu-kvm qemu-kvm-tools virt-manager libvirt virt-install -y
qemu-kvm:用户态的程序与kvm进行交互
qemu-kvm-tools:用户态的程序与kvm进行交互的工具
virt-manager:虚拟化管理的工具
libvirt:
virt-install:
创建一个10G的硬盘:
[[email protected] ~]# qemu-img create -f raw /opt/CentOS-7.1-x86_64.raw 10G
Formatting ‘/opt/CentOS-7.1-x86_64.raw‘, fmt=raw size=10737418240
查看是否有kvm:
[[email protected] ~]# lsmod |grep kvm
kvm_intel 148081 0
kvm 461126 1 kvm_intel
启动libvirtd并设置开机自动加载并查看状态:
[[email protected] ~]# systemctl enable libvirtd.service
[[email protected] ~]# systemctl start libvirtd.service
[[email protected] ~]# systemctl status libvirtd.service
将光盘挂载上:
[[email protected] ~]# mount /dev/cdrom /mnt
mount: /dev/sr0 is write-protected, mounting read-only
使用dd命令做一个ISO镜像:
[[email protected] ~]# dd if=/dev/cdrom of=/opt/CentOS-7.1.iso
8419328+0 records in
8419328+0 records out
4310695936 bytes (4.3 GB) copied, 285.28 s, 15.1 MB/s
创建一台kvm虚拟机:(首先保证有vnc客户端)
[[email protected] ~]# virt-install --name CentOS-7.1-x86_64 --virt-type kvm --ram 1024 --cdrom=/opt/CentOS-7.1.iso --disk path=/opt/CentOS-7.1-x86_64.raw --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole
Starting install...
Creating domain... | 0 B 00:00:03
Domain installation still in progress. You can reconnect to
the console to complete the installation process.
打开VNC客户端:10.0.0.111:5900
tab键:指定内核参数 net.ifnames=0 biosdevname=0 装出来的系统就是eth0
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[[email protected] qemu]# virsh list ----查看虚拟机是否运行
Id Name State
----------------------------------------------------
2 CentOS-7.1-x86_64 running
[[email protected] qemu]# virsh list -all ----查看所有虚拟机是否运行
Id Name State
----------------------------------------------------
2 CentOS-7.1-x86_64 running
[[email protected] qemu]# virsh edit CentOS-7.1-x86_64 -----编辑虚拟机,通过xml
[[email protected] qemu]# virsh start CentOS-7.1-x86_64 -----启动虚拟机
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
管理kvm虚拟机:
[[email protected] qemu]# virsh edit CentOS-7.1-x86_64
<vcpu placement=‘auto‘ current="1">4</vcpu> 将cpu设置为自动调整,最低一个,最大四个
改完之后重启才生效
[[email protected] qemu]# virsh shutdown CentOS-7.1-x86_64
[[email protected] qemu]# virsh start CentOS-7.1-x86_64
设置成2个cpu
[[email protected] qemu]# virsh setvcpus CentOS-7.1-x86_64 2 --live
设置成3个cpu
[[email protected] qemu]# virsh setvcpus CentOS-7.1-x86_64 3 --live
总结:只支持热添加,不支持热删除
回虚拟机查看:
[[email protected] qemu]cat /proc/cpuinfo ----就可以看到cpu有4核了
########设置内存的热添加与热减少########
[[email protected] qemu]# virsh edit CentOS-7.1-x86_64
<memory unit=‘KiB‘>4048576</memory>
<currentMemory unit=‘KiB‘>1048576</currentMemory>
改完之后重启才生效
[[email protected] qemu]# virsh shutdown CentOS-7.1-x86_64
[[email protected] qemu]# virsh start CentOS-7.1-x86_64
更改之后回虚拟机查看,内存从1G变成了721M
KVM之内存管理的气球模式:
[[email protected] ~]# virsh qemu-monitor-command CentOS-7.1-x86_64 --hmp --cmd info balloon
balloon: actual=1024
[[email protected] ~]# virsh qemu-monitor-command CentOS-7.1-x86_64 --hmp --cmd balloon 600
回虚拟机查看,内存从721变成了297M
[[email protected] ~]# virsh qemu-monitor-command CentOS-7.1-x86_64 --hmp --cmd balloon 2000
回虚拟机查看,内存从297变成了1697M
################网络##################
添加一个网桥:
[[email protected] opt]# brctl addbr br0
桥接网络:
[[email protected] ~]# brctl addif br0 eth0 && ip addr del dev eth0 10.0.0.111/24 && ifconfig br0 10.0.0.111/24 up && routeadd default gw 10.0.0.2 && iptables -F
[[email protected] ~]# virsh edit CentOS-7.1-x86_64
<interface type=‘bridge‘>
<mac address=‘52:54:00:f3:42:55‘/>
<source bridge=‘br0‘/>
此处修改后kvm虚拟机里面的网桥就会变成和宿主机一个网段的,并且可以用ssh客户端登录
如果IP地址出现的全部是mac地址,则需要把IP地址设置成静态的,并设置DNS,然后才能登录和ping通外网
#############硬盘############
全镜像模式 稀疏模式
raw qcow2
[[email protected] ~]# qemu-img info /opt/CentOS-7.1-x86_64.raw
image: /opt/CentOS-7.1-x86_64.raw
file format: raw
virtual size: 10G (10737418240 bytes)
disk size: 1.3G
#I/O优化 CDN公司必问
Virtio virio-net virtio-blk 半虚拟化
#scheudler
[[email protected] ~]# dmesg | grep -i scheduler
[ 0.775193] io scheduler noop registered
[ 0.775195] io scheduler deadline registered (default)
[ 0.775396] io scheduler cfq registered
[[email protected] ~]# cat /sys/block/sda/queue/scheduler
noop [deadline] cfq
[[email protected] ~]# echo cfq > /sys/block/sda/queue/scheduler
[[email protected] ~]# cat /sys/block/sda/queue/scheduler
noop deadline [cfq]
永久修改 加入内核参数 elevator=noop