What is Keepalived
The main goal of this project is to provide simple and robust facilities for loadbalancing and high-availability to Linux system and Linux based infrastructures. high-availability is achieved by VRRP protocol. VRRP is a fundamental brick for router failover. In addition, Keepalived implements a set of hooks to the VRRP finite state machine providing low-level and high-speed protocol interactions. In order to offer fastest network failure detection, Keepalived implements BFD protocol. VRRP state transition can take into account BFD hint to drive fast state transition.
Environment
- OS:Debian 9.11
- 3CX Version:V16 SP2
- KeepAlived Version:1:1.3.2-1
- 3CX1 Server:10.168.1.21
- 3CX2 Server:10.168.1.22
- Virtual IP:10.168.1.20
I hope you get a better understanding of the setup with above structure. Let’s move to configuration IP failover setup between 3CX1 and 3CX2 servers.
Step 1 – Install Keepalived
First of all, Use the following command to install required packages to configure Keepalived on the server.
apt install linux-headers-$(uname -r)
Keepalived packages are available under default apt repositories. So just use a command to install it on both servers.
apt install keepalived
Step 2 – Setup Keepalived on 3CX1
Now create or edit Keepalived configuration /etc/keepalived/keepalived.conf file on 3CX1 and add following settings. Update all red highlighted values with your network and system configuration.
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
sysadmin@mydomain.com
support@mydomain.com
}
notification_email_from 3cx1@mydomain.com
smtp_server localhost
smtp_connect_timeout 30
}
vrrp_instance VI_1 {
state MASTER
interface ens192
virtual_router_id 101
priority 101
advert_int 1
authentication {
auth_type PASS
auth_pass 1234
}
virtual_ipaddress {
10.168.1.20
}
}
Step 3 – Keepalived on 3CX2
Also, create or edit Keepalived configuration file /etc/keepalived/keepalived.conf on 3CX2 and add the following configuration. While making changes in 3CX2 configuration file, make sure to set priority values to lower than 3CX1. For example below configuration is showing 100 priority value than 3CX1 has it 101.
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
sysadmin@mydomain.com
support@mydomain.com
}
notification_email_from 3cx2@mydomain.com
smtp_server localhost
smtp_connect_timeout 30
}
vrrp_instance VI_1 {
state MASTER
interface ens192
virtual_router_id 101
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1234
}
virtual_ipaddress {
10.168.1.20
}
}
- Priority value will be higher on Master server, It doesn’t matter what you used in state. If your state is MASTER but your priority is lower than the router with BACKUP, you will lose the MASTER state.
- virtual_router_id should be same on both 3CX1 and 3CX2 servers.
- By default single vrrp_instance support up to 20 virtual_ipaddress. In order to add more addresses you need to add more vrrp_instance
Step 4 – Start Keepalived Service
Start KeepAlived service using the following command and also configure to autostart on system boot.
service keepalived start
Step 5 – Check Virtual IPs
By default virtual IP will be assigned to master server, In the case of master gets down, it will automatically assign to the slave server. Use the following command to show assigned virtual IP on the interface.
ip addr
Sample output:
Also we can check virtual IPs in 3CX management console – Settings – Network – Public IP:
Step 6 – Verify IP Failover Setup
Shutdown master server (3CX1) and check if IPs are automatically assigned to slave server.
Now start 3CX1 and stop slave server (3CX2). IPs will automatically assigned to master server.