MAAS HA

These are all the commands on both MAAS region controllers to get HA to work. We are using 16.04 Ubuntu.

Google postgresql HA setup doc:



/etc/network/interfaces    on maas1

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto ens33
iface ens33 inet static
address 192.168.1.150
netmask 255.255.255.0
gateway 192.168.1.1
dns-nameservers 10.1.1.100 192.168.1.1

auto enp3s1
iface enp3s1 inet static
address 10.1.1.100
netmask 255.255.255.0

/etc/network/interfaces    on maas2

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto enp2s0
iface enp2s0 inet static
address 192.168.1.151
netmask 255.255.255.0
gateway 192.168.1.1
dns-nameservers 10.1.1.101 192.168.1.1

auto enp3s6
iface enp3s6 inet static
address 10.1.1.101
netmask 255.255.255.0

Update to the stable version of MAAS then update:

sudo add-apt-repository ppa:maas/stable

sudo apt update

sudo apt upgrade


On both maas1 and maas2 install the region controller package:

sudo apt install maas-region-controller

On maas1 server to create user:
sudo maas createadmin

On maas1 server create a ssh key:

On command line generate the ssh key
ssh-keygen -t rsa

Just press enter through the prompts.
Copy the key to the gui, display the key with the cat command
cat ~/.ssh/id_rsa.pub

On both MAAS1 and MAAS2 servers create a replication user:

sudo -u postgres createuser -U postgres repuser -P -c 5 --replication

On both maas1 and maas2 server create an archive directory:

mkdir -p /var/lib/postgresql/9.5/main/mnt/server/archivedir

Add these lines to the bottom of your pg_hba.conf : 

sudo nano ../../etc/postgresql/9.5/main/pg_hba.conf

MAAS1

host     replication     repuser         192.168.1.151/32        md5
host     all             all             192.168.1.151/32        md5


MAAS2

host     replication     repuser         192.168.1.150/32        md5
host     all             all             192.168.1.150/32        md5


Make your postgresql.conf files look like this on both maas1 and maas2 servers:

sudo nano ../../etc/postgresql/9.5/main/postgresql.conf

Postgresql.conf

# -----------------------------
# PostgreSQL configuration file
# -----------------------------
#
# This file consists of lines of the form:
#
#   name = value
#
# (The "=" is optional.)  Whitespace may be used.  Comments are introduced with
# "#" anywhere on a line.  The complete list of parameter names and allowed
# values can be found in the PostgreSQL documentation.
#
# The commented-out settings shown in this file represent the default values.
# Re-commenting a setting is NOT sufficient to revert it to the default value;
# you need to reload the server.
#
# This file is read on server startup and when the server receives a SIGHUP
# signal.  If you edit the file on a running system, you have to SIGHUP the
# server for the changes to take effect, or use "pg_ctl reload".  Some
# parameters, which are marked below, require a server shutdown and restart to
# take effect.
#
# Any parameter can also be given as a command-line option to the server, e.g.,
# "postgres -c log_connections=on".  Some parameters can be changed at run time
# with the "SET" SQL command.
#
# Memory units:  kB = kilobytes        Time units:  ms  = milliseconds
#                MB = megabytes                     s   = seconds
#                GB = gigabytes                     min = minutes
#                TB = terabytes                     h   = hours
#                                                   d   = days


#------------------------------------------------------------------------------
# FILE LOCATIONS
#------------------------------------------------------------------------------

# The default values of these variables are driven from the -D command-line
# option or PGDATA environment variable, represented here as ConfigDir.

data_directory = '/var/lib/postgresql/9.5/main'         # use data in another directory
                                        # (change requires restart)
hba_file = '/etc/postgresql/9.5/main/pg_hba.conf'       # host-based authentication file
                                        # (change requires restart)
ident_file = '/etc/postgresql/9.5/main/pg_ident.conf'   # ident configuration file
                                        # (change requires restart)

# If external_pid_file is not explicitly set, no extra PID file is written.
external_pid_file = '/var/run/postgresql/9.5-main.pid'                  # write an extra PID file
                                        # (change requires restart)


#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------

# - Connection Settings -

listen_addresses = '*'          # what IP address(es) to listen on;
                                        # comma-separated list of addresses;
                                        # defaults to 'localhost'; use '*' for all
                                        # (change requires restart)
port = 5432                             # (change requires restart)
max_connections = 100                   # (change requires restart)
#superuser_reserved_connections = 3     # (change requires restart)
unix_socket_directories = '/var/run/postgresql' # comma-separated list of directories
                                        # (change requires restart)
#unix_socket_group = ''                 # (change requires restart)
#unix_socket_permissions = 0777         # begin with 0 to use octal notation
                                        # (change requires restart)
#bonjour = off                          # advertise server via Bonjour
                                        # (change requires restart)
#bonjour_name = ''                      # defaults to the computer name
                                        # (change requires restart)

# - Security and Authentication -

