Friday, October 4, 2019

KVM deployment on GCP nested instance

Summary:

Kernel-based Virtual Machine (KVM) is virtualization software for CentOS 7. KVM turn your server into a hypervisor. This blog shows how to setup and manage a virtualized environment with KVM in CentOS 7 on GCP.

Environment :
  • Google cloud environment (GCP)
  • Nested VM running on GCP
  • Centos 7


Create GCP nested VM image from Google cloud CLI console:


gcloud compute disks create disk1 \
--image-project centos-cloud \
--image-family centos-7 --zone australia-southeast1-a

gcloud compute images create nested-vm-image \

--source-disk disk1 \
--source-disk-zone australia-southeast1-a \
--licenses "https://www.googleapis.com/compute/v1/projects/vm-options/global/licenses/enable-vmx"



Verify Virtualization enable on KVM server: 

# lscpu | grep Virtualization


Install the required packages on KVM server:

# yum install virt-install qemu-kvm libvirt \
libvirt-python libguestfs-tools virt-manager wget -y 

Enable and start the services :

# systemctl enable libvirtd
# systemctl start libvirtd
# systemctl status libvirtd 


Update qemu to latest version :
 
# yum install centos-release-qemu-ev -y


Check kernal modules for KVM :

#  lsmod | grep -i kvm
kvm_intel             188683  0
kvm                   621392  1 kvm_intel
irqbypass              13503  1 kvm




Configure bridged networking:

# brctl show
bridge name     bridge id               STP enabled     interfaces
virbr0          8000.525400449dfa       yes             virbr0-nic
 

# virsh net-list
 Name                 State      Autostart     Persistent
----------------------------------------------------------
 default              active     yes           yes


# vi /etc/sysconfig/network-scripts/ifcfg-eth0 
add line ==> BRIDGE="br0"


Create file ifcfg-br0 on /etc/sysconfig/network-scripts

DEVICE="br0"
BOOTPROTO="dhcp"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
ONBOOT="yes"
TYPE="Bridge"
DELAY="0"

# systemctl restart NetworkManager

# brctl show
bridge name     bridge id               STP enabled     interfaces
br0             8000.000000000000       no
virbr0          8000.525400449dfa       yes             virbr0-nic




Download the iso image :

# cd /var/lib/libvirt/boot/
 

# wget https://mirrors.edge.kernel.org/centos/7.6.1810/isos/x86_64/CentOS-7-x86_64-Minimal-1810.iso
 

# wget https://mirrors.edge.kernel.org/centos/7.6.1810/isos/x86_64/sha256sum.txt

# sha256sum -c sha256sum.txt
 



Create guest virtual machine on KVM server:
 
# virt-install --name=centos7vm \
--ram=1024 \
--vcpus=1 \
--location=/var/lib/libvirt/boot/CentOS-7-x86_64-Minimal-1810.iso \
--os-type=linux --os-variant=centos7.0 \
--network=bridge=virbr0,model=virtio \
--disk path=/var/lib/libvirt/images/centos7.qcow2,size=10,bus=virtio,format=qcow2 \
--graphics none --console pty,target_type=serial \
--extra-args 'console=ttyS0,115200n8 serial'



Verify guest virtual machine :

# virsh net-list
 Name                 State      Autostart     Persistent
----------------------------------------------------------
 default              active     yes           yes

# virsh domiflist centos7vm
Interface  Type       Source     Model       MAC
-------------------------------------------------------
vnet0      bridge     virbr0     virtio      52:54:00:93:a5:23

# virsh net-dhcp-leases default
 Expiry Time          MAC address        Protocol  IP address                Hostname        Client ID or DUID
-------------------------------------------------------------------------------------------------------------------
 2019-09-20 04:39:43  52:54:00:93:a5:23  ipv4      192.168.122.46/24         centos7vm       -


# virsh console centos7vm
Connected to domain centos7vm
Escape character is ^]


Ctr + ] ( to exit from guest vm console)

 

# virsh list --all
 Id    Name                           State
----------------------------------------------------
 2     centos7vm                      running



# virsh dominfo centos7vm
Id:             2
Name:           centos7vm
UUID:           217550ae-cdcc-4c96-a222-fc122a7126c8
OS Type:        hvm
State:          running
CPU(s):         1
CPU time:       33.7s
Max memory:     1048576 KiB
Used memory:    1048576 KiB
Persistent:     yes
Autostart:      disable
Managed save:   no
Security model: selinux
Security DOI:   0
Security label: system_u:system_r:svirt_t:s0:c351,c948 (enforcing)