1

I have follow this tutorial: https://linode.com/docs/databases/postgresql/create-a-highly-available-postgresql-cluster-using-patroni-and-haproxy/ , in order to set up Highly Available PostgreSQL Cluster Using Patroni and HAProxy.

But when I try to start patroni I get this error:

ubuntu@sudo patroni /etc/patroni.yml 
2018-05-31 09:49:37,159 INFO: Failed to import patroni.dcs.consul
2018-05-31 09:49:37,166 INFO: Selected new etcd server http://privateetcdIP:2379
2018-05-31 09:49:37,173 INFO: Lock owner: None; I am postgresqlm
2018-05-31 09:49:37,175 INFO: trying to bootstrap a new cluster
pg_ctl: cannot be run as root
Please log in (using, e.g., "su") as the (unprivileged) user that will
own the server process.
2018-05-31 09:49:37,185 INFO: removing initialize key after failed attempt to bootstrap the cluster
2018-05-31 09:49:37,673 INFO: Lock owner: None; I am postgresqlm
Traceback (most recent call last):
  File "/usr/local/bin/patroni", line 9, in <module>
    load_entry_point('patroni==1.4.4', 'console_scripts', 'patroni')()
  File "/usr/local/lib/python2.7/dist-packages/patroni/__init__.py", line 176, in main
    return patroni_main()
  File "/usr/local/lib/python2.7/dist-packages/patroni/__init__.py", line 145, in patroni_main
    patroni.run()
  File "/usr/local/lib/python2.7/dist-packages/patroni/__init__.py", line 114, in run
    logger.info(self.ha.run_cycle())
  File "/usr/local/lib/python2.7/dist-packages/patroni/ha.py", line 1164, in run_cycle
    info = self._run_cycle()
  File "/usr/local/lib/python2.7/dist-packages/patroni/ha.py", line 1077, in _run_cycle
    return self.post_bootstrap()
  File "/usr/local/lib/python2.7/dist-packages/patroni/ha.py", line 976, in post_bootstrap
    self.cancel_initialization()
  File "/usr/local/lib/python2.7/dist-packages/patroni/ha.py", line 971, in cancel_initialization
    raise PatroniException('Failed to bootstrap cluster')

The configuration of /etc/patroni.yml is:

scope: postgres
namespace: /db/
name: postgresqlm

restapi:
    listen: privateIPoffirstnode:8008
    connect_address: privateIPoffirstnode:8008

etcd:
    host: privateIPofetcd:2379

bootstrap:
    dcs:
        ttl: 30
        loop_wait: 10
        retry_timeout: 10
        maximum_lag_on_failover: 1048576
        postgresql:
            use_pg_rewind: true
            max_connections: 100

    initdb:
    - encoding: UTF8
    - data-checksums

    pg_hba:
    - host replication replicator 127.0.0.1/32 md5
    - host replication replicator privateIPoffirstnode/0 md5
    - host replication replicator privateIPofsecondnode/0 md5
    - host replication replicator privateIPofthirdnode/0 md5
    - host all all 0.0.0.0/0 md5

    users:
        admin:
            password: admin
            options:
                - createrole
                - createdb

postgresql:
    listen: privateIPoffirstnode:5432
    connect_address: privateIPoffirstnode:5432
    data_dir: /data/patroni
    pgpass: /tmp/pgpass
    bin_dir: /usr/lib/postgresql/9.5/bin
    authentication:
        replication:
            username: replicator
            password: rep-pass
        superuser:
            username: postgres
            password: '12345'
    parameters:
        unix_socket_directories: '.'

tags:
    nofailover: false
    noloadbalance: false
    clonefrom: false
    nosync: false

The configuration of /etc/systemd/system/patroni.service is:

[Unit]
Description=Runners to orchestrate a high-availability PostgreSQL
After=syslog.target network.target

[Service]
Type=simple

User=postgres
Group=postgres

ExecStart=/usr/local/bin/patroni /etc/patroni.yml

KillMode=process

TimeoutSec=30

Restart=no

[Install]
WantedBy=multi-user.targ

etcd congiguration:

ETCD_LISTEN_PEER_URLS="http://privateIPofetcd:2380"

ETCD_LISTEN_CLIENT_URLS="http://localhost:2379,http://privateIPofetcd:2379"

ETCD_INITIAL_ADVERTISE_PEER_URLS="http://privateIPofetcd:2380"

ETCD_INITIAL_CLUSTER="etcd0=http://privateIPofetcd:2380,"

ETCD_ADVERTISE_CLIENT_URLS="http://privateIPofetcd:2379"

ETCD_INITIAL_CLUSTER_TOKEN="cluster1"

ETCD_INITIAL_CLUSTER_STATE="new"

Of course, I have the real ips in privateIPoffirstnode, privateIPofsecondnode etc.

So, does anyone know what this error means?

Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263
e7lT2P
  • 1,635
  • 5
  • 31
  • 57

2 Answers2

1

I think the answer is obvious. If you start patroni with sudo, it will run as root, and that is exactly the error message you get.

Why don't you start it via systemctl? Your /etc/systemd/system/patroni.service has correctly configured a User that is not root.

Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263
  • Thank you for the reply. I will try this. I have an another question too. With this configuration I will achieve load balancing in the term that the read requests would be distributed across the nodes in the system achieving a better performance? Or I need pgpool for this? – e7lT2P Jun 01 '18 at 07:47
  • You'd need a load balancer like HAProxy. But since replication is by default asynchronous, the results will not be totally consistent. – Laurenz Albe Jun 01 '18 at 07:52
  • Pgpool and ha proxy? Do you have a link besause I am completely lost. – e7lT2P Jun 01 '18 at 07:55
  • Either or. I don't know if pgpool can detect node failure. [Here](http://www.haproxy.org/) is a link for HAProxy. Stackoverflow cannot provide a tutorial... – Laurenz Albe Jun 01 '18 at 08:08
  • Thank you for the repsonse. – e7lT2P Jun 01 '18 at 08:09
  • if I use sudo systemctl start patroni and then sudo systemctl status patroni i get : Active: failed, Process: 11145 ExecStart=/usr/local/bin/patroni /etc/patroni.yml (code=exited, status=1/FAILURE) and the error says: patroni.exceptions.PatroniException: 'Can not find suitable configuration of distributed configuration store\nAvailable implementations: '. Do you know what is going wrong? – e7lT2P Jun 01 '18 at 08:46
  • Looks like you are missing environment variables for etcd. – Laurenz Albe Jun 01 '18 at 08:52
  • I updated my question with etcd configuration. Did I miss something? – e7lT2P Jun 01 '18 at 09:15
  • You probably missed adding that to the systemd configuration. – Laurenz Albe Jun 01 '18 at 09:18
  • How can add them? I run on node with etcd : sudo systemctl restart etcd – e7lT2P Jun 01 '18 at 09:22
  • Sorry, I cannot guide you through a complete Patroni setup. I think I answered your question. Look at the `systemd` documentation how to set environment variables. – Laurenz Albe Jun 01 '18 at 09:26
1

Follow this guide to configure highly available Postgresql cluster.

Its fully tested and working.

Vikram Aruchamy
  • 163
  • 1
  • 11