Keepalived sets up simple and highly available nginx web servers-secrets you don't know

Source: Internet
Author: User
Tags haproxy

Keepalived sets up simple and highly available nginx web servers-secrets you don't know

If the zookeeper balanced parts do not use LVS, then the keepalived configuration is equivalent, you only need to configure vrrp related configurations for the MASTER and SLAVE, and the zookeeper of the backend realServer can be handed over to the zookeeper for balanced configuration, for example, the zookeeper distributed file I used is haproxy. I think it is more efficient than LVS, more resource-saving, simpler configuration, and clearer configuration.

The following describes some of the problems I encountered in the process of configuring keepalived and nginx, the mechanism where keepalived does not have a configuration file to check the hosts. No matter what your configuration file crashes, you can successfully renew keepalived, it will not give you a message for any business operation, just as it does not have a problem, and you will not feel it is common during the process, it is the same as a normal process, so once the configuration file is configured with zookeeper, it will be terrible.

Nginx configuration is okay, so we should repeat zookeeper keepalived.

1. Environment

The nginx server supplemented by test2 and test3 is the main ngin server.

test2 eth2:192.168.46.132192.168.46.132test3 eth2:192.168.46.133192.168.46.133


2. Server GUARD:1

If you use LVS as an internal memory balancer, You need to enable the internal memory program to find the destination of your linux kernel, to bring the -- with-kernel-dir =/usr/src/linux kernel data, you must first install the internal kernel to develop the package: with-kernel-dir =/usr/src/linux kernel, you need to install the internal kernel to develop the package: yum install kernel-devel this is the centos security method. Next, we will make a response to the core source file: ln-s/usr/src/kernels/'uname-R'/usr/src/linux can be started now :. /configure -- prefix =/usr/local/keepalived -- with-kernel-dir =/usr/src/linuxwith-kernel-dir =/usr/src/linux if LVS is not used, you do not need to include the -- with-kernel-dir numeric value. I use nginx, so this numeric value is not specified. With-kernel-dir numeric, I use nginx, so this numeric is not specified. The configure ended. When make was started, the following transaction failed:/usr/include/stdint. h: 41: error: conflicting types for 'int64 _ t' include/stdint. h: 41: error: conflicting types forinclude/stdint. h: 41: error: conflicting types for 'int64 _ t'/usr/src/linux/include/linux/types. h: 126: error: previous declaration of 'int64 _ t' was hereinclude/linux/types. h: 126: error: previous declaration of 'int64 _ t' was here/usr/include/stdint. h: 56: error: con Flicting types for 'uint64 _ t' include/stdint. h: 56: error: conflicting types forinclude/stdint. h: 56: error: conflicting types for 'uint64 _ t'/usr/src/linux/include/linux/types. h: 124: error: previous declaration of 'uint64 _ t' was hereinclude/linux/types. h: 124: error: previous declaration of 'uint64 _ t' was hereIn file encoded from/usr/include/stdlib. h: 438, include/stdlib. h: 438, find keepalived/libipvs-in the source object- 2.6/ip_vs.h file, install it, put # include <linux/types. h> move to # include <sys/types. h>, and make again. make install to complete the installation. 2.6/ip_vs.h file, install it, put # include <linux/types. h> move to the # include2.6/ip_vs.h file and upload it to the # include <linux/types. h> move to # include <sys/types. h>, and make again. make install to complete the installation.


3. Configuration

3.1 configure keepalived for the master server test2

/etc/keepalived/keepalived.confglobal_defs {notification_email {root@localhost}notification_email_from root@localhostsmtp_server 127.0.0.1127.0.0.1smtp_connect_timeout 30router_id LVS_DEVEL}vrrp_script chk_http_port {script "/root/nginx_pid.sh"interval 2weight 2}vrrp_instance VI_1 {state MASTERinterfaceeth2virtual_router_id 51mcast_src_ip 192.168.46.132192.168.46.132priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.46.200/24}track_script {chk_http_port}}

3.2 configure keepalived on the nginx Server

[root@test3 init.d]# vim /etc/keepalived/keepalived.conf!Configuration File forkeepalivedforkeepalivedglobal_defs {notification_email {root@localhost}notification_email_from root@localhostsmtp_server 127.0.0.1127.0.0.1smtp_connect_timeout 30router_id LVS_DEVEL}vrrp_script chk_http_port {script "/root/nginx_pid.sh"interval 2weight 2}vrrp_instance VI_1 {state BACKUPinterfaceeth2virtual_router_id 51mcast_src_ip 192.168.46.133192.168.46.133priority 99advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.46.200/24}track_script {chk_http_port}}~


4. Monitor nginx scripts

Chk_http_port internal content is as follows:
[root@test2 init.d]# vim /root/nginx_pid.sh#!/bin/bashA=`ps -C nginx --no-header |wc -l`if[ $A -eq 0];then[ $A -eq 0if[ $A -eq 0];thenkill -HUP `cat /usr/local/nginx/logs/nginx.pid`sleep 3if[ `ps -C nginx --no-header |wc -l` -eq 0];then[ `ps -C nginx --no-header |wc -l` -eq 0if[ `ps -C nginx --no-header |wc -l` -eq 0];thenkillall keepalivedfifi


5. Notes:

1. The first line! Start indicates a line of zookeeper, And the keepalived configuration file's zookeeper lines are all! Or # Start. 2. vrrp_scriptchk_http_port is used to specify a vrrp zookeeper. When the MASTER node is switched to the MASTER node, the specified program in the dynamic row is automatically written, Note that vrrp_scriptchk_http_port must have at least one space between the end and the end. Otherwise, the operation will not begin, I ended up with this question for 2 hours! All the {characters in the configuration file of keepalived must be separated by at least one space. It is because keepalived does not have any configuration issues, so if you do not know it, it will be a cup ..3. track_script {, which is the script specified by the vrrp_script command of the following line. Note that it must be placed after the virtual_ipaddress configuration, this means that only when the VIP instance is successfully renewed can the specified textbook in track_script be merged. The reason is that my zookeeper balancer uses haproxy, the fixed IP address set in the nginx configuration file is this VIP, And the chk_http_port of my zookeeper is the dynamic nginx configuration file and it is triggered by the following conditions, therefore, if track_script is placed in front of it, a fixed VIP will be generated to activate nginx without taking effect, which of course cannot be successful, let's look at the chk_http_port content. When the nginx progress cannot be found, keepalived will be stopped. As a result, nginx cannot be started, and keepalived will also be stopped when it is tired, the MASTER is taken over by the SLAVE. The configuration on the SLAVE is the same as that on the MASTER. VIP cannot be activated. This problem also caused me to have over four hours .. Root cause, It is recommended that you configure all your tasks on the backend of the VIP manual configuration instead of rolling to the front!4. When the keepalived instance is in progress, chk_http_port will probably fail because the host uses a VIP, the nginx on the slave machine cannot get this IP address, and the slave machine fails to take over the MASTER node. When the slave machine takes over the MASTER node, the VIP will fail, but the chk_http_port will fail, therefore, nginx will not be dynamic. This is the reason why I want to make it happen only when the keepalived script is dynamic, the initial name is vrrp. Therefore, it is best to let the slave machine take over the MASTER machine, roll over the VIP, and then manually activate nginx. no matter whether the MASTER machine is connected to the MASTER machine or the slave machine, all services can be properly renewed and balanced.

This article is from the "Stay Alive" blog and will not be reposted!

Related Article

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.