Heartbeat for Nginx hot standby

Source: Internet
Author: User

Heartbeat realization of the principle of hot preparation:

Unlike the Keepalived application VRRP protocol, Heartbeat uses the serial port and the IP to realize the heartbeat monitoring, the node checks the network connectivity in the configuration file, if the network is not connected, the ipfail of the master node will perform the switchover, turn off the

Nginx and release the virtual IP, this time the standby node will take over the virtual IP, and the Nginx on this node to start, so that the Nginx hot standby.


Virtual ip: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

192.1683.126 usvr-126

Alternate node:

Hostname usvr-126

eth0:192.168.3.126

Eth0:0:192.168.8.126 for heartbeat monitoring

/etc/hosts

192.1683.124 usvr-124


Note: 1. It is necessary to add each other's host on both machines.

2. The primary and standby nodes require time synchronization.


1.heartbeat Installation:

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*

The relevant directory will be established in/ETC/HA.D after the installation is complete.

[Email protected] 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 relevant configuration file into/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#指名heartbeat的日志存放位置.
#crm Yes #是否开启Cluster Resource Manager (cluster resource management) feature.
#bcast eth1 #指明心跳使用以太网广播方式, and is broadcast on the Eth1 interface.
keepalive 2#指定心跳间隔时间为2秒 (that is, every two seconds a broadcast is sent on eth1).
Deadtime#指定备用节点在30秒内没有收到主节点的心跳信号后, the service resources of the master node are taken over immediately.
Warntime Ten#指定心跳延迟的时间为十秒. When the backup node fails to receive the heartbeat of the primary node within 10 seconds, a warning log is written to it, but the service is not switched at this time.
Initdead#在某些系统上, it takes a period of time for the network to work after the system starts or restarts, which is used to resolve the time interval that occurs. The value is at least twice times deadtime.
Udpport 694#设置广播通信使用的端口, 694 is the port number that is used by default.
#baud 19200 #设置串行通信的波特率.
#serial/DEV/TTYS0 #选择串行通信设备 for the use of serial cable connection between two machines. If the two machines use an Ethernet connection, you should turn off this option.
ucast eth0 192.168.8.126#采用网卡eth0的udp单播来组织心跳, the IP address followed should be the IP address of the two machines.
#mcast eth0 225.0.0.1 694 1 0 #采用网卡eth0的Udp多播来组织心跳, generally used when more than one standby node. Bcast, Ucast, and Mcast represent broadcast, unicast, and multicast, which are the three ways to organize your heartbeat, and choose one of these.
Auto_failback on#用来定义当主节点恢复后, if the service is automatically cut back, the heartbeat two hosts are the primary node and the backup node respectively. The primary node consumes resources and runs all of the services under normal circumstances, handing the resources over to the backup node and running the service by the backup node when it encounters a failure. With this option set to ON, once the master node resumes running, the resource is automatically fetched and replaced by the backup node, and if the option is set to OFF, then when the primary node resumes, it becomes the backup node, and the original backup node becomes the primary node.
The main function of the #stonith Baytech/etc/ha.d/conf/stonith.baytech # Stonith is to disengage the problematic nodes from the clustered environment, freeing up the cluster resources and avoiding the contention that two nodes are competing for one resource. Ensure the security and integrity of shared data.
#watchdog/dev/watchdog #该选项是可选配置 is to monitor the operating state of the system through heartbeat. With this feature, you need to load the "Softdog" kernel module in the kernel to generate the actual device files, and if the kernel module is not available in the system, you need to specify this module and recompile the kernel. Compile to complete the input "Insmod Softdog" to load the module. Then enter "grep misc/proc/devices" (should be 10) and enter "Cat/proc/misc |grep watchdog" (130). Finally, build the device file: "Mknod/dev/watchdog C 10 130". You can use this feature.
node usvr-124#主节点主机名, can be viewed by the command "Uanme–n".
node usvr-126#备用节点主机名.
Ping 192.168.3.1#选择ping的节点, the better the ping node chooses, the stronger the HA cluster is, the more robust it can be to select a fixed router as the ping node, but it is best not to select a member of the cluster as the ping node, and the ping node is only used to test the network connection.
respawn Hacluster/usr/lib64/heartbeat/ipfail#该选项是可选配置,list processes that are started and closed together with Heartbeat, the process is typically an integrated plug-in with heartbeat, and these processes encounter failures that can be automatically restarted. The most commonly used process is ipfail, which is used to detect and handle network failures and to detect network connectivity with ping node specified by the ping statement. Where Hacluster represents the identity of the initiating ipfail process.

