最新要闻

广告

手机

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

家电

世界今头条!搭建两台web服务器基于HAProxy实现负载均衡

来源:博客园


(相关资料图)

环境:

  • DNS服务器:192.168.10.200 仅主机模式
  • internet服务器:192.168.10.123 仅主机模式
  • web1:10.0.0.100 (安装apache2)NAT模式
  • web2:10.0.0.18 (安装httpd) NAT模式
  • HAProxy服务器:10.0.0.8(配备两块网卡,eth0NAT模式,属于内网;eth1 仅主机模式192.168.10.129,外网)
  1. 搭建DNS服务器
[root@dns ~]$ cat install_dns.sh #!/bin/bash##***********************************************************#Author:            yanli#Date:              2022-10-25#FileName:          install_dns.sh#Description:        #***********************************************************DOMAIN=yanlinux.orgHOST=wwwHOST_IP=192.168.10.129CPUS=`lscpu |awk "/^CPU\(s\)/{print $2}"`. /etc/os-releasecolor () {    RES_COL=60    MOVE_TO_COL="echo -en \\033[${RES_COL}G"    SETCOLOR_SUCCESS="echo -en \\033[1;32m"    SETCOLOR_FAILURE="echo -en \\033[1;31m"    SETCOLOR_WARNING="echo -en \\033[1;33m"    SETCOLOR_NORMAL="echo -en \E[0m"    echo -n "$1" && $MOVE_TO_COL    echo -n "["    if [ $2 = "success" -o $2 = "0" ] ;then        ${SETCOLOR_SUCCESS}        echo -n $"  OK  "        elif [ $2 = "failure" -o $2 = "1"  ] ;then         ${SETCOLOR_FAILURE}        echo -n $"FAILED"    else        ${SETCOLOR_WARNING}        echo -n $"WARNING"    fi    ${SETCOLOR_NORMAL}    echo -n "]"    echo }install_dns () {    if [ $ID = "centos" -o $ID = "rocky" ];then    yum install -y  bind bind-utilselif [ $ID = "ubuntu" ];then        color "不支持Ubuntu操作系统,退出!" 1        exit    #apt update    #apt install -y  bind9 bind9-utilselse    color "不支持此操作系统,退出!" 1    exitfi    }config_dns () {    sed -i -e "/listen-on/s/127.0.0.1/localhost/" -e "/allow-query/s/localhost/any/" /etc/named.conf    cat >> /etc/named.rfc1912.zones < /var/named/$DOMAIN.zone <
  1. 搭建两台web服务器
#web1搭建[root@web1 ~]$ apt -y install apache2[root@web1 ~]$ cat /var/www/html/index.html

10.0.0.100 www.yanlinux.org

#web2搭建[root@web2 ~]$ yum -y install httpd[root@web2 ~]$ cat > /var/www/html/index.html

10.0.0.18 www.yanlinux.org

  1. 搭建HAProxy服务器
