Heartbeat implements nginx Hot Backup
How heartbeat implements Hot Standby:
Unlike keepalived's vrrp protocol, heartbeat uses the serial port and ip address for heartbeat monitoring. The Node checks the network connectivity in the configuration file. If the network is disconnected, the ipfail of the master node performs a switchover, disable
Nginx and release the virtual ip address. At this time, the slave node takes over the virtual ip address and starts nginx on this node. This enables nginx hot backup.
Virtual ip Address: 192.168.3.233
Master node:
Hostname usvr-124
Eth0: 192.168.3.124
Eth0: 0: 192.168.8.124 for heartbeat monitoring
/Etc/hosts
1921383.126 usvr-126
Slave node:
Hostname usvr-126
Eth0: 192.168.3.126
Eth0: 0: 192.168.8.126 is used to monitor heartbeat
/Etc/hosts
1921383.124 usvr-124
Note: 1. You need to add the host of each other on the two machines.
2. Time Synchronization is required for the master and slave nodes.
1. Install heartbeat:
Wget http://mirrors.sohu.com/fedora-epel/6/i386/epel-release-6-8.noarch.rpm
Rpm-ivh epel-release-6-8.noarch.rpm
Yum-y install heartbeat *
After the installation is complete, related directories will be created in/etc/ha. d.
[Root @ usvr-124 ha. d] # rpm-q heartbeat-d
/Usr/share/doc/heartbeat-3.0.4/AUTHORS
/Usr/share/doc/heartbeat-3.0.4/COPYING
/Usr/share/doc/heartbeat-3.0.4/COPYING. LGPL
/Usr/share/doc/heartbeat-3.0.4/ChangeLog
/Usr/share/doc/heartbeat-3.0.4/README
/Usr/share/doc/heartbeat-3.0.4/apphbd. cf
/Usr/share/doc/heartbeat-3.0.4/authkeys
/Usr/share/doc/heartbeat-3.0.4/ha. cf
/Usr/share/doc/heartbeat-3.0.4/haresources
/Usr/share/man/man1/cl_status.1.gz
/Usr/share/man/man1/hb_addnode.1.gz
/Usr/share/man/man1/hb_delnode.1.gz
/Usr/share/man/man1/hb_standby.1.gz
/Usr/share/man/man1/hb_takeover.1.gz
/Usr/share/man/man5/authkeys.5.gz
/Usr/share/man/man5/ha.cf.5.gz
/Usr/share/man/man8/apphbd.8.gz
/Usr/share/man/man8/heartbeat.8.gz
Copy the configuration file to/etc/ha. d.
2. heartbeat Configuration
Master node configuration:
(1) Master configuration file (ha. cf)
# Debugfile/var/log/ha-debug
Logfile/var/log/ha-log# Indicates the location where heartbeat logs are stored.
# Crm yes # Whether to enable the Cluster Resource Manager (Cluster Resource management) function.
# Bcast eth1 # indicates that the heartbeat uses the Ethernet broadcast method and broadcasts the data on the eth1 interface.
Keepalive 2# Specify the heartbeat interval to 2 seconds (that is, a broadcast is sent on eth1 every two seconds ).
Deadtime 30# If the standby node does not receive the heartbeat signal from the master node within 30 seconds, it takes over the service resources of the master node immediately.
Warntime 10# Specify the heartbeat latency to 10 seconds. When the backup node cannot receive the heartbeat signal of the master node within 10 seconds, a warning log is written to the log, but the service is not switched.
Initdead 120# In some systems, it takes some 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 must be at least two times of deadtime.
Udpport 694# Set the port used for broadcast communication. 694 is the default port number.
# Baud 19200 # Set the baud rate of serial communication.
# Serial/dev/ttyS0 # select a serial communication device for dual-host connection with a serial line. If the dual-host is connected over Ethernet, disable this option.
Ucast eth0 192.168.8.126# The network adapter eth0 udp unicast is used to organize the heartbeat. The IP address followed by the NIC should be the IP address of the Peer machine.
# Mcast eth0 225.0.0.1 694 1 0 # Use Udp multicast of eth0 as the NIC to organize heartbeat. It is generally used when there are more than one slave node. Bcast, ucast, and mcast represent broadcast, unicast, and multicasting, respectively. They are three ways to organize heartbeat. You can choose either of them.
Auto_failback on# It defines whether to automatically switch back the service after the master node recovers. The two master nodes of heartbeat are the master node and the backup node respectively. The master node normally occupies resources and runs all services. In case of a fault, the node is handed over to the backup node and the backup node runs the services. When this option is set to on, once the master node resumes operation, the system automatically obtains the resource and replaces the backup node. If this option is set to off, after the master node recovers, the original backup node becomes the master node.
# Stonith baytech/etc/ha. d/conf/stonith. baytech # stonith is mainly used to remove faulty nodes from the cluster environment and release cluster resources to prevent two nodes from competing for one resource. Ensure the security and integrity of shared data.
# Watchdog/dev/watchdog # This option is optional and is used to monitor the running status of the system through Heartbeat. To use this feature, you need to load the "softdog" kernel module in the kernel to generate the actual device file. If this kernel module is not in the system, you need to specify this module and re-compile the kernel. Compile and input "insmod softdog" to load the module. Enter "grep misc/proc/devices" (10) and "cat/proc/misc | grep watchdog" (130 ). Finally, generate the device file: "mknod/dev/watchdog c 10 130 ". You can use this function.
Node usvr-124# Master node host name, which can be viewed by running the "uanme-n" command.
Node usvr-126# Backup node host name.
Ping 192.168.3.1# Select the ping node. The better the ping node, the stronger the HA cluster. You can select a fixed router as the ping node, but it is best not to select members in the cluster as the ping node, the ping node is only used to test network connections.
Respawn hacluster/usr/lib64/heartbeat/ipfail# This option is optional,List processes started and closed with heartbeatThis process is generally a plug-in integrated with heartbeat. If these processes encounter a fault, they can be automatically restarted. The most common process is ipfail, which is used to detect and handle network faults. You need to use the ping node specified by the ping statement to detect network connectivity. Hacluster indicates the identity to start the ipfail process.
(2) resource file (haresource)
The Haresources file is used to specify cluster resources such as the master node, cluster IP address, subnet mask, broadcast address, and startup service of the dual-machine system. Each row of the file can contain one or more resource script names, resources are separated by spaces, and parameters are separated by two colons. The files must be identical on the two HA nodes. The general format of this file is:
Node-name network <resource-group>
Node-name indicates the Host name of the master node, which must be the same as ha. the node names specified in the cf file are the same. The network is used to set the IP address, subnet mask, and network device ID of the Cluster. Note that the IP address specified here is the IP address of the Cluster's external services, rEsource-group is used to specify the services to be managed by heartbeat, that is, these services can be started and closed by heartbeat. If you want to host these services, the service must be written as a step that can be started and closed through start/stop, and then put in/etc/init. d/or/etc/ha. d/resource. in the d/directory, heartbeat will automatically go to/etc/init according to the script name. d or/etc/ha. d/resource. d/directory.
The following describes the configuration methods:
Usvr-124 IPaddr: 192.168.3.233/24/eth0 nginx
Among them, the usvr-124 is the master node of the HA cluster, IPaddr for heartbeat comes with an execution step, heartbeat first will execute/etc/ha. d/resource. d/IPaddr 192.168.3.233/24 start operation, that is, a virtual IP address with a subnet mask of 255.255.255.0 and an IP address of 192.168.3.233. This IP address is the network address that heartbeat provides external services, specify the network interface used by this IP address as eth0, and then start the nginx service.
Note: 1. The source file haresources must be identical between the master node and the backup node.
2. other resources, such as httpd, can be specified in resource-group, but start/stop must be enabled and disabled for the server to be followed and put in/etc/init. d/or/etc/ha. d/resource. d/down.
(3) authkeys)
Auth 3
#1 crc
#2 sha1 HI!
3 md5 Hello!
Ensure that the File Permission is 600
Chmod 600 authkeys
(4) Place the nginx Startup Script
#! /bin/bash# Description: Startup script for webserver on CentOS. cp it in /etc/init.d and# chkconfig --add nginx && chkconfig nginx on# then you can use server command control nginx## chkconfig: 2345 08 99# description: Starts, stops nginxset -ePATH=$PATH:/usr/local/nginx1.6/DESC="nginx daemon"NAME=nginxDAEMON=/usr/local/nginx1.6/$NAMECONFIGFILE=/usr/local/nginx1.6/nginx.confPIDFILE=/var/run/nginx.pidSCRIPTNAME=/etc/init.d/$NAME# Gracefully exit if the package has been removed.test -x $DAEMON || exit 0d_start() {$DAEMON -c $CONFIGFILE || echo -n " already running"}d_stop() {kill -QUIT `cat $PIDFILE` || echo -n " not running"}d_reload() {kill -HUP `cat $PIDFILE` || echo -n " can't reload"}case "$1" instart)echo -n "Starting $DESC: $NAME"d_startecho ".";;stop)echo -n "Stopping $DESC: $NAME"d_stopecho ".";;reload)echo -n "Reloading $DESC configuration..."d_reloadecho "reloaded.";;restart)echo -n "Restarting $DESC: $NAME"d_stopsleep 1d_startecho ".";;*)echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2exit 3;;esacexit 0
Modify the installation directory according to the actual situation.
Slave node configuration:
Copy the ha. cf haresources authkeys of the master node to the slave node.
Modifying ha. cf
Ucast eth0 192.168.8.124# The network adapter eth0 udp unicast is used to organize the heartbeat. The IP address followed by the NIC should be the IP address of the Peer machine.
3. Test
(1) Start heartbeat on the master and slave nodes
Service heartbeat start
(2) enter the command ip a to check whether the virtual ip address 192.168.3.233 is enabled on the master node.
[Root @ usvr-124 ha. d] # ip
1: lo: <LOOPBACK, UP, LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
Link/loopback 00: 00: 00: 00: 00: 00 brd 00: 00: 00: 00: 00: 00
Inet 127.0.0.1/8 scope host lo
Inet6: 1/128 scope host
Valid_lft forever preferred_lft forever
2: eth0: <BROADCAST, MULTICAST, UP, LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
Link/ether 00: 50: 56: 89: a2: 16 brd ff: ff
Inet 192.168.3.124/24 brd 192.168.3.255 scope global eth0
Inet 192.168.8.124/24 brd 192.168.8.255 scope global eth0: 0
Inet192.168.3.233/24Brd 192.168.3.255 scope global secondary eth0
Inet6 fe80: 250: 56ff: fe89: a216/64 scope link
Valid_lft forever preferred_lft forever
Nginx on the master node is also started normally, but nginx on the slave node is not started.
View on 192.168.216:
Curl http: // 192.168.3.233/1.html
Output: hello, 192.168.3.124
(3) Stop the master node's heartbeat. We can view the ip address a on the slave node.
[Root @ usvr-126 ha. d] # ip
1: lo: <LOOPBACK, UP, LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
Link/loopback 00: 00: 00: 00: 00: 00 brd 00: 00: 00: 00: 00: 00
Inet 127.0.0.1/8 scope host lo
Inet6: 1/128 scope host
Valid_lft forever preferred_lft forever
2: eth0: <BROADCAST, MULTICAST, UP, LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
Link/ether 00: 50: 56: 89: 91: a0 brd ff: ff
Inet 192.168.3.126/24 brd 192.168.3.255 scope global eth0
Inet 192.168.8.126/24 brd 192.168.8.255 scope global eth0: 0
Inet192.168.3.233/24Brd 192.168.3.255 scope global secondary eth0
Inet6 fe80: 250: 56ff: fe89: 91a0/64 scope link
Nginx is started on the slave node, but nginx on the master node has been stopped by heartbeat
View on 192.168.216:
Curl http: // 192.168.3.233/1.html
Output: hello, 192.168.3.126
Okay. Now heartbeat implements nginx hot backup. Let's apply it.