Keepalived is a software similar to the Layer3, 4 & 7 switch, which is what we normally call the 3rd, 4th, and 7th layers of exchange. The role of keepalived is to detect the state of a Web server, if a Web server crashes, or if the work fails,
Keepalived will detect and remove the faulty Web server from the system, and when the Web server is working properly, Keepalived automatically joins the Web server to the server farm, which is fully automated and does not require manual intervention. What you need to do manually is to repair the failed Web server.
Here's how to install Keepalived on Linux and build a highly available Web server.
The first is the installation of the keepalived. Keepalived need to be compiled in real time, here is the use of keepalived-1.2.2.tar.gz, here is assumed to upload the compressed file to/usr.
First log in to the root user and navigate to/usr.
#编译安装keepalived
Unzip keepalived-1.2.2.tar.gz #或者使用 "Tar zxvf keepalived-1.2.2.tar.gz" command to decompress
CD keepalived-1.2.2
./configure--prefix=/usr/local/keepalived
Make && make install #编译keepalived源码
#设定keepalived的配置文件并将keepalived注册为服务然后设定为开机启动 (Start-up front)
Mkdir/etc/keepalived
Cp/usr/local/keepalived/etc/keepalived/keepalived.conf/etc/keepalived/keepalived.conf
cp/usr/local/keepalived/etc/rc.d/init.d/keepalived/etc/init.d/
cp/usr/local/keepalived/etc/sysconfig/keepalived/etc/sysconfig/
cp/usr/local/keepalived/sbin/keepalived/usr/sbin/
Chkconfig--level 2345 keepalived on #注册开机启动
Note that, here keepalived itself installed in the/usr/local, keepalived configuration file keepalived.conf placed under the/etc/keepalived.
At this point keepalived has been installed and added to the boot start, do not worry about starting keepalived, we also configure its configuration file.
Let's talk about the requirements we're going to meet:
1. When one of the machines or keepalived is hung, the virtual IP is automatically floated to another machine for high availability. The virtual IP will automatically drift back after the machine is restored as a master.
2. When the keepalived switch, the specified script needs to be executed, and in the script we can perform some other actions (such as starting or stopping the database).
Here is the first introduction to virtual IP (VIP).
A VIP is an IP address that is not connected to a particular computer or to a network interface card (NIC) in a computer. The packet is sent to this VIP address, but all the data is still in the real network interface.
It is a kind of IP technology, enhance the network management, play the advantages of VLAN, change the network structure, rationally allocate network resources, balance Network load, effectively reduce the online broadcast information, convenient to the user's group management.
In white, the VIP is an IP address, but it is not assigned to a particular network card. In this way, multiple network cards can be rotated with this VIP to bind, so that in the case of no need to change the Web service address, the implementation of high-availability programs.
VIP Brief Introduction, the following explains how to configure the keepalived, so as to achieve high availability.
Here I use a word to summarize the principle that keepalived achieves high availability: between two machines (or multiple units) to provide VIP in turn, so that the Web service uninterrupted.
When a server hangs up, the VIP drifts, and the process is automatically done by keepalived, and all we have to do is maintain the problematic Web server.
This assumes that there are two machines A and B, their IP addresses are 192.168.1.8 and 192.168.1.9, and the VIP we set to 192.168.1.10.
To configure machine A first, my goal is to simply introduce the configuration as straightforward as possible, thus making it easier for everyone to understand ....
vi/etc/keepalived/keepalived.conf this file first.
! Configuration File for Keepalived
Global_defs {
router_id Lvs_devel
}
Vrrp_instance Vi_1 {#一个keepalived实例
State MASTER #表明这是主机
Interface Eth0 #绑定的网卡
VIRTUAL_ROUTER_ID #两台机器要一致, indicating that they were in the same group
Mcast_src_ip 192.168.1.8 #发送多播包的地址, this is the IP address of the machine (if this machine has multiple IP addresses, this is useful)
Priority #本机的竞争优先级, higher than the average master
Advert_int 1
Authentication {
Auth_type PASS #认证类型
Auth_pass 1111 #认证密码
}
virtual_ipaddress {
192.168.1.10 #提供的虚拟ip地址
}
Notify_master "/1.sh" #在keepalived切换到本机时会执行此脚本
Notify_backup "/2.sh" #在keepalived从本机切换走时会执行此脚本
Notify_fault "/2.sh" #在keepalived停止时会执行此脚本
#注意下notify_backup和notify_fault的区别, such as slave, when the VIP is switched from the slave to the host, the Notify_backup script from the machine executes; when the slave's keepalived service stops (not just the VIP drift away, Instead, the keepalived process stops), and the Notify_fault script executes.
}
Machine B
! Configuration File for Keepalived
Global_defs {
router_id Lvs_devel
}
Vrrp_instance Vi_1 {
State BACKUP #表明是从机
Interface eth0
VIRTUAL_ROUTER_ID 51
Mcast_src_ip 192.168.1.9
Priority 100
Advert_int 1
Authentication {
Auth_type PASS
Auth_pass 1111
}
virtual_ipaddress {
192.168.1.10
}
}
OK, configuration complete. Under the root user of both machines, perform each of the following:
Service keepalived Start
Then ping the VIP:
Ping 192.168.1.10
Found to have returned.
Then we turn off the a machine or stop the keepalived (service keepalived stop) of the A machine, then ping the VIP and find that there is a return, which is the VIP has already got the ticket to the B machine.
Finally we restore the keepalived service on the A machine, then ping the VIP and return, in fact, the VIP has drifted to machine a.
You can install a Web server on both machines, or use the Linux Apache server directly, and then test it in the browser.
As for the two start/stop scripts, everyone is interested to be able to test under their own.
Linux under keepalived installation configuration