#authentication_timeout = 1min          # 1s-600s
ssl = true                              # (change requires restart)
#ssl_ciphers = 'HIGH:MEDIUM:+3DES:!aNULL' # allowed SSL ciphers
                                        # (change requires restart)
#ssl_prefer_server_ciphers = on         # (change requires restart)
#ssl_ecdh_curve = 'prime256v1'          # (change requires restart)
ssl_cert_file = '/etc/ssl/certs/ssl-cert-snakeoil.pem'          # (change requires restart)
ssl_key_file = '/etc/ssl/private/ssl-cert-snakeoil.key'         # (change requires restart)
#ssl_ca_file = ''                       # (change requires restart)
#ssl_crl_file = ''                      # (change requires restart)
#password_encryption = on
#db_user_namespace = off
#row_security = on

# GSSAPI using Kerberos
#krb_server_keyfile = ''
#krb_caseins_users = off

# - TCP Keepalives -
# see "man 7 tcp" for details

#tcp_keepalives_idle = 0                # TCP_KEEPIDLE, in seconds;
                                        # 0 selects the system default
#tcp_keepalives_interval = 0            # TCP_KEEPINTVL, in seconds;
                                        # 0 selects the system default
#tcp_keepalives_count = 0               # TCP_KEEPCNT;
                                        # 0 selects the system default


#------------------------------------------------------------------------------
# RESOURCE USAGE (except WAL)
#------------------------------------------------------------------------------

# - Memory -

shared_buffers = 128MB                  # min 128kB
                                        # (change requires restart)
#huge_pages = try                       # on, off, or try
                                        # (change requires restart)
#temp_buffers = 8MB                     # min 800kB
#max_prepared_transactions = 0          # zero disables the feature
                                        # (change requires restart)
# Caution: it is not advisable to set max_prepared_transactions nonzero unless
# you actively intend to use prepared transactions.
#work_mem = 4MB                         # min 64kB
#maintenance_work_mem = 64MB            # min 1MB
#autovacuum_work_mem = -1               # min 1MB, or -1 to use maintenance_work_mem
#max_stack_depth = 2MB                  # min 100kB
dynamic_shared_memory_type = posix      # the default is the first option
                                        # supported by the operating system:
                                        #   posix
                                        #   sysv
                                        #   windows
                                        #   mmap
                                        # use none to disable dynamic shared memory

# - Disk -

#temp_file_limit = -1                   # limits per-session temp file space
                                        # in kB, or -1 for no limit

# - Kernel Resource Usage -

#max_files_per_process = 1000           # min 25
                                        # (change requires restart)
#shared_preload_libraries = ''          # (change requires restart)

# - Cost-Based Vacuum Delay -

#vacuum_cost_delay = 0                  # 0-100 milliseconds
#vacuum_cost_page_hit = 1               # 0-10000 credits
#vacuum_cost_page_miss = 10             # 0-10000 credits
#vacuum_cost_page_dirty = 20            # 0-10000 credits
#vacuum_cost_limit = 200                # 1-10000 credits

# - Background Writer -

#bgwriter_delay = 200ms                 # 10-10000ms between rounds
#bgwriter_lru_maxpages = 100            # 0-1000 max buffers written/round
#bgwriter_lru_multiplier = 2.0          # 0-10.0 multipler on buffers scanned/round

# - Asynchronous Behavior -

#effective_io_concurrency = 1           # 1-1000; 0 disables prefetching
#max_worker_processes = 8


#------------------------------------------------------------------------------
# WRITE AHEAD LOG
#------------------------------------------------------------------------------

# - Settings -

wal_level = hot_standby                 # minimal, archive, hot_standby, or logical
                                        # (change requires restart)
#fsync = on                             # turns forced synchronization on or off
#synchronous_commit = on                # synchronization level;
                                        # off, local, remote_write, or on
#wal_sync_method = fsync                # the default is the first option
                                        # supported by the operating system:
                                        #   open_datasync
                                        #   fdatasync (default on Linux)
                                        #   fsync
                                        #   fsync_writethrough
                                        #   open_sync
#full_page_writes = on                  # recover from partial page writes
#wal_compression = off                  # enable compression of full-page writes
#wal_log_hints = off                    # also do full page writes of non-critical updates
                                        # (change requires restart)
#wal_buffers = -1                       # min 32kB, -1 sets based on shared_buffers
                                        # (change requires restart)
#wal_writer_delay = 200ms               # 1-10000 milliseconds

#commit_delay = 0                       # range 0-100000, in microseconds
#commit_siblings = 5                    # range 1-1000

# - Checkpoints -

#checkpoint_timeout = 5min              # range 30s-1h
#max_wal_size = 1GB
#min_wal_size = 80MB
#checkpoint_completion_target = 0.5     # checkpoint target duration, 0.0 - 1.0
#checkpoint_warning = 30s               # 0 disables

# - Archiving -

archive_mode = on               # enables archiving; off, on, or always
                                # (change requires restart)
