From bfd741f05e7ee2f114b76315950e96c375c4e91c Mon Sep 17 00:00:00 2001 From: kappa Date: Mon, 6 Apr 2026 01:31:27 +0900 Subject: [PATCH] =?UTF-8?q?PostgreSQL=20HA(Patroni+etcd)=20=EA=B5=AC?= =?UTF-8?q?=EC=84=B1=20=EB=AC=B8=EC=84=9C=20=EC=B6=94=EA=B0=80,=20etcd=20?= =?UTF-8?q?=ED=86=B5=ED=95=A9,=20K3s=20kine=20=EB=A1=9C=EC=BB=AC=20?= =?UTF-8?q?=EC=9D=B4=EC=A0=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 4.6 (1M context) --- infra/nas-storage.md | 5 +- infra/postgresql-ha.md | 132 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+), 2 deletions(-) create mode 100644 infra/postgresql-ha.md diff --git a/infra/nas-storage.md b/infra/nas-storage.md index dbd1c0f..1eb44a1 100644 --- a/infra/nas-storage.md +++ b/infra/nas-storage.md @@ -1,6 +1,6 @@ --- title: NAS StorageClass (NFS + iSCSI) -updated: 2026-04-05 +updated: 2026-04-06 tags: [infra, k3s, storage, nfs, iscsi, synology] --- @@ -159,5 +159,6 @@ kr2의 USB 2.5GbE 어댑터(Realtek RTL8157, 0bda:8157)는 커널 기본 `cdc_nc ## 관련 문서 - [[backup]] — 백업 파이프라인 (NAS 활용) -- [[storage-plan]] — NVMe NAS + 2.5G iSCSI 기획 +- [[storage-plan]] — NVMe NAS + iSCSI 기획, 벤치마크 결과 +- [[postgresql-ha]] — PostgreSQL HA (Patroni + etcd) - [[infra-hosts]] — 서버 목록 diff --git a/infra/postgresql-ha.md b/infra/postgresql-ha.md new file mode 100644 index 0000000..b4c3deb --- /dev/null +++ b/infra/postgresql-ha.md @@ -0,0 +1,132 @@ +--- +title: PostgreSQL HA (Patroni + etcd) +updated: 2026-04-06 +tags: [infra, postgresql, patroni, etcd, ha] +--- + +## 개요 + +PostgreSQL 3노드 HA 클러스터. Patroni가 자동 failover를 관리하고, etcd를 DCS(Distributed Consensus Store)로 사용. + +K3s의 kine 데이터스토어로 사용 중. Supabase Free tier에서 로컬로 이전 완료 (2026-04-05). + +## PostgreSQL 클러스터 + +| 노드 | 호스트 | IP | 역할 | +|------|--------|-----|------| +| postgres-1 | incus-hp2 | 10.100.2.5 | Replica 또는 Leader | +| postgres-2 | incus-kr1 | 10.100.3.185 | Replica 또는 Leader | +| postgres-3 | incus-kr2 | 10.100.1.83 | Replica 또는 Leader | + +- PostgreSQL 17.9, Patroni 4.1.0 +- Patroni 설정: `/etc/patroni.yml` +- Patroni 서비스: `/etc/systemd/system/patroni.service` (ExecStart: `/opt/patroni/bin/patroni /etc/patroni.yml`) +- 클러스터 이름: `nocodb-cluster` +- 레플리케이션: async streaming +- Patroni REST API: 각 노드 8008 포트 + +### DB 목록 + +| DB | 용도 | +|----|------| +| kine | K3s 데이터스토어 (kine) | +| nocodb | NocoDB | +| n8n | n8n 워크플로 | +| outline | Outline 위키 | + +### Patroni 명령어 + +```bash +# 클러스터 상태 확인 +incus exec postgres-1 -- /opt/patroni/bin/patronictl -c /etc/patroni.yml list + +# 수동 switchover +incus exec postgres-1 -- /opt/patroni/bin/patronictl -c /etc/patroni.yml switchover + +# Replica reinitialize +incus exec postgres-1 -- /opt/patroni/bin/patronictl -c /etc/patroni.yml reinit nocodb-cluster postgres-3 --force +``` + +## etcd 클러스터 (Patroni DCS) + +| 노드 | 위치 | IP | 방식 | +|------|------|-----|------| +| etcd-nas | Synology NAS (서울) | 192.168.9.100 | Docker (`quay.io/coreos/etcd:v3.5.21`) | +| etcd-hp2 | Incus 컨테이너 hp2 (서울) | 10.100.2.214 | Alpine + `apk add etcd` (v3.5.16) | +| etcd-jp1 | Incus 컨테이너 jp1 (도쿄) | 10.253.101.233 | Alpine + `apk add etcd` (v3.5.16) | + +- NAS: 데이터 `/volume1/docker/etcd/data`, `--restart=always` +- hp2/jp1: openrc 서비스 (`/etc/init.d/etcd`), `command_background=true` +- Patroni etcd namespace: `/patroni` + +### etcd 확인 명령어 + +```bash +# 클러스터 멤버 확인 +incus exec postgres-1 -- etcdctl --endpoints=http://192.168.9.100:2379 member list -w table + +# 엔드포인트 상태 +incus exec postgres-1 -- etcdctl --endpoints=http://192.168.9.100:2379,http://10.100.2.214:2379,http://10.253.101.233:2379 endpoint status -w table +``` + +### etcd에 저장된 데이터 + +| prefix | 용도 | +|--------|------| +| `/patroni` | Patroni DCS (Leader election, 설정) | +| `/apisix-osaka` | APISIX 오사카 라우팅 설정 | +| `/apisix-tokyo` | APISIX sandbox-tokyo 라우팅 설정 | +| `/apisix-seoul` | APISIX 서울 K3s 라우팅 설정 | + +## K3s kine 연결 + +K3s → HAProxy(OpenWrt 192.168.9.1:5432) → Patroni Leader PostgreSQL + +### K3s config + +```yaml +# /etc/rancher/k3s/config.yaml (kr1, kr2) +datastore-endpoint: "postgres://kine:kine@192.168.9.1:5432/kine" +``` + +### HAProxy (OpenWrt) + +`/etc/haproxy.cfg`에 PostgreSQL backend 설정. Patroni REST API(`/primary` 엔드포인트)로 Leader를 자동 감지. + +``` +frontend ft_postgres + bind :5432 + default_backend bk_postgres_primary + +backend bk_postgres_primary + option httpchk GET /primary + http-check expect status 200 + default-server inter 3s fall 3 rise 2 on-marked-down shutdown-sessions + server postgres-1 10.100.2.5:5432 check port 8008 + server postgres-2 10.100.3.185:5432 check port 8008 + server postgres-3 10.100.1.83:5432 check port 8008 +``` + +- Patroni failover 시 HAProxy가 자동으로 새 Leader를 감지 (~3초) +- K3s config 변경 없이 Leader 전환 대응 + +## APISIX etcd 통합 (2026-04-06) + +기존 각 사이트별 독립 etcd를 통합 etcd 클러스터로 이전. + +| 사이트 | 기존 etcd | 이전 후 | prefix | +|--------|----------|---------|--------| +| osaka | Docker waf-etcd (로컬) | 통합 클러스터 (192.168.9.100) | `/apisix-osaka` | +| sandbox-tokyo | Docker apisix-etcd (로컬) | 통합 클러스터 (10.253.101.233) | `/apisix-tokyo` | +| 서울 K3s | StatefulSet 3노드 (K3s 내부) | 통합 클러스터 (192.168.9.100) | `/apisix-seoul` | + +- 데이터 이전: `etcdctl make-mirror --prefix /apisix/ --dest-prefix /apisix-{site}/` +- K3s 내 apisix-etcd StatefulSet + PVC 삭제 완료 + +## 관련 문서 + +- [[nas-storage]] — NAS StorageClass, Jumbo Frame 설정 +- [[storage-plan]] — NVMe NAS + iSCSI 기획 +- [[backup]] — 백업 파이프라인 +- [[infra-hosts]] — 서버 목록 +- [[k3s-migration]] — K3s 마이그레이션 기록