Managing KVM with Mist
Introduction
Launching a new KVM server
Let's begin by launching an Ubuntu 14.04 server. This example uses a bare metal server on packet.net, which offers the ability to launch bare metal servers through their API. We bought a server with 4 cores and 32GB RAM. Packet.net also gives us the choice to purchase a public net with 4 ip addresses, we'll buy one and make sure they are assigned to the KVM server in order to be used by the VMs. If you already have a KVM hypervisor up and running, skip on the "How to create VMs", section below.
Deploying a new VM on packet.net through the mist.io wizard.
Packet.net's network dashboard for assigning an ipv4 block.
The following command will install KVM on Ubuntu 14. There's a good guide on https://help.ubuntu.com/community/KVM/Installation on how to install KVM on Ubuntu.
root@kvm-dbg-002:~# apt-get install qemu-kvm libvirt-bin ubuntu-vm-builder bridge-utils uml-utilities genisoimage wget vim -y && apt-get update ; apt-get -y upgrade
root@kvm-dbg-002:~# virsh list Id Name State ----------------------------------------------------
Adding KVM to Mist.io
To add a KVM hypervisor to Mist.io click on Add Cloud and fill out the form as shown below
If you're having trouble, you can find more info on http://docs.mist.io/article/24-adding-kvm
Launching VMs
- Ubuntu: http://cloud-images.ubuntu.com/ eg http://cloud-images.ubuntu.com/releases/14.04.4/14.04.4/ubuntu-14.04-server-cloudimg-amd64-disk1.img
- Centos: http://cloud.centos.org/centos/7/images/ eg http://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud.qcow2
- Fedora: https://getfedora.org/en/cloud/download/ eg https://download.fedoraproject.org/pub/fedora/linux/releases/23/Cloud/x86_64/Images/Fedora-Cloud-Base-23-20151030.x86_64.qcow2
- Suse: http://download.opensuse.org/repositories/Cloud:/Images:/ eg http://download.opensuse.org/repositories/Cloud:/Images:/openSUSE_13.2/images/openSUSE-13.2-OpenStack-Guest.x86_64.qcow2
- Debian: http://cdimage.debian.org/cdimage/openstack/ eg http://cdimage.debian.org/cdimage/openstack/current/debian-8.7.0-openstack-amd64.qcow2
More cloudinit based images can be found on http://docs.openstack.org/image-guide/obtain-images.html
Launching Linux VMs
root@kvm-reload2:~# cd /var/lib/libvirt/images/ root@kvm-reload2:/var/lib/libvirt/images# wget http://cloud-images.ubuntu.com/releases/14.04.4/14.04.4/ubuntu-14.04-server-cloudimg-amd64-disk1.img
make sure genisoimage tool is available on the KVM server. If not install it (apt-get install genisoimage -y # for Debian based OS)
Creating a VM through mist.io on a cloudinit based Ubuntu image, deploying an ssh key and running a cloudinit script
The MAC address of the created VM is included on file /etc/libvirt/qemu/VM_name.xml which is the configuration file for VM_name.
root@kvm-reload2:/var/lib/libvirt/libcloud/proto# arp -an ? (192.168.122.85) at 52:54:00:ab:a0:c8 [ether] on eth0
root@kvm-reload2:/var/lib/libvirt/libcloud/proto# ssh ubuntu@192.168.122.85 -i /tmp/key ubuntu@first-ubuntu-vm:~$ ls / bin boot dev etc home initrd.img lib lib64 lost+found media mnt OK opt proc root run sbin srv sys tmp usr var vmlinuz ubuntu@first-ubuntu-vm:~$ ping mist.io PING mist.io (54.67.73.187) 56(84) bytes of data. 64 bytes from mist.io (54.67.73.187): icmp_seq=1 ttl=51 time=77.5 ms 64 bytes from mist.io (54.67.73.187): icmp_seq=2 ttl=51 time=77.6 ms ubuntu@first-ubuntu-vm:~$ sudo su root@first-ubuntu-vm:/home/ubuntu#
Connecting to VMs that only have private ips through mist.io VPN
If you're using the Saas version of mist.io - https://mist.io - you can create a VPN tunnel on the KVM server and thus be able to probe/ssh/run scripts to the VMs no matter that they have only private ips. Enter the mist.io add VPN tunnel page and make sure you specify the CIDR or CIDRS that the VMs will be part of (example 192.168.122.0/24 in our case). Run the command on the VPN server, to setup the tunnel. More details can be on the VPN page. If the tunnel is setup correctly you will be able to start the ssh web shell on the VMs, that are now accessible to mist.io through the tunnel
Setting up the tunnel
Mist.io can see the private ip of the VM
...and we can initiate the web shell
Setting up KVM to provide public IPs to the VMs
root@kvm-reload2:/var/lib/libvirt/libcloud/proto# vi /etc/libvirt/qemu/networks/default.xml <network> <name>default</name> <bridge name="eth0"/> <forward/> <ip address="192.168.122.1" netmask="255.255.255.0"> <dhcp> <range start="192.168.122.2" end="192.168.122.254"/> </dhcp> </ip> </network>
eth0 Link encap:Ethernet HWaddr fe:54:00:46:1d:2c inet addr:192.168.122.1 Bcast:192.168.122.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:174 errors:0 dropped:0 overruns:0 frame:0 TX packets:161 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:19791 (19.7 KB) TX bytes:39905 (39.9 KB)
root@kvm-reload2:/var/lib/libvirt/libcloud/proto# virsh net-edit default <network> <name>default</name> <uuid>3ed08eff-759a-49ac-9f10-ef9c72f8fc0a</uuid> <forward dev='eth0' mode='route'> <interface dev='eth0'/> </forward> <bridge name='br0' stp='on' delay='0'/> <mac address='52:54:00:37:96:0d'/> <ip address='147.75.194.212' netmask='255.255.255.0'> <dhcp> <range start='147.75.194.213' end='147.75.194.215'/> </dhcp> </ip> </network>
Note that the uuid has to be the same as the one that was auto-generated for us by KVM, otherwise if we provide another one, or omit it, we will get prompted that the operation has failed.
brctl addbr br0 ifconfig br0 147.75.194.212 netmask 255.255.255.0 up tunctl -b -u root -t qtap0 brctl addif br0 qtap0 ifconfig qtap0 up 0.0.0.0 promisc route add -host 147.75.194.213 dev br0 route add -host 147.75.194.214 dev br0 route add -host 147.75.194.215 dev br0 /etc/init.d/libvirt-bin restart
root@kvm-dbg-002:~# ifconfig br0 br0 Link encap:Ethernet HWaddr 52:54:00:37:96:0d inet addr:147.75.194.212 Bcast:147.75.194.255 Mask:255.255.255.0 UP BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
root@kvm-dbg-002:~# brctl show bridge name bridge id STP enabled interfaces br0 8000.52540037960d yes br0-nic
==> /var/log/syslog <== Jan 28 11:49:30 kvm-dbg-002 dnsmasq-dhcp[1940]: DHCPDISCOVER(br0) 52:54:00:89:ef:b1 Jan 28 11:49:30 kvm-dbg-002 dnsmasq-dhcp[1940]: DHCPOFFER(br0) 147.75.194.215 52:54:00:89:ef:b1 Jan 28 11:49:30 kvm-dbg-002 dnsmasq-dhcp[1940]: DHCPREQUEST(br0) 147.75.194.215 52:54:00:89:ef:b1 Jan 28 11:49:30 kvm-dbg-002 dnsmasq-dhcp[1940]: DHCPACK(br0) 147.75.194.215 52:54:00:89:ef:b1 ubuntu
==> /var/log/syslog <== Feb 8 06:49:23 kvmi dnsmasq-dhcp[1947]: DHCPDISCOVER(br0) 52:54:00:3f:43:67 no address available
virsh start centosVM.mist.io virsh start ubuntu-server
Run actions on existing VMS
You can run the following actions on VMs: shutdown, reboot, destroy, undefine (destroy and remove XML configuration file), suspend, resume and start.
Viewing the configuration of a VM
There is a quick way to view the VM configuration through the full metadata list on the VM page.