archive_command = 'test ! -f /var/lib/postgresql/9.5/main/mnt/server/archivedir/%f && cp %p /var/lib/postgresql/9.5/main/mnt/server/archivedir/%f'               # command to use to archive a logfile segment
                                # placeholders: %p = path of file to archive
                                #               %f = file name only
                                # e.g. 'test ! -f /mnt/server/archivedir/%f && cp %p /mnt/server/archivedir/%f'
#archive_timeout = 0            # force a logfile segment switch after this
                                # number of seconds; 0 disables


#------------------------------------------------------------------------------
# REPLICATION
#------------------------------------------------------------------------------

# - Sending Server(s) -

# Set these on the master and on any standby that will send replication data.

max_wal_senders = 3             # max number of walsender processes
                                # (change requires restart)
#wal_keep_segments = 0          # in logfile segments, 16MB each; 0 disables
#wal_sender_timeout = 60s       # in milliseconds; 0 disables

#max_replication_slots = 0      # max number of replication slots
                                # (change requires restart)
#track_commit_timestamp = off   # collect timestamp of transaction commit
                                # (change requires restart)

# - Master Server -

# These settings are ignored on a standby server.

#synchronous_standby_names = '' # standby servers that provide sync rep
                                # comma-separated list of application_name
                                # from standby(s); '*' = all
#vacuum_defer_cleanup_age = 0   # number of xacts by which cleanup is delayed

# - Standby Servers -

# These settings are ignored on a master server.

hot_standby = on                        # "on" allows queries during recovery
                                        # (change requires restart)
#max_standby_archive_delay = 30s        # max delay before canceling queries
                                        # when reading WAL from archive;
                                        # -1 allows indefinite delay
#max_standby_streaming_delay = 30s      # max delay before canceling queries
                                        # when reading streaming WAL;
                                        # -1 allows indefinite delay
#wal_receiver_status_interval = 10s     # send replies at least this often
                                        # 0 disables
#hot_standby_feedback = off             # send info from standby to prevent
                                        # query conflicts
#wal_receiver_timeout = 60s             # time that receiver waits for
                                        # communication from master
                                        # in milliseconds; 0 disables
#wal_retrieve_retry_interval = 5s       # time to wait before retrying to
                                        # retrieve WAL after a failed attempt


#------------------------------------------------------------------------------
# QUERY TUNING
#------------------------------------------------------------------------------

# - Planner Method Configuration -

#enable_bitmapscan = on
#enable_hashagg = on
#enable_hashjoin = on
#enable_indexscan = on
#enable_indexonlyscan = on
#enable_material = on
#enable_mergejoin = on
#enable_nestloop = on
#enable_seqscan = on
#enable_sort = on
#enable_tidscan = on

# - Planner Cost Constants -

#seq_page_cost = 1.0                    # measured on an arbitrary scale
#random_page_cost = 4.0                 # same scale as above
#cpu_tuple_cost = 0.01                  # same scale as above
#cpu_index_tuple_cost = 0.005           # same scale as above
#cpu_operator_cost = 0.0025             # same scale as above
#effective_cache_size = 4GB

# - Genetic Query Optimizer -

#geqo = on
#geqo_threshold = 12
#geqo_effort = 5                        # range 1-10
#geqo_pool_size = 0                     # selects default based on effort
#geqo_generations = 0                   # selects default based on effort
#geqo_selection_bias = 2.0              # range 1.5-2.0
#geqo_seed = 0.0                        # range 0.0-1.0

# - Other Planner Options -

#default_statistics_target = 100        # range 1-10000
#constraint_exclusion = partition       # on, off, or partition
#cursor_tuple_fraction = 0.1            # range 0.0-1.0
#from_collapse_limit = 8
#join_collapse_limit = 8                # 1 disables collapsing of explicit
                                        # JOIN clauses


#------------------------------------------------------------------------------
# ERROR REPORTING AND LOGGING
#------------------------------------------------------------------------------

# - Where to Log -

#log_destination = 'stderr'             # Valid values are combinations of
                                        # stderr, csvlog, syslog, and eventlog,
                                        # depending on platform.  csvlog
                                        # requires logging_collector to be on.

# This is used when logging to stderr:
#logging_collector = off                # Enable capturing of stderr and csvlog
                                        # into log files. Required to be on for
                                        # csvlogs.
                                        # (change requires restart)

# These are only used if logging_collector is on:
#log_directory = 'pg_log'               # directory where log files are written,
                                        # can be absolute or relative to PGDATA
#log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'        # log file name pattern,
                                        # can include strftime() escapes
#log_file_mode = 0600                   # creation mode for log files,
                                        # begin with 0 to use octal notation
#log_truncate_on_rotation = off         # If on, an existing log file with the
                                        # same name as the new log file will be
                                        # truncated rather than appended to.
                                        # But such truncation only occurs on
                                        # time-driven rotation, not on restarts
                                        # or size-driven rotation.  Default is
                                        # off, meaning append to existing files
                                        # in all cases.