[root@haproxy ~]$ cat install_haproxy.sh #!/bin/bashHAPROXY_VERSION=2.6.9HAPROXY_FILE=haproxy-${HAPROXY_VERSION}.tar.gzLUA_VERSION=5.4.4LUA_FILE=lua-${LUA_VERSION}.tar.gzHAPROXY_INSTALL_DIR=/apps/haproxySRC_DIR=/usr/local/srcCWD=`pwd`CPUS=`lscpu|awk "/^CPU\(s\)/{print $2}"`LOCAL_IP=$(hostname -I|awk "{print $1}")STATS_AUTH_USER=adminSTATS_AUTH_PASSWD=123456. /etc/os-releasecolor () {    RES_COL=60    MOVE_TO_COL="echo -en \\033[${RES_COL}G"    SETCOLOR_SUCCESS="echo -en \\033[1;32m"    SETCOLOR_FAILURE="echo -en \\033[1;31m"    SETCOLOR_WARNING="echo -en \\033[1;33m"    SETCOLOR_NORMAL="echo -en \E[0m"    echo -n "$1" && $MOVE_TO_COL    echo -n "["    if [ $2 = "success" -o $2 = "0" ] ;then        ${SETCOLOR_SUCCESS}        echo -n $"  OK  "    elif [ $2 = "failure" -o $2 = "1"  ] ;then        ${SETCOLOR_FAILURE}        echo -n $"FAILED"    else        ${SETCOLOR_WARNING}        echo -n $"WARNING"    fi    ${SETCOLOR_NORMAL}    echo -n "]"    echo}check_file (){    if [ ! -e ${HAPROXY_FILE} ];then        color "请下载${HAPROXY_FILE}文件!" 1        exit    elif [ ! -e ${LUA_FILE} ];then        color "请先下载${LUA_FILE}文件!" 1        exit    else        color "相关文件已准备" 0    fi}install_haproxy (){    #安装依赖环境    if [ $ID = "centos" -o $ID = "rocky" ];then        yum -y install gcc make gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel libtermcap-devel ncurses-devel libevent-devel readline-devel    elif [ $ID = "ubuntu" ];then        apt update        apt -y install gcc make openssl libssl-dev libpcre3 libpcre3-dev zlib1g-dev  libreadline-dev libsystemd-dev    else        color "不支持此操作系统!" 1        exit    fi        #安装lua环境    tar xf ${LUA_FILE} -C ${SRC_DIR}    LUA_DIR=${LUA_FILE%.tar*}  #变量高级用法,直接返回去掉.tar*的后缀    cd ${SRC_DIR}/${LUA_DIR}    make all test        #编译安装haproxy    cd ${CWD}    tar xf ${HAPROXY_FILE} -C ${SRC_DIR}    HAPROXY_DIR=${HAPROXY_FILE%.tar*}    cd ${SRC_DIR}/${HAPROXY_DIR}    make -j ${CPUS} ARCH=x86_64 TARGET=linux-glibc USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1 USE_CPU_AFFINITY=1 USE_LUA=1 LUA_INC=${SRC_DIR}/${LUA_DIR}/src/ LUA_LIB=${SRC_DIR}/${LUA_DIR}/src/ PREFIX=${HAPROXY_INSTALL_DIR}    make install PREFIX=${HAPROXY_INSTALL_DIR}    [ $? -eq 0 ] && color "HAProxy编译安装成功" 0 || { color "HAProxy编译安装失败,退出" 1;exit; }    [ -L /usr/sbin/haproxy ] || ln -s ${HAPROXY_INSTALL_DIR}/sbin/haproxy /usr/sbin/ &> /dev/null    [ -d /etc/haproxy ] || mkdir /etc/haproxy &> /dev/null    [ -d /var/lib/haproxy/ ] || mkdir -p /var/lib/haproxy &> /dev/null    #准备配置文件    cat > /etc/haproxy/haproxy.cfg < /lib/systemd/system/haproxy.service < /dev/null && color "HAProxy安装完成" 0 || { color "HAProxy安装失败" 1;exit; }    echo "-------------------------------------------------------------------"    echo -e "请访问链接: \E[32;1mhttp://${LOCAL_IP}:9999/haproxy-status\E[0m"    echo -e "用户和密码: \E[32;1m${STATS_AUTH_USER}/${STATS_AUTH_PASSWD}\E[0m"}main (){    check_file    install_haproxy}main#安装haproxy[root@haproxy ~]$ sh install_haproxy.sh#配置proxies[root@haproxy ~]$ vi /etc/haproxy/haproxy.cfg#在文件最后加上下面几行信息listen yanlinux_http_80    bind 192.168.10.129:80    mode http    option forwardfor    server web1 10.0.0.100:80 check inter 3000 fall 3 rise 5    server web2 10.0.0.18:80 check inter 3000 fall 3 rise 5#检查语法[root@haproxy ~]$ haproxy -c -f /etc/haproxy/haproxy.cfg Configuration file is valid#重启服务[root@haproxy ~]$ systemctl restart haproxy.service #端口打开[root@haproxy ~]$ ss -ntl State      Recv-Q     Send-Q            Local Address:Port           Peer Address:Port     Process     LISTEN     0          128                     0.0.0.0:22                  0.0.0.0:*                    LISTEN     0          128                     0.0.0.0:9999                0.0.0.0:*                    LISTEN     0          128              192.168.10.129:80                  0.0.0.0:*                    LISTEN     0          128                        [::]:22                     [::]:*
  1. internet服务器测试连接
[root@internet ~]$ curl www.yanlinux.org

10.0.0.100 www.yanlinux.org

[root@internet ~]$ curl www.yanlinux.org

10.0.0.18 www.yanlinux.org

  1. 健康性检测
#停掉web1的服务[root@web1 ~]$ systemctl stop apache2.service#internet测试,不会轮询到web1服务上了[root@internet ~]$ curl www.yanlinux.org

10.0.0.18 www.yanlinux.org

[root@internet ~]$ curl www.yanlinux.org

10.0.0.18 www.yanlinux.org

[root@internet ~]$ curl www.yanlinux.org

10.0.0.18 www.yanlinux.org

状态页也可以看出来web1下线了

关键词: 操作系统 安装失败 可以看出