Linux环境下利用HAProxy实现负载均衡

nginx负载均衡 :Linux环境下搭建Nginx实现动静分离的负载均衡集群_pekeka的博客-CSDN博客

HAProxy和nginx的区别 相同点:在功能上,HAProxy通过反向代理方式实现 WEB 均衡负载。 不同点:HAProxy并不是 web 服务器。以上提到所有带反向代理和负载均衡的产品,都清一色是 WEB 服务器。简单说,就是他们能处理解析页面的。而Haproxy 仅仅是一款的用于负载均衡的应用代理,其自身并不能提供web服务。其支持从4层至7层的网络交换,即覆盖所有的TCP协议。就是说,Haproxy 甚至还支持 Mysql的均衡负载。

ip主机名角色
192.168.88.67server67HAProxy
192.168.88.69server69apache服务器
192.168.88.70server70apache服务器

搭建HAProxy

[root@server67 conf]# rz
[root@server67 conf]# ls
haproxy-1.7.9.tar.gz
[root@server67 conf]# tar xvf haproxy-1.7.9.tar.gz
[root@server67 conf]# cd haproxy-1.7.9
[root@server67 haproxy-1.7.9]# uname -r
3.10.0-514.el7.x86_64
[root@server67 haproxy-1.7.9]# make TARGET=linux2628 PREFIX=/usr/local/haproxy  #指定操作系统内核类型和安装的路径
[root@server67 haproxy-1.7.9]# make install PREFIX=/usr/local/haproxy #指定Makefile配置文件中PREFIX变量的值
[root@server67 haproxy-1.7.9]# mkdir /usr/local/haproxy/etc    
[root@server67 haproxy-1.7.9]# vim /usr/local/haproxy/etc/haproxy.cfg  ##手动创建配置文件
global
log 127.0.0.1  local0
#log 127.0.0.1  local1 notice
#log loghost    local0 info
maxconn 4096
chroot /usr/local/haproxy
uid 99                          
gid 99                         
daemon                       
nbproc 1                  
pidfile /usr/local/haproxy/run/haproxy.pid 
​
defaults
log    global
log    127.0.0.1      local3       
mode    http 
option  httplog    
option  httpclose
option  dontlognull
option  forwardfor 
option  redispatch 
retries 2 
maxconn 2000
balance roundrobin
stats  uri    /haproxy-stats
timeout connect      5000
timeout client       50000
timeout server      50000
mode    http
option  httpchk GET /index.html
frontend http
bind 0.0.0.0:80
default_backend http_back
 
backend http_back
server  s1 192.168.88.69:80  weight 3 check
server  s2 192.168.88.70:80  weight 3 check
​
[root@server67 haproxy-1.7.9]# id nobody
uid=99(nobody) gid=99(nobody) 组=99(nobody)
[root@server67 haproxy-1.7.9]# cp ./examples/haproxy.init /etc/init.d/haproxy  #haproxy启动脚本
[root@server67 haproxy-1.7.9]#  chmod 755 /etc/init.d/haproxy
[root@server67 haproxy-1.7.9]# > /etc/init.d//haproxy
[root@server67 haproxy-1.7.9]# cat /etc/init.d/haproxy
#!/bin/sh
chkconfig: - 85 15
descrIPtion: HA-Proxy server
processname: haproxy
config: /usr/local/haproxy/etc/haproxy.cfg
pidfile: /usr/local/haproxy/run/haproxy.pid
Source function library.
if [ -f /etc/init.d/functions ]; then
  . /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ] ; then
  . /etc/rc.d/init.d/functions
else
  exit 0
