Nginx high availability clustering solutions Nginx + Keepalived

10pcm
https://blog.51cto.com/superpcm/2095395

1. Keepalived high availability software

Keepalived software was originally designed for LVS load balancing software designed to manage and monitor the status of each service node of LVS cluster system, and later joined the VRRP can achieve high availability features. Therefore, keepalived addition to management LVS software, but also can be used as high availability software solutions for other services.

keepalived software is to achieve high-availability functions through VRRP protocol. VRRP is Virtual Router Redundancy Protocol (Virtual Router Redundancy Protocol) acronym, the purpose is to solve VRRP appear single point of failure static routing, it can ensure that when an individual node goes down, the entire network can run uninterrupted. So, keepalived one hand LVS configuration management functions, but also has the following functions for LVS node health check, on the other hand can achieve high availability functions of the system network services.

2. Keepalived high availability failover principle of transfer

Keepalived failover transition between high availability service support is achieved by the VRRP. When keepalived service work, the main Master node will continue to send (multicast mode) heartbeat message to the standby node, used to tell Backup backup node to be alive. When the primary node fails, can not send a heartbeat message, the standby node can not continue thus detecting the arrival of the master node heartbeat. So they call themselves take over the program, to take over the IP resources and services of the master node. When the master node recovery, the standby node will release its own IP resources and services to take over when the primary node fails, revert to the original standby role.

3. Keepalived availability experimental environment Description

As shown below, the two front end Nginx load balancer to distribute the received request from the client. The previous text has been configured Nginx01, Nginx02 is the same configuration. Now done in the two load balancer Nginx high availability configuration, Nginx01 as a master node, Nginx02 as a standby node.

4. Install and enable keepalived

keepalived installation is very simple, direct use yum to install.

 yum install keepalived -y

After installation, start keepalived service, the way to write the script boot keepalived go inside. .

/etc/init.d/keepalived star
echo "/etc/init.d/keepalived start" >>/etc/rc.local

After starting the process there will be three, no problem can be turned off after keepalived software, the next keepalived to modify configuration files.

5. Modify keepalived configuration file and restart the service keepalived

/etc/init.d/keepalived stop    #关闭keepalived服务   
vim /etc/keepalived/keepalived.conf  #用vim打开编辑

The master node configuration file

! Configuration File for keepalived



global_defs {

   notification_email {

     [email protected]

     [email protected]

     [email protected]

   }

   notification_email_from [email protected]

   smtp_server 192.168.200.1

   smtp_connect_timeout 30

   router_id lb01

}



vrrp_instance VI_1 {

    state MASTER

    interface eth1

    virtual_router_id 55

    priority 150

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 123456

    }

    virtual_ipaddress {

        192.168.31.5/24 dev eth1 label eth1:1

    }

}

......

Preparation of the node configuration file

! Configuration File for keepalived



global_defs {

   notification_email {

     [email protected]

     [email protected]

     [email protected]

   }

   notification_email_from [email protected]

   smtp_server 192.168.200.1

   smtp_connect_timeout 30

   router_id lb02

}



vrrp_instance VI_1 {

    state BACKUP

    interface eth1

    virtual_router_id 55

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 123456

    }

    virtual_ipaddress {

        192.168.31.5 dev eth1 label eth1:1

    }

}

......

Note: modify the configuration file is mainly a few places in bold above, below, to explain the meaning of a few parameters:

  • router_id route is identified in a local area network which should be unique;
  • vrrp_instance VI_1 {...} This is an example of VRRP, which defines a standby state keepalived, interfaces, priority, and IP authentication information;
  • VRRP state defines the role of the interface interface definitions used here with my server's network card is eth1, according to the actual fill, virtual_router_id ID identifies a virtual routing, keepalived configure a set of master and slave are set the same, priority is the priority, the higher the number, the higher priority, auth_type is authentication, auth_pass is password authentication
  • virtual_ipaddress {...} define a virtual IP address, you can configure multiple IP addresses, where I is defined as 192.168.31.5, bound network interface eth1, the virtual interface eth1: 1

After modifying the master node is good, save and exit, and then start keepalived, will generate a virtual IP within minutes: 192.168.31.5

Then modify the configuration file backup node, start saving keepalived exit, does not generate a virtual IP, if that is generated configuration file error occurred. Standby node and the master node IP resource contention, this phenomenon is called "split-brain."

Standby switch highly available server 6. Experiment

Keepalived stopped serving the master node to view the backup node will generate VIP: 192.168.31.5

Keepalived start serving the master node, and then view the master node and backup node VIP, the master node should snatch back VIP:

7. Load balancing with Nginx to test

修改windows的hosts文件,把域名指向到VIP上

然后用浏览器打开www.pcm.com的页面,在web01上查看access.log日志记录到的客户端IP地址

可以看到日志记录到的客户端的IP地址是192.168.31.1,反向代理服务器是主服务器192.168.31.3.下面我们停止keepalived服务,看备节点会不会接替主节点的VIP和服务。

可以看到,备节点确实接替了主节点的工作。重新启用主节点,实验的结果就不验证了。

8.编写Nginx Web服务的守护脚本

上面的实验测试有一个问题就是,我们是用Nginx做负载均衡分发请求的数据包的。如果主节点的Keepalived服务正常运行,而Nginx运行异常,那么将会出现Nginx负载均衡服务失灵,无法切换到Nginx负载均衡器02上,后端的Web服务器无法收到请求。所以,我们应该要检测Nginx的服务是否正常运行,如果不是正常运行,应该停掉Keepalived的服务,这样才能自动切换到备节点上。

我们可以通过检测80端口是否开启来判定Nginx的运行情况,2秒钟检测一次,脚本如下

#!/bin/bash
while true
do
if [ $(netstat -tlnp|grep nginx|wc -l) -ne 1 ]
then
	/etc/init.d/keepalived stop
fi
sleep 2
done

实验的结果可以后台执行命令之后然后停止Nginx服务检验

发布了19 篇原创文章 · 获赞 1 · 访问量 2889

Guess you like

Origin blog.csdn.net/weixin_45788094/article/details/104455727