[实验] HAproxy 代理的设置

纪念:站主于 2021 年 2 月完成了此开源实验,并将过程中的所有命令经过整理和主是以后,形成以下教程

步骤目录:

步骤一:拓扑规划
1.1 服务器列表
1.2 拓扑图
1.3 拓扑图简介

步骤二: 系统环境要求

步骤三:部署集群环境
3.1 在 proxy 上安装 HAporxy
3.2 在 web1 上安装 web 服务
3.2.1 安装 httpd
3.2.2 创建网页文件
3.2.3 启动 web 服务并设置为开机自启
3.3 在 web2 上安装 web 服务
3.3.1 安装 httpd
3.3.2 创建网页文件
3.3.3 启动 web 服务并设置为开机自启

步骤四:配置 HAproxy 实现 web 负载均衡代理集群
4.1 开启 proxy 的路由转发
4.1.1 在 sysctl.conf 文件里添加路由转发功能
4.1.2 使刚刚添加的功能生效
4.2 修改 proxy 上的 HAproxy 配置文件
4.3 设置开机自动启动 HAproxy

步骤五:测试 Haproxy 代理集群

具体的操作步骤:

步骤一:拓扑规划
1.1 服务器列表

client eth0:10.0.0.10/24
proxy eth0:10.0.0.5/24
eth1:10.0.1.5/24
web1 eth1:10.0.1.100/24
web2 eht1:10.0.1.200/24

1.2 拓扑图

                                           web1
                                           eth1:10.0.1.100/24
     client                proxy     
eth0:10.0.0.10/24    eth0:10.0.0.5/24  
                     eth1:10.0.1.5/24
                                           web2
                                           eht2:10.0.1.200/24

1.3 拓扑图简介

(1)client 向 proxy 的 eth0:10.0.0.5/24 发送 web 请求
(2)proxy 收到 web 请求后通过 eth1:10.0.1.5/24 将请求发往 web1 或 web2
(3)web1 或 web2 回应 web 请求,并通过 proxy 返回给 client
(4)最终实现单点代理器,双网站热备份

步骤二: 系统环境要求

(1)所有服务器的系统都需要是 CentOS 8 版本
(2)所有服务器都要关闭防火墙
(3)所有服务器都要关闭 SELinux
(4)所有服务器系统都要配置好可用的软件源
(5)需要按照拓扑图给对应的服务器配置好 IP 地址和主机名
(6)所有服务器都要可以相互 ping 通自己和对方的 IP 地址

步骤三:部署集群环境
3.1 在 proxy 上安装 HAporxy

(只在 proxy 上执行以下步骤)

# yum -y install haproxy

3.2 在 web1 上安装 web 服务
3.2.1 安装 httpd

(只在 web1 上执行以下步骤)

# yum -y install httpd

3.2.2 创建网页文件

(只在 web1 上执行以下步骤)

# echo "10.0.1.100" > /var/www/html/index.html

3.2.3 启动 web 服务并设置为开机自启

(只在 web1 上执行以下步骤)

# systemctl start httpd ; systemctl enable httpd

3.3 在 web2 上安装 web 服务
3.3.1 安装 httpd

(只在 web2 上执行以下步骤)

# yum -y install httpd

3.3.2 创建网页文件

(只在 web2 上执行以下步骤)

# echo "10.0.1.200" > /var/www/html/index.html

3.3.3 启动 web 服务并设置为开机自启

(只在 web2 上执行以下步骤)

# systemctl start httpd ; systemctl enable httpd

步骤四:配置 HAproxy 实现 web 负载均衡代理菌群
4.1 开启 proxy 的路由转发
4.1.1 在 sysctl.conf 文件里添加路由转发功能

(只在 proxy 上执行以下步骤)

# vim /etc/sysctl.conf

添加以下内容:

net.ipv4.ip_forward = 1

4.1.2 使刚刚添加的功能生效

(只在 proxy 上执行以下步骤)

# sysctl -p

4.2 修改 proxy 上的 HAproxy 配置文件

(只在 proxy 上执行以下步骤)

# vim /etc/haproxy/haproxy.cfg

将全部内容修改如下:

#---------------------------------------------------------------------
# Example configuration for a possible web application.  See the
# full configuration options online.
#
#   https://www.haproxy.org/download/1.8/doc/configuration.txt
#
#---------------------------------------------------------------------

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
    # to have these messages end up in /var/log/haproxy.log you will
    # need to:
    #
    # 1) configure syslog to accept network log events.  This is done
    #    by adding the '-r' option to the SYSLOGD_OPTIONS in
    #    /etc/sysconfig/syslog
    #
    # 2) configure local2 events to go to the /var/log/haproxy.log
    #   file. A line like the following can be added to
    #   /etc/sysconfig/syslog
    #
    #    local2.*                       /var/log/haproxy.log
    #
    log         127.0.0.1 local2

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon

    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats

    # utilize system-wide crypto-policies
    ssl-default-bind-ciphers PROFILE=SYSTEM
    ssl-default-server-ciphers PROFILE=SYSTEM

#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend main
    bind *:80
    acl url_static       path_beg       -i /static /images /javascript /stylesheets
    acl url_static       path_end       -i .jpg .gif .png .css .js

    use_backend static          if url_static
    default_backend             app

#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static
    balance     roundrobin
    server      static 127.0.0.1:80 check

#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
    balance     roundrobin
    server  app1 10.0.1.100:80 check
    server  app2 10.0.1.200:80 check

4.3 设置开机自动启动 HAproxy

(只在 proxy 上执行以下步骤)

# systemctl start haproxy ; systemctl enable haproxy

步骤五:测试 Haproxy 代理集群

(只在 client 上执行以下步骤)

# curl 10.10.10.5

(注意:这一步需要多做几次)

[内容] Nginx 代理的设置 (http 和 SSH)

注意:

在设置 Nginx 代理之前要先安装 Nginx

正文:

内容一:设置 Nginx http 代理

# vi /usr/local/nginx/conf/nginx.conf

将部分内容修改如下:

......
http {
.....
upstream webserver {
   server 192.168.1.100:80;
   server 192.168.1.200:80;
}
.....
server {
listen 80;
server_name www.eternalcenter.com;
location / {
proxy_pass http://webserver;
}
......
}
......
}

或者:

......
http {
.....
upstream webserver {
Server    192.168.2.100    weight=1    max_fails=1  fail_timeout=30;
Server    192.168.2.200    weight=2    max_fails=2  fail_timeout=30;
Server    192.168.2.101    down;
}
.....
server {
listen 80;
server_name www.eternalcenter.com;
location / {
proxy_pass http://webserver;
}
......
}
......
}

内容二:设置 Nginx SSH 代理

将部分内容修改如下:

stream {
upstream backend {
server 192.168.1.100:22;
server 192.168.1.200:22;
}
server{
listen 222;
proxy_connect_timeout 1s;
proxy_pass backend;
}
}

http{
......
}