본문 바로가기

리눅스

[리눅스] keepalived 설치 및 설정

728x90

keepalived 설치 및 설정

 keepalived(이중화) : Load balancer and high availability service

구성 환경

서버 운영체제 아이피 패키지 비고
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 서버 설정

$ yum install -y keepalived
$ 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 설정

이미지 출처 : https://medium.com/naver-cloud-platform/keepalived%EB%A5%BC-%ED%99%9C%EC%9A%A9%ED%95%98%EC%97%AC-%EA%B0%84%EB%8B%A8%ED%95%98%EA%B2%8C-ha-%EA%B5%AC%EC%84%B1%ED%95%B4%EB%B3%B4%EA%B8%B0-73ab791c60fc

MASTER 설정 변경

keepalived.conf 편집(/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
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.0.100/24
    }
}
root@master:~$ systemctl --now enable keepalived

ip a(ip -br 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
       

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

BACKUP 설정 변경

keepalived.conf 편집(/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
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.0.100/24
    }
}
root@backup:~$ systemctl --now enable keepalived

ip a(ip -br 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


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

테스트

MASTER에서 VIP(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
728x90