High-availability Web architecture Keepalived + Haproxy

Source: Internet
Author: User
Tags install openssl haproxy

This article is suitable for beginners, because it was set up step by step and has never been used before. Problems Encountered during this period are displayed.

I. installation environment:

Because the system I installed is CentOS6.3 basic, many dependency packages are not installed. Therefore, some errors may occur during the installation process. You need to install the dependency package, to help you better understand the solutions for different errors, I will list the solutions for each error)

HA01 and 02 serve as Service hosts that provide keepalived + haproxy, and WEB01 and 02 serve as backend App servers.

HA01: 192.168.10.100 MASTER

HA02: 192.168.10.101 BACKUP

VIP: 192.168.10.110

WEB01: 192.168.10.200

WEB02: 192.168.10.201


Ii. Compile and install keepalived

[Root @ HA01 ~] # Tar-zxvf/root/wemux/keepalived-1.2.7

[Root @ HA01 ~] # Cd/root/wemux/keepalived-1.2.7

[Root @ HA01 keepalived-1.2.7] #./configure -- sysconfdir =/etc

Error:

checking for gcc... nochecking for cc... nochecking for cl.exe... No

Solution: [root@HA01keepalived-1.2.7] # yum-y install gcc

[Root @ HA01 keepalived-1.2.7] #./configure -- sysconfdir =/etc

Error:

configure: error:!!!OpenSSL is not properly installed on your system. !!!!!!Can not include OpenSSL headers files.!!!

Solution: [root@HA01keepalived-1.2.7] # yum-y install openssl-devel

[Root @ HA01 keepalived-1.2.7] #./configure -- sysconfdir =/etc

Error:

configure: error: Popt libraries isrequired

Solution: [root@HA01keepalived-1.2.7] # yum-y install popt-devel

[Root @ HA01 keepalived-1.2.7] #./configure -- sysconfdir =/etc

The keepalived installation is complete.

Compilation is required below

Install the make dependency package first

[Root @ HA01 keepalived-1.2.7] # yum-y install make

[Root @ HA01 keepalived-1.2.7] # make & make install // compile

After compilation, You need to modify the configuration file.

[Root@HA01keepalived-1.2.7] # vi/etc/keepalived. conf

! Configuration File for keepalived # global_defs {# icationication_email {# gaoming@123.com #}# icationication_email_from gaoming@123.com # smtp_server 192.168.200.1 # route 30 # router_id LVS_DEVEL #} vrrp_instance VI_1 {state MASTER # Backup Server hamaster change to BACKUP interface eth0 virtual_router_id 51 priority 100 # change 100 to 99 advert_int 1 authentication {auth_type PASS auth_pass 1111} virtual_ipaddress {192.168.10.110/24 dev eth0 scope global} real_server 192.168.10.100 80 {# change the IP address of the backup server to 192.168.10.101TCP _ CHECK {}} on HA02 {}}

After modification, start keepalived

[Root @ HA01 keepalived-1.2.7] #/etc/init. d/keepalived start

Error: Starting keepalived:/bin/bash: keepalived: command not found

Solution: cp the keepalived file to/usr/sbin/

[Root @ HA01 keepalived-1.2.7] # cp/usr/local/sbin/keepalived/usr/sbin/

[Root @ HA01 keepalived-1.2.7] #/etc/init. d/keepalived start

Test

[Root @ HA01 ~] # Ip add

2: eth0: <BROADCAST, MULTICAST, UP, LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen1000

Link/ether 00: 0c: 29: 22: a5: d0 brd ff: ff

Inet 192.168.10.100/24 brd 192.168.10.255 scope global eth0

Inet 192.168.10.110/24 scope global secondary eth0

Inet6 fe80: 20c: 29ff: fe22: a5d0/64 scope link

Valid_lft forever preferred_lft forever

[Root @ HA02 ~] # Ip add

2: eth0: <BROADCAST, MULTICAST, UP, LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

Link/ether 00: 0c: 29: 03: a8: 15 brd ff: ff

Inet 192.168.10.101/24 brd 192.168.10.255 scope global eth0

Inet6 fe80: 20c: 29ff: fe03: a815/64 scope link

Valid_lft forever preferred_lft forever

Stop the keepalived on the master and take over the keepalived service on the slave.

[Root @ HA02 ~] # Ip add

2: eth0: <BROADCAST, MULTICAST, UP, LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen1000

Link/ether 00: 0c: 29: 03: a8: 15 brd ff: ff

Inet 192.168.10.101/24 brd 192.168.10.255 scope global eth0

Inet 192.168.10.110/24 scope global secondary eth0

Inet6 fe80: 20c: 29ff: fe03: a815/64 scope link

Valid_lft forever preferred_lft forever


3. Compile and install Haproxy

[Root @ HA01 haproxy-1.4.22] # make TARGET = linux26 PREFIX =/usr/local/haproxy

