PostgreSQL High Availability với Patroni & etcd

Học cách triển khai PostgreSQL High Availability cluster với Patroni và etcd. Khóa học thực hành từ A-Z: cài đặt, cấu hình, failover tự động, monitoring và vận hành production. 29 bài học + labs chi tiết.

PostgreSQL High Availability với Patroni & etcd

Bài 1: Tổng quan về PostgreSQL High Availability

  • Tại sao cần HA?
  • Các phương pháp HA cho PostgreSQL
  • So sánh: Patroni vs Repmgr vs Pacemaker
  • Kiến trúc tổng quan hệ thống

Bài 2: Streaming Replication trong PostgreSQL

  • Cơ chế hoạt động của Streaming Replication
  • WAL (Write-Ahead Logging)
  • Synchronous vs Asynchronous Replication
  • Replication Slots
  • Lab: Setup replication đơn giản (Primary-Standby)

Bài 3: Giới thiệu Patroni và etcd

  • Patroni là gì? Cách hoạt động
  • DCS (Distributed Configuration Store) - etcd/Consul/ZooKeeper
  • Consensus algorithm (Raft)
  • Leader election & Failover mechanism
  • Split-brain problem và cách giải quyết

Bài 4: Chuẩn bị hạ tầng

  • Yêu cầu phần cứng & phần mềm
  • Cấu hình network và firewall
  • Setup 3 VMs/Servers (VirtualBox/VMware/Cloud)
  • SSH key-based authentication
  • Time synchronization (NTP/chrony)

Bài 5: Cài đặt PostgreSQL

  • Cài PostgreSQL từ package repository
  • Cài từ source (optional)
  • Cấu hình postgresql.conf cơ bản
  • Hiểu về pg_hba.conf
  • Lab: Cài đặt PostgreSQL trên 3 nodes

Bài 6: Cài đặt và cấu hình etcd cluster

  • Download và cài đặt etcd
  • Cấu hình etcd cluster 3 nodes
  • Tạo systemd service
  • Kiểm tra health của etcd cluster
  • etcdctl commands cơ bản
  • Lab: Setup etcd cluster hoàn chỉnh

Bài 7: Cài đặt Patroni

  • Cài đặt Python dependencies
  • Cài đặt Patroni qua pip
  • Cấu trúc file patroni.yml
  • Tạo systemd service cho Patroni
  • Lab: Cài Patroni trên 3 nodes

Bài 8: Cấu hình Patroni chi tiết

  • Phân tích file patroni.yml từng section
  • Bootstrap configuration
  • PostgreSQL parameters tuning
  • Authentication setup
  • Tags và constraints

Bài 9: Bootstrap PostgreSQL Cluster

  • Khởi động Patroni lần đầu
  • Quá trình bootstrap tự động
  • Kiểm tra cluster status với patronictl
  • Troubleshooting common issues
  • Lab: Bootstrap cluster thành công

Bài 10: Quản lý Replication

  • Synchronous vs Asynchronous replicas
  • Cấu hình synchronous_mode
  • synchronous_node_count
  • Monitoring replication lag
  • Lab: Cấu hình sync replication

Bài 11: Callback scripts và Hooks

  • on_start, on_stop, on_role_change callbacks
  • Custom scripts cho notifications
  • Tích hợp với monitoring systems
  • Lab: Viết script gửi alert khi failover

Bài 12: REST API và patronictl

  • Patroni REST API endpoints
  • Sử dụng patronictl commands
  • Automation với API
  • Lab: Quản lý cluster qua CLI và API

Module 4: Failover & Recovery (4 bài)

Bài 13: Automatic Failover

  • Cơ chế phát hiện lỗi
  • Leader election process
  • Failover timeline
  • Testing automatic failover
  • Lab: Mô phỏng primary node failure

Bài 14: Switchover có kế hoạch

  • Khi nào cần switchover?
  • Planned switchover vs Failover
  • Zero-downtime maintenance
  • Lab: Thực hiện switchover

Bài 15: Recovering failed nodes

  • Rejoin failed primary
  • pg_rewind mechanism
  • Rebuild replica từ backup
  • Lab: Recovery scenarios

Bài 16: Backup và Point-in-Time Recovery

  • pg_basebackup
  • WAL archiving
  • Cấu hình continuous archiving
  • PITR (Point-in-Time Recovery)
  • Lab: Restore database từ backup

Bài 17: Monitoring Patroni Cluster

  • Metrics cần theo dõi
  • Tích hợp Prometheus + Grafana
  • postgres_exporter
  • Alerting rules
  • Lab: Setup monitoring stack

Bài 18: Performance Tuning

  • PostgreSQL configuration tuning
  • Connection pooling (PgBouncer)
  • Load balancing (HAProxy/pgpool)
  • Read replicas scaling
  • Lab: Optimize cluster performance

Bài 19: Logging và Troubleshooting

  • PostgreSQL logs
  • Patroni logs
  • etcd logs
  • Common issues và cách fix
  • Debug techniques

Bài 20: Security Best Practices

  • SSL/TLS configuration
  • Authentication methods
  • Network security
  • Encryption at rest
  • Audit logging
  • Lab: Hardening cluster security

Bài 21: Multi-datacenter Setup

  • Cross-DC replication strategies
  • Asynchronous cascading replication
  • Disaster recovery planning
  • Geographic load balancing

Bài 22: Patroni với Kubernetes

  • Patroni operator
  • StatefulSets
  • Persistent Volumes
  • Helm charts
  • Lab: Deploy trên K8s

Bài 23: Patroni Configuration Management

  • Dynamic configuration changes
  • DCS-based configuration
  • patronictl edit-config
  • Config validation
  • Lab: Update config without downtime

Bài 24: Upgrade Strategies

  • PostgreSQL major version upgrade
  • Patroni version upgrade
  • Zero-downtime upgrade techniques
  • Rollback procedures
  • Lab: Upgrade cluster từ PG 14 lên 15

Bài 25: Real-world Case Studies

  • Production architecture examples
  • Scaling strategies
  • Cost optimization
  • Lessons learned

Bài 26: Automation với Ansible

  • Ansible playbooks cho deployment
  • Configuration management
  • Automated testing
  • CI/CD integration
  • Lab: Automated deployment

Bài 27: Disaster Recovery Drills

  • DR planning
  • Testing procedures
  • Incident response
  • Post-mortem analysis
  • Lab: Full DR simulation

Bài 28: Thiết kế kiến trúc HA

  • Requirements gathering
  • Architecture design document
  • Capacity planning
  • Cost estimation

Bài 29: Deploy Production-ready Cluster

  • Complete deployment từ đầu
  • Documentation
  • Runbook creation
  • Knowledge transfer
  • Final assessment

Prerequisites:

  • Linux cơ bản
  • PostgreSQL fundamentals
  • Networking basics
  • Shell scripting

Mục tiêu sau khóa học:

  • Triển khai PostgreSQL HA cluster production-ready
  • Quản lý và vận hành cluster
  • Xử lý incidents và troubleshooting
  • Optimize performance và security