Patroni REST API
Learning Objectives
After this lesson, you will:
- Understand Patroni REST API and endpoints
- Use REST API for health checks
- Integrate with load balancers (HAProxy, Nginx)
- Query cluster status and configuration
- Implement custom monitoring
- Secure REST API endpoints
1. REST API Overview
1.1. REST API là gì?
Patroni exposes HTTP REST API trên mỗi node để:
- 🔍 Health checks: Load balancers check node health
- 📊 Monitoring: External systems query cluster state
- ⚙️ Management: Read configuration, cluster topology
- 🔄 Automation: Integration với CI/CD, orchestration tools
1.2. API Configuration
In patroni.yml:
Default port: 8008
1.3. API Endpoints Overview
| Endpoint | Method | Purpose | Use Case |
|---|---|---|---|
| / | GET | Basic node info | Quick health check |
| /primary or /master | GET | Check if node is primary | LB primary routing |
| /replica | GET | Check if node is replica | LB read routing |
| /read-write | GET | Check if writable (primary) | LB write routing |
| /read-only or /standby | GET | Check if read-only (replica) | LB read routing |
| /synchronous | GET | Check if synchronous replica | Sync replica detection |
| /asynchronous | GET | Check if asynchronous replica | Async replica detection |
| /health | GET | Detailed health check | Monitoring |
| /patroni | GET | Detailed cluster and node info | Advanced monitoring |
| /config | GET | Cluster configuration from DCS | Config inspection |
| /cluster | GET | All cluster members info | Topology view |
| /history | GET | Failover history | Audit log |
2. Health Check Endpoints
2.1. Basic health check: GET /
Purpose: Quick check if node is running.
Response codes:
- 200 OK: Node is healthy and running
- 503 Service Unavailable: Node is unhealthy (PostgreSQL down, etc.)
2.2. Primary check: GET /primary or /master
Purpose: Check if node is current primary/leader.
Use case: Load balancer health check for write traffic routing.
2.3. Replica check: GET /replica
Purpose: Check if node is replica (standby).
Use case: Load balancer health check for read traffic routing.
2.4. Read-write check: GET /read-write
Purpose: Check if node accepts writes (primary + not in maintenance).
2.5. Read-only check: GET /read-only or /standby
Purpose: Check if node is read-only replica.
Advanced: Lag tolerance:
2.6. Synchronous replica check: GET /synchronous
Purpose: Check if node is synchronous replica.
2.7. Asynchronous replica check: GET /asynchronous
Purpose: Check if node is asynchronous replica.
2.8. Health endpoint: GET /health
Purpose: Detailed health information.
3. Cluster Information Endpoints
3.1. Detailed node info: GET /patroni
Purpose: Comprehensive node and cluster information.
3.2. Cluster configuration: GET /config
Purpose: Get cluster-wide configuration from DCS.
3.3. Cluster members: GET /cluster
Purpose: Get information about all cluster members.
3.4. Failover history: GET /history
Purpose: Get cluster failover/switchover history.
4. Load Balancer Integration
4.1. HAProxy configuration
haproxy.cfg:
Install và start HAProxy:
Test HAProxy:
4.2. Nginx (with stream module)
nginx.conf:
Note: Nginx stream module doesn't support HTTP health checks directly. Need external script or use HAProxy instead.
4.3. Health check script for external LB
Script for cloud load balancers (AWS ALB, GCP LB, etc.):
Usage:
5. Monitoring Integration
5.1. Prometheus exporter
Use postgres_exporter with custom queries:
Custom query for Patroni metrics:
5.2. Custom monitoring script
Python script using REST API:
Run monitoring:
5.3. Grafana dashboard query examples
PromQL queries:
6. Secure REST API
6.1. Enable authentication
In patroni.yml:
Access with authentication:
6.2. Enable SSL/TLS
Generate certificates:
Configure in patroni.yml:
Access with HTTPS:
6.3. Firewall rules
7. Advanced REST API Usage
7.1. Scripted failover check
7.2. Get primary endpoint dynamically
7.3. Monitor replication lag
8. Lab Exercises
Lab 1: Explore REST API endpoints
Tasks:
- Query all endpoints on each node
- Compare responses between primary and replicas
- Identify which endpoint returns 200 on primary vs replica
Lab 2: Setup HAProxy
Tasks:
- Install HAProxy
- Configure with Patroni health checks
- Test write traffic goes to primary only
- Test read traffic distributed to replicas
- Trigger failover, verify HAProxy redirects automatically
Lab 3: Create monitoring dashboard
Tasks:
- Write Python script to query all nodes
- Display cluster topology
- Show replication lag
- Highlight current primary
- Run every 5 seconds
Lab 4: Secure REST API
Tasks:
- Enable basic authentication
- Generate SSL certificates
- Configure HTTPS
- Update curl commands to use auth + SSL
- Configure firewall rules
9. Troubleshooting REST API
9.1. REST API not responding
Check:
9.2. Wrong HTTP codes returned
Debug:
9.3. SSL/TLS errors
Check:
10. Tổng kết
Key Endpoints Summary
| Endpoint | Returns 200 When | Use Case |
|---|---|---|
| /primary | Node is primary | LB write routing |
| /replica | Node is replica | LB read routing |
| /read-write | Node accepts writes | Write endpoint |
| /read-only | Node is read-only replica | Read endpoint |
| /health | Node is healthy | Detailed monitoring |
| /patroni | Always (detailed info) | Advanced monitoring |
| /cluster | Always (all members) | Topology view |
Integration Checklist
- REST API accessible from all nodes
- HAProxy configured with health checks
- Monitoring system queries REST API
- Authentication enabled
- SSL/TLS configured (production)
- Firewall rules configured
- Health check scripts tested
Architecture hiện tại
Chuẩn bị cho Bài 13
Bài 13 sẽ cover Failover và Switchover:
- Automatic failover process
- Manual switchover
- Failover scenarios và testing
- DCS role in leader election
- Minimize downtime strategies