4.0 HAProxy自动化安装和运行脚本

1,050 阅读2分钟

vim haproxy.sh

#!/bin/bash
#
#********************************************************************
#Author:                wangjian
#QQ:                    3555409634
#Date:                  2020-04-08
#FileName:             haproxy.sh
#URL:                   NOT FOUND
#Description:          The test script
#Copyright (C):         2020 All rights reserved
#********************************************************************
. boot_configure.function
. function_configure.function
. install.function

install.function
boot_configure.function
function_configure.function

vim install.function

#!/bin/bash
#
#********************************************************************
#Author:                wangjian
#QQ:                    3555409634
#Date:                  2020-04-07
#FileName:             insatll.sh
#URL:                   NOT FOUND
#Description:          The test script
#Copyright (C):         2020 All rights reserved
#********************************************************************
install.function(){
BASE_URL=`pwd`
# 安装lua依赖包
yum install gcc readline-devel -y
# 编译安装lua-是HAProxy依赖包,要5.3及以上版版本
mkdir /tar/centos_haproxy_2.0.14 -p
cd /tar/centos_haproxy_2.0.14
rm -f lua-5.3.5.tar.gz
wget http://www.lua.org/ftp/lua-5.3.5.tar.gz
tar xf lua-5.3.5.tar.gz -C /usr/local/src/
cd /usr/local/src/lua-5.3.5
make linux test
# 安装HAProxy依赖
yum install gcc openssl-devel pcre-devel systemd-devel -y
# 安装HAProxy-1.8/1.9编译参数和2.0编译参数不同
cd /tar/centos_haproxy_2.0.14
rm -f haproxy-2.0.14.tar.gz
wget http://47.93.200.163:8081/ssh/tar/centos_HAProxy_2.0.14/haproxy-2.0.14.tar.gz
tar xf haproxy-2.0.14.tar.gz -C /usr/local/src
cd /usr/local/src/haproxy-2.0.14
mkdir /apps/haproxy -p
make  ARCH=x86_64 TARGET=linux-glibc  USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1  USE_SYSTEMD=1  USE_LUA=1 LUA_INC=/usr/local/src/lua-5.3.5/src/  LUA_LIB=/usr/local/src/lua-5.3.5/src/
make install PREFIX=/apps/haproxy
cd $BASE_URL
}

vim boot_configure.function

#!/bin/bash
#
#********************************************************************
#Author:                wangjian
#QQ:                    3555409634
#Date:                  2020-04-07
#FileName:             boot_configure.sh
#URL:                   NOT FOUND
#Description:          The test script
#Copyright (C):         2020 All rights reserved
#********************************************************************
boot_configure.function(){
# 配置PATH
cd /apps/haproxy/sbin/
cp -f haproxy  /usr/sbin/
# 配置启动脚本
echo '
[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target

[Service]
ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg  -c -q
ExecStart=/usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/lib/haproxy/haproxy.pid
ExecReload=/bin/kill -USR2 $MAINPID

[Install]
WantedBy=multi-user.target
'> /usr/lib/systemd/system/haproxy.service

# 准备配置文件
mkdir  /etc/haproxy -p
echo '
global
  maxconn 100000
  chroot /apps/haproxy
  stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin
  # uid 99
  # gid 99
  user haproxy
  group haproxy
  daemon
  # nbproc 4
  # cpu-map 1 0
  # cpu-map 2 1
  # cpu-map 3 2
  # cpu-map 4 3
  pidfile /var/lib/haproxy/haproxy.pid
  log 127.0.0.1 local2 info

defaults
  option http-keep-alive
  option forwardfor
  maxconn 100000
  mode http
  timeout connect 300000ms
  timeout client  300000ms
  timeout server  300000ms

listen stats
  mode http
  bind 0.0.0.0:9999
  stats enable
  stats uri /haproxy-status
  stats auth haadmin:123456

listen web_port
  bind 10.0.0.11:80
  mode http
  log global
  server web1 127.0.0.1:8080 check inter 3000 fall 2 rise 5
' > /etc/haproxy/haproxy.cfg

# 准备用户
useradd -r -s /sbin/nologin -d /var/lib/haproxy haproxy

# 准备pid和socket文件目录
mkdir /var/lib/haproxy -p
chown haproxy:haproxy /var/lib/haproxy -R


# 启动并设为开机启动
systemctl enable --now haproxy.service
}

function_configure.function

#!/bin/bash
#
#********************************************************************
#Author:                wangjian
#QQ:                    3555409634
#Date:                  2020-04-07
#FileName:             function_configure.sh
#URL:                   NOT FOUND
#Description:          The test script
#Copyright (C):         2020 All rights reserved
#*******************************************************************
function_configure.function(){

}