Saturday, December 20, 2014

Auto add load balance members in Apache load balancing with puppet (export and stored configuration)

I assume PuppetDB already configured and Apache installation, configuration and service class is already available.

Lets say we have 3 nodes

1st Node1 - > Load Balancer
2nd Node2 -> Load Balancer Member
3rd Node3 -> Load Balancer Member

For Node1, we have balancer://mybalancer already configured in its httpd.conf and we want any new node added in future, a new file should be created automatically in /etc/httpd/conf.d.members directory for new node/member

<Proxy balancer://mybalancer>
  Include /etc/httpd/conf.d.members/*.conf

Puppet configuration

1) Create one resource balancer.pp

define apache::balancermember($url) {
        file { "/etc/httpd/conf.d.members/worker_${name}.conf":
                ensure => file,
                owner => 'root',
                group => 'root',
                mode => "644",
                content => " Balancermember $url \n",

NOTE: Defined resource will be called as Apache::Balancermember (1st character must be in CAPS)

2) Create one class worker.pp which will be called in member nodes (Node2 and Node3) section for exporting (member's Apache::Balancer values) their values in stored configuration or DB

class apache::worker {
        @@balancermember { "${fqdn}":
                url => "http://${fqdn}:81",

NOTE: As we have just passed ${fqdn} value, so content variable in apache::balancermember will be "Balancermember NODE-Name:81"

3) Create one more class lbm.pp which will be called in load balancer node i.e.; Node1 for importing Apache::Balancer values (defined in 1st step)

class apache::loadbalancer_members {
Apache::Balancermember <<||>> {notify => Service['httpd'], }  ###Import Balancermembers and refresh httpd service

4) For any new load balancer members (Node2 and Node3) section

node 'Node2'
        'Node 3' {
               include apache::worker         -------------------> from Step 2

5) For Load balancer node i.e.; Node1, section should be like

node 'Node1' {
            include apache::loadbalancer_members              ---------------> from Step 3

6) Try following command from Node1 server

puppet agent -t

This must create files /etc/httpd/conf.d.members/worker_node{2,3}.conf with following content

Balancermember http://node2:81  ------------> From Step1 Apache::Balancermember content variable
Balancermember http://node3:81  ------------> From Step1 Apache::Balancermember content variable

Thats it!! You are ready for more new load balancer members just add node name in Step 4 configuration!

No comments:

Post a Comment