Haproxy High Availability (and use Shell to write startup scripts)

Source: Internet
Author: User
Tags syslog system log haproxy rsyslog


This time using Haproxy only test the web side of high availability and load balancing, the problem is that Haproxy is a single point, so you also need to keepalived haproxy high availability, the next explanation.

Server:

*.*.*.*:haproxy

Two HTTP

What you need to know is:

1. High availability and load balancing;

2. System log Rsyslog;

3. Static and dynamic separation

A: Haproxy Introduction:

Haproxy provides high availability, load balancing, and proxies based on TCP and HTTP applications, supporting virtual hosting, which is a free, fast, and reliable solution. Haproxy is especially useful for Web sites that are heavily loaded, and often require session-hold or seven-tier processing. The haproxy runs on the current hardware and can support tens of thousands of concurrent connections. and its operating mode makes it easy and safe to integrate into your current architecture, while protecting your Web server from being exposed to the web.

Advantages of Haproxy :

1, Haproxy is support virtual host, can work in 4, 7 layer (support multi-network segment);

2, can add some of the shortcomings of Nginx such as the session of the maintenance, cookie guidance and other work;

3, support the URL detection backend server;

4, it is just like the LVS, itself is only a load balancer software, simply from the efficiency of the haproxy more than Nginx has a better load balancing speed, in concurrent processing is better than nginx;

5, Haproxy can load balance MySQL read, the backend of the MySQL node detection and load balancing, but in the back end of the number of MySQL slaves more than 10 units performance than LVS;

6, the Haproxy algorithm is more, achieves 8 kinds;

Installation:


#wget http://fossies.org/linux/misc/haproxy-1.6.9.tar.gz (this need to login to the Web page to view the Haproxy version, only one of the latest version)

#解压

#tar-ZXVF haproxy-1.6.9.tar.gz

#cd haproxy-1.6.9

#安装

#make target=linux3100 arch=x86_64 prefix=/root/haproxy #编译

#make Install Prefix=/root/haproxy

#参数说明

Target=linux3100 #内核版本,

Arch=x86_64 #系统位数

Prefix=/root/haprpxy #/root/haprpxy for HAPRPXY installation path

#touch/root/haproxy/conf/haproxy.cfg (This configuration file needs to be created manually)

Add:

on UNIX class operating systems, Rsyslog is widely used in system logs . Rsyslog log messages can be either recorded in a local file or sent over a network to a server receiving a syslog. The server receiving the Syslog can store the syslog messages of multiple devices uniformly, or parse the contents of them to do the corresponding processing. The common application scenarios are network management tools, security management system, log audit system.


First say the Haproxy system log settings, (note this is centos7 above, the file configuration on CENTOS6 is not here)

2. Turn on Rsyslog record Haproxy log function

Edit "/etc/rsyslog.conf" to open the following configuration entry:

# provides UDP syslog reception$modload Imudp$udpserverrun 514

Add the following content:

# Save Haproxy loglocal0.*/var/log/haproxy.log

3. Modify the "/etc/sysconfig/rsyslog" file as follows:

# Options for rsyslogd# Syslogd options is deprecated since Rsyslog v3.# If you want to use them, switch to compatibility Mode 2 By "-C 2" # See RSYSLOGD (8) For more detailssyslogd_options= "-r-m 0"


Related explanations:
-R: Open the ability to accept foreign log messages, which monitor 514 UDP port;
- x: Turn off automatic resolution of the FQDN information of the other log server, which can avoid the trouble of DNS incomplete;
- m: modifies the internal MARK message write interval for syslog (0 is off), for example, 240 writes "--mark--" information every 240 minutes;
- H: By default, syslog does not send messages received from the far end to other hosts, and with this option, the switch is turned on, all
the information received can be forwarded to the syslog.conf based on the @ host defined in the

4. Configure Haproxy

Edit the Haproxy configuration file and configure it in the global variable, which is the second row;

Log 127.0.0.1 Local0


# # #重启服务service Restart rsyslog## #查看日志记录tailf/var/log/haproxy.log

Down handwriting haproxy startup script:

#!/bin/bash#set -eexport lang= ' ZH_CN. UTF-8 ' source /etc/init.d/functions#path= $PATH:/root/haproxy/sbindir=/root/haproxyname=haproxydaemon= $dir/ sbin/$nameCONFIG = $dir/conf/$name. cfg#pidfile= $dir/conf/$name. pid#aa= ' Ps -ef | grep haproxy  | grep -v grep | awk  ' {print $2} ' test -x  $DAEMON  | |  exit 0start ()  {   $DAEMON  -f  $CONFIG  >/dev/null  a=$?   if [  $a  -eq 0 ]; then    action  " Haproxy service started normally "   /bin/true  fi}stop ()  {  killall haproxy   b=$?  if [  $b  -eq 0 ]; then    action  " Haproxy service off normal "   /bin/true  fi}restart ()  {  [ ! -n $ pidfile ] && start | |  stop  start}case  "$" &nbsP;instart)   start  ;; Stop)   stop  ;; Restart)   restart   ;; *)   echo  "Correct parameters, Start|stop|restart" Esac

When you are down, create a haproxy configuration file:

Vim/root/haproxy/conf/haproxy.cfg

Globallog 127.0.0.1 local0maxconn 65536chroot /root/haproxypidfile /root/haproxy/conf /haproxy.pid#log#user haproxy#group haproxydaemondefaultsmode  http      #默认的模式mode  { tcp|http|health },tcp is Layer 4, HTTP is 7 layer, health will only return oklog globaloption  dontlognull   #不记录健康检查日志信息option  httpclose   #每次请求完毕后主动关闭http通道, Haproxy does not support keep-alive, The implementation of this pattern can only be simulated retries 3     #三次连接失败就认为是服务器不可用, or can be set by the following maxconn 65536timeout  connect 70000timeout client 70000timeout server 70000listen stats       #监控bind  0.0.0.0:1080           #   Port stats uri /admin-status     #  Monitoring uristats auth admin :admin     #用户用: Password mode httpoption httplogfrontend main   bind  *:80  acl&nBSP;WEB HDR (host)  -i www.abc.com   #acl后面是规则名称,-I ignores case, followed by the domain name to be accessed, If you access the domain name www.abc.com, the Web rule is triggered.   ACL IMG HDR (host)  -i img.abc.com   use_backend webserver  if web  #如果上面定义的web规则被触发, which accesses www.abc.com, distributes the request to the webserver scope    use_backend  imgserver if img#   default_backend dynamic  # Default page backend webservermode httpbalance roundrobinserver web1   for response backend if not satisfied 192.168.116.135:80 check inter 2000 rise 2  fall 3server web2   192.168.116.136:80 check inter 2000 rise 2 fall 3#check  inter 2000  is to detect the heartbeat frequency  fall 3 is 3 times failed to think the server is unavailable, weight represents the weight backend imgservermode http# Option httpchk /index.phpbalance roundrobinserver img01 192.168.116.136:80 check  inter 2000 fall 3server img02 192.168.116.136:80 check inter 2000 fall 3#backend  dynamic# balance roundrobin# server test1 10.0.0.152:80 check maxconn 2000# Server  test2 10.0.0.155:80 check maxconn 2000

The results of the implementation are as follows:

When you close the HTTPD service above 135 or 137, another httpd service is requested.


To join the service to the system:

Edit/etc/init.d/haproxy Add:

#chkconfig 2345 20 80

#service Haproxy

Then execute the command outside:

#chkconfig--add Haproxy

#chkconfig--list Haproxy

You can use command execution to start and close:

Haproxy High Availability (and use Shell to write startup scripts)

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.