1. 程式人生 > >RedHat7.3搭建KVM

RedHat7.3搭建KVM

2.4 define route oca 桌面環境 conn 輸入密碼 rtu utils

RedHat7.3搭建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設置密碼

[[email protected] ~]# 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] 使用如下命令

[[email protected] ~]# 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/

[[email protected] 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

[[email protected] 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

查看網絡

[[email protected] 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)

[[email protected] network-scripts]# vconfig add eno1 140

Added VLAN with VID == 140 to IF -:eno1:

命令格式如下:

vconfig add 物理網卡名 vlanid

b)

[[email protected] 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: [email protected]: <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)重啟網卡

[[email protected] network-scripts]# service network restart

Restarting network (via systemctl): [ OK ]

e)查看網橋綁定情況

[[email protected] 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)查看網橋是否運行正常

[[email protected] 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: [email protected]: <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: [email protected]: <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查看虛擬機磁盤格式

[[email protected] 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克隆模板的磁盤文件

[[email protected] 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重新定義一個虛擬機

[[email protected] 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)列出宿主機所有虛擬機

[[email protected] 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

[[email protected] 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)檢查是否創建成功

[[email protected] 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)遷移配置文件

[[email protected] target]# scp /etc/libvirt/qemu/KVM2-VM8.xml [email protected]:/etc/libvirt/qemu/

KVM2-VM8.xml

c)遷移鏡像文件

[[email protected] target]# scp /var/lib/libvirt/images/KVM2-VM [email protected]:/var/lib/libvirt/images/

d)激活配置文件

[[email protected] target]# virsh define /etc/libvirt/qemu/KVM2-VM8.xml

e)查看宿主機虛機情況

[[email protected] 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