환경 : ubuntu 위에서 동작하는 centos7 도커 컨테이너 2대

버전 : postgresql 13

 

Dockerfile

FROM centos:7
ENV container docker
RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == \
  systemd-tmpfiles-setup.service ] || rm -f $i; done); \
  rm -f /lib/systemd/system/multi-user.target.wants/*;\
  rm -f /etc/systemd/system/*.wants/*;\
  rm -f /lib/systemd/system/local-fs.target.wants/*; \
  rm -f /lib/systemd/system/sockets.target.wants/*udev*; \
  rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \
  rm -f /lib/systemd/system/basic.target.wants/*;\
  rm -f /lib/systemd/system/anaconda.target.wants/*;\
  echo "sslverify=false" >> /etc/yum.conf;\
  yum install -y wget sudo;\
  wget https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm --no-check-certificate;\
  sudo yum install -y pgdg-redhat-repo-latest.noarch.rpm;\
  sudo yum install -y postgresql13-server;
VOLUME [ "/sys/fs/cgroup" ]
COPY ./start-postgresql13.sh ./start-postgresql13.sh
RUN chmod 777 ./start-postgresql13.sh
CMD [ "./start-postgresql13.sh" ]

 

start-postgresql13.sh

#!/bin/bash

sudo /usr/pgsql-13/bin/postgresql-13-setup initdb
sudo systemctl enable postgresql-13
sudo systemctl start postgresql-13

 

docker-compose

version: '3.3'

services:
  active:
    image: postcent:7
    container_name: p1
    privileged: true
    stdin_open: true
    tty: true
    entrypoint: init

  standby:
    image: postcent:7
    container_name: p2
    privileged: true
    stdin_open: true
    tty: true
    entrypoint: init

 

 

Dockerfile 을 다음과 같이 빌드하여 이미지를 만듦

sudo docker build --tag postcent:7 .

 

docker-compose 명령어로 두 centos7 서버를 실행함

sudo docker-compose up -d

 

p1, p2 컨테이너로 각각 들어감

sudo docker exec -it p1 bash
sudo docker exec -it p2 bash

 

p1, p2 에서 아래 shell script 실행하여 postgresql 13 실행

/start-postgresql13.sh

 

p1 은 active 로 사용할 예정이고

p2 는 standby (read-only) 로 사용할 예정

 

 

 

< p1 >

 

p1 에서 아래 작업 진행 

아래 경로로 이동

cd /var/lib/pgsql/13/data

 

postgres.conf 에 아래 추가

listen_addresses = '*'
wal_level = hot_standby
max_wal_senders = 2
max_replication_slots = 2

 

pg_hba.conf 에 아래 추가

host all all 0.0.0.0/0 md5
host replication replication 0.0.0.0/0 md5

두번째 0.0.0.0/0 에 standby 의 ip 가 들어가도 됨

 

 

사용자 계정을 postgres 로 변경

su postgres

 

postgresql 접속

psql


replication 전용 유저 생성

create user replication replication password 'pwd';

 

확인

\du

 

\q 명령어로 psql 빠져나옴

exit 명령어로 postgres 유저에서 root 유저로 나옴

 

postgresql13 재시작

systemctl restart postgresql-13

 

 

혹시 아래와 같은 에러가 난다면, conf 파일을 제대로 읽지 못해서 생김

(위에서 수정한 conf 파일이 잘못되었거나, conf 파일 접근 권한이 없거나, 혹은 소유자가 postgres 가 아니거나 등)

Job for postgresql-13.service failed because the control process exited with error code. See "systemctl status postgresql-13.service" and "journalctl -xe" for details.

 

 

 

 

< p2 >

 

postgres 유저로 변경

su postgres

 

data 삭제

rm -r /var/lib/pgsql/13/data

 

active 인 p1 으로부터 data 를 복사해서 p2 에 넣음

pg_basebackup -h p1 -D /var/lib/pgsql/13/data -U replication -Fp -Xs -P -R

중간 -h p1 에는 p1 의 ip 가 들어감

 

data 로 이동

cd /var/lib/pgsql/13/data


postgresql.conf 에 아래 추가

hot_standby = on
hot_standby_feedback = on

 

exit 명령어로 postgres 유저에서 root 유저로 나옴

 

postgresql13 재시작

systemctl restart postgresql-13

 

 

 

 

 

< active, standby 설정 확인 >

 

아래 명령어를 통해 wal 프로세스를 확인

ps -ef | grep wal 

 

active 인 p1 에서는 postgres: walsender 가 나와야 하고

standby 인 p2 에서는 postgres: walreceiver 가 나와야 함

 

 

psql 에 접근한 후, 아래 명령어 사용

select pg_is_in_recovery();

 

active 인 p1 에서는 f 가 나와야 하고

standby 인 p2 에서는 t 가 나와야 함

 

 

 

active p1 에서 psql 에 접근한 후, 아래 명령어로 테스트용 database 를 생성

CREATE DATABASE test;

 

standby p2 에서 psql 에 접근한 후,

테스트용 database 가 p2 에서도 잘 보이는지 아래 명령어로 확인

\list

 

 

 

< failover 및 failback>

 

active 인 p1 이 죽었을 때, standby 인 p2 가 active 역할을 하게 만드는 것(failover)과

죽은 active p1이 다시 살아났을 때 p1 이 다시 active 가 되는 것(failback)은 수동 작업에 의해 진행됨

수동작업 [참고]

 

자동 failover 및 failback 을 하려면

pgpool-II 라는 미들웨어가 필요함

 

 

참고

https://velog.io/@enosoup/PostgreSQL-HA-%EA%B5%AC%EC%84%B1

https://bingbingpa.github.io/postgresql-high-availability/

https://jinisbonusbook.tistory.com/71?category=939888 

https://browndwarf.tistory.com/4

https://chess-drive.tistory.com/56

 

 

 

 

 

 

 

'SQL' 카테고리의 다른 글

[Hive] url decoder 예제  (0) 2022.05.05
[SQL] hackerrank New Company 쿼리  (0) 2022.03.09
[MySQL] 여러가지 함수 예제 모음  (2) 2022.02.13
[SQL] WAL 이란 무엇인가?  (0) 2022.02.08
[Phoenix] update values 하는 방법  (0) 2021.11.29

+ Recent posts