fi
Source networking configuration.
. /etc/sysconfig/network
​
Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
​
This is our service name
BASENAME=haproxy
​
BIN=/usr/sbin/haproxy
​
CFG=/usr/local/haproxy/etc/haproxy.cfg
[ -f $CFG ] || exit 1
​
PIDFILE=/usr/local/haproxy/run/haproxy.pid
LOCKFILE=/usr/local/haproxy/run/haproxy
​
RETVAL=0
​
start() {
  quiet_check
  if [ $? -ne 0 ]; then
    echo "Errors found in configuration file, check it with '$BASENAME check'."
    return 1
  fi
​
echo -n "Starting $BASENAME: "
  daemon $BIN -D -f $CFG -p $PIDFILE
  RETVAL=$?
  echo
  [ $RETVAL -eq 0 ] && touch $LOCKFILE
  return $RETVAL
}
​
stop() {
  echo -n "Shutting down $BASENAME: "
  killproc $BASENAME -USR1
  RETVAL=$?
  echo
  [ $RETVAL -eq 0 ] && rm -f $LOCKFILE
  [ $RETVAL -eq 0 ] && rm -f $PIDFILE
  return $RETVAL
}
​
restart() {
  quiet_check
  if [ $? -ne 0 ]; then
    echo "Errors found in configuration file, check it with '$BASENAME check'."
    return 1
  fi
  stop
  start
}
​
reload() {
  if ! [ -s $PIDFILE ]; then
    return 0
  fi
​
quiet_check
  if [ $? -ne 0 ]; then
    echo "Errors found in configuration file, check it with '$BASENAME check'."
    return 1
  fi
  $BIN -D -f $CFG -p $PIDFILE -sf $(cat $PIDFILE)
}
​
check() {
  $BIN -c -q -V -f $CFG
}
​
quiet_check() {
  $BIN -c -q -f $CFG
}
​
rhstatus() {
  status $BASENAME
}
​
condrestart() {
  [ -e $LOCKFILE ] && restart || :
}
​
See how we were called.
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    restart
    ;;
  reload)
    reload
    ;;
  condrestart)
    condrestart
    ;;
  status)
    rhstatus
    ;;
  check)
    check
    ;;
  *)
    echo $"Usage: $BASENAME {start|stop|restart|reload|condrestart|status|check}"
    exit 1
esac
exit $?
[root@server67 haproxy-1.7.9]# diff /etc/rc.d/init.d/haproxy /etc/init.d/haproxy
[root@server67 haproxy-1.7.9]# mkdir -p /usr/local/haproxy/run
[root@server67 haproxy-1.7.9]# vim /etc/rsyslog.conf
15 $ModLoad imudp            #取消注释
16 $UDPServerRun 514          #取消注释
73 local7.*          /var/log/boot.log       #添加以下两行
local3.*          /var/log/haproxy.log
local0.*          /var/log/haproxy.log
[root@server67 ~]# systemctl restart rsyslog
[root@server67 ~]# haproxy -f /usr/local/haproxy/etc/haproxy.cfg
[root@server67 haproxy-1.7.9]# ps -aux | grep haproxy
nobody    44765  0.0  0.0  12280   828 ?        Ss   10:18   0:00 haproxy -f /usr/local/haproxy/etc/haproxy.cfg
root      44767  0.0  0.0 112828   972 pts/1    R+   10:18   0:00 grep --color=auto haproxy

配置后端服务器: server69

[root@server69 yum.repos.d]# yum install -y httpd php
[root@server69 yum.repos.d]# echo 192.168.88.69 > /var/www/html/index.html
[root@server69 html]# cat index.php 
192.168.88.69-php
<?php
phpinfo();
?>
[root@server69 html]# ll
总用量 756
-rw-r--r-- 1 root root     14 11月 29 18:58 index.html
-rw-r--r-- 1 root root     38 11月 29 18:58 index.php
-rw-r--r-- 1 root root 763002 11月 29 18:59 test.png
[root@server67 haproxy-1.7.9]# netstat -pantul |grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      44765/haproxy       
tcp        0      0 192.168.88.67:80        192.168.88.1:53559      TIME_WAIT   -                   
tcp        0      0 192.168.88.67:80        192.168.88.1:57992      TIME_WAIT   -                   
tcp        0      0 192.168.88.67:80        192.168.88.1:59632      TIME_WAIT   -                   
tcp        0      0 192.168.88.67:80        192.168.88.1:65390      TIME_WAIT   -                   
tcp        0      0 192.168.88.67:80        192.168.88.1:64955      TIME_WAIT   -                   
tcp        0      0 192.168.88.67:80        192.168.88.1:63926      TIME_WAIT   -                   
tcp        0      0 192.168.88.67:80        192.168.88.1:56181      TIME_WAIT   -                   
tcp        0      0 192.168.88.67:80        192.168.88.1:58190      TIME_WAIT   -                

配置后端服务器: server70

[root@server70 html]# yum install -y httpd php
[root@server70 html]# echo 192.168.88.70 > /var/www/html/index.html
[root@server70 html]# cat >> /var/www/html/test.php <<eof
192.168.88.70-php
<?php
phpinfo();
?>
eof
[root@server70 html]# ll
总用量 36
-rw-r--r-- 1 root root    14 11月 29 19:17 index.html
-rw-r--r-- 1 root root    37 11月 29 19:17 index.php
-rw-r--r-- 1 root root 28345 11月 29 19:00 test.png

实现

访问:http://192.168.88.67/haproxy-stats

在浏览器访问代理服务器:http://192.168.88.67/index.html

在浏览器访问代理服务器:http://192.168.88.67/index.php

在浏览器访问代理服务器:http://192.168.88.67/test.png

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值