前言

为了学习docker集群,打算在CentOS里面安装KVM虚拟机来做docker集群

  • KVM
    KVM(Kernel-based Virtual Machine, 即内核级虚拟机) 是一个开源的系统虚拟化模块。它使用Linux自身的调度器进行管理,所以相对于Xen,其核心源码很少。目前KVM已成为学术界的主流VMM之一,它包含一个为处理器提供底层虚拟化 可加载的核心模块kvm.ko(kvm-intel.ko 或 kvm-amd.ko)。kvm还需要一个经过修改的QEMU 软件(qemu-kvm),作为虚拟机上层控制和界面。KVM的虚拟化需要硬件支持(如 Intel VT技术或者AMD V技术)。是基于硬件的完全虚拟化。 KVM可以运行多个其本身运行未改动的镜像的虚拟机,例如Windows,Mac OS X ,每个虚拟机都有各自的虚拟硬件,比如网卡、硬盘核图形适配器等。

kvm相关依赖包和作用

qemu-kvm     主要的KVM程序包
python-virtinst   创建虚拟机所需要的命令行工具和程序库
virt-manager    GUI虚拟机管理工具
virt-top       虚拟机统计命令
virt-viewer     GUI连接程序,连接到已配置好的虚拟机
libvirt       C语言工具包,提供libvirt服务
libvirt-client    虚拟客户机提供的C语言工具包
virt-install     基于libvirt服务的虚拟机创建命令
bridge-utils     创建和管理桥接设备的工具

检测是否支持KVM

  • 检查cpu是否支持虚拟化
1
[root@chobon ~]# grep vmx /proc/cpuinfo

如果有vmx信息输出,就说明支持VT;如果没有任何的输出,说明你的cpu不支持,将无法使用KVM虚拟机。

  • 确保BIOS里开启虚拟化功能,即查看是否加载KVM模块
1
[root@chobon ~]# lsmod | grep kvm
  • 安装虚拟机服务
1
[root@chobon ~]# yum install qemu-kvm libvirt virt-install bridge-utils

启动服务

1
[root@chobon ~]# systemctl start libvirtd

检查下服务是否正常

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@chobon ~]# systemctl status libvirtd

● libvirtd.service - Virtualization daemon
Loaded: loaded (/usr/lib/systemd/system/libvirtd.service; enabled; vendor preset: enabled)
Active: active (running) since 三 2019-12-18 20:30:09 CST; 9s ago
Docs: man:libvirtd(8)
https://libvirt.org
Main PID: 20262 (libvirtd)
Tasks: 19 (limit: 32768)
CGroup: /system.slice/libvirtd.service
├─ 2405 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshelper
├─ 2407 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshelper
└─20262 /usr/sbin/libvirtd

1218 20:30:09 chobon systemd[1]: Starting Virtualization daemon...
1218 20:30:09 chobon systemd[1]: Started Virtualization daemon.
1218 20:30:09 chobon dnsmasq[2405]: read /etc/hosts - 2 addresses
1218 20:30:09 chobon dnsmasq[2405]: read /var/lib/libvirt/dnsmasq/default.addnhosts - 0 addresses
1218 20:30:09 chobon dnsmasq-dhcp[2405]: read /var/lib/libvirt/dnsmasq/default.hostsfile
1218 20:30:09 chobon libvirtd[20262]: 2019-12-18 12:30:09.896+0000: 20278: info : libvirt version: 4.5.0, package: 23.el7_7.3 (CentOS BuildSystem <http://bugs.centos.org>, 2019-12-02-17:45:06, x86-02.bsys.centos.org)
1218 20:30:09 chobon libvirtd[20262]: 2019-12-18 12:30:09.896+0000: 20278: info : hostname: chobon
1218 20:30:09 chobon libvirtd[20262]: 2019-12-18 12:30:09.896+0000: 20278: warning : virStorageBackendVolOpen:1622 : ignoring dangling symlink '/etc/grub2.cfg'

安装虚拟机

  • 下载镜像

wgetcurl都可以

1
2
wget http://mirrors.aliyun.com/centos/7.7.1908/isos/x86_64/CentOS-7-x86_64-Minimal-1908.iso
curl -sSLO http://mirrors.aliyun.com/centos/7.7.1908/isos/x86_64/CentOS-7-x86_64-Minimal-1908.iso
  1. 使用qemu-img命令可以创建磁盘文件
  • 创建一个raw格式的磁盘文件
1
[root@chobon ~]# qemu-img create /mnt/kvm/vm1.raw 20G
  • 转换磁盘格式为qcow2
1
[root@chobon ~]# qemu-img convert -f raw -O qcow2 /mnt/kvm/vm1.raw /mnt/kvm/vm1.qcow2
  1. 使用virt-install命令创建虚拟机

virt-install --help

查看命令选项说明

  • 常用选项:
    • -n --name 虚拟机名称
    • -r --memory 以MB为单位为客户端事件分配的内存
    • vcpus 配置虚拟机的虚拟CPU(vcpu)数量
      • --vcpus 5
      • --vcpus 5, maxcpus=10
      • --vcpus socket=2,cores=4,threads=2
    • -l --location 安装源(例如:nfs:host:/pathhttp://host/pathftp://host/path)
    • --pxe 使用 PXE 协议从网络引导
    • --import 在磁盘映像中构建客户机
    • --livecd 将光驱介质视为 Live CD
    • -x --extra-args 附加到使用 –location 引导的内核的参数
    • --disk 用各种选项指定存储
      • --disk path=/my/existing/disk,cache=none
      • --disk path=/my/new/disk,format=qcow2,size=5,sparse (单位GB)
      • --disk vol=poolname:volname,device=cdrom,bus=scsi
    • -w --network 配置客户网络接口
      • --network bridge=mybr0
      • --network network=my_libvirt_virtual_net
      • --network network=mynet,model=virtio,mac=00:11...
    • --nonetworks 不要为该客体创建网络接口
    • --graphics 配置虚拟机显示设置
      • --graphics vnc
      • --graphics spice,port=5901,tlsport=5902
      • --graphics none
      • --graphics vnc,password=foobar,port=5910,keymap=ja
    • --virt-type 要使用的管理程序名称(kvm、qemu、xen)
    • --force 对任意应用程序提示强制回答‘yes’,终止其它提示

1
2
3
4
5
6
7
8
9
10
11
virt-install \
--virt-type=kvm \
--name=vm1_centos7 \
--vcpus=2 \
--memory=2048 \
--location=/kvm/iso/CentOS-7-x86_64-Minimal-1908.iso \
--disk path=/kvm/vfs/vm1.qcow2,size=40,format=qcow2 \
--network network=default \
--graphics none \
--extra-args='console=ttyS0' \
--force

剩下的就跟安装系统一样了

  • 安装选项
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Starting installer, one moment...
anaconda 21.48.22.134-1 for CentOS 7 started.
* installation log files are stored in /tmp during the installation
* shell is available on TTY2
* when reporting a bug add logs from /tmp as separate text/plain attachments
18:21:12 Not asking for VNC because we don't have a network
================================================================================
================================================================================
Installation

1) [x] Language settings 2) [!] Time settings
(English (United States)) (Timezone is not set.)
2) [!] Installation source 4) [!] Software selection
(Processing...) (Processing...)
3) [!] Installation Destination 6) [x] Kdump
(No disks selected) (Kdump is enabled)
4) [ ] Network configuration 8) [!] Root password
(Not connected) (Password is not set.)
5) [!] User creation
(No user will be created)
Please make your choice from above ['q' to quit | 'b' to begin installation |
'r' to refresh]:

[anaconda] 1:main* 2:shell 3:log 4:storage-lo> Switch tab: Alt+Tab | Help: F1

选择好基本安装选项后,就可以继续下一步了
基础安装选项,如下显示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Installation

1) [x] Language settings 2) [x] Time settings
(English (United States)) (Asia/Shanghai timezone)
3) [x] Installation source 4) [x] Software selection
(Local media) (Minimal Install)
5) [x] Installation Destination 6) [x] Kdump
(Automatic partitioning (Kdump is disabled)
selected) 8) [x] Root password
7) [ ] Network configuration (Password is set.)
(Not connected)
9) [ ] User creation
(No user will be created)
Please make your choice from above ['q' to quit | 'b' to begin installation |
'r' to refresh]:

基本设置如下:

  1. 语言保持不变
  2. 时区选择上海
  3. 安装源选择本地镜像
  4. 软件选择最小安装
  5. 安装磁盘选择自动分别,且使用LVM逻辑卷管理
  6. 关闭Kdump,虚拟机关系不大
  7. 网络先不管,安装之后处理
  8. Root 密码设置
  9. 不创建新用户
    最后,按下b开始安装过程。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
================================================================================
================================================================================
Progress
Setting up the installation environment
.
Creating disklabel on /dev/vda
.
Creating xfs on /dev/vda1
.
Creating lvmpv on /dev/vda2
.
Creating swap on /dev/mapper/centos-swap
.
Creating xfs on /dev/mapper/centos-root
.
Running pre-installation scripts
.
Starting package installation process
Preparing transaction from installation source
Installing libgcc (1/300)
Installing grub2-common (2/300)

....

Installing iwl6000g2b-firmware (299/300)
Installing iwl5150-firmware (300/300)
Performing post-installation setup tasks
Installing boot loader
.
Performing post-installation setup tasks
.

Configuring installed system
.
Writing network configuration
.
Creating users
.
Configuring addons
.
Generating initramfs
.
Running post-installation scripts
.
Use of this product is subject to the license agreement found at /usr/share/centos-release/EULA

Installation complete. Press return to quit

几分钟后,安装结束
输入密码,进入系统

1
2
3
4
5
6
7
8
9
10
11
12
13
14
      CentOS Linux (3.10.0-862.el7.x86_64) 7 (Core)
CentOS Linux (0-rescue-8707fc0ebc304aecae24959960933077) 7 (Core)

......

[ 10.953832] Netfilter messages via NETLINK v0.30.
[ 10.967303] ip_set: protocol 6

CentOS Linux 7 (Core)
Kernel 3.10.0-862.el7.x86_64 on an x86_64

localhost login: root
Password:
[root@localhost ~]#

常用指令

  • 查看在运行中的虚拟机
1
virsh list
  • 查看所有虚拟机
1
virsh list --all
  • 挂起虚拟机
1
virsh suspend linux-xx
  • 恢复挂起的虚拟机
1
virsh resume linux-xx
  • 开机启动虚拟机
1
virsh autostart linux-xx
  • 关闭开机启动虚拟机
1
virsh auto start --disable linux-xx
  • 关闭虚拟机
1
virsh shutdown linux-xx
  • 连接虚拟机
1
virsh console linux-xx
  • 停止虚拟机
1
virsh destroy linux-xx
  • 删除虚拟机定义
1
virsh undefine linux-xx