#log_rotation_age = 1d                  # Automatic rotation of logfiles will
                                        # happen after that time.  0 disables.
#log_rotation_size = 10MB               # Automatic rotation of logfiles will
                                        # happen after that much log output.
                                        # 0 disables.

# These are relevant when logging to syslog:
#syslog_facility = 'LOCAL0'
#syslog_ident = 'postgres'

# This is only relevant when logging to eventlog (win32):
#event_source = 'PostgreSQL'

# - When to Log -

#client_min_messages = notice           # values in order of decreasing detail:
                                        #   debug5
                                        #   debug4
                                        #   debug3
                                        #   debug2
                                        #   debug1
                                        #   log
                                        #   notice
                                        #   warning
                                        #   error

#log_min_messages = warning             # values in order of decreasing detail:
                                        #   debug5
                                        #   debug4
                                        #   debug3
                                        #   debug2
                                        #   debug1
                                        #   info
                                        #   notice
                                        #   warning
                                        #   error
                                        #   log
                                        #   fatal
                                        #   panic

#log_min_error_statement = error        # values in order of decreasing detail:
                                        #   debug5
                                        #   debug4
                                        #   debug3
                                        #   debug2
                                        #   debug1
                                        #   info
                                        #   notice
                                        #   warning
                                        #   error
                                        #   log
                                        #   fatal
                                        #   panic (effectively off)

#log_min_duration_statement = -1        # -1 is disabled, 0 logs all statements
                                        # and their durations, > 0 logs only
                                        # statements running at least this number
                                        # of milliseconds


# - What to Log -

#debug_print_parse = off
#debug_print_rewritten = off
#debug_print_plan = off
#debug_pretty_print = on
#log_checkpoints = off
#log_connections = off
#log_disconnections = off
#log_duration = off
#log_error_verbosity = default          # terse, default, or verbose messages
#log_hostname = off
log_line_prefix = '%t [%p-%l] %q%u@%d '                 # special values:
                                        #   %a = application name
                                        #   %u = user name
                                        #   %d = database name
                                        #   %r = remote host and port
                                        #   %h = remote host
                                        #   %p = process ID
                                        #   %t = timestamp without milliseconds
                                        #   %m = timestamp with milliseconds
                                        #   %i = command tag
                                        #   %e = SQL state
                                        #   %c = session ID
                                        #   %l = session line number
                                        #   %s = session start timestamp
                                        #   %v = virtual transaction ID
                                        #   %x = transaction ID (0 if none)
                                        #   %q = stop here in non-session
                                        #        processes
                                        #   %% = '%'
                                        # e.g. '<%u%%%d> '
#log_lock_waits = off                   # log lock waits >= deadlock_timeout
#log_statement = 'none'                 # none, ddl, mod, all
#log_replication_commands = off
#log_temp_files = -1                    # log temporary files equal or larger
                                        # than the specified size in kilobytes;
                                        # -1 disables, 0 logs all temp files
log_timezone = 'localtime'


# - Process Title -

#cluster_name = ''                      # added to process titles if nonempty
                                        # (change requires restart)
#update_process_title = on


#------------------------------------------------------------------------------
# RUNTIME STATISTICS
#------------------------------------------------------------------------------

# - Query/Index Statistics Collector -

#track_activities = on
#track_counts = on
#track_io_timing = off
#track_functions = none                 # none, pl, all
#track_activity_query_size = 1024       # (change requires restart)
stats_temp_directory = '/var/run/postgresql/9.5-main.pg_stat_tmp'


# - Statistics Monitoring -

#log_parser_stats = off
#log_planner_stats = off
#log_executor_stats = off
#log_statement_stats = off


#------------------------------------------------------------------------------
# AUTOVACUUM PARAMETERS
#------------------------------------------------------------------------------

#autovacuum = on                        # Enable autovacuum subprocess?  'on'
                                        # requires track_counts to also be on.
#log_autovacuum_min_duration = -1       # -1 disables, 0 logs all actions and
                                        # their durations, > 0 logs only
                                        # actions running at least this number
                                        # of milliseconds.
#autovacuum_max_workers = 3             # max number of autovacuum subprocesses
                                        # (change requires restart)
#autovacuum_naptime = 1min              # time between autovacuum runs
#autovacuum_vacuum_threshold = 50       # min number of row updates before
                                        # vacuum
#autovacuum_analyze_threshold = 50      # min number of row updates before
                                        # analyze
#autovacuum_vacuum_scale_factor = 0.2   # fraction of table size before vacuum
#autovacuum_analyze_scale_factor = 0.1  # fraction of table size before analyze
#autovacuum_freeze_max_age = 200000000  # maximum XID age before forced vacuum
                                        # (change requires restart)
#autovacuum_multixact_freeze_max_age = 400000000        # maximum multixact age
                                        # before forced vacuum
                                        # (change requires restart)
#autovacuum_vacuum_cost_delay = 20ms    # default vacuum cost delay for
                                        # autovacuum, in milliseconds;
                                        # -1 means use vacuum_cost_delay
