HOWTO: Use Red Hat Cluster Suite to manage your VMs

HOWTO: Use Red Hat Cluster Suite to manage your VMs

Postby Rainmaker » 17. Oct 2010, 00:50

Hi all, first post for me, so please go easy on me.

I was looking for ways to have VirtualBox run in a cluster (not a cluster on guests), so guests could automaticlly failover in the event a host goes down.

After searching Google and these forums for a while, I had to come to the conclusion, that such a thing does not exist. So I tried to create this.

I have only 1 machine to test with, so I haven't tested the howto on a "real" cluster. For me, the real requirement was redhat cluster suite (RHCS) restarting the VM when it crashed. This works.

My host OS here is Debian, guest OS is CentOS. Though any distribution capable of running RHCS and VirtualBox should be able to use the script below to set this up.

First of all, apt-get / yum install / aptitude RHCS
On Debian, the package needed is called redhat-cluster-suite.

Multiple node only:
If you use multiple nodes, you should setup a GFS2 filesystem now, so both nodes can access the filesystem. There are multple ways to set this up; shared disk on SAN, DRBD etc. I am unfortunatly unable to test this, but there are plenty of good howtos on the net. I would recommend this one or this site.

Now, you need to create a user, which has the ability to run VirtualBox (be member of vboxusers group), and has it's home directory on the GFS. This is because the user needs access to the ~/.VirtualBox directory on all nodes.
------------------------ (end multiple node) -----------------------

Now, RHCS supports a number of tags in it's cluster.conf file. Unfortunatly, there is no <vbox> tag. So, use this script to make a <vbox> tag possible. Place this in /usr/share/cluster/
Code: Select all   Expand viewCollapse view

# Definition for <vbox> tag, for launching
# VirtualBox VM's as resources


# Grab nfs lock tricks if available

. $(dirname $0)/ocf-shellfuncs

    cat <<EOT
<?xml version="1.0" ?>
<resource-agent version="rgmanager 2.0" name="vbox">

    <longdesc lang="en">
        This is a Oracle VirtualBox VM. All types of guests which
        VirtualBox supports, should work.
    <shortdesc lang="en">
        Oracle VirtualBox VM

        <parameter name="vmname" unique="1" primary="1">
            <longdesc lang="en">
                The name or UUID for the VM, as passed to VBoxHeadless.

            <shortdesc lang="en">
                VM name or UUID

            <content type="string"/>

        <parameter name="vrdpport">
            <longdesc lang="en">
                The port the VRDP server should listen on.

            <shortdesc lang="en">
                VRDP port number

         <parameter name="shutdowntime" default="120">
            <longdesc lang="en">
                The time in seconds a VM gets to shutdown after an ACPI event,
                before forceful shutdown is used. The maximum is 600 seconds
                (10 minutes).

            <shortdesc lang="en">
                Maximum time for VM shutdown after ACPI (seconds).

        <action name="start" timeout="20"/>
        <action name="stop" timeout="600"/>
        <action name="status" interval="20" timeout="10"/>
        <action name="monitor" interval="20" timeout="10"/>
        <action name="status" depth="10" interval="60" timeout="20"/>
        <action name="monitor" depth="10" interval="60" timeout="20"/>

        <action name="meta-data" timeout="5"/>
        <action name="validate-all" timeout="20"/>

