1、环境初始化 [root@localhost ~]# hostnamectl set-hostname kvm-node1 [root@kvm-node1 ~]# sed -i '/SELINUX=/cSELINUX=disabled' /etc/selinux/config [root@kvm-node1 ~]# echo "192.168.59.129 kvm-node1" >> /etc/hosts
[root@kvm-node1 ~]# systemctl stop firewalld
[root@kvm-node1 ~]# cat /etc/redhat-release CentOS Linux release 7.3.1611 (Core)
查看虚拟化支持信息
[root@kvm-node1 ~]# egrep '(vmx|svm)' /proc/cpuinfo
2、安装kvm [root@kvm-node1 ~]# yum install qemu-kvm qemu-kvm-tools libvirt virt-manager virt-install openssh-askpass –y
//qeum 虚拟化软件,可以虚拟不同的CPU,以及模拟网卡、声卡、PCI设备等 //libvirt 用于管理KVM的工具 //virt-install 命令行安装虚拟机工具 //virt-manager 图形化管理虚拟机工具 //openssh-askpass 远程连接KVM主机
[root@kvm-node1 ~]# lsmod|grep kvm
kvm_intel 170181 0
kvm 554609 1 kvm_intel
irqbypass 13503 1 kvm
[root@kvm-node1 ~]# systemctl start libvirtd #启动kvm管理工具 libvirtd
[root@kvm-node1 ~]# systemctl status libvirtd
● libvirtd.service - Virtualization daemon
Loaded: loaded (/usr/lib/systemd/system/libvirtd.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2018-04-26 22:46:06 CST; 59s ago
Docs: man:libvirtd(8)
http://libvirt.org
Main PID: 905 (libvirtd)
CGroup: /system.slice/libvirtd.service
├─ 905 /usr/sbin/libvirtd
├─2249 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshelper
└─2250 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshelper
Apr 26 22:46:01 kvm-node1 systemd[1]: Starting Virtualization daemon...
Apr 26 22:46:06 kvm-node1 systemd[1]: Started Virtualization daemon.
Apr 26 22:46:09 kvm-node1 dnsmasq[2249]: started, version 2.66 cachesize 150
Apr 26 22:46:09 kvm-node1 dnsmasq[2249]: compile time options: IPv6 GNU-getopt DBus no-i18n IDN DHCP DHCPv6 no-Lua TFTP no-conntrack ipset auth
Apr 26 22:46:09 kvm-node1 dnsmasq-dhcp[2249]: DHCP, IP range 192.168.122.2 -- 192.168.122.254, lease time 1h
Apr 26 22:46:09 kvm-node1 dnsmasq[2249]: reading /etc/resolv.conf
Apr 26 22:46:09 kvm-node1 dnsmasq[2249]: using nameserver 192.168.59.2#53
Apr 26 22:46:09 kvm-node1 dnsmasq[2249]: read /etc/hosts - 3 addresses
Apr 26 22:46:09 kvm-node1 dnsmasq[2249]: read /var/lib/libvirt/dnsmasq/default.addnhosts - 0 addresses
Apr 26 22:46:09 kvm-node1 dnsmasq-dhcp[2249]: read /var/lib/libvirt/dnsmasq/default.hostsfile
3、设定kvm网络模式
KVM会给虚拟机分配NAT模式网络,但是在生产情况,一般使用网桥模式
[root@kvm-node1 ~]# brctl addif br0 ens33 && ip addr del dev ens33 192.168.59.129 && ifconfig br0 192.168.59.129/24 up && route add default gw 192.168.59.2
[root@kvm-node1 ~]# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.000c29e7584c no ens33
virbr0 8000.525400866da8 yes virbr0-nic
4、创建kvm虚拟机 [root@kvm-node1 ~]# mkfs.xfs /dev/sdb [root@kvm-node1 ~]# mount /dev/sdb /mnt/ [root@kvm-node1 ~]# dd if=/dev/cdrom of=/mnt/centos7.iso #创建iso镜像文件
[root@kvm-node1 ~]# qemu-img create -f qcow2 /mnt/kvm1.qcow2 20G #创建虚拟机磁盘
Formatting '/mnt/kvm1.qcow2', fmt=qcow2 size=21474836480 encryption=off cluster_size=65536 lazy_refcounts=off
[root@kvm-node1 ~]#
virt-install --name centos7-node1
--virt-type kvm
--ram 1512
--cdrom=/mnt/centos7.iso
--disk path=/mnt/kvm1.qcow2
--network bridge=br0
--graphics vnc,listen=0.0.0.0
--noautoconsole
Starting install...
Domain installation still in progress. You can reconnect to
the console to complete the installation process.
//参数概述
--virt-type:指定虚拟机类型(kvm、qemu、xen)
--name:指定虚拟机名称
--raw:指定虚拟机使用内存大小
--cpu:指定cpu的核数(默认为1)
--cdrom:指定虚拟机安装的ISO镜像
--disk:指定虚拟机数据存储磁盘qcow2
--network:指定虚拟机网络类型, 默认nat,常用bridge
--graphics:指定虚拟机可连接方式, 比如VNC
--noautoconsole:不连接虚拟机图形界面控制台