Heartbeat makes Nginx high availability cluster

One. Theory

・HA is highly available and has been It is called hot standby.

・High availability principle: Two machines A and B. Normally, A provides services and B is idle. When A is down or the service is down, switch to B Continue to provide services and use heartbeat for communication and elections

・heartbeat principle: heartbeat detection and resource takeover are two parts. The hosts in the cluster send messages to each other to tell each other their current status. If they do not receive the message from each other within a certain period of time, they are deemed dead.

・Commonly used high-availability software: heartbeat, keepalived

・Schizophrenia: Both parties cannot transmit heartbeat information, resulting in the inability to obtain other node status information

< p style="white-space:normal;">・heartbeat is suitable for high availability of services

・keepalived is suitable for high availability of lvs

・Two detection methods: send messages to each other, the second ping gateway or another third party

・The function of the heartbeat line (intranet IP): send messages to each other

・Experiment: Both virtual machines AB are equipped with nginx and heartbeat, two virtual machines AB The machines all heartbeat up, but do not start nginx.

At this time, nginx on the master node will automatically start and get a VIP.

Normally, A provides services and VIP is on A. If nginx on A fails and stops working, VIP will drift to B, nginx will also start automatically to achieve high availability effect

< strong>Second, build

Key points: Time synchronization

Each cluster node Can resolve each other

(1) Three configuration files:

 Secret key file: authkeys Heartbeat service configuration file: ha.cf Resource management configuration file: haresouce

(2) Prepare the environment

 1. Both virtual machines are installed with heartbeat and nginx 2. The hostnames are respectively: node1 node2 3. Both machines are added with two intranet addresses: 192.168.100.20, 192.168.100.30 (used to simulate the heartbeat line 4. Turn off the firewall service iptables stop 5. Turn off SElinux setenforce 0 6. Add resolution vim/etc/hosts # The addition of the host name and address is as follows: 06 and 192.168.1.104 external network address node1 7. Heartbeat installed on both machines libnet nginx yum install -y heartbeat libnet nginx yum inst all-y heartbeat* libnet nginx

(3) Main node:

 1.cd /usr/share/doc/heartbeat-3.0.4/
2.cp authkeys ha.cf haresources /etc/ha.d/ #Copy template configuration file
3.cd /etc/ha.d
4.vim authkeys # encryption disposed at each heartbeat detection
5.vim haresources # set the primary node, not modify top alternate
node1 192.168 .1.109 / 24 / eth0: 0 nginx # hostname of the master node / external network IP / mask / virtual interface / high-availability service
6.vim ha.cf
debugfile / var / log / ha -debug # debug information recording
ogfile / var / log / ha-log # log information
logfacility local0 # log level
keepalive 2 # send heartbeat messages
deadtime interval 30 # Wait 30 seconds, if the other party did not receive a heartbeat, seen as downtime
warntime 10 # 10 Miao receive, log
initdead 60 # network startup time
udpport 694 # Set the broadcast communication Port used
ucast eth1 192.168.100.20 #The first type: broadcasts to detect the other party’s heartbeat information (the other party, intranet IP)
auto_failback on #After the master node is restored, whether to take over the service of the slave node
respawn hacluster/usr/lib64/heartbeat/ipfail #Cooperate with the ping node specified by the ping statement to detect network connectivity
node zhaijunming #master node
zhainode 1 ping 192.168.1.254 #The second detection method, ping third-party stable nodes #Two detection methods: 1. Send heartbeat messages to each other 2. Ping third-party stable nodes

(4) Standby node

 1. Copy the configuration file to the slave server scp ha.cf haresources authkeys zhaijunming1:/etc/ha.d/ copy the configuration file to the slave 2. Modification of the standby node /etc/ha.cf vim /etc/ha.d/ha.cf # You only need to change one place, and modify it to the other party's heartbeat line address "ucast eth1 192.168.100.20 "change to "ucast eth1 192.168. 100.30"

Three, test

Main: 1. Start heartbeat: wait about one minute, execute ifconfig -a Check if there is a VIP, and then whether the nginx service is automatically ready for backup: 2. Start heartbeat, no VIP, and nginx cannot automatically start or stop httpd on the primary node, to see if the VIP goes to the standby node, and the standby node httpd service Whether to start

Leave a Comment

Your email address will not be published.