KVM是Kernel-based Virtual Machine的簡稱,是一個開源的虛擬化模塊,今天我將在CentOS7的操作系統上安裝KVM,以下是我的安裝步驟. 一.環境信息 系統: CentOS 7.2 IP: 10.0.0.12/24 在虛擬機安裝時,需要啟用CPU的虛擬化功能 二. KVM ...
KVM是Kernel-based Virtual Machine的簡稱,是一個開源的虛擬化模塊,今天我將在CentOS7的操作系統上安裝KVM,以下是我的安裝步驟.
一.環境信息
系統: CentOS 7.2 IP: 10.0.0.12/24
在虛擬機安裝時,需要啟用CPU的虛擬化功能
二. KVM安裝步驟
1. 安裝qemu-kvm和libvirt
[root@kvm-node1 ~]# yum install -y qemu-kvm libvirt ###qemu-kvm用來創建虛擬機硬碟,libvirt用來管理虛擬機
2. 安裝virt-install
[root@kvm-node1 ~]# yum install -y virt-install ###用來創建虛擬機
3. 啟動libvirtd,並將它設為開機啟動,啟動後使用ifconfig查看,發現會多出來一塊virbr0的網卡,ip預設為192.168.122.1/24,說明libvirtd啟動成功,如果預設沒有ifconfig命令,使用yum install -y net-tools安裝
[root@kvm-node1 ~]# systemctl start libvirtd && systemctl enable libvirtd [root@kvm-node1 ~]# ifconfig ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.0.0.12 netmask 255.255.255.0 broadcast 10.0.0.255 inet6 fe80::5505:5aa:3f96:f3f8 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:51:09:70 txqueuelen 1000 (Ethernet) RX packets 22459 bytes 29147955 (27.7 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 5161 bytes 587757 (573.9 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 1 (Local Loopback) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255 ether 52:54:00:fb:b1:55 txqueuelen 1000 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
經過以上三步,KVM安裝成功,下麵開始使用KVM創建虛擬機.
三. 使用KVM創建虛擬機(CentOS7)
1. 使用qemu命令創建一個10G的硬碟(最小10,G,可以更多),硬碟的名稱為: CentOS-7-x86_64.raw
[root@kvm-node1 ~]# qemu-img create -f raw /opt/CentOS-7-x86_64.raw 10G Formatting '/opt/CentOS-7-x86_64.raw', fmt=raw size=10737418240 [root@kvm-node1 ~]# ll -h /opt total 0 -rw-r--r-- 1 root root 10G Aug 22 07:59 CentOS-7-x86_64.raw
2. 使用virt-install創建名稱為CentOS-7-x86_64的虛擬機,在創建之前,先上傳一個CentOS7的ISO鏡像,最好在根目錄下創建一個ISO的目錄文件夾
[root@kvm-node1 ~]# virt-install --virt-type kvm --name CentOS-7-x86_64 --ram 1024 --cdrom=/ISO/CentOS-7-x86_64-Everything-1611.iso --disk path=/opt/CentOS-7-x86_64.raw --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole Starting install... Creating domain... | 0 B 00:03 Domain installation still in progress. You can reconnect to the console to complete the installation process.
這時候使用TightVNC工具,連接主機IP 10.0.0.12,設置安裝操作系統的網卡名稱為eth0,如圖所示
安裝步驟和我們平時安裝操作系統的方法一樣,安裝完成後,可以使用virsh list --all顯示KVM上所有的虛擬機
[root@kvm-node1 ~]# virsh list --all Id Name State ---------------------------------------------------- - CentOS-7-x86_64 shut off ### 可以看到虛擬機的名稱和狀態
四. 管理KVM
1. 使用virsh啟動KVM中的虛擬機
[root@kvm-node1 ~]# virsh start CentOS-7-x86_64 Domain CentOS-7-x86_64 started
啟動後,使用VNC連接工具,登錄後可以看到網卡的名稱為eth0,預設沒有ip地址,是因為網卡沒有激活,將ONBOOT改為yes,重啟網卡,可以看到ip地址為192.168.122.0網段的
當KVM中的虛擬機啟動後,可以在/etc/libvirt/qemu目錄下,看到啟動的虛擬機的信息
[root@kvm-node1 ~]# cd /etc/libvirt/qemu [root@kvm-node1 qemu]# pwd /etc/libvirt/qemu [root@kvm-node1 qemu]# ls -lh total 4.0K -rw------- 1 root root 3.8K Aug 22 08:18 CentOS-7-x86_64.xml drwx------ 3 root root 42 Aug 22 07:51 networks
CentOS-7-x86_64.xml就是啟動的虛擬機的信息,當我們使用vim編輯器查看時,會提示如下信息:
<!-- WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE OVERWRITTEN AND LOST. Changes to this xml configuration should be made using: virsh edit CentOS-7-x86_64 or other application using the libvirt API. -->
看到提示需要修改虛擬機的信息,需要使用命令 virsh edit CentOS-7-x86_64,是用這條命令後,我們可以看到很多虛擬機的信息,包括vcpu,memory等
2. CPU熱添加(KVM支持CPU的熱添加,不過局限是宿主機和虛擬機的系統相同才行,這裡宿主機是CentOS7,虛擬機也是CentOS7,所以可以模擬CPU熱添加,不過真實生產環境不建議這樣操作)
[root@kvm-node1 ~]# virsh edit CentOS-7-x86_64 6 <vcpu placement='static'>1</vcpu> 修改為: <vcpu placement='auto' current="1">4</vcpu> ##意思是當前cpu為1核,最多可以有4核,修改完虛擬機的配置文件,必須關閉虛擬機再啟動,配置才能生效
[root@kvm-node1 ~]# virsh shutdown CentOS-7-x86_64 ##關閉虛擬機
Domain CentOS-7-x86_64 is being shutdown
[root@kvm-node1 ~]# virsh list --all ##查看虛擬機狀態
Id Name State
----------------------------------------------------
- CentOS-7-x86_64 shut off
[root@kvm-node1 ~]# virsh start CentOS-7-x86_64 ##啟動虛擬機
Domain CentOS-7-x86_64 started
[root@kvm-node1 ~]# virsh list --all
Id Name State
----------------------------------------------------
3 CentOS-7-x86_64 running
先通過VNC工具查看虛擬機的cpu信息
在宿主機上通過命令添加核數
[root@kvm-node1 ~]# virsh setvcpus CentOS-7-x86_64 2 --live ##虛擬機的cpu添加到2core
再通過VNC工具查看虛擬機CPU信息
3. 記憶體熱添加(還是不建議生產中使用,知道功能就行了)
通過VNC連接虛擬機,查看虛擬機記憶體
[root@kvm-node1 ~]# virsh qemu-monitor-command CentOS-7-x86_64 --hmp --cmd info balloon ## 查看當前虛擬機的記憶體 balloon: actual=1024 [root@kvm-node1 ~]# virsh qemu-monitor-command CentOS-7-x86_64 --hmp --cmd balloon 512 ## 修改當前虛擬機的記憶體為512 [root@kvm-node1 ~]# virsh qemu-monitor-command CentOS-7-x86_64 --hmp --cmd info balloon ## 查看修改的記憶體信息已經改變成了512 balloon: actual=512
使用VNC工具連接虛擬機,查看記憶體是否有變:
4. 網卡配置(如何將KVM虛擬機中的網卡改為橋接模式,讓區域網的其他用戶可以正常訪問)
[root@kvm-node1 ~]# brctl show ##查看已有網橋 bridge name bridge id STP enabled interfaces virbr0 8000.525400fbb155 yes virbr0-nic vnet0
創建一個腳本,刪除ens33網卡的ip信息,新建一個橋接網卡,並且將ens33網卡的ip信息富裕新建的橋接網卡
[root@kvm-node1 ~]# vim br0.sh #!/bin/bash brctl addbr br0 #創建一個橋接網卡br0 brctl addif br0 ens33 #將ens33網卡的橋接信息轉到br0網卡 ip addr del dev ens33 10.0.0.12/24 #將ens33網卡的ip信息刪除 ifconfig br0 10.0.0.12/24 up #將ens33網卡的ip信息這隻到br0網卡上 route add default gw 10.0.0.2 #添加網關
[root@kvm-node1 ~]# chmod u+x br0.sh
[root@kvm-node1 ~]# ./br0.sh
使用ifocnfig查看網卡的ip信息,可以看到br0的地址已經變為了10.0.0.12/24
[root@kvm-node1 ~]# ifconfig br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.0.0.12 netmask 255.255.255.0 broadcast 10.0.0.255 inet6 fe80::20c:29ff:fe51:970 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:51:09:70 txqueuelen 1000 (Ethernet) RX packets 553 bytes 41723 (40.7 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 680 bytes 63511 (62.0 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet6 fe80::5505:5aa:3f96:f3f8 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:51:09:70 txqueuelen 1000 (Ethernet) RX packets 6107998 bytes 8708309251 (8.1 GiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 534420 bytes 68906654 (65.7 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 1 (Local Loopback) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 virbr0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255 ether 52:54:00:fb:b1:55 txqueuelen 1000 (Ethernet) RX packets 6685 bytes 278745 (272.2 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 6981 bytes 34351624 (32.7 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 vnet0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet6 fe80::fc54:ff:fe6b:5fee prefixlen 64 scopeid 0x20<link> ether fe:54:00:6b:5f:ee txqueuelen 1000 (Ethernet) RX packets 16 bytes 1650 (1.6 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 987 bytes 52236 (51.0 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
使用命令修改KVM虛擬機的信息
[root@kvm-node1 ~]# virsh edit CentOS-7-x86_64 69 <interface type='network'> 修改為: <interface type='bridge'> ##橋接 71 <source network='default'/> 修改為: <source bridge='br0'/>
修改完成後關閉虛擬機,啟動虛擬機,通過VNC工具修改ip為10.0.0.14/24,嘗試ping宿主機是否可以ping通,並通過ssh連接
[root@kvm-node1 ~]# virsh shutdown CentOS-7-x86_64 Domain CentOS-7-x86_64 is being shutdown [root@kvm-node1 ~]# virsh start CentOS-7-x86_64 Domain CentOS-7-x86_64 started
通過本地的ssh軟體,嘗試連接,可以連接,並且可以看到IP信息
以上就是CentOS7中KVM的安裝和簡單管理.