1 )源代码安装haproxy
[root@mysql-slave src]# tar xf haproxy-1.6.4.tar.gz
[root@mysql-slave src]# cd haproxy-1.6.4
[root@mysql-slave haproxy-1.6.4]# make TARGET=linux26 PREFIX=/usr/local/haproxy
[root@mysql-slave haproxy-1.6.4]# echo $?
0
[root@mysql-slave haproxy-1.6.4]# make install PREFIX=/usr/local/haproxy
[root@mysql-slave haproxy-1.6.4]# mkdir /usr/local/haproxy/conf
[root@mysql-slave haproxy-1.6.4]# ls
CHANGELOG CONTRIBUTING ebtree haproxy include MAINTAINERS README src tests VERSION
contrib doc examples haproxy-systemd-wrapper LICENSE Makefile ROADMAP SUBVERS VERDATE
[root@mysql-slave haproxy-1.6.4]# cp examples/option-http_proxy.cfg /usr/local/haproxy/conf/haproxy.cfg
2)haproxy的启动脚本配置
由于我的安装包是下载在/usr/local/src下的,我们查看下解压后的haproxy的文件
[root@mysql-slave examples]# pwd
/usr/local/src/haproxy-1.6.4/examples
[root@mysql-slave examples]# ls
acl-content-sw.cfg check.conf debug2html
haproxy.init init.haproxy ssl.cfg
auth.cfg content-sw-sample.cfg debugfind haproxy.spec option-http_proxy.cfg stats_haproxy.sh
check debug2ansi errorfiles haproxy.vim seamless_reload.txt transparent_proxy.cfg
如上,我们可以看到一个名为haproxy.init文件
简单查看下haproxy.init的内容
[root@mysql-slave examples]# cat haproxy.init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118 1#!/bin/sh
2#
3# chkconfig: - 85 15
4# description: HA-Proxy is a TCP/HTTP reverse proxy which is particularly suited \
5# for high availability environments.
6# processname: haproxy
7# config: /etc/haproxy/haproxy.cfg
8# pidfile: /var/run/haproxy.pid
9# Script Author: Simon Matter <simon.matter@invoca.ch>
10# Version: 2004060600
11# Source function library.
12if [ -f /etc/init.d/functions ]; then
13 . /etc/init.d/functions
14elif [ -f /etc/rc.d/init.d/functions ] ; then
15 . /etc/rc.d/init.d/functions
16else
17 exit 0
18fi
19# Source networking configuration.
20. /etc/sysconfig/network
21# Check that networking is up.
22[ ${NETWORKING} = "no" ] && exit 0
23# This is our service name
24BASENAME=`basename $0`
25if [ -L $0 ]; then
26 BASENAME=`find $0 -name $BASENAME -printf %l`
27 BASENAME=`basename $BASENAME`
28fi
29BIN=/usr/sbin/$BASENAME
30CFG=/etc/$BASENAME/$BASENAME.cfg
31[ -f $CFG ] || exit 1
32PIDFILE=/var/run/$BASENAME.pid
33LOCKFILE=/var/lock/subsys/$BASENAME
34RETVAL=0
35start() {
36 quiet_check
37 if [ $? -ne 0 ]; then
38 echo "Errors found in configuration file, check it with '$BASENAME check'."
39 return 1
40 fi
41 echo -n "Starting $BASENAME: "
42 daemon $BIN -D -f $CFG -p $PIDFILE
43 RETVAL=$?
44 echo
45 [ $RETVAL -eq 0 ] && touch $LOCKFILE
46 return $RETVAL
47}
48stop() {
49 echo -n "Shutting down $BASENAME: "
50 killproc $BASENAME -USR1
51 RETVAL=$?
52 echo
53 [ $RETVAL -eq 0 ] && rm -f $LOCKFILE
54 [ $RETVAL -eq 0 ] && rm -f $PIDFILE
55 return $RETVAL
56}
57restart() {
58 quiet_check
59 if [ $? -ne 0 ]; then
60 echo "Errors found in configuration file, check it with '$BASENAME check'."
61 return 1
62 fi
63 stop
64 start
65}
66reload() {
67 if ! [ -s $PIDFILE ]; then
68 return 0
69 fi
70 quiet_check
71 if [ $? -ne 0 ]; then
72 echo "Errors found in configuration file, check it with '$BASENAME check'."
73 return 1
74 fi
75 $BIN -D -f $CFG -p $PIDFILE -sf $(cat $PIDFILE)
76}
77check() {
78 $BIN -c -q -V -f $CFG
79}
80quiet_check() {
81 $BIN -c -q -f $CFG
82}
83rhstatus() {
84 status $BASENAME
85}
86condrestart() {
87 [ -e $LOCKFILE ] && restart || :
88}
89# See how we were called.
90case "$1" in
91 start)
92 start
93 ;;
94 stop)
95 stop
96 ;;
97 restart)
98 restart
99 ;;
100 reload)
101 reload
102 ;;
103 condrestart)
104 condrestart
105 ;;
106 status)
107 rhstatus
108 ;;
109 check)
110 check
111 ;;
112 *)
113 echo $"Usage: $BASENAME {start|stop|restart|reload|condrestart|status|check}"
114 exit 1
115esac
116
117exit $?
118
我们发现,稍微调整下,就是一个完整的haproxy的启动脚本,所以偷下懒,直接修改两个位置:
BIN=/usr/local/haproxy/sbin/haproxy
# haproxy命令所在的位置
CFG=/usr/local/haproxy/conf/haproxy.cfg
# haproxy.cfg为haproxy的配置文件
修改完成后,将修改后的haproxy.init拷贝到/etc/init.d/目录下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 1[root@mysql-slave examples]# cp /usr/local/src/haproxy-1.6.4/examples/haproxy.init /etc/init.d/
2[root@mysql-slave examples]# mv /etc/init.d/haproxy.init /etc/init.d/haproxy
3[root@mysql-slave examples]# chmod +x /etc/init.d/haproxy
4[root@mysql-slave examples]# /etc/init.d/haproxy
5Usage: haproxy {start|stop|restart|reload|condrestart|status|check}
6简单的测试下:
7[root@mysql-slave examples]# /etc/init.d/haproxy status
8haproxy (pid 54309) 正在运行...
9[root@mysql-slave examples]# /etc/init.d/haproxy stop
10Shutting down haproxy: [确定]
11[root@mysql-slave examples]# /etc/init.d/haproxy status
12haproxy 已停
13[root@mysql-slave examples]# /etc/init.d/haproxy start
14Starting haproxy: [确定]
15[root@mysql-slave examples]# /etc/init.d/haproxy reload
16[root@mysql-slave examples]# echo $?
170
18[root@mysql-slave examples]# /etc/init.d/haproxy condrestart
19Shutting down haproxy: [确定]
20Starting haproxy: [确定]
21[root@mysql-slave examples]# /etc/init.d/haproxy check
22Configuration file is valid
23到此,haproxy的安装与脚本的配置就已经完成
24