Many of you may remember my NUC virtualization project, it's still ongoing, but it did run into a major hiccup. ESXI 6.7 became more of a problem than I expected and the solution felt very "hackish" and unreliable.
So I took a slight detour and went with KVM instead of ESXI. This morning, I'll show you all how to create a KVM host and deploy a virtual machine onto the host! This virtualization host will be running with no GUI so a good understanding of linux command line is ideal to have before following along with this guide.
Prerequisite
- Linux installed on NUC (I am going to be using Fedora Server 30)
- Basic understanding of linux commands
- Basic understanding of networking
- Basic understanding of virtualization
Install KVM
Once you have Fedora installed on the NUC. It's time to install the necessary packages to get KVM up and running.
- Confirm your CPU has the virtualization extensions. (Intel VT or AMD-V)
cat /proc/cpuinfo | egrep "vmx|svm"
2. Install KVM, QEMU, and bridge utilities.
dnf install qemu-kvm libvirt bridge-utils virt-install -y
3. Verify kernel modules have been loaded.
lsmod | grep kvm
4. Install utility tools. (You can skip this step)
dnf install virt-top libguestfs-tools -y
5. Start up KVM.
systemctl start libvirtd
6. Enable the libvirtd service to start up on boot.
systemctl enable libvirtd
7. Verify the KVM is up and running.
systemctl status libvirtd
Creating a Virtual Machine on KVM
Now that you have KVM installed, let's create a virtual machine on it. I mean it only makes sense for a virtualization server to host virtual machines!
- Preallocate space on the disk for the virtual machine.
fallocate -l 20G centos-7.qcow2
I gave 20 GB of space for the VM.
2. Next create a new disk image for the virtual machine.
qemu-img create -f qcow2 ./centos-7.qcow2 20G
3. Provision the new virtual machine.
virt-install \
--name dck-jnk-01 \
--ram 1024 \
--vcpus 2 \
--disk path=/appl/images/centos-7.qcow2,size=20 \
--os-variant generic \
--os-type linux \
--graphics none \
--console pty,target_type=serial \
--location '/appl/iso/CentOS-7-x86_64-Minimal-1810.iso' \
--extra-args 'console=ttyS0,115200n8 serial'
After you execute that command, it will kick you into the console of the new virtual machine. From there, you will be able to complete the install.
4. Find the ip address of the newly provisioned CentOS 7 vm.
virsh net-dhcp-leases default
You should see an output like below.
[root@borg scleft]# virsh net-dhcp-leases default
Expiry Time MAC address Protocol IP address Hostname Client ID or DUID
----------------------------------------------------------------------------------------------------------------
2019-07-25 01:55:27 52:54:00:f5:e2:41 ipv4 192.168.122.80/24 centos-7-test-01 -
5. Verify if the VM is running.
virsh list --all
You will see an output similar to mine.
Id Name State
--------------------------
1 centos-7 running
Setting up a Bridge
- Enable IP forwarding.
sysctl net.ipv4.ip_forward
net.ipv4.ip_forward = 1
2. Enable IP Masquerade on the firewall to allow traffic from the WAN to connect to the KVM internal network.
wlp0s20f3 = External
virbr0 = Internal
firewall-cmd --set-default-zone=internal
firewall-cmd --change-interface=wlp0s20f3 --zone=external --permanent
firewall-cmd --permanent --direct --passthrough ipv4 -t nat -I POSTROUTING -o wlp0s20f3 -j MASQUERADE -s 192.168.122.0/24
3. Now hop onto the admin console of your router. I'm using an inexpensive Netgear router I received as a "hand-me-down". My particular router allows me to create a static route. Create a route to the 192.168.122.0
network by going through the IP addressed assigned to the NUC.
3. Test to make sure you can access the virtual machine from your local network. While on my laptop, I will ping the IP address of the new virtual machine.
ping 192.168.122.80
Or you can ssh to the virtual machine.
ssh username@192.168.122.80
If you get a response then congratulations your virtualization server is good to go!