#autovacuum_vacuum_cost_limit = -1      # default vacuum cost limit for
                                        # autovacuum, -1 means use
                                        # vacuum_cost_limit


#------------------------------------------------------------------------------
# CLIENT CONNECTION DEFAULTS
#------------------------------------------------------------------------------

# - Statement Behavior -

#search_path = '"$user", public'        # schema names
#default_tablespace = ''                # a tablespace name, '' uses the default
#temp_tablespaces = ''                  # a list of tablespace names, '' uses
                                        # only default tablespace
#check_function_bodies = on
#default_transaction_isolation = 'read committed'
#default_transaction_read_only = off
#default_transaction_deferrable = off
#session_replication_role = 'origin'
#statement_timeout = 0                  # in milliseconds, 0 is disabled
#lock_timeout = 0                       # in milliseconds, 0 is disabled
#vacuum_freeze_min_age = 50000000
#vacuum_freeze_table_age = 150000000
#vacuum_multixact_freeze_min_age = 5000000
#vacuum_multixact_freeze_table_age = 150000000
#bytea_output = 'hex'                   # hex, escape
#xmlbinary = 'base64'
#xmloption = 'content'
#gin_fuzzy_search_limit = 0
#gin_pending_list_limit = 4MB

# - Locale and Formatting -

datestyle = 'iso, mdy'
#intervalstyle = 'postgres'
timezone = 'localtime'
#timezone_abbreviations = 'Default'     # Select the set of available time zone
                                        # abbreviations.  Currently, there are
                                        #   Default
                                        #   Australia (historical usage)
                                        #   India
                                        # You can create your own file in
                                        # share/timezonesets/.
#extra_float_digits = 0                 # min -15, max 3
#client_encoding = sql_ascii            # actually, defaults to database
                                        # encoding

# These settings are initialized by initdb, but they can be changed.
lc_messages = 'en_US.UTF-8'                     # locale for system error message
                                        # strings
lc_monetary = 'en_US.UTF-8'                     # locale for monetary formatting
lc_numeric = 'en_US.UTF-8'                      # locale for number formatting
lc_time = 'en_US.UTF-8'                         # locale for time formatting

# default configuration for text search
default_text_search_config = 'pg_catalog.english'

# - Other Defaults -

#dynamic_library_path = '$libdir'
#local_preload_libraries = ''
#session_preload_libraries = ''


#------------------------------------------------------------------------------
# LOCK MANAGEMENT
#------------------------------------------------------------------------------

#deadlock_timeout = 1s
#max_locks_per_transaction = 64         # min 10
                                        # (change requires restart)
#max_pred_locks_per_transaction = 64    # min 10
                                        # (change requires restart)


#------------------------------------------------------------------------------
# VERSION/PLATFORM COMPATIBILITY
#------------------------------------------------------------------------------

# - Previous PostgreSQL Versions -

#array_nulls = on
#backslash_quote = safe_encoding        # on, off, or safe_encoding
#default_with_oids = off
#escape_string_warning = on
#lo_compat_privileges = off
#operator_precedence_warning = off
#quote_all_identifiers = off
#sql_inheritance = on
#standard_conforming_strings = on
#synchronize_seqscans = on

# - Other Platforms and Clients -

#transform_null_equals = off


#------------------------------------------------------------------------------
# ERROR HANDLING
#------------------------------------------------------------------------------

#exit_on_error = off                    # terminate session on any error?
#restart_after_crash = on               # reinitialize after backend crash?


#------------------------------------------------------------------------------
# CONFIG FILE INCLUDES
#------------------------------------------------------------------------------

# These options allow settings to be loaded from files other than the
# default postgresql.conf.

#include_dir = 'conf.d'                 # include files ending in '.conf' from
                                        # directory 'conf.d'
#include_if_exists = 'exists.conf'      # include file only if it exists
#include = 'special.conf'               # include file


#------------------------------------------------------------------------------
# CUSTOMIZED OPTIONS
#------------------------------------------------------------------------------

# Add settings for extensions here


Restart postgresql on MAAS1 :

sudo service postgresql restart


On MAAS2 server run the database backup:

Stop postgresql:

sudo service postgresql stop

Login as root:

sudo -s

Move current database files:

mv ../../var/lib/postgresql/9.5/main ../../var/lib/postgresql/9.5/main_old

Backip database on to MAAS2:

sudo -u postgres pg_basebackup -h 192.168.1.150 -D /var/lib/postgresql/9.5/main -U repuser -v -P --xlog-method=stream

On maas2, setup recovery file.:

Copy recovery.conf from template:

 cp -avr ../../usr/share/postgresql/9.5/recovery.conf.sample /../../var/lib/postgresql/9.5/main/recovery.conf

The recovery.conf file should look like this:

Recovery.conf

