Installing Patroni
Objectives
After this lesson, you will:
- Install Python and required dependencies
- Install Patroni via pip
- Understand the structure of
patroni.ymlfile - Create systemd service for Patroni
- Install Patroni on 3 nodes
1. Introduction
Patroni is a Python application, so it requires Python runtime and dependencies. In this lesson, we will:
- Install Python 3 and pip
- Install Patroni package
- Configure Patroni
- Create systemd service to manage Patroni daemon
Target architecture:
2. Installing Python Dependencies
2.1. Installing Python on Ubuntu/Debian
Perform on ALL 3 nodes.
Step 1: Install Python 3 and pip
Step 2: Install system dependencies
2.2. Installing Python on CentOS/RHEL
2.3. Upgrade pip (recommended)
3. Installing Patroni via pip
3.1. Installing Patroni
Perform on ALL 3 nodes.
Explanation of [etcd]:
- Patroni supports multiple DCS backends (etcd, consul, zookeeper)
[etcd]installs additionalpython-etcdclient library- Can install multiple backends:
patroni[etcd,consul,zookeeper]
3.2. Installed packages
3.3. Verify Patroni commands
Output:
4. Structure of patroni.yml file
4.1. Overview of patroni.yml
File patroni.yml is the main configuration file of Patroni, including:
- Scope: Cluster name
- Namespace: Prefix for keys in DCS
- Node information: Node name, REST API config
- DCS connection: etcd endpoints
- Bootstrap: Initial cluster setup
- PostgreSQL: Database configuration
- Tags: Node metadata
- Watchdog: Optional hardware watchdog
4.2. Basic structure
4.3. Explanation of main sections
Section: Global
Section: REST API
REST API endpoints:
GET /: Cluster infoGET /health: Health check (200 = healthy)GET /primary: Check if node is primaryGET /replica: Check if node is replicaPOST /restart: Restart PostgreSQLPOST /reload: Reload configuration
Section: etcd (DCS)
Section: Bootstrap
Note: Bootstrap section only applies when initializing cluster for the first time.
Section: PostgreSQL
Section: Tags
Section: Watchdog (Optional)
5. Create Patroni configuration files
5.1. Create configuration directories
On ALL 3 nodes:
5.2. Node 1 - /etc/patroni/patroni.yml
5.3. Node 2 - /etc/patroni/patroni.yml
5.4. Node 3 - /etc/patroni/patroni.yml
5.5. Set permissions
On ALL 3 nodes:
6. Create systemd service for Patroni
6.1. Create systemd unit file
Create file /etc/systemd/system/patroni.service on ALL 3 nodes:
6.2. Explanation of systemd unit file
| Directive | Explanation |
|---|---|
| After=etcd.service | Start after etcd is ready |
| Type=simple | Process runs in foreground |
| User=postgres | Run with postgres user |
| ExecStart | Command to start Patroni |
| ExecReload | Send HUP signal to reload config |
| Restart=on-failure | Auto restart if fails |
| RestartSec=5 | Wait 5 seconds before restart |
| LimitNOFILE=65536 | Max open files |
| StandardOutput=journal | Log to systemd journal |
6.3. Enable and verify service
On ALL 3 nodes:
7. Verify installation
7.1. Check Patroni installation
On ALL 3 nodes:
7.2. Check etcd connectivity
7.3. Pre-flight checklist
Before starting Patroni, verify:
8. Troubleshooting
8.1. Issue: pip install fails
8.2. Issue: Cannot find PostgreSQL binaries
8.3. Issue: Permission denied on data directory
8.4. Issue: YAML syntax error
9. Summary
Key Takeaways
✅ Patroni: Python application, installed via pip
✅ Dependencies: Python 3, pip, psycopg2, python-etcd
✅ Configuration: patroni.yml contains all settings
✅ systemd service: Manages Patroni daemon
✅ Security: Config file has permissions 600 (contains passwords)
Checklist after Lab
- Python 3 and pip installed on all 3 nodes
- Patroni 3.2.0+ installed on all 3 nodes
- File
/etc/patroni/patroni.ymlcreated on each node with individual config - systemd service
patroni.servicecreated and enabled - Correct permissions for config file (600, owner postgres)
- etcd cluster running and healthy
Current Architecture
Preparation for Lesson 8
Lesson 8 will go deeper into detailed Patroni configuration:
- Analysis of each section in
patroni.yml - Bootstrap configuration options
- PostgreSQL parameters tuning
- Authentication setup
- Tags and constraints
Lesson 9: Bootstrap cluster
After Patroni is installed and configured, Lesson 9 will guide:
- Starting Patroni for the first time
- Automatic bootstrap process
- Checking cluster status
- Troubleshooting