为简化工作中手动部署Haproxy的工作量以及降低错误概率,特地编写依菜单的形式安装Haproxy的脚本,具体如下:

    #!/bin/bash                                                                                                                                          
    #*************************************************************
    #Author:             fanfubin
    #QQ:                 502422514
    #Date:               2019-03-19
    #FileName:           haproxy.sh
    #Description:        The install haproxy script
    #Copyright (C):      2019 All rights reserved
    #*************************************************************
    source /etc/init.d/functions

    BLACK_COLOR='\E[1;30m'       
    RED_COLOR='\E[1;31m'       
    GREEN_COLOR='\E[1;32m'       
    YELLOW_COLOR='\E[1;33m'       
    BLUE_COLOR='\E[1;34m'       
    PINK_COLOR='\E[1;35m' #紫色       
    SKYBLUE_COLOR='\E[1;36m' #天蓝色       
    WHITE_COLOR='\E[1;37m' #白色       

    SHAN='\E[31;5m' #字体闪烁       
    RES='\E[0m'

    f_color() {
        case "$1" in
                black)
                        echo -e "${PINK_COLOR}$2 ${RES}"
                        ;;
                red)
                        echo -e "${RED_COLOR}$2 ${RES}"
                        ;;
                green)
                        echo -e "${GREEN_COLOR}$2 ${RES}"
                        ;;
                yellow)
                        echo -e "${YELLOW_COLOR}$2 ${RES}"
                        ;;
                blue)
                        echo -e "${BLUE_COLOR}$2 ${RES}"
                        ;;
                skyblue)
                        echo -e "${PINK_COLOR}$2 ${RES}"
                        ;;
                pink)
                        echo -e "${PINK_COLOR}$2 ${RES}"
                        ;;
                white)
                        echo -e "${PINK_COLOR}$2 ${RES}"
                        ;;
                shan)
                        echo -e "${SHAN}$2 ${RES}"
                        ;;
                *)
                        exit    
    esac
    }

    #返回值: 0-错误  1-正确     只能判断+/- int num
    f_is_num() {
            [ $# -eq 1 ] && {
                    expr $1 + 1 &>/dev/null
                    if [ $? -eq 0 -a "$1" != "-1" ];then
                            return 1
                    else
                            return 0
                    fi
            } || {
                    return 0
            }
    }

    f_main_title() {
            f_color green "`echo -e "\t服务器监控系统"`"
    }

    cpu_count=`lscpu|awk -F ' +' '/^CPU\(s\):/{print $2}'`
            if [ $cpu_count -le 2 ];then
                    export cpu_num="1"
            else
                    let cpu_num=`lscpu|awk -F ' +' '/^CPU\(s\):/{print $2}'`-1
                    export cpu_num
            fi

    export httpd_user="nginx" 
    export httpd_group="nginx"
    export httpd_id="2000"
    export haproxy_install_ver="haproxy-1.8.20.tar.gz"

    ##创建通用的用户名和id(user:nginx  id:2000)
    id $httpd_user                                                                                                                                                          
            if [ `echo $?` -ne 0 ];then
                    useradd -s /sbin/nologin -M -u $httpd_id $httpd_user
            else
                 test_httpd_id=`id $httpd_user | awk -F '[=(]' '{print $2}'`
                 test_httpd_gid=`id $httpd_user | awk -F '[=(]' '{print $4}'`
                 if [ $test_httpd_id -ne $httpd_id -o $test_httpd_gid -ne $httpd_id ];then
                         userdel -r $httpd_id
                         useradd -s /sbin/nologin -M -u $httpd_id $httpd_user
                 fi
            fi

    f_menu_3_install_server_haproxy_list() {

        local path
        local haproxy_install_dir
        local num
        local target
        local system_one_ver
        local system_two_ver
        local system_three_ver
        local choose_num
        local locate_server_num

        echo
        f_main_title
        echo "==================================" 
        cat <<-EOF
        【1. 】 Show install version
        【2. 】 Source install haproxy 
        【0. 】 Exit

        EOF
        echo "=================================="

        # haproxy  .tar. is exist?
        yum -y install mlocate &> /dev/null
        updatedb &> /dev/null
        locate_server_num=`locate ${haproxy_install_ver}| wc -l`
        if [ ! -e $haproxy_install_ver ];then
            if [ $locate_server_num -eq 1 ];then
                \cp `locate ${haproxy_install_ver}` . -a
            elif [ $locate_server_num -ge 2 ];then
                \cp `locate ${haproxy_install_ver} | head -n1` . -a
            else
                f_color red "`echo "${haproxy_install_ver} is not exist!"`"
                f_menu_3_install_server_haproxy_list
            fi
        fi
        haproxy_cpu_count=`lscpu|awk -F ' +' '/^CPU\(s\):/{print $2}'`
        haproxy_install_dir=`echo $haproxy_install_ver | awk -F '.tar.' '{print $1}'`
        sys_one_ver=`uname -r|awk -F '.' '{print $1}'`
        sys_two_ver=`uname -r|awk -F '.' '{print $2}'`
        sys_three_ver=`uname -r|awk -F '.' '{print $3}'`
        if [ $sys_one_ver -eq 2 ];then
            if [ $sys_two_ver -ge 6 ];then
                if [ $sys_three_ver -ge 28 ];then
                    target="linux2628"
                else
                    target="linux26"
                fi
            else
                f_color red "`echo "Your system is not exist $haproxy_install_ver"`"
                f_menu_3_install_server_haproxy_list
            fi
        elif [ $sys_one_ver -eq 3 ];then
            target="linux2628"
        else
            f_color red "`echo "Your system is not exist $haproxy_install_ver"`"
            f_menu_3_install_server_haproxy_list
        fi

        read -p "Pls input a num:" num
        f_is_num $num
        [ $? -eq 1 ] || { action "Pls input valid num!" false ; f_menu_3_install_server_haproxy_list;}

        case "$num" in 
            1)
                echo
                echo -e "\t$haproxy_install_ver"
                f_menu_3_install_server_haproxy_list
                ;;
            2)
                echo
                read -p "Pls input 1-install or 2-quit: " choose_num
                f_is_num $choose_num
                [ $? -eq 1 ] || { action "Pls input valid num!" false ; f_menu_3_install_server_haproxy_list;}

                case "$choose_num" in
                    1)
                        echo
                        read -p "Pls input install Path or null: " path

                        if [ -z "$path" ];then
                            echo "Defaults install Path is /apps/${haproxy_install_dir}"

                            yum -y install gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel \
                                systemd-devel zlib-devel &> /dev/null

                            tar xf $haproxy_install_ver 
                            cd $haproxy_install_dir
                            sleep 1

                            make ARCH=x86_64 TARGET="$target" USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1 \
                                USE_CPU_AFFINITY=1 PREFIX="/apps/${haproxy_install_dir}"
                            sleep 1
                            make install PREFIX="/apps/${haproxy_install_dir}"
                            cp haproxy /usr/sbin/
                            sleep 1

                            cat >/usr/lib/systemd/system/haproxy.service<<-EOF
                                [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 /run/haproxy.pid
                                ExecReload=/bin/kill -USR2 \$MAINPID
                                [Install]
                                WantedBy=multi-user.target
                            EOF

                            mkdir -p /etc/haproxy
                            cat >/etc/haproxy/haproxy.cfg<<-EOF
                                global
                                maxconn 100000
                                chroot "/apps/${haproxy_install_dir}"
                                #stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin
                                uid $httpd_id
                                gid $httpd_id
                                daemon
                                nbproc $haproxy_cpu_count 
                                pidfile /run/haproxy.pid
                                log 127.0.0.1 local3 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:9000
                                    stats enable
                                    log global
                                    stats uri     /haproxy-status
                                    stats auth    haadmin:q1w2e3r4ys

                            EOF
                            for i in `seq $haproxy_cpu_count`
                                do
                                                        let bind_cpu=$i-1
                                                        sed -ri '/nbproc/acpu-map '$i' '$bind_cpu'' /etc/haproxy/haproxy.cfg
                                done

                            mkdir -p /var/lib/haproxy
                            chown ${httpd_user}.${httpd_user} /var/lib/haproxy/ -R
                            systemctl daemon-reload
                            systemctl restart haproxy
                            cd - &> /dev/null

                            f_color shan "`echo "haproxy install path is: /apps/${haproxy_install_dir}"`"
                            sleep 1
                        else
                            yum -y install gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel \
                                systemd-devel zlib-devel &> /dev/null

                            tar xf $haproxy_install_ver 
                            cd $haproxy_install_dir
                            sleep 1

                            make ARCH=x86_64 TARGET="$target" USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1 \
                                USE_CPU_AFFINITY=1 PREFIX="${path}"
                            sleep 1
                            make install PREFIX="${path}"
                            cp haproxy /usr/sbin/
                            sleep 1

                            cat >/usr/lib/systemd/system/haproxy.service<<-EOF
                                [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 /run/haproxy.pid
                                ExecReload=/bin/kill -USR2 \$MAINPID
                                [Install]
                                WantedBy=multi-user.target
                            EOF

                            mkdir -p /etc/haproxy
                            cat >/etc/haproxy/haproxy.cfg<<-EOF
                                global
                                maxconn 100000
                                chroot "${path}"
                                #stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin
                                uid $httpd_id
                                gid $httpd_id
                                daemon
                                nbproc $haproxy_cpu_count 
                                pidfile /run/haproxy.pid
                                log 127.0.0.1 local3 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:9000
                                    stats enable
                                    log global
                                    stats uri     /haproxy-status
                                    stats auth    haadmin:q1w2e3r4ys
                            EOF

                            for i in `seq $haproxy_cpu_count`
                                do
                                                        let bind_cpu=$i-1
                                                        sed -ri '/nbproc/acpu-map '$i' '$bind_cpu'' /etc/haproxy/haproxy.cfg
                                done

                            mkdir -p /var/lib/haproxy
                            chown ${httpd_user}.${httpd_user} /var/lib/haproxy/ -R
                            systemctl daemon-reload
                            systemctl restart haproxy
                            cd - &> /dev/null

                            f_color shan "`echo "haproxy install path is: ${path}"`"
                            sleep 1
                        fi

                        f_menu_3_install_server_haproxy_list
                        ;;
                    2)
                        f_menu_3_install_server_haproxy_list
                        ;;
                    *)      
                        f_menu_3_install_server_haproxy_list
                        ;;
                esac
                ;;
            0)
                exit 1
                ;;
            *)
                echo
                f_menu_3_install_server_haproxy_list
        esac    
        unset path
        unset haproxy_install_dir
        unset num
        unset target
        unset system_one_ver
        unset system_two_ver
        unset system_three_ver
        unset choose_num
        unset locate_server_num
    }

    Flag=true
    while $Flag
    do
        f_menu_3_install_server_haproxy_list
            if [ $? -eq 0 ];then
                    Flag=false
            fi
    done