Haproxy的安装以及启动脚本的调试

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

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
#!/bin/sh
#
# chkconfig: - 85 15
# description: HA-Proxy is a TCP/HTTP reverse proxy which is particularly suited \
#              for high availability environments.
# processname: haproxy
# config: /etc/haproxy/haproxy.cfg
# pidfile: /var/run/haproxy.pid
# Script Author: Simon Matter <simon.matter@invoca.ch>
# Version: 2004060600
# 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=` basename  $0`
if  [ -L $0 ];  then
   BASENAME=` find  $0 -name $BASENAME - printf  %l`
   BASENAME=` basename  $BASENAME`
fi
BIN= /usr/sbin/ $BASENAME
CFG= /etc/ $BASENAME/$BASENAME.cfg
[ -f $CFG ] ||  exit  1
PIDFILE= /var/run/ $BASENAME.pid
LOCKFILE= /var/lock/subsys/ $BASENAME
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  $?

我们发现,稍微调整下,就是一个完整的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
[root@mysql-slave examples] # cp /usr/local/src/haproxy-1.6.4/examples/haproxy.init /etc/init.d/
[root@mysql-slave examples] # mv /etc/init.d/haproxy.init /etc/init.d/haproxy
[root@mysql-slave examples] # chmod +x /etc/init.d/haproxy
[root@mysql-slave examples] # /etc/init.d/haproxy 
Usage: haproxy {start|stop|restart|reload|condrestart|status|check}
简单的测试下:
[root@mysql-slave examples] # /etc/init.d/haproxy status
haproxy (pid  54309) 正在运行...
[root@mysql-slave examples] # /etc/init.d/haproxy stop
Shutting down haproxy:                                     [确定]
[root@mysql-slave examples] # /etc/init.d/haproxy status
haproxy 已停
[root@mysql-slave examples] # /etc/init.d/haproxy start
Starting haproxy:                                          [确定]
[root@mysql-slave examples] # /etc/init.d/haproxy reload
[root@mysql-slave examples] # echo $?
0
[root@mysql-slave examples] # /etc/init.d/haproxy condrestart
Shutting down haproxy:                                     [确定]
Starting haproxy:                                          [确定]
[root@mysql-slave examples] # /etc/init.d/haproxy check
Configuration  file  is valid
到此,haproxy的安装与脚本的配置就已经完成









本文转自 冰冻vs西瓜 51CTO博客,原文链接:http://blog.51cto.com/molewan/1866746,如需转载请自行联系原作者
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
8月前
|
应用服务中间件 数据安全/隐私保护 nginx
Haproxy-安装与配置
安装 Haproxy
68 0
|
9月前
|
应用服务中间件 Linux Shell
【Shell脚本】Linux安装Nginx以及开机自启
【Shell脚本】Linux安装Nginx以及开机自启
【Shell脚本】Linux安装Nginx以及开机自启
|
机器学习/深度学习 应用服务中间件 Shell
Shell实现简单的管理Nginx服务启动脚本
实现的功能 一:Nginx启动自检功能二:自检报错,自动进入所在文件的所在行,让运维人员进行修改!三:文件锁功能使得该脚本只能让系统管理员执行,并保证不能同时执行多次!四:可适用较好,实现简单的start,status,restart,reload,stop等功能! 脚本的缺点 一:未引用方法,使.
1278 0
|
负载均衡 数据安全/隐私保护 网络协议
|
监控 负载均衡 网络协议
|
Shell 应用服务中间件 nginx
|
监控 负载均衡 算法