: This article mainly introduces Keepalived + nginx to achieve dual-master high-availability load balancing. if you are interested in the PHP Tutorial, refer to it. Nginx + keepalived:
1. Nginx + keepalived master/slave configuration
This solution uses a vip address. the front end uses two machines, one master and one slave, but only one machine works at the same time, another backup machine is always in waste when the host is not faulty. this solution is not economical for websites with few servers, so it will not be used this time.
2. Nginx + keepalived dual-master configuration
In this solution, two vip addresses are used, two machines are used at the front end, and the master and slave nodes are used at the same time. when one machine fails, requests from two machines are transferred to one machine, which is very suitable for the current architecture environment. Therefore, this solution is used to implement a high-availability architecture for the website.
The second configuration shows how to achieve dual-master high-availability load balancing.
Group 1
VIP1: 192.168.36.100
Primary nginx: 192.168.36.99
Slave nginx: 192.168.36.86
Group 2
VIP2: 192.168.36.100
Primary nginx: 192.168.36.86
Slave nginx: 192.168.36.99
Web Services:
Web1: 192.168.36.215
Web2: 192.168.36.80
I. nginx configuration (installation omitted)
worker_processes 1;events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; sendfile on; upstream web1 { server 192.168.36.215:80 max_fails=3 fail_timeout=3s; } upstream web2 { server 192.168.36.80:80 max_fails=3 fail_timeout=3s; } server { listen 88; server_name localhost; location / { root html; index index.html index.htm; proxy_pass http://web1; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } server { listen 99; server_name localhost; location / { root html; index index.html index.htm; proxy_pass http://web2; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }}Configure and start nginx for 192.168.36.99 and 192.168.36.86. Check whether the access is normal.
Web1
Http: // 192.168.36.86: 88/
Http: // 192.168.36.99: 88/
Web2
Http: // 192.168.36.86: 99/
Http: // 192.168.36.99: 99/
II. install and configure keepalived (installation omitted)
1) 192.168.36.86 configuration
Vi/etc/keepalived. conf
! Configuration File for keepalivedglobal_defs { notification_email { monitor@3evip.cn #failover@firewall.loc } notification_email_from tianwei7518@163.com smtp_server smtp.163.com smtp_connect_timeout 30 router_id LVS_DEVEL}vrrp_script chk_nginx { script "/etc/keepalived/chk_nginx.sh" interval 2 weight 2}vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 100 priority 50 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { chk_nginx } virtual_ipaddress { 192.168.36.100 }}vrrp_instance VI_2 { state MASTER interface eth0 virtual_router_id 200 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { chk_nginx } virtual_ipaddress { 192.168.36.200 }}2) 192.168.36.99 configuration
Vi/etc/keepalived. conf
! Configuration File for keepalivedglobal_defs { notification_email { monitor@3evip.cn #failover@firewall.loc } notification_email_from tianwei7518@163.com smtp_server smtp.163.com smtp_connect_timeout 30 router_id LVS_DEVEL}vrrp_script chk_nginx { script "/etc/keepalived/chk_nginx.sh" interval 2 weight 2}vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 100 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { chk_nginx } virtual_ipaddress { 192.168.36.100 }}vrrp_instance VI_2 { state BACKUP interface eth0 virtual_router_id 200 priority 50 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { chk_nginx } virtual_ipaddress { 192.168.36.200 }}3) The nginx process check script is required for both machines. If no process is detected, restart nginx. If no restart is available, disable keepalived so that the vip can switch to the slave machine.
Vi/etc/keepalived/chk_nginx.sh
#! /Bin/sh # description: # if the startup fails, stop keepalivedstatus =$ (ps-C nginx -- no-heading | wc-l) if ["$ {status}" = "0"]; then/usr/local/nginx/sbin/nginx status2 =$ (ps-C nginx -- no-heading | wc-l) if ["$ {status2}" = "0"]; then service keepalived stop fifiSet the execution permission: chmod + x/etc/keepalived/chk_nginx.sh
III. start and test
Start nginx and keepalived respectively:
/Usr/local/nginx/sbin/nginx
Service keepalived start
Access:
Web1
Http: // 192.168.36.100: 88/
Http: // 192.168.36.200: 88/
Web2
Http: // 192.168.36.100: 99/
Http: // 192.168.36.200: 99/
Normal access.
We can try to disable a keepalived. Disable keepalived on 192.168.36.86
Service keepalived stop
View logs:
Tail-f/var/log/messages
192.168.36.86 log:
Feb 7 00:39:05 slave-d Keepalived[5738]: Stopping Keepalived v1.2.15 (02/07,2015)Feb 7 00:39:05 slave-d Keepalived_vrrp[5741]: VRRP_Instance(VI_2) sending 0 priorityFeb 7 00:39:05 slave-d Keepalived_vrrp[5741]: VRRP_Instance(VI_2) removing protocol VIPs.Feb 7 00:39:05 slave-d Keepalived_healthcheckers[5740]: Netlink reflector reports IP 192.168.36.200 removedFeb 7 00:39:05 slave-d avahi-daemon[1823]: Withdrawing address record for 192.168.36.200 on eth0.
192.168.36.99 log:
Feb 7 00:39:11 slave-c Keepalived_vrrp[25103]: VRRP_Instance(VI_2) Transition to MASTER STATEFeb 7 00:39:12 slave-c Keepalived_vrrp[25103]: VRRP_Instance(VI_2) Entering MASTER STATEFeb 7 00:39:12 slave-c Keepalived_vrrp[25103]: VRRP_Instance(VI_2) setting protocol VIPs.Feb 7 00:39:12 slave-c Keepalived_healthcheckers[25102]: Netlink reflector reports IP 192.168.36.200 addedFeb 7 00:39:12 slave-c avahi-daemon[1832]: Registering new address record for 192.168.36.200 on eth0.IPv4.Feb 7 00:39:12 slave-c Keepalived_vrrp[25103]: VRRP_Instance(VI_2) Sending gratuitous ARPs on eth0 for 192.168.36.200Feb 7 00:39:17 slave-c Keepalived_vrrp[25103]: VRRP_Instance(VI_2) Sending gratuitous ARPs on eth0 for 192.168.36.200
Enable keepalived on 192.168.36.86:
192.168.36.86 log:
Feb 7 00:40:42 slave-d Keepalived[6004]: Starting Keepalived v1.2.15 (02/07,2015)Feb 7 00:40:42 slave-d Keepalived[6005]: Starting Healthcheck child process, pid=6007Feb 7 00:40:42 slave-d Keepalived[6005]: Starting VRRP child process, pid=6008Feb 7 00:40:42 slave-d Keepalived_vrrp[6008]: Netlink reflector reports IP 192.168.36.86 addedFeb 7 00:40:42 slave-d Keepalived_vrrp[6008]: Netlink reflector reports IP fe80::20c:29ff:fe6a:66ff addedFeb 7 00:40:42 slave-d Keepalived_vrrp[6008]: Registering Kernel netlink reflectorFeb 7 00:40:42 slave-d Keepalived_vrrp[6008]: Registering Kernel netlink command channelFeb 7 00:40:42 slave-d Keepalived_vrrp[6008]: Registering gratuitous ARP shared channelFeb 7 00:40:42 slave-d Keepalived_vrrp[6008]: Opening file '/etc/keepalived/keepalived.conf'.Feb 7 00:40:42 slave-d Keepalived_healthcheckers[6007]: Netlink reflector reports IP 192.168.36.86 addedFeb 7 00:40:42 slave-d Keepalived_healthcheckers[6007]: Netlink reflector reports IP fe80::20c:29ff:fe6a:66ff addedFeb 7 00:40:42 slave-d Keepalived_healthcheckers[6007]: Registering Kernel netlink reflectorFeb 7 00:40:42 slave-d Keepalived_healthcheckers[6007]: Registering Kernel netlink command channelFeb 7 00:40:42 slave-d Keepalived_healthcheckers[6007]: Opening file '/etc/keepalived/keepalived.conf'.Feb 7 00:40:42 slave-d Keepalived_vrrp[6008]: Configuration is using : 44182 BytesFeb 7 00:40:42 slave-d Keepalived_vrrp[6008]: Using LinkWatch kernel netlink reflector...Feb 7 00:40:42 slave-d Keepalived_vrrp[6008]: VRRP_Instance(VI_1) Entering BACKUP STATEFeb 7 00:40:42 slave-d Keepalived_vrrp[6008]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]Feb 7 00:40:42 slave-d Keepalived_healthcheckers[6007]: Configuration is using : 7257 BytesFeb 7 00:40:42 slave-d Keepalived_healthcheckers[6007]: Using LinkWatch kernel netlink reflector...Feb 7 00:40:42 slave-d Keepalived_vrrp[6008]: VRRP_Instance(VI_2) Transition to MASTER STATEFeb 7 00:40:42 slave-d Keepalived_vrrp[6008]: VRRP_Instance(VI_2) Received lower prio advert, forcing new electionFeb 7 00:40:43 slave-d Keepalived_vrrp[6008]: VRRP_Instance(VI_2) Entering MASTER STATEFeb 7 00:40:43 slave-d Keepalived_vrrp[6008]: VRRP_Instance(VI_2) setting protocol VIPs.Feb 7 00:40:43 slave-d avahi-daemon[1823]: Registering new address record for 192.168.36.200 on eth0.IPv4.Feb 7 00:40:43 slave-d Keepalived_vrrp[6008]: VRRP_Instance(VI_2) Sending gratuitous ARPs on eth0 for 192.168.36.200Feb 7 00:40:43 slave-d Keepalived_healthcheckers[6007]: Netlink reflector reports IP 192.168.36.200 addedFeb 7 00:40:48 slave-d Keepalived_vrrp[6008]: VRRP_Instance(VI_2) Sending gratuitous ARPs on eth0 for 192.168.36.200
192.168.36.99 log:
Feb 7 00:40:47 slave-c Keepalived_vrrp[25103]: VRRP_Instance(VI_2) Received higher prio advertFeb 7 00:40:47 slave-c Keepalived_vrrp[25103]: VRRP_Instance(VI_2) Entering BACKUP STATEFeb 7 00:40:47 slave-c Keepalived_vrrp[25103]: VRRP_Instance(VI_2) removing protocol VIPs.Feb 7 00:40:47 slave-c Keepalived_healthcheckers[25102]: Netlink reflector reports IP 192.168.36.200 removedFeb 7 00:40:47 slave-c avahi-daemon[1832]: Withdrawing address record for 192.168.36.200 on eth0.
You can still access the service normally after it is disabled.
After nginx is disabled, nginx is started immediately.
References:
CentOS6.5 keepalived
Use NginX + keepalived to achieve high-availability load balancing
Keepalived + Nginx for high availability and dual-master load balancing
Nginx + keepalived implements nginx dual-master high-availability load balancing
The above introduces Keepalived + nginx to achieve dual-master high-availability load balancing, including content, hope to be helpful to friends who are interested in PHP tutorials.