虚拟化kvm部署

[TOC]

172.17.0.192

环境初始化

修改防火墙规则

vim /etc/sysconfig/selinux 
......
SELINUX=disabled

临时生效

setenforce 0
setenforce: SELinux is disabled

查看状态

getenforce 

Disabled

直接执行这行

/usr/sbin/setsebool httpd_can_network_connect true

开启转发

sysctl -w net.ipv4.ip_forward=1
sysctl -p

一 部署kvm环境

1.1修改yum源

yum install wget -y
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum -y install epel-release
yum clean all
yum makecache

yum install net-tools vim lrzsz -y

1.2 安装kvm软件

yum install bridge-utils qemu-kvm libvirt libvirt-python libguestfs-tools virt-install virt-manager python-virtinst libvirt-client virt-viewer -y

1.3配置桥接网络,(备注:br0绑定eth1)

cd /etc/sysconfig/network-scripts/
cat ifcfg-eth1
TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
NAME=eth1
DEVICE=eth1
ONBOOT=yes
IPADDR=192.168.56.50
PREFIX=24
GATEWAY=192.168.56.2
DNS1=192.168.56.2
BRIDGE=br0 
NM_CONTROLLED=no
cat ifcfg-br0
TYPE=Bridge
DEVICE=br0
NM_CONTROLLED=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
NAME=eth1
ONBOOT=yes
IPADDR=192.168.56.50
PREFIX=24
GATEWAY=192.168.56.2
DNS1=192.168.56.2

1.4 创建cdrom系统镜像目录 disk数据目录 (把ios镜像放入到cdrom目录)

mkdir -p /data1/kvm/{cdrom,disk}

1.5 网卡配置

打开网卡

brctl stp br0 on

1.6重启网络服务

systemctl restart network

1.7 启动libvirt

systemctl restart libvirtd && systemctl status libvirtd

1.8 测试

virsh -c qemu:///system list

virsh --version

virt-install --version

ln -s /usr/libexec/qemu-kvm /usr/bin/qemu-kvm
lsmod |grep kvm

1.9 查看网桥

brctl show

1.10 创建虚拟机

virt-install --virt-type kvm \
--os-type=linux \
--os-variant rhel7 \
--name centos7 \
--memory 1024 \
--vcpus 1 \
--disk /data1/kvm/disk/centos7.raw,format=raw,size=10 \
--cdrom /data1/kvm/cdrom/CentOS-7-x86_64-DVD-1908.iso \
--network network=default \
--graphics vnc,listen=0.0.0.0 \
--noautoconsole

1.11 查看创建虚拟机

virsh list

至此虚拟化结束