Installing and configuring walctl

There's something to be said for simplicity. So far, the tools we've discussed in this chapter are larger client-server mechanisms or components of entire toolkits. One of the central tenets of the Unix philosophy is to build tools that do one thing well. In this case, we turn to Peak6 and their walctl WAL-management tools.

I created walctl specifically to address shortcomings in existing WAL-related utilities. Primarily of note is the question of architecture. Existing WAL tools follow an architecture diametrically opposed to the end goal of high availability. We often see this:

Installing and configuring walctl

In this kind of model, the master node is tasked with transmitting transaction streams or WAL files to every node in the cluster. This makes it fantastically difficult to change the active master node and potentially overloads the master node itself. The primary write node of any cluster should be focused on fulfilling client requests. The purpose of walctl is to impose a structure like this:

Installing and configuring walctl

Instead of forcing the master node to supply each standby, the master transmits WAL data to a central archive server. Then, each clone can pull from that location as needed. In this recipe, we will install walctl so that we can take advantage of the structure it advocates.

Getting ready

Currently, walctl is very new. As such, it resides primarily on GitHub. You can download a copy of walctl from https://github.com/OptionsHouse/walctl.

We also suggest that you install rsync, openssh, and PostgreSQL server development libraries. For most PostgreSQL servers, it's very likely these are already installed.

How to do it...

For this procedure, we will need three servers. The archive server should be named pg-arc, our primary PostgreSQL server is pg-primary, and the new standby will be pg-clone. As usual, the PostgreSQL data directory will be located at /db/pgdata. For simplicity, the system user on all machines will be postgres. Be sure to have the password for this user!

  1. As a root-capable user on pg-primary and pg-clone, run these commands to install walctl:
    git clone https://github.com/OptionsHouse/walctl
    cd walctl
    sudo make install
    
  2. As a root-capable user on pg-arc, create the WAL storage directory:
    sudo mkdir -m 0600 /db/wal_archive
    sudo chown postgres:postgres /db/wal_archive
    
  3. On pg-primary, create and export an SSH key to the pg-arc and pg-clone servers:
    ssh-keygen -t rsa -N ''
    ssh-copy-id pg-arc
    ssh-copy-id pg-clone
    
  4. Repeat the previous step on the pg-clone server:
    ssh-keygen -t rsa -N ''
    ssh-copy-id pg-arc
    ssh-copy-id pg-primary
    
  5. Execute this SQL on pg-primary to create a database user for walctl:
    CREATE USER walctl
      WITH PASSWORD 'test' SUPERUSER REPLICATION;
  6. Modify pg_hba.conf on pg-primary and add these lines:
    host   replication   walctl   pg-clone     md5
    host   replication   walctl   pg-primary   md5
  7. On pg-clone and pg-primary, ensure this line appears in the .pgpass file for the postgres user:
    *:*:*:walctl:test
  8. On pg-clone and pg-primary, create a file named /etc/walctl.conf with these contents:
    PGDATA=/db/pgdata
    ARC_HOST=pg-arc
    ARC_PATH=/db/wal_archive
  9. On pg-primary, execute this command to set up walctl:
    walctl_setup master
    
  10. If instructed by walctl_setup, restart the PostgreSQL server:
    pg_ctl -D /db/pgdata restart
    

How it works...

Currently, the best source for the walctl files is from GitHub. We suggest that you clone the repository and install the latest version with the included Makefile. After doing so, most of the installation steps are actually things that we've already done, such as creating and distributing SSH keys, allowing host connections in pg_hba.conf, or adding authentication information to .pgpass. It doesn't actually matter how you do this, but the end result must match these requirements:

  • Both pg-primary and pg-clone must be able to communicate via SSH with pg-arc
  • The pg-clone server must be able to connect to pg-primary to clone data and potentially stream it as well
  • We don't suggest using trust-based authentication, so some higher-security method such as md5 should be used to authenticate the walctl database user

Given the above has been accomplished—either by our instructions or otherwise—we can configure walctl. A minimal configuration requires three settings before walctl will operate normally. To read or write WAL files to their expected locations, PGDATA must be set. Then, it needs ARC_HOST to send files to the archive server, and ARC_PATH so that it knows where to store archived WAL files.

The walctl_setup utility has one purpose: prepare PostgreSQL for walctl integration. When called with the master parameter as we've done here, it modifies postgresql.conf so that WAL files are compatible with archival, and streaming replicas can connect. In addition, it enables archive mode and sets archive_command to invoke a walctl utility named walctl_push, which sends WAL files to the archive server. While calling walctl_setup on our test server, this was the output:

How it works...

Walctl knows which settings can be changed by reloading PostgreSQL configuration files and which require a full service restart. It even tells us how to do it if we don't already know. If that last NOTICE doesn't appear in the output, the pg-primary server is already archiving WAL files on pg-arc. Otherwise, restarting PostgreSQL will initialize the process.

See also

  • Currently, all documentation for walctl is located at the GitHub repository at https://github.com/OptionsHouse/walctl

    The README file in the source code also contains very similar instructions to what we described in this recipe

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.20.68