标签:openstack
KVM虚拟化的特性
嵌入到linux正式kernel(提高了兼容性)
代码级资源调用(提高性能)
虚拟机就是一个进程(内存易于管理)
直接支持NUMA技术(提高扩展性)
虽然被RedHat收购了,但是依然保持开源发展模式,社区活跃
更好的商业支持及服务保障
CentOS7较CentOS6默认支持cpu热添加,内存热添加,大页内存默认都是开启的。
支持虚拟化的条件
Inter的cpu简称为:VMX
AMD的cpu简称为:SVM
VMware需要开启如下两个条件即可,如果是物理机,需要在bios里面设置,默认都是开启的。
通过下面的命令查看虚拟化信息:
grep -E "svm|vmx" /proc/cpuinfo
flags : fpu vme de pse tsc msr pae mce cx8apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss syscallnx rdtscp lm constant_tsc arch_perfmon pebs bts nopl xtopology tsc_reliablenonstop_tsc aperfmperf eagerfpu pni pclmulqdq vmxssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt aes xsave avx f16c rdrandhypervisor lahf_lm ida arat epb pln pts dtherm tpr_shadow vnmi ept vpidfsgsbase smep xsaveopt flags : fpu vme de pse tsc msr pae mce cx8apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss htsyscall nx rdtscp lm constant_tsc arch_perfmon pebs bts nopl xtopologytsc_reliable nonstop_tsc aperfmperf pni pclmulqdq vmxssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer xsave a* vxhypervisor lahf_lm arat epb pln pts dtherm tpr_shadow vnmi ept vpid tsc_adjust
KVM的安装实战
安装管理工具和virt命令
yum install -y qemu-kvm libvirt virt-install
Kvm:linux内核的一个模块,模块不需要安装,只需要加载 Qemu:虚拟化软件,可以虚拟不同的cpu,支持异构(x86架构可虚拟化出不是x86的架构) Qemu-kvm:用户态管理kvm,网卡,声卡,PCI设备等都是qemu来管理的
创建虚拟磁盘
qemu-img create -f raw /opt/Centos7_x86_64.raw 10G Formatting ‘/opt/Centos7_x86_64.raw‘, fmt=raw size=10737418240
创建qcow2格式的虚拟磁盘
qemu-img create -f qcow2 /opt/Centos7_x86_64.qcow2 10G Formatting ‘/opt/Centos7_x86_64.qcow2‘, fmt=qcow2 size=10737418240 encryption=off cluster_size=65536 lazy_refcounts=off refcount_bits=16
转换虚拟磁盘格式:
qemu-img convert -c -f raw -O qcow2CentOS-7-x86_64.raw CentOS-7-x86_64.qcow2
qcow2和raw的区别:
1.raw 创建多大占用多大磁盘空间
2.qcow2 占用磁盘大小根据使用量来决定 ->
[root@linux-node1 ~]# qemu-img info /opt/Centos7_x86_64.raw image: /opt/Centos7_x86_64.raw file format: raw virtual size: 10G (10737418240 bytes) disk size: 0 [root@linux-node1 ~]# qemu-img info /opt/Centos7_x86_64.qcow2 image: /opt/Centos7_x86_64.qcow2 file format: qcow2 virtual size: 10G (10737418240 bytes) disk size: 196K cluster_size: 65536 Format specific information: compat: 1.1 lazy refcounts: false refcount bits: 16 corrupt: false
显示内核中kvm的状态:
[root@linux-node1 ~]# lsmod |grep kvm kvm_intel 162153 0 kvm 525259 1 kvm_intel
启动管理工具服务:
[root@linux-node1 ~]# systemctl enable libvirtd [root@linux-node1 ~]# systemctl start libvirtd [root@linux-node1 ~]# systemctl status libvirtd
上传镜像到node1机器中
[root@linux-node1~]# cd /opt/ [root@linux-node1opt]# wget http://mirrors.aliyun.com/centos/7.2.1511/isos/x86_64/CentOS-7-x86_64-DVD-1511.iso 法二: 利用CRT的SFTP上传 CRT==> File ==> Connect SFTP Session (英文版CRT) CRT==> 文件 ==> 连接到SFTP (中文版CRT) sftp>cd /opt sftp>put D:/ISO/CentOS-7-x86_64-DVD-1511.iso # 将镜像上传到/opt下 法三: [root@linux-node1~]# cd /opt/ [root@linux-node1opt]# dd if=/dev/cdrom of=/opt/CentOS-7.1-x86_64.iso
创建虚拟机:
virt-install --virt-type kvm --name Centos7.2_mini_x64 --ram 1024 --cdrom=/tmp/CentOS-7-x86_64-Minimal-1611.iso --disk path=/opt/Centos7_x86_64.qcow2 --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole
创建完毕的检查命令:
[root@linux-node1 tmp]# virsh list --all Id Name State ---------------------------------------------------- 1 Centos7.2_mini_x64 running
生成kvm虚拟机:virt-install
查看再运行的虚拟机:virsh list
查看kvm虚拟机配置文件:virsh dumpxml name
启动kvm虚拟机:virsh start name
正常关机:virsh shutdown name
非正常关机:virsh destroy name
删除:virsh undefine name (彻底删除,无法找回,除非备份xml文件)
根据配置文件定义虚拟机:virsh define file-name.xml
挂起,终止:virsh suspend name
回复挂起状态:virsh resumed name
[root@linux-node1opt]# cd /etc/libvirt/qemu/ <- 虚拟机xml路径 -> [root@linux-node1qemu]# ll total4 -rw-------1 root root 3887 Dec 11 11:19 CentOS-7-x86_64.xml <-虚拟机xml文件-> 注:修改虚拟机xml文件不直接修改上文所述文件,要在用virsh管理命令修改 virsh# edit CentOS-7-x86_64 <- 控制台内修改 -> [root@linux-node1qemu]# virsh edit CentOS-7-x86_64 <- 控制台外修改 -> 注:热添加的CPU数不能超过最大值 [root@linux-node1qemu]# virsh edit CentOS-7-x86_64 <- 编辑配置文件 -> <vcpuplacement=‘static‘>1</vcpu> <- 修改此行内容 -> <vcpu placement=‘auto‘ current="1">4</vcpu> <- 当前为1 自动扩容 最大为4 -> [root@linux-node1~]# virsh shutdown CentOS-7-x86_64 <- 关闭虚拟机 -> DomainCentOS-7-x86_64 is being shutdown [root@linux-node1~]# virsh start CentOS-7-x86_64 <- 开启虚拟机 -> DomainCentOS-7-x86_64 started 没有热添加cpu之前先查看cpu的个数
virsh setvcpusCentOS-7-x86_64 2 --live <- 热添加CPU ->
添加完cpu后,查看cpu个数:
l 激活cpu
[root@linux-node1~]# echo "1" > /sys/devices/system/cpu/cpu1/online 高版本的kvm不需要这样做,直接可以激活 [root@linux-node1~]# cat /sys/devices/system/cpu/cpu0/online 1 [root@linux-node1~]# cat /sys/devices/system/cpu/cpu1/online 1 第二种添加cpu的方法:安装时指定 [root@linux-node1~]# virt-install –vcpus 5 –vcpus 5,maxcpus=10,cpuset=1-4,6,8 –vcpussockets=2,cores=4,thread=2
内存的设置拥有一个“气球(balloon)机制”,可以增大减少,但是也要设置一个最大值,默认并没有设置最大值,也可以在安装的时候指定
[root@linux-node1~]# virsh edit CentOS-7-x86_64 <- 编辑配置文件 -> <memoryunit=‘KiB‘>4194304</memory> <- 最大内存:4G -> <currentMemoryunit=‘KiB‘>1048576</currentMemory> <- 当前内存:1G -> [root@linux-node1~]# virsh qemu-monitor-command CentOS-7-x86_64 --hmp --cmd info balloon <- 查看当前内存 -> [root@linux-node1~]# virsh qemu-monitor-command CentOS-7-x86_64 --hmp --cmd balloon 512 <- 修改内存:512 ->
默认情况下是NAT,但是在生产环境,使用的更多的是桥接模式,更改虚拟机为网桥模式
网桥的基本原理就是创建一个桥接接口br0,在屋里网卡和虚拟网络接口之间传递数据。
[root@linux-node1~]# brctl addbr br0 <- 添加网桥br0 -> [root@linux-node1~]# brctl addif br0 eth0 <- 桥接br0和eth0 -> 注:此时会断网如果是远程操作服务器不在身边,建议写成脚本来执行 [root@linux-node1 ~]# ip addr del dev eth0192.168.56.11/24 <- 删除eth0的IP -> [root@linux-node1 ~]# ifconfig br0192.168.56.11/24 up <- 给br0设置IP -> [root@linux-node1 ~]# route add default gw192.168.56.2 <- 给br0设置网关 -> 为了避免断网,直接写成脚本即可: #!/bin/bash brctl addbr br0 brctl addif br0 eth0 ip addr del dev eth0192.168.56.11/24 ifconfig br0192.168.56.11/24 up route add default gw192.168.56.2 [root@linux-node1 ~]#virsh edit CentOS-7-x86_64 <- 修改虚拟机配置 -> 将: <interfacetype=‘network‘ <- 虚拟机网络连接方式-> <mac address=‘52:54:00:0b:f8:a5‘/> <source network=‘default‘/> <- 当前主机网络名称 -> 修改为: <interfacetype=‘bridge‘> <mac address=‘52:54:00:0b:f8:a5‘/> <source bridge=‘br0‘/> [root@linux-node1 ~]#virsh shutdown CentOS-7-x86_64 <- 关闭虚拟机 -> Domain CentOS-7-x86_64 isbeing shutdown [root@linux-node1 ~]#virsh start CentOS-7-x86_64 <- 启动虚拟机 -> Domain CentOS-7-x86_64started [root@linux-node1~]# ifconfig <- 查看网卡信息 -> br0:flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.56.11 netmask 255.255.255.0 broadcast 192.168.56.255 inet6 fe80::20c:29ff:fefe:1620 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:fe:16:20 txqueuelen 0 (Ethernet) RX packets 74 bytes 8909 (8.7 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 63 bytes 11462 (11.1 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 eth0:flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet6 fe80::20c:29ff:fefe:1620 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:fe:16:20 txqueuelen 1000 (Ethernet) RX packets 26401 bytes 26066467 (24.8 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 8582 bytes 1939240 (1.8 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo:flags=73<UP,LOOPBACK,RUNNING> mtu65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 0 (Local Loopback) RX packets 4 bytes 216 (216.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 4 bytes 216 (216.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 virbr0:flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255 ether 00:00:00:00:00:00 txqueuelen 0 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 2 bytes 84 (84.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 vnet0:flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 ether fe:54:00:0b:f8:a5 txqueuelen 500 (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
本文出自 “圈中一鸟” 博客,请务必保留此出处http://sgk2011.blog.51cto.com/1551358/1886277
Openstack 实战讲解之-----------02-控制节点kvm虚拟化配置
标签:openstack
原文地址:http://sgk2011.blog.51cto.com/1551358/1886277