自学内容网 自学内容网

Linux kvm环境搭建

1.1 安装KVM虚拟机

#系统是否支持KVM虚拟化
[root@jztserver01 ~]# cat /proc/cpuinfo | egrep 'vmx|svm'
[root@jztserver01 ~]# egrep '(vmx|svm)' /proc/cpuinfo |wc -l
#关闭selinux;设置selinux立即生效
[root@jztserver01 ~]# vi /etc/sysconfig/selinux
[root@jztserver01 ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
[root@jztserver01 ~]# setenforce 0
#配置KVM虚拟机; 需关闭|禁用防火墙
[root@jztserver01 ~]# systemctl stop firewalld
[root@jztserver01 ~]# systemctl disable firewalld
#KVM使用的是桥接的方式上网, 打开数据转发功能
[root@jztserver01 ~]# sed -i '/net.ipv4.ip_forward/s/0/1/' /etc/sysctl.conf
#网络数据转发生效
[root@jztserver01 ~]# sysctl -p
#切换目录; 备份镜像源, 下载阿里云镜像
[root@jztserver01 ~]# cd /etc/yum.repos.d/
[root@jztserver01 yum.repos.d]# mv CentOS-Base.repo  CentOS-Base.repo.backup
[root@jztserver01 yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
#清空并生成缓存
[root@jztserver01 yum.repos.d]# yum clean all
[root@jztserver01 yum.repos.d]# yum makecache
[root@jztserver01 yum.repos.d]# yum update
#安装KVM所有需要的依赖包
[root@jztserver01 yum.repos.d]# yum -y install kvm python-virtinst libvirt tunctl bridge-utils virt-manager qemu-kvm-tools virt-viewer virt-v2v virt-install libguestfs-tools
#执行[重启|状态|开机]启动
[root@jztserver01 yum.repos.d]# systemctl restart libvirtd
[root@jztserver01 yum.repos.d]# systemctl status libvirtd
[root@jztserver01 yum.repos.d]# systemctl enable libvirtd
#查看虚机列表
[root@jztserver01 yum.repos.d]# virsh list
#查看KVM使用的模块
[root@jztserver01 yum.repos.d]# lsmod |grep kvm

1.2 配置网络

#切换网络配置目录
[root@jztserver01 /]# cd /etc/sysconfig/network-scripts/
#复制网卡1(物理网口做的交换机)信息桥接网络
[root@jztserver01 network-scripts]# cp ifcfg-enp2s0f0 ifcfg-br1
#配置桥接网络1,上述中KVM采用桥接网络模式
[root@jztserver01 network-scripts]# vi ifcfg-br1
#桥接网络类型为Bridge
TYPE=Bridge
PROXY_METHOD=none
BROWSER_ONLY=no
NM_CONTROLLED=no
#网络驱动为static
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
IPV6_PRIVACY=no
NAME=br1
DEVICE=br1
ONBOOT=yes

#配置桥接网络
IPADDR=xxx.xxx.xxx.xxxx
GATEWAY=192.168.0.1
NETMASK=255.255.255.0
DNS1=xxx.xxx.xxx.xxxx
DNS2=114.114.114.114

#配置网卡1信息如下
[root@jztserver01 network-scripts]# vi ifcfg-enp2s0f0
#网络类型为Ethernet
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
NM_CONTROLLED=no
#网络驱动为dhcp
BOOTPROTO=dhcp
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
IPV6_PRIVACY=no
NAME=enp2s0f0
DEVICE=enp2s0f0
ONBOOT=yes
#网卡UUID标识
UUID=78befe25-7a8c-4b2f-a947-b64d773b6c0d
HWADDR=f0:2f:a7:3b:ca:13
#指向桥接网络名
BRIDGE=br1

#重启网络服务
[root@jztserver01 network-scripts]# systemctl restart network
#查看网络情况
[root@jztserver01 network-scripts]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: enp2s0f0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq master br1 state UP group default qlen 1000
    link/ether f0:2f:a7:3b:ca:13 brd ff:ff:ff:ff:ff:ff
    inet6 fe80::f22f:a7ff:fe3b:ca13/64 scope link 
       valid_lft forever preferred_lft forever
3: enp2s0f1: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq state DOWN group default qlen 1000
    link/ether f0:2f:a7:3b:ca:14 brd ff:ff:ff:ff:ff:ff
4: br1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether f0:2f:a7:3b:ca:13 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.240/23 brd 192.168.1.255 scope global br1
       valid_lft forever preferred_lft forever
    inet6 fe80::f22f:a7ff:fe3b:ca13/64 scope link 
       valid_lft forever preferred_lft forever
5: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether 52:54:00:26:5d:d9 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
6: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN group default qlen 1000
    link/ether 52:54:00:26:5d:d9 brd ff:ff:ff:ff:ff:ff
7: vnet0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master br1 state UNKNOWN group default qlen 1000
    link/ether fe:54:00:d0:11:ce brd ff:ff:ff:ff:ff:ff
    inet6 fe80::fc54:ff:fed0:11ce/64 scope link 
       valid_lft forever preferred_lft forever
8: vnet1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master br1 state UNKNOWN group default qlen 1000
    link/ether fe:54:00:67:95:6e brd ff:ff:ff:ff:ff:ff
    inet6 fe80::fc54:ff:fe67:956e/64 scope link 
       valid_lft forever preferred_lft forever
9: vnet2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master br1 state UNKNOWN group default qlen 1000
    link/ether fe:54:00:04:36:5a brd ff:ff:ff:ff:ff:ff
    inet6 fe80::fc54:ff:fe04:365a/64 scope link 
       valid_lft forever preferred_lft forever
10: vnet3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master br1 state UNKNOWN group default qlen 1000
    link/ether fe:54:00:41:50:cf brd ff:ff:ff:ff:ff:ff
    inet6 fe80::fc54:ff:fe41:50cf/64 scope link 
       valid_lft forever preferred_lft forever

#查看网桥状态
[root@jztserver01 network-scripts]# brctl show
bridge name     bridge id               STP enabled     interfaces
br1             8000.f02fa73bca13       no              enp2s0f0                #配置的桥接网卡
virbr0          8000.525400265dd9       yes             virbr0-nic               #NAT模式的网卡
#切换目录
[root@jztserver01 network-scripts]# cd /mnt/sdb1
[root@jztserver01 sdb1]# mkdir -p centos-kvm/iso
[root@jztserver01 sdb1]# cd centos-kvm/iso/
#下载阿里云centos7.9 iso镜像
[root@jztserver01 sdb1]# wget https://mirrors.aliyun.com/centos/7.9.2009/isos/x86_64/CentOS-7-x86_64-DVD-2009.iso
#查看镜像
[root@jztserver01 iso]# ls -lh
总用量 4.4G
-rw-r--r-- 1 qemu qemu 4.4G 1024 12:31 CentOS-7-x86_64-DVD-2009.iso
#切换上层目录
[root@jztserver01 iso]# cd ..
#查看当前目录
[root@jztserver01 centos-kvm]# pwd
/mnt/sdb1/centos-kvm
#说明磁盘mnt/sdb1、sdb2、sdb3、sdb4依次大小为1408G;创建镜像大小不能超过1408G
#执行创建磁盘镜像1200G[预留空间给磁盘]
[root@jztserver01 centos-kvm]# qemu-img create -f raw cenos79.raw 1200G
#说明按当前主机参数配置均分[CPU:64核;内存:64G]
#执行创建虚拟机centos1 [CPU:16核;内存:16G]
[root@jztserver01 centos-kvm]# virt-install --name centos1 --vcpus 16 --ram 16384 --location=/mnt/sdb1/centos-kvm/iso/CentOS-7-x86_64-DVD-2009.iso --disk path=/mnt/sdb1/centos-kvm/cenos79.raw,size=1200,format=raw --network=bridge=br1 --os-type=linux --os-variant=rhel7 --extra-args='console=ttyS0' --noautoconsole –force
#启动虚拟机centos1
[root@jztserver01 centos-kvm]# virsh start centos1
#从主机进入虚拟机centos1
[root@jztserver01 centos-kvm]# virsh console centos1
#切换到虚拟机centos1网卡配置目录
[root@jztserver01 centos-kvm]# cd /etc/sysconfig/network-scripts
#================================================================
#配置虚拟机centos1虚拟网卡ifcfg-eth0
[root@localhost network-scripts]# vi ifcfg-eth0
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=eth0
UUID=6388371e-23ae-43f4-9f20-7fce5a0d42f7
DEVICE=eth0
ONBOOT=yes

#配置虚拟机网络ip: 192.168.0.241
IPADDR=192.168.0.241
GATEWAY=192.168.0.1
NETMASK=255.255.254.0
DNS1=xxx.xxx.xxx.xxx
DNS2=114.114.114.114
#重启虚拟机centos1网络
[root@localhost network-scripts]# service network restart
#查看虚拟机centos1网络ip
[root@localhost network-scripts]# ip addr
#检查虚拟机centos1网络[宿主机和百度]
[root@localhost network-scripts]# ping 192.168.0.240
[root@localhost network-scripts]# ping 192.168.0.241
[root@localhost network-scripts]# ping www.baidu.com
#================================================================
#退出虚拟机centos1进入宿主机
ctrl + ]
#克隆虚拟机centos1前需暂停
[root@jztserver01 centos-kvm]# virsh shutdown centos1
#依次创建虚拟目录
[root@jztserver01 centos-kvm]# mkdir -p /mnt/sdb2/centos-kvm
[root@jztserver01 centos-kvm]# mkdir -p /mnt/sdb3/centos-kvm
[root@jztserver01 centos-kvm]# mkdir -p /mnt/sdb4/centos-kvm
#从宿主中克隆虚拟机centos2,centos3,centos4; 以centos1为模板
[root@jztserver01 centos-kvm]# virt-clone -o centos1 -n centos2 -f /mnt/sdb2/centos-kvm/centos2.qcow2
[root@jztserver01 centos-kvm]# virt-clone -o centos1 -n centos3 -f /mnt/sdb3/centos-kvm/centos3.qcow2
[root@jztserver01 centos-kvm]# virt-clone -o centos1 -n centos4 -f /mnt/sdb4/centos-kvm/centos4.qcow2
#克隆完毕后;依次启动虚拟机centos1、centos2、centos3、centos4
[root@jztserver01 centos-kvm]# virsh start centos1
[root@jztserver01 centos-kvm]# virsh start centos2
[root@jztserver01 centos-kvm]# virsh start centos3
[root@jztserver01 centos-kvm]# virsh start centos4
#依次进入虚拟机centos2,centos3,centos4; 配置网络, 重启网络;ping外网、内网、宿主
[root@jztserver01 centos-kvm]# virsh console centos2
#依次切换到虚拟机centos2、centos3、centos4网卡配置目录
[root@localhost ~]# cd /etc/sysconfig/network-scripts
#配置网络; 此处缺省;参考#====位置依次配置centos2,centos3,centos4
[root@localhost network-scripts]# cat ifcfg-eth0

1.3 KVM常见命令

#查看正在运行的虚拟机
[root@jztserver01 /]# virsh list
#查看虚拟机版本
[root@jztserver01 /]# virsh –version
#开机自动启动
[root@jztserver01 /]# virsh autostart 虚拟机名
#启动虚拟机
[root@jztserver01 /]# virsh start 虚拟机名
#暂停虚拟机
[root@jztserver01 /]# virsh shutdown 虚拟机名
#进入虚拟机
[root@jztserver01 /]# virsh console 虚拟机名
#销毁虚拟机
[root@jztserver01 /]# virsh destroy 虚拟机名
#挂起虚拟机
[root@jztserver01 /]# virsh suspend 虚拟机名
#修改配置文件
[root@jztserver01 /]# virsh edit 虚拟机名
#取消定义虚拟机
[root@jztserver01 /]# virsh undefine 虚拟机名
#查看虚拟机硬件信息
[root@jztserver01 /]# virsh dominfo centos1
Id:             2
名称:       centos1
UUID:           37c389a7-749d-41b6-8eee-f9788f185719
OS 类型:    hvm
状态:       running
CPU:          16
CPU 时间:   54.9s
最大内存: 16777216 KiB
使用的内存: 16777216 KiB
持久:       是
自动启动: 启用
管理的保存: 否
安全性模式: none
安全性 DOI: 0
#查看网卡信息
[root@jztserver01 /]# virsh domiflist centos1
接口     类型     源        型号      MAC
-------------------------------------------------------
vnet1      bridge     br1        virtio      52:54:00:67:95:6e     

原文地址:https://blog.csdn.net/suenpeng/article/details/143733180

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!