The configuration process of high-availability HA Using HeartBeat is described in detail.
I. Preface
HA (high available) is also known as dual-machine hot backup for key services. Simply put, there are two machines A and B. Normally, A provides services and B is on standby. When A goes down or the service goes down, it switches to B to continue providing services. Common open-source software for high availability include heartbeat and keepalived.
In this way, if a web server provides web services 24 hours a day, there will inevitably be failures of web services or server downtime, and users will not be able to access the services, this is certainly not what we expect. In this case, there are two servers, A provides external web services, and B serves as A backup. If A fails, B immediately replaces location A to provide web services, this is transparent to users. However, there is A problem: the ip address of server A is 10.0.0.100, And the ip address of server B is 10.0.0.101. Obviously, it is not feasible to provide the ip address of A or B to users, because the user cannot always switch the ip address for access. In this case, heartbeat or keepalived can provide A virtual IP Address: 10.0.0.102. You only need to access 10.0.0.102. When A provides services, the VIP address is set on server A. When B provides services, VIP is set on server B, so that you can access 10.0.0.102 to obtain the web service. Even if server A or server B switches, normal access is not affected.
Next we will use heartbeat for the HA cluster and use the nginx service as the corresponding HA service.
2. Prepare the lab environment
Server:
Host Name: master
Operating System: CentOS6.8 64-bit
Eth0 NIC address: 192.168.0.18
Eth1 NIC address: 172.16.254.18
Server B:
Host Name: slave
Operating System: CentOS6.8 64-bit
Eth0 NIC address: 192.168.0.28
Eth1 NIC address: 172.16.254.28
Virtual VIP:
VIP: 192.168.0.38
3. Set the Host Name
Set hostname on the master node
Hostname mastervim/etc/sysconfig/network: edit the configuration file HOSTNAME = master
Set hostname for the slave Node
# Hostname slave # vim/etc/sysconfig/network edit the configuration file: HOSTNAME = slave
4. Disable the firewall and selinux (perform operations on both nodes)
Disable iptables
# iptables -F# service iptables save# service iptables stop
Disable selinux:
# setenforce 0# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
5. Configure the hosts file (perform operations on both nodes)
# Add the following content to vim/etc/hosts: 192.168.0.18 master192.168.0.28 slave
6. Install the epel extension source (both of them are operated)
# yum install -y epel-release
7. Install heartbeat (both servers are operated)
# yum install -y heartbeat* libnet nginx
8. master node configuration
1. Copy the configuration file:
# cd /usr/share/doc/heartbeat-3.0.4/# cp authkeys ha.cf haresources /etc/ha.d/# cd /etc/ha.d
2. Modify authkeys
# Vim authkeys: auth 33 md5 Hello! Then modify the permission # chmod 600 authkeys
3. Edit the haresources File
# Vim haresources Add the following line: master 192.168.0.38/24/eth0: 0 nginx
Description: master node hostname, 192.168.0.38 is vip,/24 is the network segment with the mask of 24, eth0:0 is the device name of the vip, nginx is the service of heartbeat monitoring, and is also the core service provided by the two machines.
4. Edit ha. cf.
# Vim ha. modify cf to the following content: debugfile/var/log/ha-plugin/var/log/ha-logfacility plugin 2 deadtime 30 warntime 10 initdead 60 udpport 694 ucast eth1 plugin _ failback onnode masternode slaveping restart hacluster/usr/ lib64/heartbeat/ipfail
5. configuration instructions:
Debugfile/Var/Log/Ha-debug:This file stores heartbeat debugging information.
Logfile/Var/Log/Ha-log:Heartbeat log file.
Keepalive2: Interval of heartbeat. The default unit is seconds.
Deadtime30: If the heartbeat of the other node is not received beyond this interval, the other node is deemed to have died.
Warntime10: If the heartbeat of the peer node is not received beyond this interval, a warning is issued and recorded in the log.
Initdead60: In a system, it takes a period of time for the Network to work normally after the system is started or restarted. This option is used to solve the time interval generated in this case. The value is at least twice the deadtime.
Udpport694: Set the port used for broadcast communication. 694 is the default port number.
Ucast eth1172.16.254.28: Sets the NIC and IP address for heartbeat detection on the target machine.
Auto_failback on: Heartbeat master nodes and slave nodes respectively. The master node normally occupies resources and runs all services. In case of a fault, the node is handed over to the slave node for running services from the slave node. If this option is set to on, the slave node is automatically acquired and replaced once the master node resumes operation. Otherwise, the slave node is not replaced.
Respawn heartbeat/usr/lib/heartbeat/ipfail: Specifies the process to be started and closed together with heartbeat. The process is automatically monitored and restarted in case of a fault. The most common process is ipfail, which is used to detect and handle network faults. It must be used with the ping node specified by the ping statement to detect network connections. If your system is 64bit, pay attention to the file path.
9. Copy the three configuration files on the master node to the slave node.
# cd /etc/ha.d# scp authkeys ha.cf haresources slave:/etc/ha.d
10. Edit ha. cf from the node slave
# In vim/etc/ha. d/ha. cf, you only need to change the ucast eth1 172.16.254.28 to ucast eth1 172.16.254.18.
11. Start the heartbeat Service
After the configuration is complete, start the master first, and then start the slave.
# service heartbeat start
12. check and test
# Ifconfig check whether the eth0: 0 # ps aux | grep nginx interface exists to check whether the nginx process exists
XIII. Test Method 1
Intentionally disable ping on the master node
# iptables -I INPUT -p icmp -j DROP
Iv. Test method 2
The master node stops the heartbeat service.
# service heartbeat stop
15th. Split brain test
Both master node and slave node slave drop eth1 Nic
# ifdown eth1