配置Haproxy

一、安装Haproxy

Haproxy的安装非常简单,从以下网址下载源码文件进行编译安装即可:

http://haproxy.1wt.eu/

然后解压缩源码包,我下载的是1.4.9的版本:

  1. tar -zxvf haproxy-1.49.tar.gz  
  2. cd haproxy-1.4.9
  3. make TARGET=linux26 PREFIX=/haproxy 
  4. make install PREFIX=/haproxy

这样就安装好了。
我的Haproxy的配置文件haproxy.cfg的内容为:

 
  
1 global
2 log 127.0 . 0.1 local3
3 # log 127.0.0.1 local1 notice
4 # log loghost local0 info
5 maxconn 4096
6 # chroot /usr/local/haproxy
7 # chroot /home/haproxy
8 uid 502
9 gid 502
10 daemon
11 nbproc 1
12 pidfile / home / haproxy / logs / haproxy.pid
13 # debug
14 # quiet
15
16 defaults
17 log global
18 mode http
19 option httplog
20 option dontlognull
21 option forwardfor
22 option redispatch
23 log 127.0 . 0.1 local3
24 retries 3
25 maxconn 32000
26 balance roundrobin
27 stats uri / haproxy - stats
28 contimeout 5000
29 clitimeout 50000
30 srvtimeout 50000
31
32 listen web_proxy * : 80
33 appsession JSESSIONID len 52 timeout 3h
34 cookie SRV insert indirect nocache
35 mode http
36 stats enable
37 stats hide - version
38 stats uri / haproxy - stats
39 stats refresh 10s
40 monitor - uri / haproxy_test
41 balance roundrobin
42 option httpclose
43 option forwardfor
44 option httpchk HEAD / index.html HTTP / 1.0
45 # option httpchk GET /ping.jsp
46 # server webA 10.1.29.1:80 cookie A check
48 # server webB 10.1.29.3:80 cookie B check
49 server webC 10.1 . 29.1 : 80 cookie C check
50 listen web_proxy_2 * : 1080
51 balance roundrobin
52 option forwardfor
53 option ssl - hello - chk
54 stats enable
55 stats hide - version
56 stats uri / haproxy - stats
57 stats refresh 10s
58 monitor - uri / haproxy_test
59 option httpchk HEAD / index.html HTTP / 1.0
60 server webD 10.1 . 29.25 : 1080 cookie D check

 

启动 haproxy:

sbin/haproxy -f haproxy.cfg

可以打开 stats 看,http://10.1.29.3/haproxy-stats, 如:

如果修改 haproxy.cfg 配置,想重启 haproxy 用 kill -HUP `cat logs/haproxy.pid` 是不行的。必须使用 -sf 或 -st 参数,如:

sbin/haproxy -f haproxy.cfg -st `cat /home/haproxy/logs/haproxy.pid`

另外网上的一些设置log的方法基本都不行。

二、安装heartbeat

heartbeat依赖于一系列软件包,安装heartbeat前要先安装这些软件包:

  • A C compiler (typically gcc) and associated C development libraries;
  • the flex scanner generator and the bison parser compiler;
  • net-snmp development headers, to enable SNMP related functionality;
  • OpenIPMI development headers, to enable IPMI related functionality;
  • Python (just the language interpreter, not library headers)
  • the cluster-glue development headers.

其中cluster-glue是从源码编译的,下载地址为:http://linux-ha.org/wiki/Downloads

解压缩后执行以下命令进行编译和安装:

$ ./autogen.sh

$ ./configure

$  make

$  make install

在编译时会遇到一个警告式的错误:函数声明不是一个原型。定位后会发现version()函数的参数为空,自己加一个void参数,再编译就好了。

 

安装好cluster-glue后,编译heartbeat,下载地址同上。编译方法为:

$ ./bootstrap

$ ./ConfigureMe configure

$  make

$  make install

这两个过程写doc文件夹中的文件的时间都很长,不知道在写什么。

编译heartbeat时,make最后阶段写doc时报错,但我直接make install了,出现了两个可以忽略的错误(glue中也出现了一个这样的错误)。其原因是要创建haclient用户组和haserver用户,再进行编译安装,一切正常。

安装好后进行启动,问题又来了,就是shellfunc文件没有创建,我对比了ubuntu上直接安装的heartbeat,此文件是创建好了的。cp过来修改了一下启动还是有问题,索性决定直接用rpm包安装。搜了一下,找到了一个地址:

http://www.clusterlabs.org/rpm/epel-5/x86_64/

两台机器下载安装都很正常。然后编辑/etc/sysctl.conf,加入如下选项:

#Allow Haproxy shared IP

net.ipv4.ip_nonlocal_bind = 1

运行sysctl -p

接下来就是配置各个文件了,切换到/etc/ha.d目录。首先在lb1和lb2(lb即load blance的缩写)上都创建authkeys文件:

 

 
  
1 auth 3
2   3 md5 password

其中password需要自己设置,是两台机器之间进行认证的密码。创建完成后利用chmod 600 authkeys命令修改文件访问权限,保证只有root用户可以读写。

 

