工作中用到haproxy+keepalived,把安装步骤记录下来
1.安装haproxy
http://wtdown1.onlinedown.net/down/haproxy-1.4.17.tar.zip 下载地址,unzip haproxy-1.4.17.tar.zip; tar -xvf haproxy-1.4.17.tar.gz
# wget http://haproxy.1wt.eu/download/1.3/src/haproxy-1.3.20.tar.gz# tar zcvf haproxy-1.3.20.tar.gz
# cd haproxy-1.3.20
# make TARGET=linux26 PREFIX=/usr/local/haproxy #将haproxy安装到/usr/local/haproxy
# make install PREFIX=/usr/local/haproxy
安装完毕,在/usr/local/haproxy/目录下会看到三个目录:doc、sbin、share
vim /etc/haproxy/haproxy.cfg
2.加入负载服务器地址:
server web1 10.16.0.9:8085 cookie 1 weight 5 check inter 2000 rise 2 fall 3
server web2 10.16.0.10:8085 cookie 2 weight 3 check inter 2000 rise 2 fall 3
#cookie 1表示serverid为1,check inter 1500 是检测心跳频率
#rise 2是2次正确认为服务器可用,fall 3是3次失败认为服务器不可用,weight代表权重
3.加入监控地址:
vim /etc/haproxy/haproxy.cfg #在最后加入如下:
listen admin_stat #status
bind 0.0.0.0:8080 #监听端口
mode http #http的7层模式
stats refresh 30s #统计页面自动刷新时间
stats uri /haproxy-admin #统计页面URL
stats realm Haproxy\ Statistics #统计页面密码框上提示文本
stats auth admin:admin #统计页面用户名和密码设置
stats hide-version #隐藏统计页面上HAProxy的版本信息
stats admin if TRUE #手工启用/禁用,后端服务器
重启服务:
#service haproxy restart
打开监控地址:
http://xx.xx.xx.xx:8080/haproxy-admin用户名,密码 admin
#################################################################################
#参考资源 (resources) #
#本文仅作为引子,Haproxy 配置以其功能远远不止这些。更多资料可到以下网站中获取。 #
# Haproxy 中文 http://cn.haproxy.org #
# Haproxy 英文 http://www.haproxy.org #
# 中国开源社区 #
#################################################################################
安装keepalived
需要安装编译环境:
# yum install -y gcc gcc-c++ openssl openssl-devel
系统环境:
# uname -a
下载:keepalived
# wget -c http://www.keepalived.org/software/keepalived-1.2.12.tar.gz # tar zxvf keepalived-1.2.12.tar.gz
# cd keepalived-1.2.12
# ./configure --prefix=/usr/local/keepalived #指定安装路径
配置成功会有如下显示
Keepalived configuration
------------------------
Keepalived version : 1.2.12
Compiler : gcc
Compiler flags : -g -O2
Extra Lib : -lssl -lcrypto -lcrypt
Use IPVS Framework : Yes
IPVS sync daemon support : Yes
IPVS use libnl : No
Use VRRP Framework : Yes
Use VRRP VMAC : Yes
SNMP support : No
SHA1 support : No
Use Debug flags : No
# make
# make install
将keepalived软连接到bin下
# ln -s /usr/local/keepalived/sbin/keepalived /usr/bin/keepalived
添加启动脚本且方便用service keepalived start/stop/restart管理
# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalived
# chmod 755 /etc/init.d/keepalived //添加执行权限
# chkconfig keepalived on //开机启动
修改/etc/init.d/keepalived中的程序路径
# Source configuration file (we set KEEPALIVED_OPTIONS there)
. /etc/sysconfig/keepalived
改为:
# Source configuration file (we set KEEPALIVED_OPTIONS there)
. /usr/local/keepalived/etc/sysconfig/keepalived
默认情况下,keepalived 会读取 /etc/keepalived 下keepalived.conf 文件
如果没有建立这个文件,keepalived也不会报错,但是会发现,所创建的关于keepalived的相关参数根本就没有生效。
# mkdir /etc/keepalived
# ln -s /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
# service keepalived start #启动keepalived
配置keepalived
# vim /etc/keepalived/keepalived.conf