CentOS 8 네임서버(PowerDNS) - MariaDB(Replication) 이용한 1차 / 2차 네임서버 구성

    MariaDB(Replication) 레플리케이션(이)란?

    기본적으로 데이터베이스(Database)는 레플리케이션(Replication) 기능이 대부분 있습니다. 보통 시스템 의미적으로 마스터(Master)/슬레이브(Slave)를 뜻하며 원본과 복사본 사이를 다룹니다. 마스터는 변경 사항을 기록하고 그 결과를 그대로 슬레이브에게 복제 전달합니다. 이는 마스터 서버에서 정상적으로 값이 수정되거나 삭제를 했을 때 슬레이브에게 전달하도록 합니다.

     

    여기서의 1차/2차 네임서버 구성은 PowerDNS 솔루션 자체의 마스터/슬레이브 기능을 의미하는 것은 아니며 MariaDB만 레플리케이션 기능을 이용한 방법입니다. 실제 도메인 정보를 데이터베이스에 저장하는 PowerDNS 특성을 이용한 방법이며 한번 설정을 하면 2차 네임서버를 같이 관리하는 수고를 덜 수 있습니다. 물론 두 개 이상의 서버 시스템이 있어야 하며 여러 대 설정할 경우 복잡도로 인해 실용적이지는 못하다고 하지만 단순 2대의 마스터/슬레이브 설정하는 거라면 크게 문제가 없다고 생각하며 실제로 이렇게 문제없이 5년 이상정도 PowerDNS 서버를 유지해왔습니다.

     

    ※ 구성도 예시

    1차 네임서버
    (ns1.testdns.com)
    2차 네임서버
    (ns2.testdns.com)
    웹(PowerAdmin) 웹(PowerAdmin)
    PowerDNS PowerDNS
    MariaDB(Master) ↔ MariaDB(Slave)

    위와 같은 구성으로 이루어져 있으며 웹(PowerAdmin) / PowerDNS 세팅은 동일하며, DB만 마스터/슬레이브 상호작용을 하신다고 생각하면 됩니다. 그럼 이제부터 세팅방법을 알아보도록 하겠습니다.

    ■ [설치환경 및 준비물]

    릴리즈 설치 테스트 : 2020-07-29

    1. CentOS 8 PowerDNS 서버 준비 [포스팅 참고]

    2. 동일한 세팅을 한 서버를 두대 준비하기 [VMware 가상 솔루션은 복제(Clone) 기능 이용]

    3. 마스터(Master) 서버 IP[192.168.232.141] / 마스터(Slave) 서버 IP[192.168.232.142] 사설 세팅되어있다고 가정합니다.


    ■ 진행순서

     

    ① 1차 네임서버(Master DB) 서버 IP [192.168.232.141] 세팅 진행

    STEP01 → MariaDB Config 설정 후 재시작

    ※ 저의 경우는 include /etc/my.cnf.d/server.cnf 설정을 했지만 /etc/my.cnf 설정해도 상관없습니다.

    ◇ 마스터 서버 1순위 설정

    [root@localhost ~]# vi /etc/my.cnf.d/server.cnf

    # this is only for the mysqld standalone daemon

    [mysqld]
    server-id=1  [라인 추가]
    log-bin=mysql-bin  [라인 추가]

    ◇ MariaDB 서비스 재시작

    [root@localhost ~]# systemctl restart mysqld

    ◇ 바이너리 파일 생성 확인

    [root@localhost ~]# ls -al /var/lib/mysql | grep bin

    -rw-rw----   1 mysql mysql       328  7월 28 21:45 mysql-bin.000001

    -rw-rw----   1 mysql mysql        19  7월 28 21:45 mysql-bin.index

     

     

    STEP02 → MariaDB 접속 후 세팅 [순서 중요]

    ※ 접속 세션을 2개 연결하여 진행

    ◇ 1번 세션

    [root@localhost ~]# mysql -u root -p   [MariaDB 접속]

     

    MariaDB [(none)]> grant replication slave on *.* to 'pdnsbak' identified by 'test123'; [Slave 접속할 계정 생성]
    Query OK, 0 rows affected (0.122 sec)

     

    MariaDB [(none)]> flush privileges;  [적용]
    Query OK, 0 rows affected (0.008 sec)

     

    MariaDB [(none)]> flush tables with read lock;  [테이블 락 걸기]
    Query OK, 0 rows affected (0.001 sec)

    ◇ 2번 세션 전환

    [root@localhost ~]# mysqldump -u root -p pdns > pdns_bak.sql  [PDNS 데이터베이스 백업]

    Enter password: [패스워드 입력]

     

    [root@localhost ~]# ls -al pdns_bak.sql  [백업 파일 확인]

    -rw-r--r-- 1 root root 15725  7월 28 21:52 pdns_bak.sql

    ◇ 1번 세션 전환

    MariaDB [(none)]> show master status;  [중요! - 파일 및 포지션 숫자 확인 / 슬레이브 세팅 시 필요]
    +------------------+----------+--------------+------------------+
    | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
    +------------------+----------+--------------+------------------+
    mysql-bin.000001 |      637 |              |                  |
    +------------------+----------+--------------+------------------+
    1 row in set (0.013 sec)

     

    MariaDB [(none)]> unlock tables;  [테이블 락 해제]
    Query OK, 0 rows affected (0.000 sec)

    ◇ 2번 세션 전환

    [root@localhost ~]# systemctl restart mysqld  [서비스 재시작]

    ※ 예시

     

     

    STEP03 → 슬레이브에서 DB 접근 가능할 수 있도록 3306 포트 허용

    ◇ iptables 수정 후 저장[순서 중요]

    [root@localhost ~]# vi /etc/sysconfig/iptables

    # sample configuration for iptables service
    # you can edit this manually or use system-config-firewall
    # please do not ask us to add additional ports/services to this default configuration
    *filter
    :INPUT ACCEPT [0:0]
    :FORWARD ACCEPT [0:0]
    :OUTPUT ACCEPT [0:0]
    -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
    -A INPUT -p icmp -j ACCEPT
    -A INPUT -i lo -j ACCEPT
    -A INPUT -p tcp -m state --state NEW -m tcp --dport 8888 -j ACCEPT
    -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
    -A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT  [라인 추가]
    -A INPUT -j REJECT --reject-with icmp-host-prohibited
    -A FORWARD -j REJECT --reject-with icmp-host-prohibited
    COMMIT

    ◇ 서비스 재시작

    [root@localhost ~]# systemctl restart iptables

    ◇ 포트 허용 확인

    [root@localhost ~]# iptables -nL | grep 3306
    ACCEPT  tcp --  0.0.0.0/0   0.0.0.0/0  state NEW tcp dpt:3306

     

     

    ② 2차 네임서버(Slave DB) 서버 IP [192.168.232.142] 세팅 진행

    STEP01 → MariaDB Config 설정 후 재시작

    ◇ 슬레이브 서버 2순위 설정

    [root@localhost ~]# vi /etc/my.cnf.d/server.cnf

    # this is only for the mysqld standalone daemon

    [mysqld] 
    server-id=2  [라인 추가]
    log-bin=mysql-bin  [라인 추가]

    ◇ MariaDB 서비스 재시작

    [root@localhost ~]# systemctl restart mysqld

    ◇ 바이너리 파일 생성 확인

    [root@localhost ~]# ls -al /var/lib/mysql | grep bin

    -rw-rw----   1 mysql mysql       328  7월 28 22:18 mysql-bin.000001

    -rw-rw----   1 mysql mysql        19  7월 28 22:18 mysql-bin.index

     

     

    STEP02 → 마스터 서버에서 백업한 pdns 데이터베이스 파일을 가져옵니다.

    ◇ 알 싱크(rsync)를 이용한 파일 가져오기

    [root@localhost ~]# rsync -avz -e 'ssh -p 8888' root@192.168.232.141:/root/pdns_bak.sql /root/

    The authenticity of host '[192.168.232.141]:8888 ([192.168.232.141]:8888)' can't be established.
    ECDSA key fingerprint is...

    Are you sure you want to continue connecting (yes/no/[fingerprint])? yes

    Warning: Permanently added '[192.168.232.141]:8888' (ECDSA) to the list of known hosts.
    root@192.168.232.141's password: [패스워드 입력]
    receiving incremental file list
    pdns_bak.sql

     

    sent 43 bytes  received 2,580 bytes  276.11 bytes/sec
    total size is 15,725  speedup is 6.00

    ◇ 파일 확인

    [root@localhost ~]# ls -al | grep pdns
    -rw-r--r-- 1 root root  15725  7월 28 21:52 pdns_bak.sql

     

     

    STEP03 → MariaDB 접속 후 세팅[순서 중요]

    ※ 접속 세션을 2개 연결하여 진행

    ◇ 1번 세션

    [root@localhost ~]# mysql -u root -p  [MariaDB 접속]

     

    MariaDB [(none)]> drop database pdns;  [기존 데이터베이스 삭제]
    Query OK, 16 rows affected (0.190 sec)

     

    MariaDB [(none)]> create database pdns;  [재생성]
    Query OK, 1 row affected (0.019 sec)

    ◇ 2번 세션 전환

    [root@localhost ~]# mysql -u root -p pdns < pdns_bak.sql  [Master 서버에서 받은 파일을 복원]

    Enter password: [패스워드 입력]

    ◇ 1번 세션 전환

    MariaDB [(none)]> use pdns;  [데이터베이스 선택]
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A

     

    Database changed
    MariaDB [pdns]> show tables;  [정상 복원되었는지 확인]
    +--------------------+
    | Tables_in_pdns     |
    +--------------------+
    | comments           |
    | cryptokeys         |
    | domainmetadata     |
    | domains            |
    | migrations         |
    | perm_items         |
    | perm_templ         |
    | perm_templ_items   |
    | records            |
    | records_zone_templ |
    | supermasters       |
    | tsigkeys           |
    | users              |
    | zone_templ         |
    | zone_templ_records |
    | zones              |
    +--------------------+
    16 rows in set (0.000 sec)

     

    MariaDB [pdns]> RESET SLAVE;  [Slave 설정을 초기화]
    Query OK, 0 rows affected (0.018 sec)

     

    MariaDB [pdns]> STOP SLAVE;  [Slave 정지]
    Query OK, 0 rows affected, 1 warning (0.000 sec)

     

    [Slave 세팅 / Master server status 값과 일치]

    MariaDB [pdns]> change master to
     master_host='192.168.232.141',
     master_user='pdnsbak',
     master_port=3306,
     master_password='test123',
     master_log_file='mysql-bin.000001',
     master_log_pos=637;
    Query OK, 0 rows affected (0.009 sec)

     

    MariaDB [pdns]> START SLAVE;  [Slave 시작]
    Query OK, 0 rows affected (0.009 sec)

    ※ 예시

    여기까지 오면 세팅은 완료가 되었습니다.

    이제 문제없이 마스터/슬레이브 역할을 하고 있는지 확인해보도록 하겠습니다.

     

     

    ③ 레플리케이션(동기화) 기능 작동 확인

    ◇ 1차 네임서버[192.168.232.141] 마스터 서버 status 값 확인

    MariaDB [(none)]> show master status \G;
    *************************** 1. row ***************************
                File: mysql-bin.000002
            Position: 342
        Binlog_Do_DB:
    Binlog_Ignore_DB:
    1 row in set (0.000 sec)

    ◇ 2차 네임서버[192.168.232.142] 슬레이브 status 값 확인

    MariaDB [pdns]> show slave status \G;
    *************************** 1. row ***************************
                    Slave_IO_State: Waiting for master to send event
                       Master_Host: 192.168.232.141
                       Master_User: pdnsbak
                       Master_Port: 3306
                     Connect_Retry: 60
                   Master_Log_File: mysql-bin.000002  [값 일치 확인]
               Read_Master_Log_Pos: 342  [값 일치 확인]
                    Relay_Log_File: localhost-relay-bin.000003
                     Relay_Log_Pos: 641
             Relay_Master_Log_File: mysql-bin.000002
                  Slave_IO_Running: Yes
                 Slave_SQL_Running: Yes
                   Replicate_Do_DB:
               Replicate_Ignore_DB:
                Replicate_Do_Table:
            Replicate_Ignore_Table:
           Replicate_Wild_Do_Table:
       Replicate_Wild_Ignore_Table:
                        Last_Errno: 0
                        Last_Error:
                      Skip_Counter: 0
               Exec_Master_Log_Pos: 342  [값 일치 확인]
                   Relay_Log_Space: 1253
                   Until_Condition: None
                    Until_Log_File:
                     Until_Log_Pos: 0
                Master_SSL_Allowed: No
                Master_SSL_CA_File:
                Master_SSL_CA_Path:
                   Master_SSL_Cert:
                 Master_SSL_Cipher:
                    Master_SSL_Key:
             Seconds_Behind_Master: 0
     Master_SSL_Verify_Server_Cert: No
                     Last_IO_Errno: 0
                     Last_IO_Error:
                    Last_SQL_Errno: 0
                    Last_SQL_Error:
       Replicate_Ignore_Server_Ids:
                  Master_Server_Id: 1  [마스터 서버 ID]
                    Master_SSL_Crl:
                Master_SSL_Crlpath:
                        Using_Gtid: No
                       Gtid_IO_Pos:
           Replicate_Do_Domain_Ids:
       Replicate_Ignore_Domain_Ids:
                     Parallel_Mode: optimistic
                         SQL_Delay: 0
               SQL_Remaining_Delay: NULL
           Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
                  Slave_DDL_Groups: 0
    Slave_Non_Transactional_Groups: 0
        Slave_Transactional_Groups: 0

    와 같이 마스터 서버 상태 값과 전부 일치하면 문제없이 실시간 동기화가 진행되고 있는 부분입니다.

    만약 에러가 나면 다음과 같이 바로 표시하니 확인하여 수정해나가시면 됩니다.

     

    ◇ 에러가 날 경우 예시

    ※ 지금의 경우는 마스터 서버 쪽 3306 포트를 허용하지 않아 에러가 발생한 부분입니다.

    MariaDB [pdns]> show slave status \G;
    *************************** 1. row ***************************

    Master_SSL_Verify_Server_Cert: No
                    Last_IO_Errno: 2003
                    Last_IO_Error: error connecting to master 'pdnsbak@192.168.232.141:3306' - retry-time: 60  maximum-retries: 86400  message:
                                   Can't connect to MySQL server on '192.168.232.141' (113 "No route to host")
                   Last_SQL_Errno: 0

     

     

    ④ 레플리케이션(동기화) 기능 작동 확인

    ◇ 존(Zone) 파일 생성 확인[1차 생성 → 2차 전파 확인]

     

    ◇ 존(Zone) 파일 삭제 확인[1차 생성 → 2차 전파 확인]

    ※ 주의사항

    1. 존(Zone)파일/레코드(Records) 등록 및 삭제는 꼭 마스터 서버에서 진행할 것

    슬레이브 서버에서 작업 시에는 동기화가 되지 않음, 즉 마스터 서버 쪽으로 값이 반영되지 않습니다.

     

    2. 하드웨어 장애로 인한 서버가 강제 종료가 되었다면 재부팅 시 마스터/슬레이브 기능이 제대로 작동하지 않을 수 있으므로 문제가 발생할 경우는 슬레이브 서버만 DB를 재시작해주거나 위 동기화 작업을 다시 해주면 됩니다.

     

     

    마무리

    네임서버 구성도 운영방식에 따라 다양하므로 그중 하나의 방법입니다. 저는 BIND보다는 PowerDNS를 더 신뢰하다 보니 서버 구축할 때마다 항상 이 방식으로 운영을 해왔습니다. 백엔드로 DB에 직접 정보를 저장하여 질의응답을 잘못된 정보가 발생할 확률이 낮으며 Poweradmin 웹 관리자와 연결하면 편하게 추가 및 삭제를 할 수 있습니다. 크리티컬 한 보안 문제가 발생하지 않는다면야 앞으로도 이 방법을 주로 이용할 것으로 보입니다.

    댓글

    Designed by JB FACTORY