(2) resource file (Haresource)

The

Haresources file is used to specify cluster resources such as the master node, cluster IP, subnet mask, broadcast address, and service started for a dual-machine system, and each line of the file can contain one or more resource script names, separated by spaces between the resources, separated by two colons between the parameters. The file must be fully consistent on two HA nodes, the general format of this file is:
Node-name network  <resource-group>
Node-name represents the host name of the primary node, Must be consistent with the node name specified in the HA.CF file, network is used to set the IP address of the cluster, subnet mask, network device identity, etc., it should be noted that the IP address specified here is the IP address of the cluster external service, The R Esource-group is used to specify services that require heartbeat hosting, which can be started and shut down by heartbeat, and if the services are to be hosted, the service must be written as a footstep that can be started and closed through Start/stop. Then put it in the/etc/init.d/or/etc/ha.d/resource.d/directory, heartbeat will automatically/etc/init.d or/etc/ha.d/resource.d/according to the name of the script. Directory to find the appropriate footstep to start or close the operation.
The following configuration method is specified:
usvr-124 ipaddr::192.168.3.233/24/ eth0 nginx
where usvr-124 is the primary node of the HA cluster, ipaddr a step for heartbeat, Heartbeat first executes/ETC/HA.D/RESOURCE.D /IPADDR 192.168.3.233/24 start operation, That is, the virtual out of a subnet mask to 255.255.255.0,ip to 192.168.3.233 address, this IP for Heartbeat external service network address, while specifying this IP used the network interface is eth0, and then start the Nginx service.
Note: 1. The resource files in the master node and the backup node haresources exactly the same.

Other resources, such as httpd, can also be specified in 2.resource-group, but subsequent servers need to have start/stop started and shut down and placed under/etc/init.d/or/etc/ha.d/resource.d/.

(3) Certification document (Authkeys)

Auth 3
#1 CRC
#2 SHA1 hi!
3 MD5 hello!

Ensure that the file permissions are 600

chmod Authkeys

(4) Placing 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 NGI nx## chkconfig:2345 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" $ 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


According to the actual situation to modify the installation directory, etc.

Alternate node configuration :

Copy the HA.CF haresources Authkeys of the primary node to the standby node

Modifying the HA.CF

ucast eth0 192.168.8.124 #采用网卡eth0的udp单播来组织心跳, the IP address followed should be the IP address of the two machines.

3. Testing

(1) Start the heartbeat on the primary and standby node

Service Heartbeat Start

(2) Enter command IP A to see if the virtual ip192.168.3.233 on the primary node is started

[[email protected] ha.d]# IP a
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 Qdisc MQ State up Qlen
    Link/ether 00:50:56: 89:a2:16 BRD ff:ff:ff:ff:ff:ff
    inet 192.168.3.124/24 BRD 192.168.3.255 scope global eth0
  &NBSP ; inet 192.168.8.124/24 BRD 192.168.8.255 scope Global eth0:0
    inet 192.168.3.233/24 BRD 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 also starts normally, but nginx on the standby node does not start

View on 192.168.216:

Curl http://192.168.3.233/1.html

Output: hello,192.168.3.124

(3) The heartbeat of the master node is stopped, we are on the standby node IP a view

[[Email protected] ha.d]# IP A
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 Qdisc MQ State up Qlen 1000
Link/ether 00:50:56:89:91:a0 BRD FF:FF:FF:FF: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
inet 192.168.3.233/24 BRD 192.168.3.255 scope Global secondary eth0
Inet6 FE80::250:56FF:FE89:91A0/64 Scope link

The Nginx on the standby node is started, but the nginx on the main 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


OK, so heartbeat implement Nginx hot-standby, let us apply it


Heartbeat for Nginx hot standby

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.