From 183b99c0d276b8104ad055e24fa8581bf1c973fb Mon Sep 17 00:00:00 2001 From: Yanis Guenane Date: Mon, 13 Jan 2014 16:43:21 -0500 Subject: [PATCH] loadbalancer: Implementing a proper failover based on the router state Due to the fact that two HAProxy can not listen on the same vip at the same time, the management of the HAProxy service has been delegated to the keepalived process. With this patch, the keepalived process starts/stops the HAProxy daemon based on the state is is entering. If it enters MASTER mode then it starts the HAProxy daemon. If it enters BACKUP mode then it stops the HAProxy daemon. close #25 --- manifests/loadbalancer.pp | 16 ++++++++++------ spec/classes/cloud_loadbalancer_spec.rb | 17 +++++++++++++---- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/manifests/loadbalancer.pp b/manifests/loadbalancer.pp index 906a6b43..6d35d31b 100644 --- a/manifests/loadbalancer.pp +++ b/manifests/loadbalancer.pp @@ -56,7 +56,9 @@ class cloud::loadbalancer( $mysql_vip = $os_params::mysql_vip ){ - class { 'haproxy': } + class { 'haproxy': + manage_service => false, + } class { 'keepalived': } @@ -65,11 +67,13 @@ class cloud::loadbalancer( } keepalived::instance { '1': - interface => $keepalived_interface, - virtual_ips => split(join(flatten([$keepalived_ipvs, ['']]), " dev ${keepalived_interface},"), ','), - state => $keepalived_state, - track_script => ['haproxy'], - priority => 50, + interface => $keepalived_interface, + virtual_ips => split(join(flatten([$keepalived_ipvs, ['']]), " dev ${keepalived_interface},"), ','), + state => $keepalived_state, + track_script => ['haproxy'], + priority => 50, + notify_master => '"/etc/init.d/haproxy start"', + notify_backup => '"/etc/init.d/haproxy stop"', } $monitors_data = inline_template(' diff --git a/spec/classes/cloud_loadbalancer_spec.rb b/spec/classes/cloud_loadbalancer_spec.rb index c2dfc322..73a7de84 100644 --- a/spec/classes/cloud_loadbalancer_spec.rb +++ b/spec/classes/cloud_loadbalancer_spec.rb @@ -63,7 +63,9 @@ describe 'cloud::loadbalancer' do end it 'configure haproxy server' do - should contain_class('haproxy') + should contain_class('haproxy').with({ + 'manage_service' => 'false', + }) end it 'configure keepalived server' do @@ -72,7 +74,9 @@ describe 'cloud::loadbalancer' do it 'configure vrrp_instance with MASTER state' do should contain_keepalived__instance('1').with({ - 'state' => 'MASTER', + 'state' => 'MASTER', + 'notify_master' => '"/etc/init.d/haproxy start"', + 'notify_backup' => '"/etc/init.d/haproxy stop"', }) end @@ -121,7 +125,9 @@ describe 'cloud::loadbalancer' do end it 'configure haproxy server' do - should contain_class('haproxy') + should contain_class('haproxy').with({ + 'manage_service' => 'false', + }) end it 'configure keepalived server' do @@ -129,7 +135,10 @@ describe 'cloud::loadbalancer' do end it 'configure vrrp_instance with BACKUP state' do - should contain_keepalived__instance('1') + should contain_keepalived__instance('1').with({ + 'notify_master' => '"/etc/init.d/haproxy start"', + 'notify_backup' => '"/etc/init.d/haproxy stop"', + }) end end