[Root @ HA01 haproxy-1.4.22] # make install PREFIX =/usr/local/haproxy

1. cp Template File

[Root @ HA01 haproxy-1.4.22] # cp-ar examples/usr/local/haproxy/

[Root @ HA01 haproxy-1.4.22] # rsync-arvz/usr/local/haproxy/share/man/usr/share/

[Root @ HA01 haproxy-1.4.22] # cp-ar tests // usr/local/haproxy/

[Root @ HA01 haproxy-1.4.22] # cp doc/configuration.txt/usr/local/haproxy/

[Root @ HA01 haproxy-1.4.22] # rsync-avz examples/errorfiles // usr/local/haproxy/

[Root @ HA01 haproxy-1.4.22] # mkdir/usr/local/haproxy/etc

[Root @ HA01 haproxy-1.4.22] # cp examples/haproxy. cfg/usr/local/haproxy/etc/

[Root @ HA01 haproxy-1.4.22] # cp examples/haproxy. init/etc/init. d/haproxy

2. Edit the configuration file

[Root @ HA01selinux] # vi/usr/local/haproxy/etc/haproxy. cfg

Global log quota local2 # log quota local1 notice # maxconn 4096 user haproxy group haproxy quota log global mode http option tcplog option dontlognull retries 3 option redispatch maxconn 20000 contimeout 3600 s clitimeout 3600 s srvtimeout 3600 slisten app: 8083 # port mode http option forwardfor option httpclose option httpchk GET/index provided to the front end. jsp balance roundrobin # balance sourceserver Server1 192.168.10.200: 8082 check inter 30000 rise 1 fall 2 # backend App Server IP & portserver Server2 192.168.10.201: 8082 check inter 30000 rise 1 fall 2 # backend App Server IP & portlisten http-in # haproxy monitoring configuration bind: 8080 # listening port mode http stats refresh 5S stats auth admin: password # login account password stats uri/monitor # url address

3. Configure the Init script

[Root @ HA01 haproxy-1.4.22] # cat/etc/init. d/haproxy

#!/bin/sh## haproxy## chkconfig:   - 85 15# description:  HAProxy is a free, very fast and reliable solution \#               offering high availability, load balancing, and \#               proxying for TCP and  HTTP-based applications# processname: haproxy# config:      /etc/haproxy/haproxy.cfg# pidfile:     /var/run/haproxy.pid# Source function library.. /etc/rc.d/init.d/functions# Source networking configuration.. /etc/sysconfig/network# Check that networking is up.[ "$NETWORKING" = "no" ] && exit 0exec="/usr/sbin/haproxy"prog=$(basename $exec)[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$proglockfile=/var/lock/subsys/haproxycheck() {    $exec -c -V -f /etc/$prog/$prog.cfg}start() {    $exec -c -q -f /etc/$prog/$prog.cfg    if [ $? -ne 0 ]; then        echo "Errors in configuration file, check with $prog check."        return 1    fi    echo -n $"Starting $prog: "    # start it up here, usually something like "daemon $exec"    daemon $exec -D -f /etc/$prog/$prog.cfg -p /var/run/$prog.pid    retval=$?    echo    [ $retval -eq 0 ] && touch $lockfile    return $retval}stop() {    echo -n $"Stopping $prog: "    # stop it here, often "killproc $prog"killproc $progretval=$?    echo    [ $retval -eq 0 ] && rm -f $lockfile    return $retval}restart() {    $exec -c -q -f /etc/$prog/$prog.cfg    if [ $? -ne 0 ]; then        echo "Errors in configuration file, check with $prog check."        return 1    fi    stop    start}reload() {    $exec -c -q -f /etc/$prog/$prog.cfg    if [ $? -ne 0 ]; then        echo "Errors in configuration file, check with $prog check."        return 1    fi    echo -n $"Reloading $prog: "    $exec -D -f /etc/$prog/$prog.cfg -p /var/run/$prog.pid -sf $(cat /var/run/$prog.pid)    retval=$?    echo    return $retval}force_reload() {    restart}fdr_status() {    status $prog}case "$1" in    start|stop|restart|reload)        $1        ;;    force-reload)        force_reload        ;;    check)        check        ;;    status)        fdr_status        ;;    condrestart|try-restart)        [ ! -f $lockfile ] || restart        ;;    *)        echo $"Usage: $0 {start|stop|status|restart|try-restart|reload|force-reload}"        exit 2esac

[Root @ HA01 haproxy-1.4.22] # chmod a + x/etc/init. d/haproxy

[Root @ HA01 haproxy-1.4.22] # chkconfig -- add haproxy

4. Start the service

[Root @ HA01 haproxy-1.4.22] # service haproxy start

Iv. Test

Use the VIP test to enter the haproxy monitoring interface. This is the current architecture of our company)

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/01354IA9-0.png "title =" haproxy.png "/>

This article is from the wemux blog, please be sure to keep this source http://wemux.blog.51cto.com/2848943/1204717

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.