This is a repository that contains scripts that would allow you to create a Postgresql database cluster of a Master and 2 Slaves, while leveraging on the database being replicated and load balanced with HAProxy for High Availabilty.
In the root directory, you would see a folder called packer
, that is where the json
file packer would use to create the machine image is.
In order to create a machine image on AWS, please do the following:
-
export the AWS_ACCESS_KEY, AWS_SECRET_KEY & AWS_REGION in your terminal like this
export AWS_ACCESS_KEY=<paste your aws access keys here>
export AWS_SECRET_KEY=<paste your secret keys here>
export AWS_REGION=<paste your region here i.e. eu-west-1>
-
After exporting the variables above, run the command
packer build packer_templates/master_temp.json
to begin the master image build process and runpacker build packer_templates/slave_temp.json
to build the slave- After running the command you'll see the build flow on your terminal and you can also check your
AWS EC2 console
Thepacker build
commands above would build twoAmazon Machine Image
s where in themaster
database is configured with the scriptdb_config/master_script.sh
and the slave database is created with the scriptdb_config/slave_script.sh
. - Note that before you run the
packer
command above, ensure that you have AWSElastic IP
generated first and then associated with theMaster DB server
after launching the instance, there after in thedb_config/slave_script
file change-h < ELastic IP
to theMaster DB server
Elastic IP. Also, in thedb_config/pg_hba.conf
file, in thereplication
section where you would see these valueshost replication replicauser 54.76.225.227/24 md5
, please leave the IP address section empty in or change it toall
. - Also in
line 124
in thedb_config/recovery.conf
file, please replace the value assigned to thehost=< MASTER_DB_ELASTIC_IP >
with the Elatic IP address of the Master database, else the replication would not work as the slave databases would not be able to make any connection to the Master database.
- After running the command you'll see the build flow on your terminal and you can also check your
In order to build the HAProxy AMI on AWS, do the following:
- Create 3 Elastic IP addresses for the MASTER and the 2 SLAVES EC2 instances on AWS
- open the file
haproxy.cfg
in the directoryload_balancer/haproxy.cfg
- in the
haproxy.cfg
- in line 43 change
<MASTER ELASIC IP>
to one of the 3 Elastic IP addresses you get above - in line 44 change
<SLAVE_1 ELASIC IP>
to one of the remaining 2 Elastic IP addresses - in line 45 change
<SLAVE_2 ELASIC IP>
to one of the last remaining Elastic IP address
- in line 43 change
- run the command
packer build packer_templates/haproxy_temp.json
to begin the HAProxy image build process
To launch AMI machine images on AWS do the following
-
Depending on whether you intend to launch
Master | Slave
AMIs, change directory into thedb_config/terraform_< master | slave >
folder by running this commandcd db_config/terraform_< master | slave >
from your terminal -
In the
db_config_terraform_< master | slave >
directory, run the commandterraform init
-
Before running any other terraform commands, you should export these value
export AWS_AMI_ID=<AMI ID generated at the end of the packer build>
,AWS_SECRET_KEY=<AWS SECRET KEY>
,AWS_ACCESS_KEY=<AWS ACCESS KEY>
,AWS_REGION=<AWS REGION>
,MASTER_IP=<ELASTIC IP FOR MASTER>
,SLAVE1_IP=<ELASTIC IP FOR SLAVE1>
,SLAVE2_IP=<ELASTIC IP FOR SLAVE2>
in the terminal. -
After initialising the directory,
- For the
master DB
run the commandterraform plan -var="access_key=${AWS_ACCESS_KEY}" -var="secret_key=${AWS_SECRET_KEY}" -var="region=${AWS_REGION}" -var="ami_image_id=${PACKER_IMG}" -var="master_eip=${MASTER_IP}" -auto-approve
after the packer image has been build - After running
terraform plan
above, run the commandterraform apply -var="access_key=${AWS_ACCESS_KEY}" -var="secret_key=${AWS_SECRET_KEY}" -var="region=${AWS_REGION}" -var="ami_image_id=${PACKER_IMG}" -var="master_eip=${MASTER_IP}" -auto-approve
to begin the process of launching the instance. - For the
slave DB
:- NOTE. Same process above applies to the slave instances but the command to run is
terraform apply -var="access_key=${AWS_ACCESS_KEY}" -var="secret_key=${AWS_SECRET_KEY}" -var="region=${AWS_REGION}" -var="ami_image_id=${PACKER_IMG}" -var="slave1_eip=${SLAVE1_IP}" -var="slave2_eip=${SLAVE2_IP}" -auto-approve
- NOTE. Same process above applies to the slave instances but the command to run is
- For the
-
First create an Elastic IP on AWS for the HAProxy instance
-
In the
load_balancer/terraform_haproxy
directory, run the commandterraform init
-
Before running any other terraform commands, you should export these value
export AWS_AMI_ID=<AMI ID generated at the end of the packer build>
,AWS_SECRET_KEY=<AWS SECRET KEY>
,AWS_ACCESS_KEY=<AWS ACCESS KEY>
,AWS_REGION=<AWS REGION>
,HAPROXY_IP=<ELASTIC IP FOR HAPROXY>
in the terminal. -
change directory
cd load_balancer/terraform_haproxy
-
run the command
terraform plan -var="access_key=${AWS_ACCESS_KEY}" -var="secret_key=${AWS_SECRET_KEY}" -var="region=${AWS_REGION}" -var="ami_image_id=${PACKER_IMG}" -var="haproxy_eip=${HAPROXY_IP}" -auto-approve
after the packer image has been build -
After running
terraform plan
above, run the commandterraform apply -var="access_key=${AWS_ACCESS_KEY}" -var="secret_key=${AWS_SECRET_KEY}" -var="region=${AWS_REGION}" -var="ami_image_id=${PACKER_IMG}" -var="haproxy_eip=${HAPROXY_IP}" -auto-approve
to begin the process of launching the instance.
In the root of the project, there is a deploy.sh
script you can use to create both the packer AMI
and deploy & launch the AMI created on AWS.
To run the script, ensure that you have all the necesary environment variables described above exported into the environment. Then run the command ./deploy.sh
and then watch the terminal to see how the deployment process runs.
After the instance is launched, go to this address to see the health check of the instances <HAPROXY_IP:PORT>/admin?stats
Login with the username: admin1
, password: AdMiN123