Keepalived + haproxy + nginx

Source: Internet
Author: User
Tags syslog haproxy

Keepalived + haproxy + nginx

650) This. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "Title =" haproxy keepliaved ningx "border =" 0 "alt =" haproxy keepliaved ningx "src =" http://img1.51cto.com/attachment/201409/19/1384120_1411144958J6p4.jpg "888" Height = "596"/>

Vm1-keepalived + haproxy
Eth0 172.16.3.2/16 up
Eth1 192.168.1.2/24 up
Vm2-keepalived + haproxy
Eth0 172.16.3.3/16 up
Eth1 192.168.1.3/24 up
Vm3-web:
Eth0 192.168.1.1/24 up
Vm4-web:
Ech0 192.168.1.10/24 up
Vrrp: 172.16.3.88/16

Test Machine:

Vm1-keepalived + haproxy Configuration
# Ifconfig eth0 172.16.3.2/16 up
# Ifconfig eth1 192.168.1.2/24 up
# Yum install keepalived haproxy-y
Haproxy configuration file:
# Vim/etc/haproxy. cfg
#---------------------------------------------------------------------
# Example configuration for a possible web application. See
# Full configuration options online.
#
# Http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
Global
# To have these messages end up in/var/log/haproxy. log you will
# Need:
#
#1) Configure syslog To accept network log events. This is done
# By adding the '-R' option to the syslogd_options in
#/Etc/sysconfig/syslog
#
#2) Configure local2 events to go to the/var/log/haproxy. Log
# File. A line like the following can be added
#/Etc/sysconfig/syslog
#
# Local2. */var/log/haproxy. Log
#
Log 127.0.0.1 local2

Chroot/var/lib/haproxy
Pidfile/var/run/haproxy. PID
Maxconn 4000
User haproxy
Group haproxy
Daemon

# Turn on stats UNIX socket
Stats socket/var/lib/haproxy/stats

#---------------------------------------------------------------------
# Common defaults that all the 'listen' and 'backend' sections will
# Use if not designated in their block
#---------------------------------------------------------------------
Ults
Mode HTTP
Log global
Option httplog
Option dontlognull
Option http-server-close
Option forwardfor partition t 127.0.0.0/8
Option redispatch
Retries 3
Timeout http-request 10 s
Timeout queue 1 m
Timeout connect 10 s
Timeout client 1 m
Timeout Server 1 m
Timeout http-keep-alive 10 s
Timeout check 10 s
Maxconn 3000

Frontend main
BIND: 80
BIND: 8088
# ACL clear dst_port 8088
# ACL login path_beg/login
# Redirect location http://www.baidu.com if login
# Redirect prefix/if clear
# Reqadd X-Proto: \ SSL if clear
# Rspadd X-via: \ haproxy if clear
# Option forwardfor partition t 127.0.0.0/8
# ACL url_static path_beg-I/images/stylesheets/vedios/JavaScript
# ACL url_static path_end-I. jpg. html. CSS. js. PNG. gif
# Use_backend static if url_static
Default_backend webservers

# Backend static
# Balance roundrobin
# Server S1 192.168.1.10: 80 check port 80
# Server B1 127.0.01: 8080 backup check port 8080
Backend webservers
Balance roundrobin
# Cookie ninghongliang insert nocache
Server S2 192.168.1.1: 80 check port 80
Server S1 192.168.1.10: 80 check port 80
Server B1 127.0.01: 8080 backup check port 8080
Stats enable
Stats hide-version
Stats uri/haha? Stats
Stats scope.
Stats realm haporx \ boy
Stats auth admin1: Admin
Stats admin if true
Keepalived configuration file
! Configuration file for keepalived

Global_defs {
Notification_email {
[Email protected]
}
Notification_email_from [email protected]
Smtp_server 127.0.0.1
Smtp_connect_timeout 30
}
Vrrp_script chk_sched_down {
Script "[-E/etc/keepalived/down] & Exit 1 | exit 0"
Interval 2
Weight-50
Fall 2
Rise 1
}
Vrrp_script chk_haproxy {
Script "/etc/keepalived/check_haproxy.sh"
Interval 2
Weight-50
Fall 2
Rise 1
}

