0Pricing
Advanced PostgreSQL: Indexing, Partitioning, Replication · บทเรียน

เครื่องมือสลับการทำงานอัตโนมัติ (Patroni)

กำหนดค่าและจัดการการสลับการทำงานอัตโนมัติโดยใช้เครื่องมืออย่าง Patroni เพื่อลดเวลาหยุดทำงานเมื่อเซิร์ฟเวอร์หลักล้มเหลว

เครื่องมือสลับการทำงานอัตโนมัติ (Patroni) เป็นบทเรียน Advanced PostgreSQL: Indexing, Partitioning, Replication ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Advanced PostgreSQL: Indexing, Partitioning, Replication และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Advanced PostgreSQL: Indexing, Partitioning, Replication มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Why Automatic Failover?

Imagine your main PostgreSQL database suddenly stops working. Without a plan, your application goes down, leading to unhappy users and lost business.

Automatic failover is a strategy to prevent this. It quickly switches to a backup database (a 'standby') when the primary fails, often without any manual intervention.

This ensures your service stays available, minimizing downtime and improving reliability.

Introducing Patroni

Meet Patroni – a powerful, open-source tool designed to manage highly available PostgreSQL clusters.

Patroni acts as a 'template' or 'framework' for PostgreSQL HA, handling complex tasks like primary election, failover, and standby management automatically.

It simplifies the setup and maintenance of robust, fault-tolerant PostgreSQL systems.

Patroni's Core Architecture

Patroni doesn't work alone. It orchestrates several components to achieve high availability:

  • PostgreSQL Instances: The actual database servers (primary and standbys).
  • Patroni Agents: A process running on each PostgreSQL node, monitoring its health and communicating with the cluster.
  • Distributed Consensus Store (DCS): A reliable external service (like etcd, Consul, or ZooKeeper) that stores the cluster state and facilitates primary election.

These components work together to ensure a healthy primary is always available.

The Distributed Consensus Store (DCS)

The Distributed Consensus Store (DCS) is Patroni's brain. It's a critical component for maintaining a consistent view of the cluster state across all nodes.

Popular DCS options include:

  • etcd: A highly-available key-value store.
  • Consul: A service networking solution with a key-value store.
  • ZooKeeper: A centralized service for maintaining configuration information.

Patroni uses the DCS to store information like the current primary, replication status, and configuration, enabling robust primary election and failover.

Patroni Agents & PostgreSQL

Each PostgreSQL server in your cluster runs a Patroni agent. This agent is responsible for:

  • Monitoring the health of its local PostgreSQL instance.
  • Communicating its status to the DCS.
  • Participating in primary election.
  • Performing actions like promoting a standby to primary or configuring replication.

The Patroni agent ensures that the PostgreSQL instance it manages is always in the correct state, whether it's a primary or a standby.

Setting Up Patroni: Overview

A typical Patroni setup involves:

  1. Installing Patroni and PostgreSQL on multiple servers.
  2. Configuring a DCS (e.g., a 3-node etcd cluster).
  3. Creating a patroni.yml configuration file for each PostgreSQL node.
  4. Starting Patroni on all nodes, allowing it to initialize PostgreSQL and form the cluster.

Patroni then automatically handles the initial primary election and sets up streaming replication.

Basic Patroni Configuration (YAML)

Patroni is configured using a YAML file, typically named patroni.yml. Here's a simplified example showing key sections:

This file defines the cluster name, REST API settings, and PostgreSQL-specific parameters.

scope: my_pg_cluster
name: pg1

restapi:
  listen: 0.0.0.0:8008
  connect_address: 192.168.1.101:8008

postgresql:
  listen: 0.0.0.0:5432
  connect_address: 192.168.1.101:5432
  data_dir: /var/lib/postgresql/data
  pgpass: /etc/patroni/pgpass
  replication:
    username: replicator
    password: supersecretpassword
  parameters:
    archive_mode: 'on'
    archive_command: 'cp %p /var/lib/postgresql/archives/%f'
    max_wal_senders: 10

Monitoring Your Patroni Cluster

You can check the health and status of your Patroni cluster using the patronictl command-line tool.

The patronictl list command provides a quick overview of all cluster members, their roles (primary/replica), status, and replication lag.

This helps you confirm that your HA setup is running as expected.

patronictl list my_pg_cluster

# Expected output similar to:
# + Cluster: my_pg_cluster (6956637370217961204)
# +-- Member + Host + Role + State + TL + Lag in MB | Pri. Txid + Conn. URL + 
# | pg1      | 10.0.0.1 | Primary | running | 1 |         0 | 123456789 | host=10.0.0.1 port=5432 user=admin dbname=postgres |
# | pg2      | 10.0.0.2 | Replica | running | 1 |         0 |           | host=10.0.0.2 port=5432 user=admin dbname=postgres |
# | pg3      | 10.0.0.3 | Replica | running | 1 |         0 |           | host=10.0.0.3 port=5432 user=admin dbname=postgres |

How Failover Happens

When the primary PostgreSQL server fails, Patroni initiates an automatic failover:

  1. The Patroni agent on the failed primary stops updating its status in the DCS.
  2. Other Patroni agents detect this absence through the DCS.
  3. The remaining healthy standbys compete to become the new primary.
  4. A new primary is elected by the DCS.
  5. The new primary is promoted, and other standbys are reconfigured to follow it.

This entire process is automated, ensuring minimal disruption to your application.

Quick Check: Patroni Components

Which of the following are essential components that Patroni leverages to provide automatic failover capabilities for a PostgreSQL cluster?

Recap: Patroni for HA

In this lesson, we explored Patroni, a powerful tool for achieving automatic failover and high availability in PostgreSQL.

  • Patroni orchestrates PostgreSQL instances, using Patroni agents on each node.
  • It relies on a Distributed Consensus Store (DCS) like etcd for cluster state and primary election.
  • This architecture ensures that if a primary fails, a new one is automatically promoted, minimizing downtime.

Understanding Patroni is key to building resilient PostgreSQL systems.

คำถามที่พบบ่อย

บทเรียน “เครื่องมือสลับการทำงานอัตโนมัติ (Patroni)” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “เครื่องมือสลับการทำงานอัตโนมัติ (Patroni)” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Advanced PostgreSQL: Indexing, Partitioning, Replication ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Advanced PostgreSQL: Indexing, Partitioning, Replication มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “เครื่องมือสลับการทำงานอัตโนมัติ (Patroni)”

กำหนดค่าและจัดการการสลับการทำงานอัตโนมัติโดยใช้เครื่องมืออย่าง Patroni เพื่อลดเวลาหยุดทำงานเมื่อเซิร์ฟเวอร์หลักล้มเหลว คุณปฏิบัติ Advanced PostgreSQL: Indexing, Partitioning, Replication ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Advanced PostgreSQL: Indexing, Partitioning, Replication หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Advanced PostgreSQL: Indexing, Partitioning, Replication บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “เครื่องมือสลับการทำงานอัตโนมัติ (Patroni)” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Advanced PostgreSQL: Indexing, Partitioning, Replication นี้ได้ไหม

ได้ บทเรียน Advanced PostgreSQL: Indexing, Partitioning, Replication ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. เครื่องมือสลับการทำงานอัตโนมัติ (Patroni)
  2. การตรวจสอบสถานะการจำลองข้อมูล
  3. กลยุทธ์การกู้คืนจากภัยพิบัติ
  4. การกำหนดเส้นทางการเชื่อมต่อด้วย PgBouncer และ HAProxy
← กลับไปที่ Advanced PostgreSQL: Indexing, Partitioning, Replication