æ°ãæåã«ãAmazonããå°çšãµãŒããŒïŒHetznerïŒã«ç§»è¡ããŸããããã®çç±ã®1ã€ã¯ãRDSã®ã³ã¹ããé«ãããšã§ãã å°çšãµãŒããŒã§ãã¹ã¿ãŒã¹ã¬ãŒãã¯ã©ã¹ã¿ãŒãæ§æããŠèµ·åããã¿ã¹ã¯ããããŸããã ã°ãŒã°ã«ã§å ¬åŒããã¥ã¡ã³ããèªãã åŸãé«å¯çšæ§Postgreséåæã¯ã©ã¹ã¿ãŒçšã®ç¬èªã®ãœãªã¥ãŒã·ã§ã³ãæ§ç¯ããããšã«ããŸããã
ç®æš
- ã§ããã ãå°ãªãããŒã«ãšäŸåé¢ä¿ã䜿çšããŠãã ããã
- éææ§ãç®æããŠãéæ³ã¯ãããŸããïŒ
- pg-poolãstolonãªã©ã®ãã¹ãŠãå«ãã³ã³ãã€ã³ã䜿çšããªãã§ãã ããã
- ããã«ãŒãšåœŒã®ãã³ã䜿çšããŠãã ããã
ããã§ã¯å§ããŸãããã å®éã«ã¯ãPostgresèªäœãšãã¬ããªã±ãŒã·ã§ã³ç®¡çãšã¯ã©ã¹ã¿ãŒç£èŠãæ±ãrepmgrãªã©ã®ãã°ãããããŒã«ãå¿ èŠã§ãã
ãã®ãããžã§ã¯ãã¯pg-dockãšåŒã°ãã3ã€ã®éšåã§æ§æãããŸããåéšåã¯githubäžã«ãããããããèªç±ã«å€æŽã§ããŸãã
- pg-dock-configã¯æºåå®äºã§ã-æ§æãã¡ã€ã«ã®ã»ããã§ã2ã€ã®ããŒãããã¹ã¿ãŒã¹ã¬ãŒãã«ç»é²ãããŠããŸãã
- pg-dockã¯ãæ§æãããã¯ããããããé©åãªåœ¢åŒã§é©åãªå Žæã«ããããŒãã«é ä¿¡ããŸãã
- pg-dock-baseã¯ãããŒãã§å®è¡ãããåºæ¬çãªdockerã€ã¡ãŒãžã§ãã
åéšåã詳现ã«åæããŸãããïŒ
pg-dock-config
ã¯ã©ã¹ã¿ãŒæ§æã®æ§é ã¯æ¬¡ã®ãšããã§ã
| ããã«ããŒããããå Žåã¯ã2ã€ã®ããŒãïŒn1ãn2ïŒããªããžããªã«æ¢ã«ç»é²ãããŠãããããæ°ããããŒãã®ååã§å¥ã®ãã©ã«ããŒãäœæããã ãã§ãã åããŒãã«ã¯ç¬èªã®æ§æãã¡ã€ã«ããããŸãã ãã¹ãŠãéåžžã«åçŽã§ããããã«æããŸããããšãã°ãenvãã©ã«ããŒã¯ãdocker-composeãpostgresãã©ã«ããŒãpostgres configsãªã©ã«ãã£ãŠååŸãããç°å¢å€æ°ã§ãã
ããšãã°ããã¡ã€ã«pg-dock-conf / n1 / env / main
postgresã®åæåæåäžã«ããŠãŒã¶ãŒpostgresãštestdbããŒã¿ããŒã¹ãäœæãããããšãæããŠãããŸãã ãŸããå€ããã¹ã¿ãŒããŒãã䜿çšã§ããªããªã£ãå Žåã«æ°ãããã¹ã¿ãŒããŒãã«ipãå€æŽããfailover-ipã¹ã¯ãªããã®ç»é²æžã¿å€æ°ããããŸãã pg-dock-conf / n1 / env / backup s3äžã®ããŒã¿ããŒã¹ã®ã€ã³ã¿ãŒãã«ããã¯ã¢ããã®ç°å¢å€æ°ã¯ããµãŒãã¹ã®éå§æã«docker-composeã«ãã£ãŠååŸãããŸãã |
å ±éã®æ§æãã¡ã€ã«ãããå ŽåãããŒãããšã«ããããè€è£œããªãããã«ãå ±æãã©ã«ããŒã«é 眮ããŸãã
ãã®æ§é ãèŠãŠãããŸãããã
- ãã§ã€ã«ãªãŒããŒ
ç§ã®å ŽåãHetzner failover-ipã®ã¹ã¯ãªããããããŸããããã¯ãipãæ°ãããã¹ã¿ãŒã«å€æŽããŸãã ããªãã®å Žåãããã¯keepalivedã¹ã¯ãªãããŸãã¯é¡äŒŒã®ãã®ã§ããå¯èœæ§ããããŸãã - initdb
ãã¹ãŠã®SQLåæåã¯ãšãªã¯ããã®ãã©ã«ããŒã«é 眮ããå¿ èŠããããŸãã - ssh
å¥ã®ããŒãã«æ¥ç¶ããããã®ããŒã次ã«ç€ºããŸãããã®äŸã§ã¯ããã¹ãŠã®ããŒãã®ããŒã¯åãã§ãããããå ±æãã©ã«ããŒã«ãããŸãã ֌֌Sshã§ã¯ãã¹ã€ãããªãŒããŒãªã©ã®æäœãè¡ãããã«repmgrãå¿ èŠã§ãã - sshd
sshãµãŒããŒã®æ§æãã¡ã€ã«sshã¯ããŒã2222ã§æ©èœããããããã¹ãã®ããã©ã«ãããŒããšéè€ããŸããïŒ22ïŒ
pg-dock
ããã§ã¯ãæ§æã¯åããŒãã«å®éã«ããã±ãŒãžåãããŠããŸãã
äžçªäžã®è¡ã¯ãããã«ãŒã€ã¡ãŒãžã«ããŒãã®æ§æãããã¯ãããããŸãã¯ã¬ãžã¹ããªã«ããã·ã¥ããŠãããããŒãã§æŽæ°ãè¡ãããšã§ãã
| äœæ¥ã«ã¯åºæ¬çãªæäœããããæ§æãã«ãïŒbuild.shïŒãäœæããããŒãã®æ§æãæŽæ°ããŸã
ïŒupdate.shïŒããã³ã¯ã©ã¹ã¿ãŒèªäœïŒdocker-compose.ymlïŒãéå§ããŸã
|
èµ·åæïŒ
PG_DOCK_NODE=n1 PG_DOCK_CONF_IMAGE=n1v1 ./build.sh docker images REPOSITORY TAG IMAGE ID CREATED SIZE n1v1 latest 712e6b2ace1a 6 minutes ago 1.17MB
æ§æpg-dock-conf / n1ããã©ã«ããŒpg-dock / pg-dock-conf-n1ã«ã³ããŒããããã¹ãŠã®äŸåé¢ä¿ã§dockerãã«ããéå§ãããŸããåºåã¯ãn1v1ãšããååã®ã€ã¡ãŒãžã§ãããŒãn1ã®æ§æãä¿åãããŸãã
èµ·åæïŒ
PG_DOCK_CONF_IMAGE=n1v1 ./update.sh
ããã«ããããã¹ãäžã®ãã¹ãŠã®æ§æãã¡ã€ã«ãæŽæ°ããã³ã³ãããèµ·åããŸãã ãããã£ãŠãè€æ°ã®æ§æã€ã¡ãŒãžãäœæããããç°ãªãããŒãžã§ã³ã«ããŒã«ããã¯ãããã§ããŸãã
pg-docker-base
ã¯ã©ã¹ã¿ãŒã®ãã¹ãŠã®ããã±ãŒãžãã€ã³ã¹ããŒã«ãããŠããåºæ¬çãªdockerã€ã¡ãŒãžïŒrepmgrãrsyncãopenssh-serverãsupervisorïŒ Dockerfile ïŒã ã€ã¡ãŒãžèªäœã¯ææ°ããŒãžã§ã³ã®postgres 9.6.3ã«åºã¥ããŠããŸãããä»ã®ãã«ãã䜿çšã§ããŸãã ã³ã³ããŒãã³ãã¯ãpostgresãŠãŒã¶ãŒã®äžããã¹ãŒããŒãã€ã¶ãŒã«ãã£ãŠèµ·åãããŸãã ãµãŒããŒã§ãã®ã€ã¡ãŒãžãå®è¡ããŸãïŒrepmgrãæ©èœãããã«ã¯ãrsyncãopenssh-serverãå¿ èŠã§ãïŒã
ã¯ã©ã¹ã¿ãŒãéå§ããŸãããïŒ
䟿å®äžããã®èšäºã§ã¯ãã¹ãŠã®æäœã¯docker-machineã䜿çšããŠè¡ãããŸãã
pg-dockããã³pg-dock-confãããžã§ã¯ããäœæ¥ãã©ã«ããŒïŒlabãªã©ïŒã«è€è£œããŸãã
mkdir ~/lab && cd ~/lab git clone https://github.com/xcrezd/pg-dock git clone https://github.com/xcrezd/pg-dock-conf
ããŒããã°ã«ãŒããpostgresãŠãŒã¶ãŒãäœæããŸãïŒuidãgidã¯ãã¹ãäžãšã³ã³ããå ã§5432ã§ãªããã°ãªããŸããïŒ
docker-machine create n1 docker-machine ssh n1 sudo addgroup postgres --gid 5432 docker-machine ssh n1 sudo adduser -u 5432 -h /home/postgres --shell /bin/sh -D -G postgres postgres # debian/ubuntu #sudo adduser --uid 5432 --home /home/postgres --shell /bin/bash --ingroup postgres --disabled-password postgres docker-machine create n2 docker-machine ssh n2 sudo addgroup postgres -g 5432 docker-machine ssh n2 sudo adduser -u 5432 -h /home/postgres --shell /bin/sh -D -G postgres postgres
/ etc / hostsã«ipããŒããè¿œå ããŸã
docker-machine ip n1 #192.168.99.100 docker-machine ip n2 #192.168.99.101 # n1 docker-machine ssh n1 "sudo sh -c 'echo 192.168.99.100 n1 >> /etc/hosts'" docker-machine ssh n1 "sudo sh -c 'echo 192.168.99.101 n2 >> /etc/hosts'" # n2 docker-machine ssh n2 "sudo sh -c 'echo 192.168.99.100 n1 >> /etc/hosts'" docker-machine ssh n2 "sudo sh -c 'echo 192.168.99.101 n2 >> /etc/hosts'"
ãã·ã³ã®IPãèšäºã®IPãšç°ãªãå Žåãããããè¿œå ããå¿ èŠããããŸã
- pg-dock-config / n1 / postgres / pg_hba.conf
- pg-dock-config / n2 / postgres / pg_hba.conf
æ§æã®ã€ã¡ãŒãžãäœæããããã«ããŒãã§æŽæ°ããŸã
cd pg-dock docker-machine use n1 PG_DOCK_NODE=n1 PG_DOCK_CONF_IMAGE=n1v1 ./build.sh PG_DOCK_CONF_IMAGE=n1v1 ./update.sh docker-machine use n2 PG_DOCK_NODE=n2 PG_DOCK_CONF_IMAGE=n2v1 ./build.sh PG_DOCK_CONF_IMAGE=n2v1 ./update.sh
docker-machine useã³ãã³ãïŒå®è¡æ¹æ³ ïŒã«æ³šæããŠãã ããã䜿çšãããã³ã«ãdockerã¯ã©ã€ã¢ã³ãã®ã³ã³ããã¹ããå€æŽããŸããã€ãŸããæåã®ã±ãŒã¹ã§ã¯ãdockerã§ã®ãã¹ãŠã®æäœã¯ããŒãn1ã§ã次ã«n2ã§è¡ãããŸãã
ã³ã³ãããŒãèµ·åãã
docker-machine use n1 PG_DOCK_NODE=n1 docker-compose up -d docker-machine use n2 PG_DOCK_NODE=n2 docker-compose up -d
docker-composeã¯ãs3ãžã®å®æçãªããã¯ã¢ãããè¡ãpg-dock-backupã³ã³ãããŒãèµ·åããŸãã
次ã«ãå¿ èŠãªãã¡ã€ã«ãä¿åãããŠããå ŽæãèŠãŠã¿ãŸãããã
ãã¡ã€ã«
| ãã¹ã
| ã³ã³ãã
|
Db
| / opt / pg-dock /ããŒã¿
| / var / lib / postgresql /ããŒã¿
|
ãã°
| / opt / pg-dock /ãã°
| / var / log /ã¹ãŒããŒãã€ã¶ãŒ
|
æ§æãšã¹ã¯ãªãã
| / opt / pg-dock /ã¹ã¯ãªãã
| ** docker-compose.ymlãåŠã¶
|
ã¯ã©ã¹ã¿ãŒãæ§æããŸã
docker-machine use n1 # docker exec -it -u postgres pg-dock repmgr master register docker-machine use n2 # n1 docker exec -it -u postgres -e PG_DOCK_FROM=n1 pg-dock manage/repmgr_clone_standby.sh # docker exec -it -u postgres pg-dock repmgr standby register
以äžã§ãã¯ã©ã¹ã¿ãŒã®æºåã¯å®äºã§ã
docker exec -it -u postgres pg-dock repmgr cluster show Role | Name | Upstream | Connection String ----------+------|----------|-------------------------------------------- * master | n1 | | host=n1 port=5432 user=repmgr dbname=repmgr standby | n2 | n1 | host=n2 port=5432 user=repmgr dbname=repmgr
圌ã®ããããæ§ã確èªããŸãããã pg-dock-config / shared / testsãã©ã«ããŒã«ã¯ãã¯ã©ã¹ã¿ãŒããã¹ãããããã®æºåããããŸãã
# cat tests/prepare.sh CREATE TABLE IF NOT EXISTS testtable (id serial, data text); GRANT ALL PRIVILEGES ON TABLE testtable TO postgres; # 100000 cat tests/insert.sh insert into testtable select nextval('testtable_id_seq'::regclass), md5(generate_series(1,1000000)::text); # cat tests/select.sh select count(*) from testtable;
ãã¹ãããŒãã«ãäœæããããŒã¿ãå ¥åããŠãã¹ã¬ãŒãäžã«ãããã©ããã確èªããŸãã
docker-machine use n1 # docker exec -it -u postgres pg-dock config/tests/prepare.sh # docker exec -it -u postgres pg-dock config/tests/insert.sh INSERT 0 1000000 docker-machine use n2 # n2 () docker exec -it -u postgres pg-dock config/tests/select.sh count --------- 1000000 (1 row)
å©çïŒ
ããã§ã¯ããŠã£ã¶ãŒãã®ã¯ã©ãã·ã¥ã·ããªãªãèŠãŠã¿ãŸãããã
# docker-machine use n1 docker stop pg-dock # repmgr docker-machine use n2 docker exec -it pg-dock tailf /var/log/supervisor/repmgr-stderr.log #NOTICE: STANDBY PROMOTE successful
å®å
šãªãã°
[2017-07-12 12:51:49] [ãšã©ãŒ]ã¢ããã¹ããªãŒã ããŒãã«æ¥ç¶ã§ããŸããïŒãµãŒããŒã«æ¥ç¶ã§ããŸããã§ããïŒæ¥ç¶ãæåŠãããŸãã
ãµãŒããŒã¯ãã¹ã "n1"ïŒ192.168.99.100ïŒã§å®è¡ãããåãå ¥ããŠããŸãã
ããŒã5432ã§ã®TCP / IPæ¥ç¶ïŒ
[2017-07-12 12:51:49] [ãšã©ãŒ]ããŒã¿ããŒã¹ãžã®æ¥ç¶ã«å€±æããŸããïŒãµãŒããŒã«æ¥ç¶ã§ããŸããã§ããïŒæ¥ç¶ãæåŠãããŸãã
ãµãŒããŒã¯ãã¹ã "n1"ïŒ192.168.99.100ïŒã§å®è¡ãããåãå ¥ããŠããŸãã
ããŒã5432ã§ã®TCP / IPæ¥ç¶ïŒ
[2017-07-12 12:51:49] [èŠå]ãã¹ã¿ãŒãžã®æ¥ç¶ã倱ããã埩æ§ããããšããŠããŸã...ãã§ã€ã«ãªãŒããŒæ±ºå®ã®60ç§å
[2017-07-12 12:51:59] [èŠå]ãã¹ã¿ãŒãžã®æ¥ç¶ã倱ããã埩æ§ããããšããŠããŸã...ãã§ã€ã«ãªãŒããŒæ±ºå®ã®50ç§å
[2017-07-12 12:52:09] [èŠå]ãã¹ã¿ãŒãžã®æ¥ç¶ã倱ããã埩æ§ããããšããŠããŸã...ãã§ã€ã«ãªãŒããŒæ±ºå®ã®40ç§å
[2017-07-12 12:52:19] [èŠå]ãã¹ã¿ãŒãžã®æ¥ç¶ã倱ããã埩æ§ããããšããŠããŸã...ãã§ã€ã«ãªãŒããŒæ±ºå®ã®30ç§å
[2017-07-12 12:52:29] [èŠå]ãã¹ã¿ãŒãžã®æ¥ç¶ã倱ããã埩æ§ããããšããŠããŸã...ãã§ã€ã«ãªãŒããŒæ±ºå®ã®20ç§å
[2017-07-12 12:52:39] [èŠå]ãã¹ã¿ãŒãžã®æ¥ç¶ã倱ããã埩æ§ããããšããŠããŸã...ãã§ã€ã«ãªãŒããŒæ±ºå®ã®10ç§å
[2017-07-12 12:52:49] [ãšã©ãŒ]ãã¹ã¿ãŒã«åæ¥ç¶ã§ããŸããïŒã¿ã€ã ã¢ãŠã60ç§ïŒ...
[2017-07-12 12:52:54] [泚æ]ãã®ããŒãã¯ãæ°ãããã¹ã¿ãŒã«æé©ãªåè£ã§ããã...
ïŒ TotalïŒ ReceivedïŒ Xferdå¹³åé床æéæéæéçŸåš
Dload Upload Total Spent Left Speed
100171100143 0 28 3 0 0:00:47 0:00:39 0:00:08 31
ãšã©ãŒïŒããŒã¿ããŒã¹ãžã®æ¥ç¶ã«å€±æããŸããïŒãµãŒããŒã«æ¥ç¶ã§ããŸããã§ããïŒæ¥ç¶ã¯æåŠãããŸãã
ãµãŒããŒã¯ãã¹ã "n1"ïŒ192.168.99.100ïŒã§å®è¡ãããåãå ¥ããŠããŸãã
ããŒã5432ã§ã®TCP / IPæ¥ç¶ïŒ
泚æïŒã¹ã¿ã³ãã€ã®ä¿é²
泚æïŒã/usr/lib/postgresql/9.6/bin/pg_ctl -D / var / lib / postgresql / data Promoteãã䜿çšããŠãµãŒããŒãææ Œ
éç¥ïŒã¹ã¿ã³ãã€ããã¢ãŒã·ã§ã³æå
ãµãŒããŒã¯ãã¹ã "n1"ïŒ192.168.99.100ïŒã§å®è¡ãããåãå ¥ããŠããŸãã
ããŒã5432ã§ã®TCP / IPæ¥ç¶ïŒ
[2017-07-12 12:51:49] [ãšã©ãŒ]ããŒã¿ããŒã¹ãžã®æ¥ç¶ã«å€±æããŸããïŒãµãŒããŒã«æ¥ç¶ã§ããŸããã§ããïŒæ¥ç¶ãæåŠãããŸãã
ãµãŒããŒã¯ãã¹ã "n1"ïŒ192.168.99.100ïŒã§å®è¡ãããåãå ¥ããŠããŸãã
ããŒã5432ã§ã®TCP / IPæ¥ç¶ïŒ
[2017-07-12 12:51:49] [èŠå]ãã¹ã¿ãŒãžã®æ¥ç¶ã倱ããã埩æ§ããããšããŠããŸã...ãã§ã€ã«ãªãŒããŒæ±ºå®ã®60ç§å
[2017-07-12 12:51:59] [èŠå]ãã¹ã¿ãŒãžã®æ¥ç¶ã倱ããã埩æ§ããããšããŠããŸã...ãã§ã€ã«ãªãŒããŒæ±ºå®ã®50ç§å
[2017-07-12 12:52:09] [èŠå]ãã¹ã¿ãŒãžã®æ¥ç¶ã倱ããã埩æ§ããããšããŠããŸã...ãã§ã€ã«ãªãŒããŒæ±ºå®ã®40ç§å
[2017-07-12 12:52:19] [èŠå]ãã¹ã¿ãŒãžã®æ¥ç¶ã倱ããã埩æ§ããããšããŠããŸã...ãã§ã€ã«ãªãŒããŒæ±ºå®ã®30ç§å
[2017-07-12 12:52:29] [èŠå]ãã¹ã¿ãŒãžã®æ¥ç¶ã倱ããã埩æ§ããããšããŠããŸã...ãã§ã€ã«ãªãŒããŒæ±ºå®ã®20ç§å
[2017-07-12 12:52:39] [èŠå]ãã¹ã¿ãŒãžã®æ¥ç¶ã倱ããã埩æ§ããããšããŠããŸã...ãã§ã€ã«ãªãŒããŒæ±ºå®ã®10ç§å
[2017-07-12 12:52:49] [ãšã©ãŒ]ãã¹ã¿ãŒã«åæ¥ç¶ã§ããŸããïŒã¿ã€ã ã¢ãŠã60ç§ïŒ...
[2017-07-12 12:52:54] [泚æ]ãã®ããŒãã¯ãæ°ãããã¹ã¿ãŒã«æé©ãªåè£ã§ããã...
ïŒ TotalïŒ ReceivedïŒ Xferdå¹³åé床æéæéæéçŸåš
Dload Upload Total Spent Left Speed
100171100143 0 28 3 0 0:00:47 0:00:39 0:00:08 31
ãšã©ãŒïŒããŒã¿ããŒã¹ãžã®æ¥ç¶ã«å€±æããŸããïŒãµãŒããŒã«æ¥ç¶ã§ããŸããã§ããïŒæ¥ç¶ã¯æåŠãããŸãã
ãµãŒããŒã¯ãã¹ã "n1"ïŒ192.168.99.100ïŒã§å®è¡ãããåãå ¥ããŠããŸãã
ããŒã5432ã§ã®TCP / IPæ¥ç¶ïŒ
泚æïŒã¹ã¿ã³ãã€ã®ä¿é²
泚æïŒã/usr/lib/postgresql/9.6/bin/pg_ctl -D / var / lib / postgresql / data Promoteãã䜿çšããŠãµãŒããŒãææ Œ
éç¥ïŒã¹ã¿ã³ãã€ããã¢ãŒã·ã§ã³æå
ã¯ã©ã¹ã¿ãŒã®ç¶æ ã確èªããŸãã
docker exec -it -u postgres pg-dock repmgr cluster show Role | Name | Upstream | Connection String ---------+------|----------|-------------------------------------------- FAILED | n1 | | host=n1 port=5432 user=repmgr dbname=repmgr * master | n2 | | host=n2 port=5432 user=repmgr dbname=repmgr
ããã§ãæ°ãããã¹ã¿ãŒã¯n2ã«ãªãããã§ã€ã«ãªãŒããŒipããããæããŸãã
ä»ãå€ããã¹ã¿ãŒãæ°ããã¹ã¬ãŒããšããŠè¿ããŸããã
docker-machine use n1 # PG_DOCK_NODE=n1 docker-compose up -d # # n2 docker exec -it -u postgres -e PG_DOCK_FROM=n2 pg-dock manage/repmgr_clone_standby.sh # docker exec -it -u postgres pg-dock repmgr standby register -F
ã¯ã©ã¹ã¿ãŒã®ç¶æ ã確èªããŸãã
docker exec -it -u postgres pg-dock repmgr cluster show Role | Name | Upstream | Connection String ---------+------|-----------|-------------------------------------------- * master | n2 | | host=n2 port=5432 user=repmgr dbname=repmgr standby| n1 | n2 | host=n1 port=5432 user=repmgr dbname=repmgr
ã§ããïŒ ãããŠããããç§ãã¡ãäœãšãã§ããããšã§ãã ãã¹ã¿ãŒãåé€ããæ°ãããã¹ã¿ãŒã«ããã¹ã¬ãŒãã®èªåå²ãåœãŠãæ©èœãããã§ãŒã«ãªãŒããŒIPãå€æŽãããŸããã ã·ã¹ãã ã¯æ©èœãç¶ããŸãã 次ã«ãããŒãn1ã埩掻ãããŠãæ°ããã¹ã¬ãŒãã«ããŸããã èå³ãåŒãããã«ãå ã»ã©ãšåæ§ã«ãn1ããã¹ã¿ãŒã«ãn2ãã¹ã¬ãŒãã«æåã§åãæ¿ããŸãã ãããrepmgrã®ç®çã§ãããsshãå¿ èŠã§ããã¹ã¬ãŒãã¯sshãä»ããŠãã¹ã¿ãŒã«æ¥ç¶ããã¹ã¯ãªããã䜿çšããŠå¿ èŠãªæäœãè¡ããŸãã
åãæ¿ãïŒ
docker-machine use n1 docker exec -it -u postgres pg-dock repmgr standby switchover #NOTICE: switchover was successful
å®å
šãªãã°
泚æïŒçŸåšã®ããŒã1ããã¹ã¿ãŒãµãŒããŒã«åãæ¿ããçŸåšã®ãã¹ã¿ãŒãã¹ã¿ã³ãã€ã«éæ ŒããŸã...
èŠåïŒæ¢ç¥ã®ãã¹ãã®ãªã¹ãã«ã[n2]ïŒ2222ã[192.168.99.101]ïŒ2222ãïŒECDSAïŒãæ°žä¹ çã«è¿œå ããŸããã
泚æïŒ1ã€ã®ãã¡ã€ã«ã/ tmp / repmgr-n2-archiveã«ã³ããŒãããŸãã
泚æïŒçŸåšã®ãã¹ã¿ãŒã¯åæ¢ããŠããŸã
ãšã©ãŒïŒããŒã¿ããŒã¹ãžã®æ¥ç¶ã«å€±æããŸããïŒãµãŒããŒã«æ¥ç¶ã§ããŸããã§ããïŒæ¥ç¶ã¯æåŠãããŸãã
ãµãŒããŒã¯ãã¹ã "n2"ïŒ192.168.99.101ïŒã§å®è¡ãããåãå ¥ããŠããŸãã
ããŒã5432ã§ã®TCP / IPæ¥ç¶ïŒ
泚æïŒã¹ã¿ã³ãã€ã®ä¿é²
泚æïŒã/usr/lib/postgresql/9.6/bin/pg_ctl -D / var / lib / postgresql / data Promoteãã䜿çšããŠãµãŒããŒãææ Œ
ãµãŒããŒä¿é²
éç¥ïŒã¹ã¿ã³ãã€ããã¢ãŒã·ã§ã³æå
泚æïŒå€ããã¹ã¿ãŒãµãŒããŒã§pg_rewindãå®è¡
èŠåïŒæ¢ç¥ã®ãã¹ãã®ãªã¹ãã«ã[n2]ïŒ2222ã[192.168.99.101]ïŒ2222ãïŒECDSAïŒãæ°žä¹ çã«è¿œå ããŸããã
èŠåïŒæ¢ç¥ã®ãã¹ãã®ãªã¹ãã«ã[n2]ïŒ2222ã[192.168.99.101]ïŒ2222ãïŒECDSAïŒãæ°žä¹ çã«è¿œå ããŸããã
泚æïŒ/ var / lib / postgresql / dataã«ã³ããŒããã1ã€ã®ãã¡ã€ã«
èŠåïŒæ¢ç¥ã®ãã¹ãã®ãªã¹ãã«ã[n2]ïŒ2222ã[192.168.99.101]ïŒ2222ãïŒECDSAïŒãæ°žä¹ çã«è¿œå ããŸããã
èŠåïŒæ¢ç¥ã®ãã¹ãã®ãªã¹ãã«ã[n2]ïŒ2222ã[192.168.99.101]ïŒ2222ãïŒECDSAïŒãæ°žä¹ çã«è¿œå ããŸããã
泚æïŒã/usr/lib/postgresql/9.6/bin/pg_ctl -w -D / var / lib / postgresql / data -m fast restartãã䜿çšããŠãµãŒããŒãåèµ·åãã
pg_ctlïŒPIDãã¡ã€ã«ã/var/lib/postgresql/data/postmaster.pidããååšããŸãã
ãµãŒããŒã¯å®è¡äžã§ããïŒ
ãšã«ãããµãŒããŒãèµ·åãã
泚æïŒããŒã2ã§è€è£œã¹ããããrepmgr_slot_1ããåé€ãããŸãã
泚æïŒåãæ¿ãã¯æåããŸãã
èŠåïŒæ¢ç¥ã®ãã¹ãã®ãªã¹ãã«ã[n2]ïŒ2222ã[192.168.99.101]ïŒ2222ãïŒECDSAïŒãæ°žä¹ çã«è¿œå ããŸããã
泚æïŒ1ã€ã®ãã¡ã€ã«ã/ tmp / repmgr-n2-archiveã«ã³ããŒãããŸãã
泚æïŒçŸåšã®ãã¹ã¿ãŒã¯åæ¢ããŠããŸã
ãšã©ãŒïŒããŒã¿ããŒã¹ãžã®æ¥ç¶ã«å€±æããŸããïŒãµãŒããŒã«æ¥ç¶ã§ããŸããã§ããïŒæ¥ç¶ã¯æåŠãããŸãã
ãµãŒããŒã¯ãã¹ã "n2"ïŒ192.168.99.101ïŒã§å®è¡ãããåãå ¥ããŠããŸãã
ããŒã5432ã§ã®TCP / IPæ¥ç¶ïŒ
泚æïŒã¹ã¿ã³ãã€ã®ä¿é²
泚æïŒã/usr/lib/postgresql/9.6/bin/pg_ctl -D / var / lib / postgresql / data Promoteãã䜿çšããŠãµãŒããŒãææ Œ
ãµãŒããŒä¿é²
éç¥ïŒã¹ã¿ã³ãã€ããã¢ãŒã·ã§ã³æå
泚æïŒå€ããã¹ã¿ãŒãµãŒããŒã§pg_rewindãå®è¡
èŠåïŒæ¢ç¥ã®ãã¹ãã®ãªã¹ãã«ã[n2]ïŒ2222ã[192.168.99.101]ïŒ2222ãïŒECDSAïŒãæ°žä¹ çã«è¿œå ããŸããã
èŠåïŒæ¢ç¥ã®ãã¹ãã®ãªã¹ãã«ã[n2]ïŒ2222ã[192.168.99.101]ïŒ2222ãïŒECDSAïŒãæ°žä¹ çã«è¿œå ããŸããã
泚æïŒ/ var / lib / postgresql / dataã«ã³ããŒããã1ã€ã®ãã¡ã€ã«
èŠåïŒæ¢ç¥ã®ãã¹ãã®ãªã¹ãã«ã[n2]ïŒ2222ã[192.168.99.101]ïŒ2222ãïŒECDSAïŒãæ°žä¹ çã«è¿œå ããŸããã
èŠåïŒæ¢ç¥ã®ãã¹ãã®ãªã¹ãã«ã[n2]ïŒ2222ã[192.168.99.101]ïŒ2222ãïŒECDSAïŒãæ°žä¹ çã«è¿œå ããŸããã
泚æïŒã/usr/lib/postgresql/9.6/bin/pg_ctl -w -D / var / lib / postgresql / data -m fast restartãã䜿çšããŠãµãŒããŒãåèµ·åãã
pg_ctlïŒPIDãã¡ã€ã«ã/var/lib/postgresql/data/postmaster.pidããååšããŸãã
ãµãŒããŒã¯å®è¡äžã§ããïŒ
ãšã«ãããµãŒããŒãèµ·åãã
泚æïŒããŒã2ã§è€è£œã¹ããããrepmgr_slot_1ããåé€ãããŸãã
泚æïŒåãæ¿ãã¯æåããŸãã
ã¯ã©ã¹ã¿ãŒã®ç¶æ ã確èªããŸãã
docker exec -it -u postgres pg-dock repmgr cluster show Role | Name | Upstream | Connection String ----------+------|----------|-------------------------------------------- standby | n2 | | host=n2 port=5432 user=repmgr dbname=repmgr * master | n1 | | host=n1 port=5432 user=repmgr dbname=repmgr
ããããã¹ãŠã次ã«ããŒãã®æ§æãæŽæ°ããå¿ èŠããããšãããããpostgresãrepmgrããŸãã¯ã¹ãŒããã€ã¶æ§æã§ããããšããããããã¯ããŠæŽæ°ããã ãã§ãïŒ
PG_DOCK_NODE=n1 PG_DOCK_CONF_IMAGE=n1v1 ./build.sh PG_DOCK_CONF_IMAGE=n1v1 ./update.sh
æ°ããæ§æãæŽæ°ããåŸïŒ
# postgres docker exec -it -u postgres pg-dock psql -c "SELECT pg_reload_conf();" # supervisor docker exec -it -u postgres pg-dock supervisorctl reread # docker exec -it -u postgres pg-dock supervisorctl restart foo:sshd
* ãã°ãããããŒãã¹ã§ããã¹ãŒããŒãã€ã¶ãŒã«ã¯ãã°ããŒããŒã·ã§ã³ã®æ©èœããããããããã«ã€ããŠãå¿é ããå¿ èŠã¯ãããŸããã
* ã³ã³ããã¯ãã¹ããããã¯ãŒã¯ãä»ããŠçŽæ¥åäœããããããããã¯ãŒã¯ä»®æ³åã®é 延ãåé¿ã§ããŸãã
* æ¢åã®å®çšŒåããŒããdocker-machineã«è¿œå ããããšããå§ãããŸããããã«ãããäœæ¥ãå€§å¹ ã«ç°¡çŽ åãããŸãã
次ã«ãã¯ãšãªãã©ã³ã·ã³ã°ã®ãããã¯ã«è§ŠããŸãã è€éã«ããããããŸããã§ããïŒã€ãŸããpg-poolãhaproxyãstolonã䜿çšããŸããïŒã®ã§ãã¢ããªã±ãŒã·ã§ã³åŽã§ãã©ã³ã¹ãåããããã«ããããã©ã³ãµãŒèªèº«ã®é«å¯çšæ§ãç·šæãã責任ãã解æŸãããŸãã ããã¯ãšã³ãã¯æ©æŠã§èšè¿°ãããŠãããããgem makaraãéžæããŸããã gemã¯ãããŒã¿ã®éžæãšå€æŽïŒæ¿å ¥/æŽæ°/åé€/å€æŽïŒã®ãªã¯ãšã¹ããåé¢ã§ããŸããéžæã®ãªã¯ãšã¹ãã¯ãè€æ°ã®ããŒãïŒã¹ã¬ãŒãïŒéã§åæ£ã§ããŸãã ããŒãã®1ã€ã«é害ãçºçããå Žåããã ã¯äžæçã«ãããããŒã«ããé€å€ã§ããŸãã
database.ymlèšå®ãã¡ã€ã«ã®äŸïŒ
production: adapter: 'postgresql_makara' makara: # the following are default values blacklist_duration: 5 master_ttl: 5 master_strategy: failover sticky: true connections: - role: master database: mydb host: 123.123.123.123 port: 6543 weight: 3 username: <%= ENV['DATABASE_USERNAME'] %> password: <%= ENV['DATABASE_PASSWORD'] %> - role: slave database: mydb host: 123.123.123.124 port: 6543 weight: 7 username: <%= ENV['DATABASE_USERNAME'] %> password: <%= ENV['DATABASE_PASSWORD'] %>
ä»ã®èšèª/ãã¬ãŒã ã¯ãŒã¯ã®ã©ã€ãã©ãªïŒ
â ã©ã©ãã«
â Yii2
â Node.js
ãããã«
ãããã£ãŠãçµæãšããŠåŸããããã®ïŒ
- èªå·±å®çµåã®ãã¹ã¿ãŒ/ã¹ã¿ã³ãã€ã¯ã©ã¹ã¿ãŒã¯ãæŠéã«åããŠããŸãã
- ãã¹ãŠã®ã³ã³ããŒãã³ãã®éææ§ãç°¡åãªäºææ§ã
- ãã¹ã¿ãŒã«é害ãçºçããå Žåã®èªåãã§ã€ã«ãªãŒããŒïŒrepmgrïŒ
- ã¯ã©ã€ã¢ã³ãã®è² è·åæ£ã«ããããã©ã³ãµãŒã®å¯çšæ§ã«å¯Ÿãã責任ãé€å»ãããŸãã
- åäžé害ç¹ããªããããrepmgrã¯IPã¢ãã¬ã¹ãæ°ããããŒãã«è»¢éããã¹ã¯ãªãããå®è¡ããŸããæ°ããããŒãã¯ãé害ãçºçããå Žåã«ãã¹ã¿ãŒã«ã¢ããã°ã¬ãŒããããŸããã ãã³ãã¬ãŒãã«ã¯hetznerçšã®ã¹ã¯ãªããããããŸãããkeepalivedãaws elasticIpãdrdbãpacemakerãcorosyncã®è¿œå ã劚ãããã®ã¯ãããŸããã
- ããŒãžã§ã³ç®¡çãåé¡/ abãã¹ãã®å Žåã«ããŒã«ããã¯ãè¡ãæ©èœã
- ã·ã¹ãã ãèªåçšã«ã«ã¹ã¿ãã€ãºããããããŒããè¿œå ããããrepmgr witnessãããšãã°æ§æã®æè»æ§ãå€æŽã確èªããæ©èœã
- S3ãžã®å®æçãªããã¯ã¢ãã
次åã®èšäºã§ã¯ãé«å¯çšæ§ã倱ããã«pg-dockãšPgBouncerãåãããŒãã«é 眮ããæ¹æ³ã説æããŸãããæž èŽããããšãããããŸããïŒ
ç¿çã®ããã®æšå¥šäºé ïŒ