Patroni 沒有 start 和 stop,但它有維護模式啊。

Patroni 爲什麼沒有停止的功能

說實話,這個問題我也是比較納悶的。很多高可用程序都擁有 startup/stop 的選項,但是 Patroni 沒有這些選項。

因爲開發軟件的人認爲 Patroni 的主要目的和任務是運行高可用集羣,停止和啓動它是非常奇怪的事。而且從技術上來說,因爲它通過在 Patroni 節點上運行的 REST API 進行通信,所以無法停止。

我個人覺得還有一個點 Patroni 他要和 Etcd 和 watchdog 進行通信,所以停止它確實不推薦。但是能不能手工停呢?我個人試過,只要把你的數據庫關閉,然後在把 Patroni 進程 kill 也是可行的。但是有 watchdog 的情況就不清楚了,畢竟需要給狗投食,所以建議最好先關閉 watchdog。

Patroni 的維護模式

在某一些情況下,我們需要退出集羣管理,但是我們仍然想要保留 Patroni 與 DCS 的狀態和通信,所以我們需要把 Patroni 與正在運行的集羣進行 “分離”,從而實現像 Pacemaker 一樣的維護模式。

而 Patroni 使用 paused 選項就可以進入維護模式,接下來我們來測試一下維護模式。

[postgres@133e0e204e206 ~]$ patronictl -c /etc/patroni.yml list
+ Cluster: patnori-test (6962171552537974697) --+----+-----------+
| Member    | Host          | Role    | State   | TL | Lag in MB |
+-----------+---------------+---------+---------+----+-----------+
| postgres1 | 133.0.204.206 | Leader  | running | 15 |           |
| postgres2 | 133.0.204.207 | Replica | running | 15 |         0 |
| postgres3 | 133.0.204.208 | Replica | running | 15 |         0 |
+-----------+---------------+---------+---------+----+-----------+
[postgres@133e0e204e206 ~]$ patronictl -c /etc/patroni.yml pause
Success: cluster management is paused
[postgres@133e0e204e206 ~]$ patronictl -c /etc/patroni.yml list
+ Cluster: patnori-test (6962171552537974697) --+----+-----------+
| Member    | Host          | Role    | State   | TL | Lag in MB |
+-----------+---------------+---------+---------+----+-----------+
| postgres1 | 133.0.204.206 | Leader  | running | 15 |           |
| postgres2 | 133.0.204.207 | Replica | running | 15 |         0 |
| postgres3 | 133.0.204.208 | Replica | running | 15 |         0 |
+-----------+---------------+---------+---------+----+-----------+
 Maintenance mode: on

我們在 Leader 節點打開維護模式。現在我們就能夠正常的關閉 Patroni 節點了。

[postgres@133e0e204e206 ~]$ ps -ef | grep patroni
postgres 24311     1  0 Jun03 ?        00:03:28 /usr/local/bin/python3.9 /home/postgres/.local/bin/patroni /etc/patroni.yml
postgres 30373 26101  0 16:00 pts/1    00:00:00 grep --color=auto patroni
[postgres@133e0e204e206 ~]kill -9 24311

關閉了 Patroni 節點之後,在 Replica 節點上查詢集羣狀態。

[postgres@133e0e204e207 ~]$ patronictl -c /etc/patroni.yml list
+ Cluster: patnori-test (6962171552537974697) --+----+-----------+-----------------+
| Member    | Host          | Role    | State   | TL | Lag in MB | Pending restart |
+-----------+---------------+---------+---------+----+-----------+-----------------+
| postgres2 | 133.0.204.207 | Replica | running | 15 |         0 | *               |
| postgres3 | 133.0.204.208 | Replica | running | 15 |         0 | *               |
+-----------+---------------+---------+---------+----+-----------+-----------------+
 Maintenance mode: on

可以看到另外兩個節點,沒發生自動故障轉移。數據庫仍然保持穩定的運行。

你登錄 leader 節點的 postgresql 數據庫,可以正常執行操作,沒發生切換。

[postgres@133e0e204e206 ~]$  psql
psql (13.2)
Type "help" for help.

postgres=# select pg_is_in_recovery();
 pg_is_in_recovery 
-------------------
 f
(1 row)

此時我們就可以對 Patroni 進行升級等一系列維護操作,不會影響集羣的使用,但是如果接下來出現問題,就需要你手工進行故障轉移了。

還有一個應用場景是你可能要停止你的數據庫進行維護,但是 Patroni 會自動立馬迅速的拉起它,或者是進行故障轉移,此時你不想讓它拉起來。你也可以把狀態設置成維護模式,然後停止數據庫。我們來測試一下。

