본문 바로가기

리눅스

CentOS 7에서 keepalived를 설치하고 설정하여 A 서버와 B 서버 간에 VIP (Master와 Backup) 구성하는 방법

728x90

CentOS 7에서 keepalived를 설치하고 설정하여 A 서버와 B 서버 간에 VIP (Master와 Backup) 구성하는 방법

keepalived는 리눅스 환경에서 고가용성을 위해 사용되는 데몬으로, 가상 IP(VIP)를 두 개 이상의 서버 간에 이중화하는데 사용됩니다.

구성 환경

서버 운영체제 아이피 패키지 비고
VIP   192.168.0.100    
master CentOS 7.9 192.168.0.8 keepalived  
backup CentOS 7.9 192.168.0.9 keepalived  

keepalived 패키지 설치

  • MASTER/BACKUP 서버에 설정
sudo yum install epel-release
sudo yum install -y keepalived
keepalived -v
$ keepalived -v
Keepalived v1.3.5 (03/19,2017), git commit v1.3.5-6-g6fa32f2

Copyright(C) 2001-2017 Alexandre Cassen, <acassen@gmail.com>

Build options:  PIPE2 LIBNL3 RTA_ENCAP RTA_EXPIRES RTA_PREF RTA_VIA FRA_OIFNAME FRA_SUPPRESS_PREFIXLEN FRA_TUN_ID RTAX_CC_ALGO RTAX_QUICKACK LIBIPTC LIBIPSET_DYNAMIC LVS LIBIPVS_NETLINK VRRP VRRP_AUTH VRRP_VMAC SOCK_NONBLOCK SOCK_CLOEXEC FIB_ROUTING INET6_ADDR_GEN_MODE SNMP_V3_FOR_V2 SNMP SNMP_KEEPALIVED SNMP_CHECKER SNMP_RFC SNMP_RFCV2 SNMP_RFCV3 SO_MARK

keepalived 설정

A 서버와 B 서버 각각에서 keepalived 설정 파일인 /etc/keepalived/keepalived.conf를 편집합니다.

auth_pass에 사용할 비밀번호를 설정하고, interfacevirtual_ipaddress 값을 적절하게 변경하세요.

  • MASTER 설정 변경
    • keepalived.conf 편집(/etc/keepalived/keepalived.conf)
vim /etc/keepalived/keepalived.conf
root@master:~$ vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state MASTER
    interface enp0s3
    virtual_router_id 51
    priority 200 # Master의 우선순위는 높게 설정
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.0.100/24 # VIP 설정
    }
}
  • keepalived 실행 및 서비스 활성화
systemctl --now enable keepalived
  • ip a(ip -br a) 확인 결과
ip a
root@master:~$ ip a
...
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:65:3a:04 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.8/24 brd 192.168.0.255 scope global noprefixroute enp0s3
       valid_lft forever preferred_lft forever
    inet 192.168.0.100/24 scope global secondary enp0s3
       valid_lft forever preferred_lft forever
    inet6 fe80::c81c:fb28:a0f6:eecc/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
ip -brief address show
root@master:~$ ip -brief address show
lo               UNKNOWN        127.0.0.1/8 ::1/128 
enp0s3           UP             192.168.0.8/24 192.168.0.100/24 fe80::c81c:fb28:a0f6:eecc/64 
enp0s8           UP             
enp0s9           UP
728x90
  • BACKUP 설정 변경
    • keepalived.conf 편집(/etc/keepalived/keepalived.conf)
vim /etc/keepalived/keepalived.conf
root@backup:~$ vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state BACKUP
    interface enp0s3
    virtual_router_id 51
    priority 100 # Backup의 우선순위는 낮게 설정
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.0.100/24 # VIP 설정
    }
}
  • keepalived 실행 및 서비스 활성화
systemctl --now enable keepalived
  • ip a(ip -br a) 확인 결과
ip a
root@backup:~$ ip a
...
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:2f:b9:a3 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.9/24 brd 192.168.0.255 scope global noprefixroute enp0s3
       valid_lft forever preferred_lft forever
    inet6 fe80::3677:2cd:819d:bffc/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
ip -brief address show
root@backup:~$ ip -brief address show
lo               UNKNOWN        127.0.0.1/8 ::1/128 
enp0s3           UP             192.168.0.9/24 fe80::3677:2cd:819d:bffc/64 
enp0s8           UP             
enp0s9           UP

테스트

VIP가 A 서버의 Master 또는 B 서버의 Backup으로 동작하는지 확인합니다. keepalived가 작동하는 동안 VIP가 항상 하나의 서버에 할당됩니다. Master 서버가 다운되면 Backup 서버로 VIP가 이전됩니다.

  • MASTER에서 VIP(192.168.0.10)로 핑 테스트
ping -c 5 192.168.0.10
root@master:~$ ping -c 5 192.168.0.10
PING 192.168.0.10 (192.168.0.10) 56(84) bytes of data.
64 bytes from 192.168.0.10: icmp_seq=1 ttl=64 time=0.010 ms
64 bytes from 192.168.0.10: icmp_seq=2 ttl=64 time=0.057 ms
64 bytes from 192.168.0.10: icmp_seq=3 ttl=64 time=0.056 ms
64 bytes from 192.168.0.10: icmp_seq=4 ttl=64 time=0.057 ms
64 bytes from 192.168.0.10: icmp_seq=5 ttl=64 time=0.058 ms

--- 192.168.0.10 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4010ms
rtt min/avg/max/mdev = 0.010/0.047/0.058/0.020 ms
  • MASTER에서 keepalived 데몬 중지
root@master:~$ systemctl stop keepalived
root@master:~$ ip -brief address show
lo               UNKNOWN        127.0.0.1/8 ::1/128 
enp0s3           UP             192.168.0.8/24 fe80::c81c:fb28:a0f6:eecc/64 
enp0s8           UP             
enp0s9           UP
  • BACKUP으로 VIP(192.168.0.10)가 이동
root@backup:~$ ip -brief address show
lo               UNKNOWN        127.0.0.1/8 ::1/128 
enp0s3           UP             192.168.0.9/24 192.168.0.100/24 fe80::3677:2cd:819d:bffc/64 
enp0s8           UP             
enp0s9           UP

 

참고URL

- NAVER CLOUD PLATFORM : Keepalived를 활용하여 간단하게 HA 구성해보기

 

728x90