linux haproxy 脚本,haproxy部署脚本
最近為了測(cè)試haproxy的負(fù)載均衡,打算搭建幾臺(tái)haproxy測(cè)試機(jī),但我又懶想直接用腳本搭建起就可以使用,以后要測(cè)試的時(shí)候,也可以直接就用腳本搞定.下面來(lái)看腳本吧.
cat /root/soft_shell/haproxy_install.sh
#!/bin/bash
#install haproxy
#20160224 by rocdk890
#variables
dir=/usr/local
ha_dir=${dir}/haproxy
ha_cfg=${ha_dir}/conf/haproxy.cfg
ha_init=/etc/init.d/haproxy
kernel=`uname -r | grep '2.6'`
pcre=$(rpm -qa | grep 'pcre' | wc -l)
echo "$dir, $ha_dir, $ha_cfg, $kernel, $pcre"
#check
if [ ! "$kernel" -o "$pcre" -lt "2" ];then
echo -e "the script need linux 2.6 kernel and pcre pcre-devel \nyou can usage 'yum install pcre pcre-devel' or 'rpm -ivh pcre-devel-6.6-2.el5_1.7.x86_64.rpm'"
exit 1
fi
#function
install_ha_cfg (){
#configure haproxy.cfg
#default configure file for test,but need your change the frontend server and backend server ip address,
#good luck!
echo '
global
log 127.0.0.1 local0
maxconn 4096 #最大連接數(shù)
chroot /usr/local/haproxy #安裝目錄
uid 99 #用戶haproxy
gid 99 #組haproxy
daemon #守護(hù)進(jìn)程運(yùn)行
nbproc 1 #進(jìn)程數(shù)量
#pidfile /usr/local/haproxy/logs/haproxy.pid #haproxy pid
pidfile /usr/local/haproxy/haproxy.pid #haproxy pid
defaults
log global
mode http #7層 http;4層tcp
option httplog #http 日志格式
option httpclose #主動(dòng)關(guān)閉http通道
option redispatch #serverId對(duì)應(yīng)的服務(wù)器掛掉后,強(qiáng)制定向到其他健康的服務(wù)器
option dontlognull
maxconn 2000 #最大連接數(shù)
contimeout 5000 #連接超時(shí)(毫秒)
clitimeout 50000 #客戶端超時(shí)(毫秒)
srvtimeout 50000 #服務(wù)器超時(shí)(毫秒)
frontend haproxy_test #定義前端服務(wù)器(haproxy)
bind *:80 #監(jiān)聽(tīng)地址
default_backend server_pool #指定后端服務(wù)器群
#errorfile 502 /usr/local/haproxy/html/maintain.html
#errorfile 503 /usr/local/haproxy/html/maintain.html
#errorfile 504 /usr/local/haproxy/html/maintain.html
backend server_pool #定義后端服務(wù)器群(web server/apache/nginx/iis..)
mode http
option forwardfor #后端服務(wù)器(apache/nginx/iis/*),從Http Header中獲得客戶端IP
balance roundrobin #負(fù)載均衡的方式,輪詢方式
#balance leastconn #負(fù)載均衡的方式,最小連接
cookie SERVERID #插入serverid到cookie中,serverid后面可以定義
option httpchk HEAD /check.html #用來(lái)做健康檢查html文檔
server server1 10.0.1.252:80 cookie server1 check inter 2000 rise 3 fall 3 weight 3
server server1 10.0.1.253:80 cookie server1 check inter 2000 rise 3 fall 3 weight 3
server server1 10.0.1.254:80 cookie server1 check inter 2000 rise 3 fall 3 weight 3
#server server2 10.0.1.253:80 cookie server2 check inter 2000 rise 3 fall 3 maxconn 120 weight 3
#server server3 10.0.1.254:80 cookie server3 check maxconn 90 rise 2 fall 3 weight 3
#服務(wù)器定義:
#cookie server1表示serverid為server1;
#check inter 2000 是檢測(cè)心跳頻率(check 默認(rèn) );
#rise 3 表示 3次正確認(rèn)為服務(wù)器可用;
#fall 3 表示 3次失敗認(rèn)為服務(wù)器不可用;
#weight 表示權(quán)重。
listen admin_stat #status
bind *:8080 #監(jiān)聽(tīng)端口
mode http #http的7層模式
stats refresh 30s #統(tǒng)計(jì)頁(yè)面自動(dòng)刷新時(shí)間
stats uri /haproxy-stats #統(tǒng)計(jì)頁(yè)面URL
stats realm Haproxy\ Statistics #統(tǒng)計(jì)頁(yè)面密碼框上提示文本
stats auth admin:admin #統(tǒng)計(jì)頁(yè)面用戶名和密碼設(shè)置
stats hide-version #隱藏統(tǒng)計(jì)頁(yè)面上HAProxy的版本信息
stats admin if TRUE #手工啟用/禁用,后端服務(wù)器
' > "$ha_cfg" && sed -i '1 d' "$ha_cfg"
}
install_ha_init.d (){
#configure /etc/init.d/haproxy
if [ ! -e "$ha_init" ];then
wget -c http://download.slogra.com/haproxy/haproxy -O /etc/init.d/haproxy
chmod +x /etc/init.d/haproxy
chkconfig haproxy on
else
echo "File haproxy already there !"
fi
}
#install
if [ ! -e "$ha_dir" ];then
wget -nc http://download.slogra.com/haproxy/haproxy-1.4.26.tar.gz
tar zxf haproxy*.tar.gz
cd haproxy*/
make TARGET=linux26 USE_STATIC_PCRE=1 PREFIX=/usr/local/haproxy && make install PREFIX=/usr/local/haproxy && mkdir /usr/local/haproxy/{html,logs,conf}
cd ../
#
if [ ! -e "$ha_dir" ];then
echo "error! can't install haproxy please check ! Will now out of the script !"
exit 1
else
! grep 'haproxy' /etc/syslog.conf && echo 'local1.* /var/log/haproxy.log' >> /etc/syslog.conf
sed -ir 's/SYSLOGD_OPTIONS="-m 0"/SYSLOGD_OPTIONS="-r -m 0"/g' /etc/sysconfig/syslog && /etc/init.d/syslog restart
install_ha_cfg
install_ha_init.d
rm -rf haproxy*/
fi
else
echo "haproxy is already exists!"
fi
本腳本把啟動(dòng)文件也放進(jìn)去了的,所以啟動(dòng)的時(shí)候可以直接執(zhí)行service haproxy start,當(dāng)然最好是先改好了配置文件再啟動(dòng).好了,配置文件里也有說(shuō)明方便大家自己修改.
夜空- 本站版權(quán)
1、本站所有主題由該文章作者發(fā)表,該文章作者與夜空享有文章相關(guān)版權(quán)
2、其他單位或個(gè)人使用、轉(zhuǎn)載或引用本文時(shí)必須同時(shí)征得該文章作者和夜空的同意
3、本帖部分內(nèi)容轉(zhuǎn)載自其它媒體,但并不代表本站贊同其觀點(diǎn)和對(duì)其真實(shí)性負(fù)責(zé)
4、如本帖侵犯到任何版權(quán)問(wèn)題,請(qǐng)立即告知本站,本站將及時(shí)予與刪除并致以最深的歉意
5、原文鏈接:blog.slogra.com/post-642.html
總結(jié)
以上是生活随笔為你收集整理的linux haproxy 脚本,haproxy部署脚本的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 各种oracle索引类型介绍,各种Ora
- 下一篇: linux 网卡驱动分析,LINUX_网