RedHat7.3搭建KVM
Posted FlyBack
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RedHat7.3搭建KVM相关的知识,希望对你有一定的参考价值。
RedHat7搭建KVM虚拟机
1. 宿主机安装RedHat7.3系统
1.1选择语言
中文、简体中文(中国)
1.2安装位置
1.2.1自定义分区,选择LVM,将分区空间全部分配给根
1.2.2禁用Kdump
2.安装KVM
2.1安装前准备
2.1.1配置yum源
2.1.2防火墙处理
setenforce 0
sed -i \'s/SELINUX=enforcing/SELINUX=disabled/g\' /etc/sysconfig/selinux
systemctl disable firewalld
systemctl stop firewalld
2.2安装GNOME桌面环境
yum -y groupinstall "X Window System"
yum install gnome-classic-session gnome-terminal nautilus-open-terminal control-center liberation-mono-fonts –y
2.2.1设置默认以桌面启动
将/etc/inittab中的
#graphical.target: analogous to runlevel 5
修改为
graphical.target: analogous to runlevel 5
systemctl set-default graphical.target
2.3安装vncserver
2.3.2安装vncserver
yum -y install vnc *vnc-server*
2.3.3设置密码
[root@KVM2 ~]# vncserver
You will require a password to access your desktops.
Password:***** ###输入密码
Verify:***** ###确认密码
2.3.4启动服务
vncserver :1
2.3.5停止服务
vncserver –kill :1
2.4安装KVM
2.4.1查看主机是否支持VT
cat /proc/cpuinfo |grep vmx #intel cpu
cat /proc/cpuinfo |grep svm # amd cpu
#如果flags: 里有vmx 或者svm就说明支持VT;如果没有任何的输出,说明你的cpu不支持,将无法使用KVM虚拟机
2.4.2确保Bios里开启VT: Intel(R) Virtualization Tech [Enabled] 使用如下命令
[root@KVM2 ~]# lsmod | grep kvm
kvm_intel 170181 0
kvm 554609 1 kvm_intel
irqbypass 13503 1 kvm
2.4.3桥接网络
安装bridge-utils ,用来管理网桥的工具brctl
yum -y install bridge-utils
2.4.4安装qemu-kvm libvirt virt-install virt-manager
yum -y install qemu-kvm libvirt virt-install virt-manager openssh-askpass
2.4.5配置修改
/etc/libvirt/qemu.conf
将
dynamic_ownership=1
#user = "root"
#group = "root"
修改为
dynamic_ownership=0
user = "root"
group = "root"
2.4.6重启服务设置开机自启动
systemctl restart libvirtd
systemctl enable libvirtd
2.4.7修改网络配置文件
nmcli c add type bridge autoconnect yes con-name br0 ifname br0
cd /etc/sysconfig/network-scripts/
[root@KVM2 network-scripts]# cat ifcfg-br0
DEVICE=br0
STP=yes
BRIDGING_OPTS=priority=32768
TYPE=Bridge
BOOTPROTO=none
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
NAME=br0
ONBOOT=yes
IPADDR=192.161.14.247
NETMASK=255.255.255.0
GATEWAY=192.161.14.1
[root@KVM2 network-scripts]# cat ifcfg-ens192
TYPE=Ethernet
BOOTPROTO=none
BRIDGE=br0
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
NAME=ens192
UUID=89e79501-94d5-4e32-a215-dad967527107
DEVICE=ens192
ONBOOT=yes
重启网络systemctl restart network
查看网络
[root@KVM2 network-scripts]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
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: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master br0 state UP qlen 1000
link/ether 00:50:56:83:03:6a brd ff:ff:ff:ff:ff:ff
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN qlen 1000
link/ether 52:54:00:26:16:70 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
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN qlen 1000
link/ether 52:54:00:26:16:70 brd ff:ff:ff:ff:ff:ff
7: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP qlen 1000
link/ether 00:50:56:83:03:6a brd ff:ff:ff:ff:ff:ff
inet 192.161.14.247/24 brd 192.161.14.255 scope global br0
valid_lft forever preferred_lft forever
inet6 fd3c:dfbd:20c3:d000:250:56ff:fe83:36a/64 scope global mngtmpaddr dynamic
valid_lft 7094sec preferred_lft 3494sec
inet6 fe80::250:56ff:fe83:36a/64 scope link
valid_lft forever preferred_lft forever
2.5各宿主机实现双机互信
a)
ssh-keygen -t rsa
#生产公钥、私钥
b)同步各宿主机/root/.ssh/authorized_keys配置文件
2.6配置vlan
2.6.1设置开机启动加载8021q模块
echo ‘/usr/sbin/modprobe 8021q’ >> /etc/rc.local
chmod +x /etc/rc.local
2.6.2上传vconfig-1.9-8.1.el6.x86_64.rpm用于创建vlan
rpm -ivh vonfig-1.9-8.1.el6.x86_64.rpm
2.6.3创建vlan140
a)
[root@KVM1 network-scripts]# vconfig add eno1 140
Added VLAN with VID == 140 to IF -:eno1:
命令格式如下:
vconfig add 物理网卡名 vlanid
b)
[root@KVM1 network-scripts]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
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: eno1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq master br0 state UP qlen 1000
link/ether 30:e1:71:55:a8:f4 brd ff:ff:ff:ff:ff:ff
inet6 fe80::32e1:71ff:fe55:a8f4/64 scope link
valid_lft forever preferred_lft forever
3: eno2: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq state DOWN qlen 1000
link/ether 30:e1:71:55:a8:f5 brd ff:ff:ff:ff:ff:ff
4: eno3: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq state DOWN qlen 1000
link/ether 30:e1:71:55:a8:f6 brd ff:ff:ff:ff:ff:ff
5: eno4: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq state DOWN qlen 1000
link/ether 30:e1:71:55:a8:f7 brd ff:ff:ff:ff:ff:ff
45: eno1.140@eno1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN
link/ether 30:e1:71:55:a8:f4 brd ff:ff:ff:ff:ff:ff
#多了一个eno1.140的虚拟网卡
c)创建相应虚拟网卡配置文件和桥接配置文件
d)重启网卡
[root@KVM1 network-scripts]# service network restart
Restarting network (via systemctl): [ OK ]
e)查看网桥绑定情况
[root@KVM1 network-scripts]# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.30e17155a8f4 yes eno1
br140 8000.30e17155a8f4 yes eno1.140
br20 8000.30e17155a8f4 yes eno1.20
vnet1
virbr0 8000.5254009c7586 yes virbr0-nic
#网桥绑定成功
f)查看网桥是否运行正常
[root@KVM1 network-scripts]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
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: eno1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq master br0 state UP qlen 1000
link/ether 30:e1:71:55:a8:f4 brd ff:ff:ff:ff:ff:ff
inet6 fe80::32e1:71ff:fe55:a8f4/64 scope link
valid_lft forever preferred_lft forever
3: eno2: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq state DOWN qlen 1000
link/ether 30:e1:71:55:a8:f5 brd ff:ff:ff:ff:ff:ff
4: eno3: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq state DOWN qlen 1000
link/ether 30:e1:71:55:a8:f6 brd ff:ff:ff:ff:ff:ff
5: eno4: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq state DOWN qlen 1000
link/ether 30:e1:71:55:a8:f7 brd ff:ff:ff:ff:ff:ff
8: br20: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
link/ether 30:e1:71:55:a8:f4 brd ff:ff:ff:ff:ff:ff
inet 172.16.4.100/24 brd 172.16.4.255 scope global br20
valid_lft forever preferred_lft forever
inet6 fe80::32e1:71ff:fe55:a8f4/64 scope link
valid_lft forever preferred_lft forever
9: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN
link/ether 52:54:00:9c:75:86 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
10: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN qlen 500
link/ether 52:54:00:9c:75:86 brd ff:ff:ff:ff:ff:ff
40: vnet1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master br20 state UNKNOWN qlen 500
link/ether fe:54:00:f3:1a:6a brd ff:ff:ff:ff:ff:ff
inet6 fe80::fc54:ff:fef3:1a6a/64 scope link
valid_lft forever preferred_lft forever
45: eno1.140@eno1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master br140 state UP
link/ether 30:e1:71:55:a8:f4 brd ff:ff:ff:ff:ff:ff
inet6 fe80::32e1:71ff:fe55:a8f4/64 scope link
valid_lft forever preferred_lft forever
46: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
link/ether 30:e1:71:55:a8:f4 brd ff:ff:ff:ff:ff:ff
inet 192.161.14.247/24 brd 192.161.14.255 scope global br0
valid_lft forever preferred_lft forever
inet6 fd3c:dfbd:20c3:d000:32e1:71ff:fe55:a8f4/64 scope global mngtmpaddr dynamic
valid_lft 7004sec preferred_lft 3404sec
inet6 fd51:8056:6705:0:32e1:71ff:fe55:a8f4/64 scope global mngtmpaddr dynamic
valid_lft 7200sec preferred_lft 1800sec
inet6 fd51:8056:6705:4:32e1:71ff:fe55:a8f4/64 scope global mngtmpaddr dynamic
valid_lft 7200sec preferred_lft 1800sec
inet6 fe80::32e1:71ff:fe55:a8f4/64 scope link
valid_lft forever preferred_lft forever
47: br140: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
link/ether 30:e1:71:55:a8:f4 brd ff:ff:ff:ff:ff:ff
inet 172.16.4.40/24 brd 172.16.4.255 scope global br140
valid_lft forever preferred_lft forever
inet6 fe80::32e1:71ff:fe55:a8f4/64 scope link
valid_lft forever preferred_lft forever
48: eno1.20@eno1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master br20 state UP
link/ether 30:e1:71:55:a8:f4 brd ff:ff:ff:ff:ff:ff
inet6 fe80::32e1:71ff:fe55:a8f4/64 scope link
valid_lft forever preferred_lft forever
#运行正常
3.虚机迁移
virsh migrate --live rhel7.3 qemu+ssh://192.161.14.250/system
3.1查看虚拟机磁盘格式
[root@KVM1 images]# qemu-img info redhat7.3
image: redhat7.3
file format: qcow2
virtual size: 60G (64424509440 bytes)
disk size: 1.3G
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: true
3.2克隆模板的磁盘文件
[root@KVM1 images]# qemu-img create -f qcow2 -b redhat7.3 KVM2-VM1
###克隆格式为qcow2
Formatting \'KVM2-VM1\', fmt=qcow2 size=64424509440 backing_file=\'redhat7.3\' encryption=off cluster_size=65536 lazy_refcounts=off
3.3克隆模板的配置文件
virsh dumpxml rhel7.3 > /etc/libvirt/qemu/KVM2-VM1.xml
###其中rhel7.3为模板文件的名字,KVM2-VM1为要生成模板文件名字,注意跟前面的磁盘文件名保持一致
3.4删除网卡的MAC、模板虚拟机的UUID
<mac address=\'52:54:00:f3:1a:6a\'/>
<uuid>18f4b3eb-4d0f-4cac-bc3f-e3798fa4746c</uuid>
3.5修改磁盘名称
<source file=\'/var/lib/libvirt/images/redhat7.3\'/>
3.6修改虚拟机name
<name>rhel7.3</name>
3.7重新定义一个虚拟机
[root@KVM1 qemu]# virsh define /etc/libvirt/qemu/KVM2-VM1.xml
Domain KVM2-VM1 defined from /etc/libvirt/qemu/KVM2-VM1.xml
3.8迁移报错
Error starting domain: internal error: process exited while connecting to monitor: 2017-08-29T05:09:58.146446Z qemu-kvm: -chardev socket,id=charchannel0,path=/var/lib/libvirt/qemu/channel/target/domain-rhel7.3/org.qemu.guest_agent.0,server,nowait: Failed to bind socket: No such file or directory
2017-08-29T05:09:58.146488Z qemu-kvm: -chardev socket,id=charchannel0,path=/var/lib/libvirt/qemu/channel/target/domain-rhel7.3/org.qemu.guest_agent.0,server,nowait: chardev: opening backend "socket" failed
创建该目录/var/lib/libvirt/qemu/channel/target/下相应目录问题就可解决
3.9虚拟克隆到迁移步骤
3.9.1利用virt-mangaer克隆
a)
#选择要克隆的主机(该主机必须是关闭或Pause状态)
b)
#设置要克隆的虚机名称,Storage,选择Details(这里不要选择默认的,否则会导致主机名跟磁盘名不一致,不便于管理)
c)
#设置虚机机磁盘位置、名称,这里要求跟虚拟机名字一致
d)
#选择克隆即可
e)
#克隆完毕之后,即可看到虚拟机列表
3.9.2virt-clone命令克隆
a)克隆命令格式
virt-clone -o 源虚机名 -n 目标虚机名 -f /var/lib/libvirt/images/目标虚拟机名
b)列出宿主机所有虚拟机
[root@KVM1 qemu]# virsh list --all
Id Name State
----------------------------------------------------
16 rhel7.3 paused
- KVM2-VM1 shut off
- KVM2-VM2 shut off
- KVM2-VM3 shut off
- KVM2-VM5 shut off
- KVM2-VM6 shut off
- KVM2-VM7 shut off
c)克隆虚机KVM2-VM8
[root@KVM1 qemu]# virt-clone -o rhel7.3 -n KVM2-VM8 -f /var/lib/libvirt/images/KVM2-Vm8
WARNING Setting the graphics device port to autoport, in order to avoid conflicting.
Allocating \'KVM2-Vm8\' | 60 GB 00:00:02
Clone \'KVM2-VM8\' created successfully.
d)检查是否创建成功
[root@KVM1 target]# virsh list --all
Id Name State
----------------------------------------------------
16 rhel7.3 paused
- KVM2-VM1 shut off
- KVM2-VM2 shut off
- KVM2-VM3 shut off
- KVM2-VM5 shut off
- KVM2-VM6 shut off
- KVM2-VM7 shut off
- KVM2-VM8 shut off
3.9.3虚拟机静态迁移
a)将虚拟机KVM2-VM8迁移到宿主机KVM2上
b)迁移配置文件
[root@KVM1 target]# scp /etc/libvirt/qemu/KVM2-VM8.xml root@KVM2:/etc/libvirt/qemu/
KVM2-VM8.xml
c)迁移镜像文件
[root@KVM1 target]# scp /var/lib/libvirt/images/KVM2-VM root@KVM2:/var/lib/libvirt/images/
d)激活配置文件
[root@KVM2 target]# virsh define /etc/libvirt/qemu/KVM2-VM8.xml
e)查看宿主机虚机情况
[root@KVM2 target]# virsh list --all
Id Name State
----------------------------------------------------
- KVM2-VM1 shut off
- KVM2-VM2 shut off
- KVM2-VM3 shut off
- KVM2-VM5 shut off
- KVM2-VM6 shut off
- KVM2-VM8 shut off
#迁移成功
参考
http://www.linuxidc.com/Linux/2015-12/126690.htm ###安装桌面
http://www.linuxidc.com/Linux/2016-07/132835.htm ###vnc安装
http://blog.csdn.net/skykingf/article/details/51944455 ###安装kvm1
http://www.cnblogs.com/lvxiaobo616/p/5704646.html ###安装kvm2
http://blog.csdn.net/qq_19646075/article/details/51780530 ###虚机迁移1
http://www.cnblogs.com/sammyliu/p/4572287.html ###虚机迁移2
备注
如在虚拟机中测试,网卡一定选择成混杂模式,否则,在虚机获取桥接网络时,会获取不到
以上是关于RedHat7.3搭建KVM的主要内容,如果未能解决你的问题,请参考以下文章