Skip to content
This repository has been archived by the owner on Aug 4, 2018. It is now read-only.

Commit

Permalink
Merge pull request #3 from python/pgpool
Browse files Browse the repository at this point in the history
Implement a pgpool-II/keepalived pair to sit in front of the PostgreSQL cluster.
  • Loading branch information
ewdurbin committed Nov 24, 2013
2 parents 4d4cd0b + a8bcdc0 commit 058c333
Show file tree
Hide file tree
Showing 17 changed files with 455 additions and 5 deletions.
22 changes: 22 additions & 0 deletions Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,28 @@ Vagrant.configure("2") do |config|
end
end

config.vm.define "pgpool0" do |pgpool0|
pgpool0.vm.network "private_network", ip: "192.168.57.7"
pgpool0.vm.network "private_network", ip: "172.16.57.7"

pgpool0.vm.provision :salt do |s|
s.verbose = true
s.minion_config = "provisioning/salt/minion/pg_cluster-pgpool0"
s.run_highstate = true
end
end

config.vm.define "pgpool1" do |pgpool1|
pgpool1.vm.network "private_network", ip: "192.168.57.8"
pgpool1.vm.network "private_network", ip: "172.16.57.8"

pgpool1.vm.provision :salt do |s|
s.verbose = true
s.minion_config = "provisioning/salt/minion/pg_cluster-pgpool1"
s.run_highstate = true
end
end

end

end
9 changes: 9 additions & 0 deletions provisioning/salt/minion/pg_cluster-pgpool0
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
file_client: local
file_roots:
base:
- /srv/salt
grains:
roles:
- develop
- postgresql_pgpool
- primary
8 changes: 8 additions & 0 deletions provisioning/salt/minion/pg_cluster-pgpool1
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
file_client: local
file_roots:
base:
- /srv/salt
grains:
roles:
- develop
- postgresql_pgpool
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@

firewall:
ports:
- 5432

postgresql_cluster:
primary_server: 172.16.57.5
standby_servers:
- 172.16.57.6

pgpool_cluster:
nodes:
- 172.16.57.7
- 172.16.57.8

22 changes: 22 additions & 0 deletions provisioning/salt/roots/pillar/postgresql/pgpool.sls
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

pgpool_cluster:
nodes:
- 172.16.57.7
- 172.16.57.8
virtual_ip:
172.16.57.100

virtual_ips:
172.16.57.100:
auth_pass: sosecret
vrid: 51
cidr: 172.16.57.0/24

firewall:
ports:
- 9000
- 9694
- 9898
- 9999
- 5432

6 changes: 6 additions & 0 deletions provisioning/salt/roots/pillar/postgresql/postgresql.sls
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

firewall:
ports:
- 5432


7 changes: 6 additions & 1 deletion provisioning/salt/roots/pillar/top.sls
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ base:

'roles:postgresql_cluster':
- match: grain
- postgresql
- postgresql.cluster
- postgresql.postgresql
'roles:postgresql_pgpool':
- match: grain
- postgresql.cluster
- postgresql.pgpool
19 changes: 19 additions & 0 deletions provisioning/salt/roots/salt/_modules/ip_picker.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,22 @@ def ip_addrs(interface=None, include_loopback=False, cidr=None):
return [i for i in addrs if salt.utils.network.in_subnet(cidr, [i])]
else:
return addrs

def interfaces_for_cidr(cidr='0.0.0.0/0'):
'''
Return a dictionary of information about all the interfaces on the minion
which have an address in the givin CIDR.
CLI Example:
.. code-block:: bash
salt '*' network.interfaces_for_cidr cidr="192.168.1.1/24"
'''
interfaces = salt.utils.network.interfaces()
matched = []
for interface, data in interfaces.iteritems():
for net in data.get('inet'):
if salt.utils.network.in_subnet(cidr, [net['address']]):
matched.append(interface)
return list(set(matched))
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{% for vip, config in salt['pillar.get']('virtual_ips').iteritems() %}
{% set auth_pass = config.get('auth_pass', 'pass') %}
{% set vrid = config.get('vrid', '50') %}
{% set interface = salt['ip_picker.interfaces_for_cidr'](cidr=config['cidr'])[0] %}

vrrp_instance VI_{{ loop.index }} {
interface {{ interface }}
virtual_router_id {{ vrid }}
{% if 'primary' in salt['grains.get']('roles') %}
state MASTER
priority 150
{% else %}
state BACKUP
priority 100
{% endif %}
advert_int 1
authentication {
auth_type PASS
auth_pass {{ auth_pass }}
}
virtual_ipaddress {
{{ vip }}
}
}

{% endfor %}
24 changes: 24 additions & 0 deletions provisioning/salt/roots/salt/keepalived/init.sls
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
net.ipv4.ip_forward:
sysctl.present:
- value: 1

/etc/keepalived/keepalived.conf:
file.managed:
- source: salt://keepalived/config/keepalived.conf.jinja
- template: jinja
- user: root
- group: root
- mode: 640

keepalived:
pkg:
- installed
service:
- running
- enable: True
- reload: True
- watch:
- file: /etc/keepalived/keepalived.conf
- require:
- file: /etc/keepalived/keepalived.conf
- sysctl: net.ipv4.ip_forward
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
postgres:e8a48653851e28c69d0506508fb27fc5
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
#TYPE DATABASE USER ADDRESS METHOD
{% set config = salt['pillar.get']('postgresql_cluster', {}) %}
{% set pgpool_config = salt['pillar.get']('pgpool_cluster', {}) %}
local all all ident
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
host replication postgres {{ config.get('primary_server') }}/32 trust
{% for server in config.get('standby_servers') %}
host replication postgres {{ server }}/32 trust
{% endfor %}
{% for server in pgpool_config.get('nodes') %}
host all postgres {{ server }}/32 trust
{% endfor %}
Loading

0 comments on commit 058c333

Please sign in to comment.