#Check the state of the specified VM
function status_of_vm {
        ocf_log debug "Getting state for VM $1"
        STATE=$(VBoxManage showvminfo $1 --machinereadable | grep -E "^VMState=")

        if [ $STATE = 'VMState="running"' ]; then
                ocf_log debug "VM $1 is running"
                return 0
                ocf_log warning "VM $1 is NOT running!"
                return 1

function wait_for_start {
        until status_of_vm $1; do
                ocf_log info "VM $1 is not yet started!"
                sleep 1
                if [ $I -gt $2 ]; then
                        ocf_log err "VM $1 was not started after $I itterations!"
                        return 1
        ocf_log info "VM $1 has been started"
        return 0

#Try to get the VM to shutdown itself
function shutdown_vm {
        if ! status_of_vm $1; then
                ocf_log info "No shutdown neccessery, VM $1 not running"
                return 0
                ocf_log info "Sending $1 ACPI event"
                VBoxManage controlvm $1 acpipowerbutton
                return $?

#Poweroff the specified VM (force)
function poweroff_vm {
        ocf_log warning "Forcefully shutting down VM $1"
        VBoxManage controlvm $1 poweroff
        return $?

#Function which tries to shutdown the VM, or use forceful shutdown
#if the timeout expires
function stop_vm {
        shutdown_vm $1
        until ! status_of_vm $1; do
                ocf_log debug "Waiting for $1 to shutdown (waited $I seconds)"
                sleep 1
                if [ $I -gt 10 ]; then
                        #Shutdown timer expired.
                        ocf_log warning "Shutdown timer for VM $1 expired! Shutting down forcefully!"
                        poweroff_vm $1
                        return $?
        return 0

#Start the specified VM. Mind the &, VBoxHeadless does not return.
function poweron_vm {
        ocf_log info "Starting VM $1"
        VBoxHeadless -p $2 -s $1 &
        ocf_log info "VBoxHeadless returned $CODE"
        ocf_log info "VM $1 started"

case $1 in
        poweron_vm ${OCF_RESKEY_vmname} ${OCF_RESKEY_vrdpport}
        wait_for_start ${OCF_RESKEY_vmname}
        exit $?
        stop_vm ${OCF_RESKEY_vmname} ${OCF_RESKEY_shutdowntime}
        exit $?
        status_of_vm ${OCF_RESKEY_vmname}
        exit $?
        stop_vm ${OCF_RESKEY_vmname} ${OCF_RESKEY_shutdowntime}
        poweron_vm ${OCF_RESKEY_vmname} ${OCF_RESKEY_vrdpport}
        wait_for_start ${OCF_RESKEY_vmname}
        exit $?
        exit 0
        echo "usage: $0 {start|stop|status|monitor|restart|meta-data|validate-all}"

After placing this script in /usr/share/cluster and restarting cman, rgmanager will recognize the <vbox> tag.

The <vbox> tag takes 3 arguments:
vboxname (required): The VM name or UUID for the guest
vrdpport (required): The VRDP port number
shutdowntime (optional, default 120 seconds): time in seconds the script will wait for a guest to shutdown, before using VBoxManager's poweroff function on it.

Here is the cluster.conf I use. Keep in mind this has no GFS, only single node, hence no fencing. When using GFS, always configure fencing.

My cluster.conf:
Code: Select all   Expand viewCollapse view
<?xml version="1.0"?>
<cluster name="vboxcluster" config_version="6">
<cman expected_votes="1">
<fence_daemon post_fail_delay="1" post_join_delay="3" clean_start="0"/>
        <fencedevice name="human" agent="fence_manual"/>

        <clusternode name="oread" nodeid="1">
                        <method name="fence1">
                                <device name="human"/>

                <failoverdomain name="vboxdom">
                        <failoverdomainnode name="oread" priority="1"/>
        <service autostart="1" recovery="restart" domain="vboxdom" name="LDAP1_service">
                <vbox vmname="LDAPMaster1" vrdpport="8001" shutdowntime="60" name="VM1"/>
        <service autostart="1" recovery="restart" domain="vboxdom" name="LDAP2_service">
                <vbox vmname="LDAPMaster2" vrdpport="8002" name="VM2"/>
        <service autostart="1" recovery="restart" domain="vboxdom" name="IPA1">
                <vbox vmname="IPA1" vrdpport="8003" name="VM3"/>
        <service autostart="1" recovery="restart" domain="vboxdom" name="IPAC_service">
                <vbox vmname="IPAClient" vrdpport="8004" name="VM4"/>


As said, this is pretty minimal. But it does work as expected.

Have fun with it.
Posts: 3
Joined: 17. Oct 2010, 00:20
Primary OS: Debian other
VBox Version: PUEL
Guest OSses: CentOS

Re: HOWTO: Use redhat cluster suite to manage your VM's

Postby Technologov » 18. Oct 2010, 16:05

I will move this to "HOW-TOs" section of the forum.

In addition, it would be nice to test with several hosts and on RHEL host.

Please discuss this topic in separate thread.
Site Moderator
Posts: 2754
Joined: 10. May 2007, 16:59
Location: Israel

Return to Linux Hosts

Who is online

Users browsing this forum: No registered users and 0 guests