29.4. Configuring Agents #

On all servers:

  1. Install agents:

    # apt install ppem-agent
    
  2. Create the DBMS user on behalf of which the agent will connect to the DBMS server databases:

    # sudo -u postgres createuser --pwprompt ppem_agent
    

    When executing this command, specify the DBMS user password.

  3. Grant the DBMS user the privilege to read the system catalog and run functions.

    It is recommended to grant the following privileges:

    GRANT pg_monitor, pg_maintain, pg_signal_backend, pg_read_all_settings TO ppem_agent;
    

    Grant the DBMS user the following privileges on every database in the instance:

    GRANT EXECUTE ON FUNCTION pg_catalog.pg_stat_file(TEXT) TO ppem_agent;
    GRANT EXECUTE ON FUNCTION pg_catalog.pg_stat_file(TEXT, BOOLEAN) TO ppem_agent;
    GRANT SELECT ON pg_catalog.pg_statistic TO ppem_agent;
    GRANT SELECT ON pg_catalog.pg_config TO ppem_agent;
    GRANT EXECUTE ON FUNCTION pg_catalog.pg_config() TO ppem_agent;
    GRANT SELECT ON pg_catalog.pg_file_settings TO ppem_agent;
    GRANT EXECUTE ON FUNCTION pg_catalog.pg_show_all_file_settings() TO ppem_agent;
    GRANT SELECT ON pg_catalog.pg_authid TO ppem_agent;
    

    Grant the DBMS user the following privileges to manage backups:

    ALTER ROLE ppem_agent WITH REPLICATION;
    
    GRANT USAGE ON SCHEMA pg_catalog TO ppem_agent;
    GRANT EXECUTE ON FUNCTION pg_catalog.current_setting(text) TO ppem_agent;
    GRANT EXECUTE ON FUNCTION pg_catalog.set_config(text, text, boolean) TO ppem_agent;
    GRANT EXECUTE ON FUNCTION pg_catalog.pg_is_in_recovery() TO ppem_agent;
    GRANT EXECUTE ON FUNCTION pg_catalog.pg_backup_start(text, boolean) TO ppem_agent;
    GRANT EXECUTE ON FUNCTION pg_catalog.pg_backup_stop(boolean) TO ppem_agent;
    GRANT EXECUTE ON FUNCTION pg_catalog.pg_create_restore_point(text) TO ppem_agent;
    GRANT EXECUTE ON FUNCTION pg_catalog.pg_switch_wal() TO ppem_agent;
    GRANT EXECUTE ON FUNCTION pg_catalog.pg_last_wal_replay_lsn() TO ppem_agent;
    GRANT EXECUTE ON FUNCTION pg_catalog.txid_current() TO ppem_agent;
    GRANT EXECUTE ON FUNCTION pg_catalog.txid_current_snapshot() TO ppem_agent;
    GRANT EXECUTE ON FUNCTION pg_catalog.txid_snapshot_xmax(txid_snapshot) TO ppem_agent;
    GRANT EXECUTE ON FUNCTION pg_catalog.pg_control_checkpoint() TO ppem_agent;
    

    You can grant privileges only for the database that will be used for connecting the user to the instance.

    For more information about privileges, refer to the official Postgres Pro documentation on pg_probackup.

  4. Ensure that the DBMS user can connect to the following databases:

    • On the server where the manager is installed, to the repository database.

      For example, to check access to the ppem database:

      # psql -h localhost -U ppem_agent -d ppem
      

      In case of connection issues, ensure that the authorization method and the HBA rule allowing the DBMS user to connect to the database are specified in the pg_hba.conf configuration file.

      Example pg_hba.conf entries:

      host    ppem  ppem_agent  ::1/128       trust
      host    ppem  ppem_agent  127.0.0.1/32  trust
      

    • On the server where the DBMS to be managed are installed, to all databases.

      For example, to check access to the db1 database:

      # psql -h localhost -U ppem_agent -d db1
      

      In case of connection issues, ensure that the authorization method and the HBA rule allowing the DBMS user to connect to the database are specified in the pg_hba.conf configuration file.

      Example pg_hba.conf entries:

      host    all  ppem_agent  ::1/128       trust
      host    all  ppem_agent  127.0.0.1/32  trust
      

  5. Get an API key to configure the agent:

    1. Log in to the web application.

    2. Go to the summary page.

    3. If the agent configuration instruction is displayed on the page, then copy the key from the instruction.

    4. If no instruction is displayed, then complete the following steps:

      1. Go to the Infrastructure navigation tab → Agents.

      2. In the Authentication key column for any agent, click either to copy the key or to view the key.

  6. In the /etc/ppem-agent.yml agent configuration file, specify values for the following parameters:

    • agent.name

      Specify the unique agent name.

    • agent.manager.url

      Specify the following value and substitute the necessary data:

      http://ppem_manager_address:8080
      
    • agent.manager.api_key

      Specify the value of the API key that was obtained earlier.

    • jwt.secret

      For security reasons, specify a value different from the default one.

    • agent.instance.connection_defaults

      Specify the parameters which are used by the agent when connecting to the DBMS instances on the host where the agent is installed.

  7. Create the main and additional agent catalogs and make the operating system user their owner:

      sudo mkdir --parents /var/lib/ppem/{instances,postgresql,systemd,backups}
      sudo chown --recursive ppem:ppem /var/lib/ppem
    

    The main agent catalog is used for storing instances and their parameters, instance management units, and local storages. You must specify the /var/lib/ppem/instances catalog when creating instances and the /var/lib/ppem/backups catalog when creating local storages.

  8. Create a catalog for configuring systemd:

      sudo mkdir /etc/systemd/system.conf.d
    
  9. Create the systemd configuration file:

      sudo nano /etc/systemd/system.conf.d/custom_path.conf
    
  10. In the systemd configuration file, specify:

      [Manager]
      ManagerEnvironment="SYSTEMD_UNIT_PATH=/var/lib/ppem/systemd:"
    
  11. Add the operating system user to the group for working with instances:

      sudo usermod -aG postgres ppem
    
  12. In the sudoers configuration file, enable the operating system user to execute commands under the superuser by specifying:

      Cmnd_Alias PG_SYS_V = /usr/bin/systemctl status *postgres*.service, \
      /usr/bin/systemctl start *postgres*.service, \
      /usr/bin/systemctl stop *postgres*.service, \
      /usr/bin/systemctl restart *postgres*.service, \
      /usr/bin/systemctl reload *postgres*.service, \
      /usr/bin/systemctl enable *postgres*.service, \
      /usr/bin/systemctl disable *postgres*.service, \
      /usr/bin/systemctl daemon-reload, \
      /usr/bin/mkdir --parents /var/lib/ppem/*, \
      /usr/bin/mkdir /var/lib/ppem/*, \
      /usr/bin/mkdir --parents /sys/fs/cgroup/ppem*, \
      /usr/bin/mkdir /sys/fs/cgroup/ppem*, \
      /usr/bin/chmod --recursive 0[5-7][0-7][0-7] /var/lib/ppem/*, \
      /usr/bin/chmod 0[5-7][0-7][0-7] /var/lib/ppem/*, \
      /usr/bin/chown --recursive [a-z]*\:[a-z]* /var/lib/ppem/*, \
      /usr/bin/chown [a-z]*\:[a-z]* /var/lib/ppem/*, \
      /usr/bin/chown [a-z]*\:[a-z]* /tmp/*, \
      /usr/bin/chown --recursive [a-z]*\:[a-z]* /tmp/*, \
      /usr/bin/echo, \
      /usr/bin/test, \
      /usr/bin/sh -c /usr/bin/echo, \
      /usr/bin/mv, \
      /usr/bin/rm /var/lib/ppem/*, \
      /usr/bin/rm /tmp/*, \
      /usr/bin/rm --recursive --force /var/lib/ppem/*
    
      Cmnd_Alias PG_CTL_V = /usr/lib/postgresql/[0-9][0-9]/bin/pg_ctl, \
      /usr/lib/postgresql/[0-9][0-9]/bin/initdb, \
      /usr/bin/pg_probackup-[0-9][0-9], \
      /opt/pgpro/ent-[0-9][0-9]/bin/initdb, \
      /opt/pgpro/ent-[0-9][0-9]/bin/pg_ctl, \
      /opt/pgpro/ent-[0-9][0-9]/bin/pg_probackup
    
      ppem ALL = (root) NOPASSWD:SETENV: PG_SYS_V
      ppem ALL = (postgres) NOPASSWD:SETENV: PG_CTL_V
    

    The agent runs as the non-superuser of the operating system, so it is granted the privilege to execute only the commands required for managing instances. The commands are grouped into the PG_SYS_V and PG_CTL_V command aliases. The ppem ALL = (root) and ppem ALL = (postgres) lines allow the ppem operating system user to run the commands of the PG_SYS_V alias on behalf of the superuser and the commands of the PG_CTL_V alias on behalf of the postgres operating system user. The NOPASSWD tag disables the password prompt and the SETENV tag allows the agent to pass environment variables to the executed commands. The commands are described below.

    Command

    Purpose

    Usage

    /usr/bin/systemctl status|start|stop|restart|reload *postgres*.service

    Checking the state of an instance, starting, stopping, restarting, and reloading it using systemd.

    Used to manage the instance state through systemd. It is the main scenario of agent operation.

    /usr/bin/systemctl enable|disable *postgres*.service

    Adding the systemd units of instances to the server startup and removing them from it.

    Used when an instance is initialized and when it is deleted.

    /usr/bin/systemctl daemon-reload

    Reloading the systemd manager configuration.

    Used after adding or removing the systemd unit of an instance for systemd to pick up the changes.

    /usr/bin/mkdir --parents /var/lib/ppem/*, /usr/bin/mkdir /var/lib/ppem/*

    Creating subdirectories in the /var/lib/ppem directory.

    Used to create subdirectories in /var/lib/ppem for instances, backups, and other data. Required for the agent to work properly without superuser privileges.

    /usr/bin/mkdir --parents /sys/fs/cgroup/ppem*, /usr/bin/mkdir /sys/fs/cgroup/ppem*

    Creating subdirectories in the /sys/fs/cgroup/ directory with names starting with ppem.

    Used only for instances managed through pg_ctl. Required for creating operating system control groups.

    /usr/bin/chmod --recursive 0[5-7][0-7][0-7] /var/lib/ppem/*, /usr/bin/chmod 0[5-7][0-7][0-7] /var/lib/ppem/*

    Changing access privileges for subdirectories in the /var/lib/ppem agent directory.

    Used with the 0[5-7][0-7][0-7] pattern and the /var/lib/ppem/* path pattern, which restrict the allowed values and locations.

    /usr/bin/chown --recursive [a-z]*\:[a-z]* /var/lib/ppem/*, /usr/bin/chown [a-z]*\:[a-z]* /var/lib/ppem/*

    Changing the owner of subdirectories in the /var/lib/ppem agent directory, for example, changing the owner of the future instance data directory.

    Used with the [a-z]*:[a-z]* pattern and the /var/lib/ppem/* path pattern, which restrict the allowed values and locations.

    /usr/bin/chown [a-z]*\:[a-z]* /tmp/*, /usr/bin/chown --recursive [a-z]*\:[a-z]* /tmp/*

    Changing the owner of auxiliary files in /tmp.

    Used to change the owner of temporary files for instance initialization and replication that are created in /tmp and then deleted. For example, the agent changes the owner of the temporary password file created during instance initialization.

    /usr/bin/echo

    Writing data to files.

    Used when the PPEM agent running under a non-superuser cannot write to system files by its own means, for example, to add the instance process identifier to the cgroup.procs file of the instance control group.

    /usr/bin/test

    Checking file and directory existence.

    Used to check that a file or directory does not already exist before creating it. Otherwise, the object cannot be created.

    /usr/bin/mv

    Moving files.

    Used to move files, for example, configuration files during instance initialization.

    /usr/bin/rm /var/lib/ppem/*, /usr/bin/rm /tmp/*

    Deleting files.

    Used to delete instance files and temporary files. The /var/lib/ppem/* and /tmp/* path patterns restrict the allowed locations.

    /usr/bin/rm --recursive --force /var/lib/ppem/*

    Deleting subdirectories in the /var/lib/ppem directory.

    Used, for example, to delete the instance data directory.

    /usr/lib/postgresql/[0-9][0-9]/bin/pg_ctl

    Managing instances.

    Used to run pg_ctl on behalf of the postgres user. The [0-9][0-9] pattern covers different versions of the utility. The given path is typical for Debian-based systems; adjust it to the installation used on the target server.

    /usr/lib/postgresql/[0-9][0-9]/bin/initdb

    Initializing instances.

    Used to run initdb on behalf of the postgres user in PostgreSQL installations.

    /usr/bin/pg_probackup-[0-9][0-9]

    Working with backups using pg_probackup.

    Used for PostgreSQL installations.

    /opt/pgpro/ent-[0-9][0-9]/bin/initdb

    Initializing instances.

    Used for Postgres Pro Enterprise installations.

    /opt/pgpro/ent-[0-9][0-9]/bin/pg_ctl

    Managing instances.

    Used for Postgres Pro Enterprise installations.

    /opt/pgpro/ent-[0-9][0-9]/bin/pg_probackup

    Working with backups using pg_probackup.

    Used for Postgres Pro Enterprise installations.

    For Postgres Pro Standard, replace /opt/pgpro/ent-[0-9][0-9] with /opt/pgpro/std-[0-9][0-9] in the PG_CTL_V alias commands.

  13. In the ppem-agent.yml agent configuration file, specify:

      system:
        postgresql_configs_path: "/var/lib/ppem/postgresql"
        systemd_units_path: "/var/lib/ppem/systemd"
    
  14. Configure the agent service to start on behalf of the operating system user:

    1. Start editing the systemd unit:

      # systemctl edit ppem-agent
      
    2. In the [Service] section, specify the operating system user:

      [Service]
      User=ppem
      
    3. Ensure that the operating system user is granted the privilege to read the ppem-agent.yml agent configuration file.

      If the privilege is not granted, execute:

      # chown ppem:ppem /etc/ppem-agent.yml
      # chmod 400 /etc/ppem-agent.yml
      
    4. Save systemd unit parameters, then reload it:

      # systemctl daemon-reload
      
  15. Start the agent service and add it to the server startup:

    # systemctl start ppem-agent
    # systemctl enable ppem-agent
    
FAQ