linux中haproxy部署shell脚本分享

 最近为了测试haproxy的负载均衡,打算搭建几台haproxy测试机,但我又懒想直接用脚本搭建起就可以使用,以后要测试的时候,也可以直接就用脚本搞定.下面来看脚本吧.

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              #最大连接数
    chroot /usr/local/haproxy #安装目录
    uid 99                    #用户haproxy
    gid 99                    #组haproxy
    daemon                    #守护进程运行
    nbproc 1                  #进程数量
    #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          #主动关闭http通道
   option  redispatch         #serverId对应的服务器挂掉后,强制定向到其他健康的服务器

 

   option  dontlognull
   maxconn 2000               #最大连接数
   contimeout      5000       #连接超时(毫秒)
   clitimeout      50000      #客户端超时(毫秒)
   srvtimeout      50000      #服务器超时(毫秒)

 

frontend haproxy_test         #定义前端服务器(haproxy)
        bind *:80             #监听地址
        default_backend server_pool  #指定后端服务器群
        #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           #定义后端服务器群(web server/apache/nginx/iis..)
        mode http
        option  forwardfor    #后端服务器(apache/nginx/iis/*),从Http Header中获得客户端IP
        balance roundrobin    #负载均衡的方式,轮询方式
        #balance leastconn     #负载均衡的方式,最小连接
        cookie SERVERID       #插入serverid到cookie中,serverid后面可以定义
        option  httpchk HEAD /check.html #用来做健康检查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
#服务器定义:
#cookie server1表示serverid为server1;
#check inter 2000 是检测心跳频率(check 默认 );
#rise 3 表示 3次正确认为服务器可用;
#fall 3 表示 3次失败认为服务器不可用;
#weight 表示权重。

 

listen admin_stat                   #status
    bind *:8080                     #监听端口
    mode http                       #http的7层模式
    stats refresh 30s               #统计页面自动刷新时间
    stats uri /haproxy-stats        #统计页面URL
    stats realm Haproxy\ Statistics #统计页面密码框上提示文本
    stats auth admin:admin          #统计页面用户名和密码设置
    stats hide-version              #隐藏统计页面上HAProxy的版本信息
    stats admin if TRUE             #手工启用/禁用,后端服务器
' > "$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

 

本脚本把启动文件也放进去了的,所以启动的时候可以直接执行service haproxy start,当然最好是先改好了配置文件再启动.好了,配置文件里也有说明方便大家自己修改.

时间: 2024-07-29 20:20:07

linux中haproxy部署shell脚本分享的相关文章

Linux下实现SSH免密码登录和实现秘钥的管理、分发、部署SHELL脚本分享_linux shell

环境: ssh server: 192.168.100.29  server.example.com ssh client: 192.168.100.30  client.example.com 通过root用户建立秘钥认证实现SHELL脚本管理,分发,部署 首先client端创建秘钥对,并将公钥分发给需要登录的SSH服务端 注:公钥相当于锁,私钥相当于钥匙,我们这里相当于在客户端创建一对钥匙和锁,想要做到SSH免密码登录,就相当于我们将锁分发到服务端并装锁,然后客户端就可以利用钥匙开锁. 一.

linux中mysql备份shell脚本代码_linux shell

第一步:在你的linux服务器中定义备份目录: 复制代码 代码如下: mkdir /var/lib/mysqlbackup cd /var/lib/mysqlbackup 第二步:下面是最重要的一步了,就是写定时备份脚本. 复制代码 代码如下: vi dbbackup.sh 代码文件如下 复制代码 代码如下: #!/bin/sh# mysql data backup script## use mysqldump --help,get more detail.#BakDir=/root/back/

linux中resin启动shell脚本

代码如下  代码如下 复制代码 [root@client01 ~]# cat /etc/init.d/resind     #!/bin/sh       #created by teddylu at 2014-12-12   #used to startup for resin version 3.1   #chkconfig: 345 85 15     #set up environment variable to fix the failure of resin automatical

linux中bash漏洞shell脚本修复

 代码如下 复制代码 #/bin/bash #Author Alex Fang. Updates may apply soon. clear echo "Press anykey to continue..." $anykey ; read anykey echo "BBBBBBBBBBBAAAAAAAAAAAAAAAAAAAASSSSSSSSSSSSSSSSSSHHHHHHHHHHHHHHHHHHHH!" echo "ShellShockFixer v0

8个实用的Shell脚本分享

  这篇文章主要介绍了8个实用的Shell脚本分享,本文给出了判断输入为数字.字符或其他.求平均数.自减输出.在文件中添加前缀.批量测试文件是否存在等实用脚本,需要的朋友可以参考下 几个Shell脚本的例子,觉得还不错. [例子:001]判断输入为数字,字符或其他 代码如下: #!/bin/bash read -p "Enter a number or string here:" input case $input in [0-9]) echo -e "Good job, Y

怎样在java代码中调用执行shell脚本呀

问题描述 遇到个问题   在本地压缩服务器上的xml文件 我就想编写shell教本 脚本内容是链接服务器 找到待压缩文件 压缩文件  说实话 我不知道这样是否可行  试试  但我不知道怎样在java代码中 调用执行shell脚本  谁能指点指点  求教...  问题补充:首先谢谢各位朋友的回答  在补充个小问题 <br />能在调用shell脚本时 同时给shell脚本传参数吗  不止一个 能这样写吗     <br />Runtime.getRuntime().exec(&quo

Linux 守护进程 自动重启 shell 脚本分享

#!/bin/sh #本脚本为守护进程, 如果发现进程僵死时, 自动重启进程   function check_and_kill_and_start(){ log_file=$1 ps_name=$2 dead_duration=$3 # echo "check the progress "$ps_name ymd=$(date +%Y-%m-%d) log_file=${log_file}${ymd}.log # echo $log_file 判断是否存在log_file if [ 

MySQL的一些功能实用的Linux shell脚本分享_Mysql

Memcached启动脚本 # vim /etc/init.d/memcached #!/bin/bash #======================================================================================= # chkconfig: - 80 12 # description: Distributed memory caching daemon # processname: memcached #===========

Linux中高效编写Bash脚本的10个技巧

Shell 脚本编程 是你在 Linux 下学习或练习编程的最简单的方式.尤其对 系统管理员要处理着自动化任务,且要开发新的简单的实用程序或工具等(这里只是仅举几例)更是必备技能. 本文中,我们将分享 10 个写出高效可靠的 bash 脚本的实用技巧,它们包括: 1. 脚本中多写注释 这是不仅可应用于 shell 脚本程序中,也可用在其他所有类型的编程中的一种推荐做法.在脚本中作注释能帮你或别人翻阅你的脚本时了解脚本的不同部分所做的工作. 对于刚入门的人来说,注释用 # 号来定义. # TecM