Vrrp_instance vi_1 {
State master
Interface eth0
Virtual_router_id 77
Priority100
Advert_int 1
Authentication {
Auth_type pass
Auth_pass Ning
}
Virtual_ipaddress {
172.16.3.88
}
Track_script {
Chk_sched_down
Chk_haproxy
}
}
Vm2-keepalived + haproxy Configuration
# Ifconfig eth0 172.16.3.3/16 up
# Ifconfig eth1 192.168.1.3/24 up
# Yum install keepalived haproxy-y
Haproxy configuration file: (the configuration here is the same as the above, not described here)
Keepalived configuration file
! Configuration file for keepalived

Global_defs {
Notification_email {
[Email protected]
}
Notification_email_from [email protected]
Smtp_server 127.0.0.1
Smtp_connect_timeout 30
}
Vrrp_script chk_sched_down {
Script "[-E/etc/keepalived/down] & Exit 1 | exit 0"
Interval 2
Weight-50
Fall 2
Rise 1
}
Vrrp_script chk_haproxy {
Script "/etc/keepalived/check_haproxy.sh"
Interval 2
Weight-50
Fall 2
Rise 1
}

Vrrp_instance vi_1 {
State backup
Interface eth0
Virtual_router_id 77
Priority 99
Advert_int 1
Authentication {
Auth_type pass
Auth_pass Ning
}
Virtual_ipaddress {
172.16.3.88
}
Track_script {
Chk_sched_down
Chk_haproxy
}
}
The detection script haprxoy script chk_haproxy is used here (if haproxy cannot be launched after 3 seconds, disable keepalived)
#! /Bin/bash
A = 'ps-C haproxy -- no-header | WC-l'
If [$ A-EQ 0]; then
/Usr/sbin/haproxy-F/etc/haproxy. cfg
Sleep 3
If ['ps-C haproxy -- no-header | WC-l'-EQ 0]; then
/Etc/init. d/keepalived stop
Fi
Fi

Vm3-web: Configuration
# Ifconfig eth0 192.168.1.1/24 up
# Yum install nginx-y
# Echo "192.168.1.1">/usr/share/nginx/html/index.html
Vm4-web: Configuration
# Ifconfig eth0 192.168.1.10/24 up
# Yum install nginx-y
# Echo "192.168.1.10">/usr/share/nginx/html/index.html

Test:
(1) run both the master and slave nodes # ip addr show
MASTER: eth0: <broadcast, multicast, up, lower_up> MTU 1500 qdisc pfifo_fast state up qlen 1000
Link/ether 00: 0C: 29: D7: F7: 9C brd ff: FF
Inet 172.16.3.2/16 BRD 172.16.255.255 scope global eth0
Inet 172.16.3.88/32 scope global eth0
Valid_lft forever preferred_lft forever
From: eth0: <broadcast, multicast, up, lower_up> MTU 1500 qdisc pfifo_fast state up qlen 1000
Link/ether 00: 0C: 29: 0b: 35: 6a brd ff: FF
Inet 172.16.3.3/16 BRD 172.16.255.255 scope global eth0
Inet6 fe80: 20c: 29ff: fe0b: 356a/64 scope Link
Valid_lft forever preferred_lft forever
(2) Stop haproxy on the master. keepalived will automatically start it again after 2 seconds.
(3) Stop the master keepalived and the slave server takes over the service immediately.
Eth0: <broadcast, multicast, up, lower_up> MTU 1500 qdisc pfifo_fast state up qlen 1000
Link/ether 00: 0C: 29: 0b: 35: 6a brd ff: FF
Inet 172.16.3.3/16 BRD 172.16.255.255 scope global eth0
Inet 172.16.3.88/32 scope global eth0
Valid_lft forever preferred_lft forever
(4) Local testing: (because the above is just a simple definition of rotation, there is no definition of dynamic/static separation)
Http: // 172.16.3.88
Images and truth

650) This. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "Title =" haproxy keepalived test Fig 1 "border =" 0 "alt =" haproxy keepalived test Fig 1 "src =" http://img1.51cto.com/attachment/201409/19/1384120_1411144958j6mC.jpg "499" Height = "202"/>

650) This. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "Title =" haproxy keepalived test Fig 2 "border =" 0 "alt =" haproxy keepalived test Fig 2 "src =" http://img1.51cto.com/attachment/201409/19/1384120_1411144958cVNV.jpg "441" Height = "213"/>

Keepalived + haproxy + nginx

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.