假設我現在的 leader 在節點 3 上。

[postgres@133e0e204e206 ~]$  patronictl -c /etc/patroni.yml list
+ Cluster: patnori-test (6962171552537974697) --+----+-----------+-----------------+
| Member    | Host          | Role    | State   | TL | Lag in MB | Pending restart |
+-----------+---------------+---------+---------+----+-----------+-----------------+
| postgres1 | 133.0.204.206 | Replica | running | 16 |         0 |                 |
| postgres2 | 133.0.204.207 | Replica | running | 16 |         0 | *               |
| postgres3 | 133.0.204.208 | Leader  | running | 16 |           | *               |
+-----------+---------------+---------+---------+----+-----------+-----------------+

我先把節點 3 進入到維護模式。

[postgres@133e0e204e208 ~]$  patronictl -c /etc/patroni.yml pause
Success: cluster management is paused

然後我把節點 3 的數據庫停止。

[postgres@133e0e204e208 ~]$ psql
psql (13.2)
Type "help" for help.
postgres=# select pg_is_in_recovery();
 pg_is_in_recovery 
-------------------
 f
(1 row)
postgres=# \q
[postgres@133e0e204e208 ~]$ pg_ctl  stop
waiting for server to shut down.... done
server stopped

此時我把主庫 shutdown 了,在維護模式下,數據庫並沒有被立馬拉起來,也沒有發生 failover。

[postgres@133e0e204e208 ~]$ patronictl -c /etc/patroni.yml list
+ Cluster: patnori-test (6962171552537974697) --+----+-----------+-----------------+
| Member    | Host          | Role    | State   | TL | Lag in MB | Pending restart |
+-----------+---------------+---------+---------+----+-----------+-----------------+
| postgres1 | 133.0.204.206 | Replica | running | 16 |         0 |                 |
| postgres2 | 133.0.204.207 | Replica | running | 16 |         0 | *               |
| postgres3 | 133.0.204.208 | Replica | stopped |    |   unknown | *               |
+-----------+---------------+---------+---------+----+-----------+-----------------+
 Maintenance mode: on

整個數據庫集羣,另外兩個從庫都只能是隻讀狀態。我們可以在主庫上做一些維護操作了,維護完了在把主庫啓動。最後把 Patroni 維護模式關閉,整個集羣就完好如初。

[postgres@133e0e204e208 ~]$ pg_ctl start
waiting for server to start....2021-06-07 17:09:00.200 CST [4283] LOG:  redirecting log output to logging collector process
2021-06-07 17:09:00.200 CST [4283] HINT:  Future log output will appear in directory "log".
 done
server started

[postgres@133e0e204e208 ~]$ patronictl -c /etc/patroni.yml list
+ Cluster: patnori-test (6962171552537974697) --+----+-----------+-----------------+
| Member    | Host          | Role    | State   | TL | Lag in MB | Pending restart |
+-----------+---------------+---------+---------+----+-----------+-----------------+
| postgres1 | 133.0.204.206 | Replica | running | 16 |         0 |                 |
| postgres2 | 133.0.204.207 | Replica | running | 16 |         0 | *               |
| postgres3 | 133.0.204.208 | Leader  | running | 16 |           | *               |
+-----------+---------------+---------+---------+----+-----------+-----------------+
 Maintenance mode: on
 
 [postgres@133e0e204e208 ~]$ patronictl -c /etc/patroni.yml resume
Success: cluster management is resume

[postgres@133e0e204e208 ~]$ patronictl -c /etc/patroni.yml list
+ Cluster: patnori-test (6962171552537974697) --+----+-----------+
| Member    | Host          | Role    | State   | TL | Lag in MB |
+-----------+---------------+---------+---------+----+-----------+
| postgres1 | 133.0.204.206 | Replica | running | 16 |         0 |
| postgres2 | 133.0.204.207 | Replica | running | 16 |         0 |
| postgres3 | 133.0.204.208 | Leader  | running | 16 |           |
+-----------+---------------+---------+---------+----+-----------+

後記

維護模式還是很重要的,不然咱們稍微想動一下,就有可能導致整個集羣發生故障轉移,這不要背維護的鍋了嗎?所以掌握 Patroni 的維護模式,就能夠方便的進行各種維護任務。

參考鏈接:

https://github.com/zalando/patroni/issues/447

本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/bVoEfN44jh23uvCuAdeYqg