# -------------------------------
# PostgreSQL recovery config file
# -------------------------------
#
# Edit this file to provide the parameters that PostgreSQL needs to
# perform an archive recovery of a database, or to act as a replication
# standby.
#
# If "recovery.conf" is present in the PostgreSQL data directory, it is
# read on postmaster startup.  After successful recovery, it is renamed
# to "recovery.done" to ensure that we do not accidentally re-enter
# archive recovery or standby mode.
#
# This file consists of lines of the form:
#
#   name = value
#
# Comments are introduced with '#'.
#
# The complete list of option names and allowed values can be found
# in the PostgreSQL documentation.
#
#---------------------------------------------------------------------------
# ARCHIVE RECOVERY PARAMETERS
#---------------------------------------------------------------------------
#
# restore_command
#
# specifies the shell command that is executed to copy log files
# back from archival storage.  The command string may contain %f,
# which is replaced by the name of the desired log file, and %p,
# which is replaced by the absolute path to copy the log file to.
#
# This parameter is *required* for an archive recovery, but optional
# for streaming replication.
#
# It is important that the command return nonzero exit status on failure.
# The command *will* be asked for log files that are not present in the
# archive; it must return nonzero when so asked.
#
# NOTE that the basename of %p will be different from %f; do not
# expect them to be interchangeable.
#
#restore_command = ''           # e.g. 'cp /mnt/server/archivedir/%f %p'
#
#
# archive_cleanup_command
#
# specifies an optional shell command to execute at every restartpoint.
# This can be useful for cleaning up the archive of a standby server.
#
#archive_cleanup_command = ''
#
# recovery_end_command
#
# specifies an optional shell command to execute at completion of recovery.
# This can be useful for cleaning up after the restore_command.
#
#recovery_end_command = ''
#
#---------------------------------------------------------------------------
# RECOVERY TARGET PARAMETERS
#---------------------------------------------------------------------------
#
# By default, recovery will rollforward to the end of the WAL log.
# If you want to stop rollforward at a specific point, you
# must set a recovery target.
#
# You may set a recovery target either by transactionId, by name,
# or by timestamp. Recovery may either include or exclude the
# transaction(s) with the recovery target value (ie, stop either
# just after or just before the given target, respectively).
#
#
#recovery_target_name = ''      # e.g. 'daily backup 2011-01-26'
#
#recovery_target_time = ''      # e.g. '2004-07-14 22:39:00 EST'
#
#recovery_target_xid = ''
#
#recovery_target_inclusive = true
#
#
# Alternatively, you can request stopping as soon as a consistent state
# is reached, by uncommenting this option.
#
#recovery_target = 'immediate'
#
#
# If you want to recover into a timeline other than the "main line" shown in
# pg_control, specify the timeline number here, or write 'latest' to get
# the latest branch for which there's a history file.
#
#recovery_target_timeline = 'latest'
#
#
# If recovery_target_action = 'pause', recovery will pause when the
# recovery target is reached. The pause state will continue until
# pg_xlog_replay_resume() is called. This setting has no effect if
# no recovery target is set. If hot_standby is not enabled then the
# server will shutdown instead, though you may request this in
# any case by specifying 'shutdown'.
#
#recovery_target_action = 'pause'
#
#---------------------------------------------------------------------------
# STANDBY SERVER PARAMETERS
#---------------------------------------------------------------------------
#
# standby_mode
#
# When standby_mode is enabled, the PostgreSQL server will work as a
# standby. It will continuously wait for the additional XLOG records, using
# restore_command and/or primary_conninfo.
#
#standby_mode = off
#
# primary_conninfo
#
# If set, the PostgreSQL server will try to connect to the primary using this
# connection string and receive XLOG records continuously.
#
#primary_conninfo = ''          # e.g. 'host=localhost port=5432'
#
# If set, the PostgreSQL server will use the specified replication slot when
# connecting to the primary via streaming replication to control resource
# removal on the upstream node. This setting has no effect if primary_conninfo
# is not set.
#
#primary_slot_name = ''
#
# By default, a standby server keeps restoring XLOG records from the
# primary indefinitely. If you want to stop the standby mode, finish recovery
# and open the system in read/write mode, specify a path to a trigger file.
# The server will poll the trigger file path periodically and start as a
# primary server when it's found.
#
#trigger_file = ''
#
# By default, a standby server restores XLOG records from the primary as
# soon as possible. If you want to explicitly delay the replay of committed
# transactions from the master, specify a minimum apply delay. For example,
# if you set this parameter to 5min, the standby will replay each transaction
# commit only when the system time on the standby is at least five minutes
# past the commit time reported by the master.
#
#recovery_min_apply_delay = 0
#
#---------------------------------------------------------------------------
# HOT STANDBY PARAMETERS
#---------------------------------------------------------------------------
#
# Hot Standby related parameters are listed in postgresql.conf
#
#---------------------------------------------------------------------------
standby_mode = on
primary_conninfo = 'host=192.168.1.150 port=5432 user=repuser password=jensen'
trigger_file = '/failed/trigger/trigger'