然后在lb1和lb2上创建haresources文件:

 

 
  
1 lm 10.1 . 29.9

其中lm是运行uname -n命令得到的主机名,这个主机名可以在/etc/sysconfig/network中修改,修改后需要重启。而主机名后面的ip地址为虚拟ip,是lb1和lb2共享的ip地址。

 

最后在lb1和lb2上创建ha.cf文件:

 

 
  
1 #
2 # keepalive: how many seconds between heartbeats
3 #
4   keepalive 2
5   #
6 # deadtime: seconds-to-declare-host-dead
7 #
8   deadtime 10
9   #
10 # What UDP port to use for udp or ppp-udp communication?
11 #
12   udpport 694
13 bcast br0
14 mcast br0 225.0 . 0.1 694 1 0
15 ucast br0 10.1 . 29.3
16   # What interfaces to heartbeat over?
17 udp eth0
18 #
19 # Facility to use for syslog()/logger (alternative to log/debugfile)
20 #
21 logfacility local3
22 #
23 # Tell what machines are in the cluster
24 # node nodename ... -- must match uname -n
25 node cc
26 node lm

其中我们要修改的就是接口和ip以及主机名。因为我的环境用的是桥接,所以设置是br0,ip为自己的ip(某些文章介绍是对方lb的ip),node后面的名称为uname -a得到的host name。

这样两台主备haproxy就配置好了。首先启动两台机器上的heartbeat:

/etc/init.d/heartbeat start

然后在lb1运行ip addr sh br0可以看到的显示内容如下:

 

 
  
[root @ cc ha.d] # ip addr sh br0
5 : br0: <BROADCAST,MULTICAST,UP,LOWER_UP > mtu 1500 qdisc noqueue
link
/ ether d8:5d:4c: 74 :ee:e8 brd ff:ff:ff:ff:ff:ff
inet
10.1 . 29.3 / 16 brd 10.1 . 255.255 scope global br0
inet6 fe80
:: da5d:4cff:fe74:eee8 / 64 scope link
valid_lft forever preferred_lft forever

同样在lb2上运行ip addr sh br0:

 

 
  
[root @ lm haproxy] # ip addr sh br0
4 : br0: <BROADCAST,MULTICAST,UP,LOWER_UP > mtu 1500 qdisc noqueue
link
/ ether e0:cb:4e:b8: 19 : 16 brd ff:ff:ff:ff:ff:ff
inet
10.1 . 29.1 / 16 brd 10.1 . 255.255 scope global br0
inet
10.1 . 29.9 / 16 brd 10.1 . 255.255 scope global secondary br0: 0
inet6 fe80
:: e2cb:4eff:feb8: 1916 / 64 scope link
valid_lft forever preferred_lft forever

这说明主机lb2进行代理工作,而备机lb1处于监听状态。然后拔掉主机lb1的网线,此时备机接替主机的工作成为服在均衡代理服务器,运行以上命令:

 

 
  
[root @ cc ha.d] # ip addr sh br0
5 : br0: <BROADCAST,MULTICAST,UP,LOWER_UP > mtu 1500 qdisc noqueue
link
/ ether d8:5d:4c: 74 :ee:e8 brd ff:ff:ff:ff:ff:ff
inet
10.1 . 29.3 / 16 brd 10.1 . 255.255 scope global br0
inet
10.1 . 29.9 / 16 brd 10.1 . 255.255 scope global secondary br0: 0
inet6 fe80
:: da5d:4cff:fe74:eee8 / 64 scope link
valid_lft forever preferred_lft forever

 

 

 

这说明备机lb2已经接替了主机lb1的工作。我们再将主机lb1的网线插上,运行以上命令:

 

 

 
  
[root @ lm haproxy] # ip addr sh br0
4 : br0: <BROADCAST,MULTICAST,UP,LOWER_UP > mtu 1500 qdisc noqueue
link
/ ether e0:cb:4e:b8: 19 : 16 brd ff:ff:ff:ff:ff:ff
inet
10.1 . 29.1 / 16 brd 10.1 . 255.255 scope global br0
inet
10.1 . 29.9 / 16 brd 10.1 . 255.255 scope global secondary br0: 0
inet6 fe80
:: e2cb:4eff:feb8: 1916 / 64 scope link
valid_lft forever preferred_lft forever
 
  
[root @ cc ha.d] # ip addr sh br0
5 : br0: <BROADCAST,MULTICAST,UP,LOWER_UP > mtu 1500 qdisc noqueue
link
/ ether d8:5d:4c: 74 :ee:e8 brd ff:ff:ff:ff:ff:ff
inet
10.1 . 29.3 / 16 brd 10.1 . 255.255 scope global br0
inet6 fe80
:: da5d:4cff:fe74:eee8 / 64 scope link
valid_lft forever preferred_lft forever

说明主机和备机已经各自恢复。以上说明heartbeat运行正常。

 

然后在两台机器上运行haproxy,这样带有ha功能的haproxy就配置好了。

 

http://www.oschina.net/bbs/thread/4090

转载于:https://www.cnblogs.com/chinacloud/archive/2010/11/17/1879931.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值