KVM环境部署
一 环境准备
1.1 查看是否支持虚拟化
[root@k8s-01 yum.repos.d]# 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 dts mmx fxsr sse sse2 ss ht syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts nopl xtopology tsc_reliable nonstop_tsc aperfmperf eagerfpu pni pclmulqdq vmx ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx hypervisor lahf_lm epb ibrs ibpb stibp tpr_shadow vnmi ept vpid tsc_adjust dtherm ida arat pln pts spec_ctrl intel_stibp flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss ht syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts nopl xtopology tsc_reliable nonstop_tsc aperfmperf eagerfpu pni pclmulqdq vmx ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx hypervisor lahf_lm epb ibrs ibpb stibp tpr_shadow vnmi ept vpid tsc_adjust dtherm ida arat pln pts spec_ctrl intel_stibp
注意:intel为vmx,amd为svm。
1.2 确定宿主机相关参数
[root@k8s-01 yum.repos.d]# cat /proc/cpuinfo | grep name | cut -d: -f2 | uniq -c 2 Intel(R) Core(TM) i5-2450M CPU @ 2.50GHz [root@k8s-01 yum.repos.d]# cat /proc/cpuinfo | grep physical | sort -n | uniq -c 2 address sizes : 42 bits physical, 48 bits virtual 2 physical id : 0
[root@k8s-01 yum.repos.d]# dmidecode | grep -A16 "Memory Device" | grep Speed | sort -n | uniq -c 128 Speed: Unknown 主频
1.3 检查相关yum源
配置好公共yum源,推荐网易yum源。
1.4 关闭SELinux
[root@k8s-01 yum.repos.d]# sestatus SELinux status: disabled
二 部署KVM软件
- libvirt:操作和管理KVM虚机的虚拟化 API,使用 C 语言编写,可以由 Python,Ruby, Perl, PHP, Java 等语言调用。可以操作包括 KVM,vmware,XEN,Hyper-v, LXC 等 Hypervisor。
- virsh:基于 libvirt 的 命令行工具 (CLI)
- virt-Manager:基于 libvirt 的 GUI 工具
- virt-v2v:虚机格式迁移工具
- virt-* 工具:包括 Virt-install (创建KVM虚机的命令行工具), Virt-viewer (连接到虚机屏幕的工具),Virt-clone(虚机克隆工具),virt-top 等
- sVirt:安全工具
2.1 yum安装kvm
[root@k8s-01 yum.repos.d]# yum -y install qemu-kvm libvirt libvirt-python libguestfs-tools virt-install virt-manager [root@k8s-01 yum.repos.d]# lsmod | grep kvm kvm_intel 188688 0 kvm 636931 1 kvm_intel irqbypass 13503 1 kvm
2.2 设置libvirtd服务自启
[root@k8s-01 yum.repos.d]# systemctl enable libvirtd [root@k8s-01 yum.repos.d]# systemctl start libvirtd [root@k8s-01 yum.repos.d]# virsh -c qemu:///system list #查看虚拟机环境(virsh list --all) Id 名称 状态 ---------------------------------------------------- [root@k8s-01 yum.repos.d]#
2.3 创建虚拟机相关目录
[root@k8s-01 yum.repos.d]# mkdir -p /data/images #存放kvm虚拟机目录 [root@k8s-01 yum.repos.d]# mkdir -p /data/iso #存放ISO镜像目录
三 配置网桥
3.1 新增虚拟网桥
[root@k8s-01 yum.repos.d]# cat /etc/sysconfig/network-scripts/ifcfg-ens33 NAME=ens33 DEVICE=ens33 ONBOOT=yes BRIDGE=br0 [root@k8s-01 yum.repos.d]# cat /etc/sysconfig/network-scripts/ifcfg-br0 TYPE=Bridge BOOTPROTO=static DEVICE=br0 ONBOOT=yes DNS1=114.114.114.114 IPADDR=192.168.1.221 PREFIX=24 GATEWAY=192.168.1.1 DELAY=0 [root@k8s-01 yum.repos.d]# vi /etc/sysctl.conf # sysctl settings are defined through files in # /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/. # # Vendors settings live in /usr/lib/sysctl.d/. # To override a whole file, create a new file with the same in # /etc/sysctl.d/ and put new settings there. To override # only specific settings, add a file with a lexically later # name in /etc/sysctl.d/ and put new settings there. # # For more information, see sysctl.conf(5) and sysctl.d(5). net.ipv4.ip_forward=1 [root@k8s-01 yum.repos.d]# sysctl -p net.ipv4.ip_forward = 1 [root@k8s-01 yum.repos.d]# systemctl restart network
3.2 查看网桥
[root@k8s-01 yum.repos.d]# brctl show bridge name bridge id STP enabled interfaces br0 8000.000c292e1bfd no ens33 virbr0 8000.5254003fa4a6 yes virbr0-nic