On MAAS2 server configure the region to point at the main database:

Get back in root:

sudo -s

Stop region controller:

sudo systemctl stop maas-regiond

Remove password files:

sudo rm /var/lib/maas/{maas_id,secret}

Make your regiond.conf file look like this on MAAS2:

sudo nano /etc/maas/regiond.conf

Region.conf MAAS2

database_host: 192.168.1.150
database_name: maasdb
database_pass: zvJlugqdjw0M
database_user: maas
maas_url: http://192.168.1.155:5240/MAAS


Set regiond.conf owner:

sudo chown root:maas /etc/maas/regiond.conf

Set regiond.conf permissions:

sudo chmod 640 /etc/maas/regiond.conf

Restart region controller:

sudo systemctl restart maas-regiond


Start POSTGRESQL:

sudo service postgresql restar

Now you should see the second region controller in the MAAS gui.


On both maas1 and maas2 servers, clean up the bind9 conflicts:

sudo maas-region edit_named_options --migrate-conflicting-options

Restart bind:

sudo systemctl restart bind9


Setup HAproxy for load balancing on both MAAS1 and MAAS2 servers:

Stop apache:

sudo systemctl stop apache2

Disable apache:

sudo systemctl disable apache2

Install HAPROXY:

sudo apt install haproxy

Add these lines to the bottom of your haproxy.conf file:

sudo nano /etc/haproxy/haproxy.cfg

haproxy.cfg

frontend maas
    bind    *:80
    retries 3
    option  redispatch
    option  http-server-close
    default_backend maas

backend maas
    timeout server 30s
    balance roundrobin
    server localhost localhost:5240 check
    server maas1 192.168.1.150:5240 check
    server maas2 192.168.1.151:5240 check

Restart haproxy of both nodes:


sudo systemctl restart haproxy


Now if you refresh the gui you will see the region jump from one server to the other back and forth.

Now setup the virtual IP interface with keepalive failover on both MAAS1 and MAAS2 servers:

Install Keepalived:

sudo apt install keepalived

Promote ip_vs:

sudo modprobe ip_vs

Add ip_vs to modules list:

sudo sh -c 'echo modprobe ip_vs >> /etc/modules'

Go into root mode:

sudo -s

Add IPV4 option to config file:

sudo sh -c 'echo net.ipv4.ip_nonlocal_bind=1' > /etc/sysctl.d/60-keepalived-nonlocal.conf

Restart linux utilities:

sudo systemctl restart procps

Create a generic keepalived.conf file:

sudo sh -c 'cat >>/etc/keepalived/keepalived.conf <<EOL
     # Un-comment when using haproxy.
     #vrrp_script chk_haproxy {
     #    script "killall -0 haproxy"
     #    interval 2
     #}
     # Un-comment when using apache2.
     #vrrp_script chk_apache2 {
     #    script "killall -0 apache2"
     #    interval 2
     #}
     vrrp_script chk_named {
         script "killall -0 named"
         interval 2
     }
     vrrp_instance maas_region {
         state MASTER
         interface <interface_name>
         priority <priority>
         virtual_router_id 51
         authentication {
             auth_type PASS
            auth_pass <random_password>
        }
        track_script {
            # Un-comment when using haproxy
            #chk_haproxy
            # Un-comment when using apache2
            #chk_apache2
            chk_named
        }
        virtual_ipaddress {
            <vip>
        }
    }'

Make your keepalived.conf file look like this, change your password and interface names:

sudo nano /etc/keepalived/keepalived.conf
 

Keepalived.conf

# Un-comment when using haproxy.
vrrp_script chk_haproxy {
    script "killall -0 haproxy"
    interval 2
}

# Un-comment when using apache2.
#vrrp_script chk_apache2 {
#    script "killall -0 apache2"
#    interval 2
#}

vrrp_script chk_named {
    script "killall -0 named"
    interval 2
}

vrrp_instance maas_region {
    state BACKUP
    interface enp2s0
    priority 100
    virtual_router_id 51
    notify /failed/check.sh
    authentication {
        auth_type PASS
        auth_pass jensen
    }

    track_script {
        # Un-comment when using haproxy
        chk_haproxy
        # Un-comment when using apache2
        #chk_apache2
        chk_named
    }

    virtual_ipaddress {
        192.168.1.155
    }
}
vrrp_instance maas_region_private {
    state BACKUP
    interface enp3s1
    priority 100
    virtual_router_id 52
        authentication {
        auth_type PASS
        auth_pass jensen
    }

    track_script {
        # Un-comment when using haproxy
        chk_haproxy
        # Un-comment when using apache2
        #chk_apache2
        chk_named
    }

    virtual_ipaddress {
        10.1.1.155
    }
}



Restart keepalived on both MAAS1 and MAAS2:

sudo systemctl restart keepalived


Now you should be able to access both nodes from 192.168.1.155.


Now lets add some scripts to automate our fail over.

Create this directory on both servers:

sudo mkdir -p /failed/trigger

