| 1 |
# @summary |
| 2 |
# This type will setup a balancer member inside a listening service |
| 3 |
# configuration block in /etc/haproxy/haproxy.cfg on the load balancer. |
| 4 |
# |
| 5 |
# @note |
| 6 |
# Currently it only has the ability to specify the instance name, |
| 7 |
# ip address, port, and whether or not it is a backup. More features |
| 8 |
# can be added as needed. The best way to implement this is to export |
| 9 |
# this resource for all haproxy balancer member servers, and then collect |
| 10 |
# them on the main haproxy load balancer. |
| 11 |
# |
| 12 |
# @note |
| 13 |
# Currently requires the puppetlabs/concat module on the Puppet Forge and |
| 14 |
# uses storeconfigs on the Puppet Server to export/collect resources |
| 15 |
# from all balancer members. |
| 16 |
# |
| 17 |
# |
| 18 |
# @param listening_service |
| 19 |
# The haproxy service's instance name (or, the title of the |
| 20 |
# haproxy::listen resource). This must match up with a declared |
| 21 |
# haproxy::listen resource. |
| 22 |
# |
| 23 |
# @param ports |
| 24 |
# An array or commas-separated list of ports for which the balancer member |
| 25 |
# will accept connections from the load balancer. Note that cookie values |
| 26 |
# aren't yet supported, but shouldn't be difficult to add to the |
| 27 |
# configuration. If you use an array in server_names and ipaddresses, the |
| 28 |
# same port is used for all balancermembers. |
| 29 |
# |
| 30 |
# @param port |
| 31 |
# A port for server-template. It is an optional specification. |
| 32 |
# |
| 33 |
# @param server_names |
| 34 |
# The name of the balancer member server as known to haproxy in the |
| 35 |
# listening service's configuration block. This defaults to the |
| 36 |
# hostname. Can be an array of the same length as ipaddresses, |
| 37 |
# in which case a balancermember is created for each pair of |
| 38 |
# server_names and ipaddresses (in lockstep). |
| 39 |
# |
| 40 |
# @param ipaddresses |
| 41 |
# The ip address used to contact the balancer member server. |
| 42 |
# Can be an array, see documentation to server_names. |
| 43 |
# |
| 44 |
# @param prefix |
| 45 |
# A prefix for the server-template for the server names to be built. |
| 46 |
# |
| 47 |
# @param amount |
| 48 |
# If "amount" is provided, the server-template initializes <num> servers |
| 49 |
# with 1 up to <num> as server name suffixes. A range of numbers |
| 50 |
# <num_low>-<num_high> may also be used to use <num_low> up to |
| 51 |
# <num_high> as server name suffixes. |
| 52 |
# |
| 53 |
# @param fqdn |
| 54 |
# A FQDN for all the servers the server-template initializes. |
| 55 |
# |
| 56 |
# @param options |
| 57 |
# An array of options to be specified after the server declaration |
| 58 |
# in the listening service's configuration block. |
| 59 |
# |
| 60 |
# @param define_cookies |
| 61 |
# If true, then add "cookie SERVERID" stickiness options. |
| 62 |
# Default false. |
| 63 |
# |
| 64 |
# @param defaults |
| 65 |
# Name of the defaults section the backend or listener use. |
| 66 |
# Defaults to undef. |
| 67 |
# |
| 68 |
# @param config_file |
| 69 |
# Optional. Path of the config file where this entry will be added. |
| 70 |
# Assumes that the parent directory exists. |
| 71 |
# Default: $haproxy::params::config_file |
| 72 |
# |
| 73 |
# @param verifyhost |
| 74 |
# Optional. Will add the verifyhost option to the server line, using the |
| 75 |
# specific host from server_names as an argument. |
| 76 |
# Default: false |
| 77 |
# |
| 78 |
# @param weight |
| 79 |
# Optional. Will add the weight option to the server line |
| 80 |
# Default: undef |
| 81 |
# |
| 82 |
# @param instance |
| 83 |
# Optional. Defaults to 'haproxy' |
| 84 |
# |
| 85 |
# @param type |
| 86 |
# Optional. Defaults to 'server' |
| 87 |
# |
| 88 |
# @example |
| 89 |
# Exporting the resource for a balancer member: |
| 90 |
# |
| 91 |
# @@haproxy::balancermember { 'haproxy': |
| 92 |
# listening_service => 'puppet00', |
| 93 |
# ports => [8140], |
| 94 |
# server_names => $::hostname, |
| 95 |
# ipaddresses => $::ipaddress, |
| 96 |
# options => 'check', |
| 97 |
# } |
| 98 |
# |
| 99 |
# |
| 100 |
# Collecting the resource on a load balancer |
| 101 |
# |
| 102 |
# Haproxy::Balancermember <<| listening_service == 'puppet00' |>> |
| 103 |
# |
| 104 |
# @example |
| 105 |
# Creating the resource for multiple balancer members at once |
| 106 |
# (for single-pass installation of haproxy without requiring a first |
| 107 |
# pass to export the resources if you know the members in advance): |
| 108 |
# |
| 109 |
# haproxy::balancermember { 'haproxy': |
| 110 |
# listening_service => 'puppet00', |
| 111 |
# ports => 8140, |
| 112 |
# server_names => ['server01', 'server02'], |
| 113 |
# ipaddresses => ['192.168.56.200', '192.168.56.201'], |
| 114 |
# options => 'check', |
| 115 |
# } |
| 116 |
# |
| 117 |
# @example |
| 118 |
# Implemented in HAPROXY 1.8: |
| 119 |
# Set a template to initialize servers with shared parameters. |
| 120 |
# The names of these servers are built from <prefix> and <amount> parameters. |
| 121 |
# |
| 122 |
# Initializes 5 servers with srv1, srv2, srv3, srv4 and srv5 as names, |
| 123 |
# myserver.example.com as FQDN, 8140 as port, and health-check enabled. |
| 124 |
# |
| 125 |
# haproxy::balancermember { 'haproxy': |
| 126 |
# listening_service => 'puppet00', |
| 127 |
# type => 'server-template' |
| 128 |
# port => '8140', |
| 129 |
# prefix => 'srv', |
| 130 |
# amount => '1-5', |
| 131 |
# fqdn => 'myserver.example.com', |
| 132 |
# options => 'check', |
| 133 |
# } |
| 134 |
# |
| 135 |
# (this resource can be declared anywhere) |
| 136 |
# |
| 137 |
define haproxy::balancermember ( |
| 138 |
String $listening_service, |
| 139 |
Enum['server', 'default-server', 'server-template'] $type = 'server', |
| 140 |
Optional[Haproxy::Ports] $ports = undef, |
| 141 |
Optional[Variant[String, Stdlib::Port]] $port = undef, |
| 142 |
Variant[String[1], Array] $server_names = $facts['networking']['hostname'], |
| 143 |
Variant[String, Array] $ipaddresses = $facts['networking']['ip'], |
| 144 |
String $prefix = 'server', |
| 145 |
String $amount = '1', |
| 146 |
Optional[String] $fqdn = undef, |
| 147 |
Optional[Variant[String, Array]] $options = undef, |
| 148 |
Boolean $define_cookies = false, |
| 149 |
String $instance = 'haproxy', |
| 150 |
Optional[String] $defaults = undef, |
| 151 |
Optional[Stdlib::Absolutepath] $config_file = undef, |
| 152 |
Boolean $verifyhost = false, |
| 153 |
Optional[Variant[String, Integer]] $weight = undef, |
| 154 |
) { |
| 155 |
include haproxy::params |
| 156 |
|
| 157 |
if $instance == 'haproxy' { |
| 158 |
$instance_name = 'haproxy' |
| 159 |
$_config_file = pick($config_file, $haproxy::config_file) |
| 160 |
} else { |
| 161 |
$instance_name = "haproxy-${instance}" |
| 162 |
$_config_file = pick($config_file, inline_template($haproxy::params::config_file_tmpl)) |
| 163 |
} |
| 164 |
|
| 165 |
if $defaults == undef { |
| 166 |
$order = "20-${listening_service}-01-${name}" |
| 167 |
} else { |
| 168 |
$order = "25-${defaults}-${listening_service}-02-${name}" |
| 169 |
} |
| 170 |
|
| 171 |
$parameters = { |
| 172 |
'type' => $type, |
| 173 |
'ipaddresses' => $ipaddresses, |
| 174 |
'server_names' => $server_names, |
| 175 |
'ports' => $ports, |
| 176 |
'define_cookies' => $define_cookies, |
| 177 |
'options' => $options, |
| 178 |
'verifyhost' => $verifyhost, |
| 179 |
'weight' => $weight, |
| 180 |
'prefix' => $prefix, |
| 181 |
'amount' => $amount, |
| 182 |
'fqdn' => $fqdn, |
| 183 |
'port' => $port, |
| 184 |
} |
| 185 |
# Template uses $ipaddresses, $server_name, $ports, $option |
| 186 |
concat::fragment { "${instance_name}-${listening_service}_balancermember_${name}": |
| 187 |
order => $order, |
| 188 |
target => $_config_file, |
| 189 |
content => epp('haproxy/haproxy_balancermember.epp', $parameters), |
| 190 |
} |
| 191 |
} |