Create a check script for keepalived to use on both servers:

sudo nano /failed/check.sh

check.sh will look like this on both servers, make sure your notify command is correct in your sudo nano /etc/keepalived/keepalived.conf see above:


check.sh

#!/bin/bash
TYPE=$1
NAME=$2
STATE=$3
case $STATE in
        "MASTER") logger KEEPALIVE $STATE
                  sudo /failed/failover.sh >> /tmp/fail_output.log
                  ;;
        "BACKUP") logger KEEPALIVE $STATE
                  ;;
        "FAULT")  logger KEEPALIVE $STATE
                  exit 0
                  ;;
        *)        /sbin/logger "ipsec unknown state"
                  exit 1
                  ;;
esac


Create a failover script for check.sh to use on both servers:

sudo nano /failed/failover.sh

Failover.sh

#!/bin/bash
if [ -f /var/lib/postgresql/9.5/main/recovery.conf ]
  then
    echo "recovery file exists."

logger failover started
sudo maas-region local_config_set --database-host localhost
sudo systemctl restart maas-regiond
echo "fast" >> /failed/trigger/trigger
sleep 30
cat /var/log/postgresql/postgresql-9.5-main.log |grep "database system is ready" |tail -1
fi



Create a recover script on both servers using the opposite IP. This file will create a standby node after a failover has occurred:

MAAS1 File:

MAAS1 recover.sh

#!/bin/bash
today=`date +"%Y%m%d_%H%M%S"`
cat /var/log/postgresql/postgresql-9.5-main.log |grep "database system is ready" |tail -1
sudo service postgresql stop
sudo mv "../../var/lib/postgresql/9.5/main" "../../var/lib/postgresql/9.5/main_old_$today"
sudo -u postgres pg_basebackup -h 192.168.1.151 -D /var/lib/postgresql/9.5/main -U repuser -v -P --xlog-method=stream
sudo maas-region local_config_set --database-host 192.168.1.151
sudo systemctl restart maas-regiond
sudo cp -avr ../../usr/share/postgresql/9.5/recovery.conf.sample /../../var/lib/postgresql/9.5/main/recovery.conf
sudo echo "standby_mode = on" >> /../../var/lib/postgresql/9.5/main/recovery.conf
sudo echo "primary_conninfo = 'host=192.168.1.151 port=5432 user=repuser password=jensen'" >> /../../var/lib/postgresql/9.5/main/recovery.conf
sudo echo "trigger_file = '/failed/trigger/trigger'" >> /../../var/lib/postgresql/9.5/main/recovery.conf
sudo chmod 777 /var/lib/postgresql/9.5/main/recovery.conf
sudo service postgresql start
sudo rm /var/lib/postgresql/9.5/main/recovery.done
sleep 30
cat /var/log/postgresql/postgresql-9.5-main.log |grep "data

MAAS2 file:

MAAS2 recover.sh

#!/bin/bash
today=`date +"%Y%m%d_%H%M%S"`
cat /var/log/postgresql/postgresql-9.5-main.log |grep "database system is ready" |tail -1
sudo service postgresql stop
sudo mv "../../var/lib/postgresql/9.5/main" "../../var/lib/postgresql/9.5/main_old_$today"
sudo -u postgres pg_basebackup -h 192.168.1.150 -D /var/lib/postgresql/9.5/main -U repuser -v -P --xlog-method=stream
sudo maas-region local_config_set --database-host 192.168.1.150
sudo systemctl restart maas-regiond
sudo cp -avr ../../usr/share/postgresql/9.5/recovery.conf.sample /../../var/lib/postgresql/9.5/main/recovery.conf
sudo echo "standby_mode = on" >> /../../var/lib/postgresql/9.5/main/recovery.conf
sudo echo "primary_conninfo = 'host=192.168.1.150 port=5432 user=repuser password=jensen'" >> /../../var/lib/postgresql/9.5/main/recovery.conf
sudo echo "trigger_file = '/failed/trigger/trigger'" >> /../../var/lib/postgresql/9.5/main/recovery.conf
sudo chmod 777 /var/lib/postgresql/9.5/main/recovery.conf
sudo service postgresql start
sudo rm /var/lib/postgresql/9.5/main/recovery.done
sleep 30
cat /var/log/postgresql/postgresql-9.5-main.log |grep "database system is 


Change permission on the failed folder:

sudo chmod -R 777 /failed

cat /var/log/postgresql/postgresql-9.5-main.log |grep "database system is ready" |tail -1

Use this command to check the database condition:

cat /var/log/postgresql/postgresql-9.5-main.log |grep "database system is ready" |tail -1


If you are seeing DHCP problems and not enlisting on the failover node, make sure you update to the stable repository and update/upgrade.

sudo add-apt-repository ppa:maas/stable

sudo apt update

sudo apt upgrade


Make sure you are switching gateways when you failover, I have included it in the failover script.

ą
Chris Riley,
Dec 30, 2016, 7:41 AM
Comments