集群基础3——haproxy负载均衡apache

文章目录

一、环境说明

  • 使用haproxy对apache进行负载均衡。
主机IP 角色 安装服务
192.168.161.131 后端服务器1 httpd,80端口
192.168.161.132 后端服务器2 httpd,8080端口
192.168.161.133 调度服务器 haproxy,8189端口

二、安装配置httpd

  • 参考文章,需要对两台后端服务器安装httpd服务,并配置https。

1.使用http访问。

2.使用https访问。

三、安装配置haproxy

1.安装依赖包,创建系统用户。

复制代码
//安装依赖包。
yum -y install make gcc pcre-devel bzip2-devel openssl-devel systemd-devel

//创建用户。
useradd -r -M -s /sbin/nologin haproxy

2.解压安装包,编译安装。

复制代码
tar zxf haproxy-2.9-dev1.tar.gz 
cd haproxy-2.9-dev1

//编译安装。
make -j $(grep 'processor' /proc/cpuinfo |wc -l)  \
TARGET=linux-glibc  \
USE_OPENSSL=1  \
USE_ZLIB=1  \
USE_PCRE=1  \
USE_SYSTEMD=1

make install PREFIX=/usr/local/haproxy

3.配置各个负载的内核参数。

复制代码
echo 'net.ipv4.ip_nonlocal_bind = 1' >>  /etc/sysctl.conf
echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf

//重新读取。
sysctl -p

4.提供配置文件。

复制代码
mkdir /etc/haproxy

//添加以下内容。
cat > /etc/haproxy/haproxy.cfg <<EOF
#--------------全局配置----------------
global
    log 127.0.0.1 local0  info
    #log loghost local0 info
    maxconn 20480
#chroot /usr/local/haproxy
    pidfile /var/run/haproxy.pid
    #maxconn 4000
    user haproxy
    group haproxy
    daemon
#---------------------------------------------------------------------
#common defaults that all the 'listen' and 'backend' sections will
#use if not designated in their block
#---------------------------------------------------------------------
defaults
    mode http
    log global
    option dontlognull
    option httpclose
    option httplog
    #option forwardfor
    option redispatch
    balance roundrobin
    timeout connect 10s
    timeout client 10s
    timeout server 10s
    timeout check 10s
    maxconn 60000
    retries 3
#--------------统计页面配置------------------
listen admin_stats
    bind 0.0.0.0:8189
    stats enable
    mode http
    log global
    stats uri /haproxy_stats
    stats realm Haproxy\ Statistics
    stats auth admin:admin
    #stats hide-version
    stats admin if TRUE
    stats refresh 30s
#---------------web设置-----------------------
listen webcluster
    bind 0.0.0.0:80
    mode http
    #option httpchk GET /index.html
    log global
    maxconn 3000
    balance roundrobin
    cookie SESSION_COOKIE insert indirect nocache
    server web01 172.16.103.130:80 check inter 2000 fall 5
    #server web01 192.168.80.102:80 cookie web01 check inter 2000 fall 5
EOF


//修改配置文件。
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg 
#    cookie SESSION_COOKIE insert indirect nocache         //将其注释掉
    server web01 192.168.161.131:80 check inter 2000 fall 5  //添加后端服务器ip,注意这里的80端口就是httpd的监听端口。
    server web02 192.168.161.132:80 check inter 2000 fall 5

5.设置系统服务。

复制代码
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 /var/run/haproxy.pid
ExecReload=/bin/kill -USR2 $MAINPID

[Install]
WantedBy=multi-user.target
EOF

//重新加载。
systemctl daemon-reload

6.启用日志。

复制代码
vim /etc/rsyslog.conf 
local0.*       /var/log/haproxy.log    //添加此行

//重启服务。
systemctl restart rsyslog

7.启动haproxy。

复制代码
systemctl start haproxy

四、验证http负载均衡

1.使用harproxy的IP访问网页,访问一次显示RS1,刷新一下显示RS2,再刷新一下RS1。

2.命令访问。

五、配置https负载均衡

1.需要提前配置两台后端服务器的httpd为https模式。

2.修改haproxy配置文件。

复制代码
vim /etc/haproxy/haproxy.cfg 
#---------------------------------------------------------------------
defaults
    mode tcp                //将此处改成tcp协议
    
#---------------web设置-----------------------
listen webcluster
    bind 0.0.0.0:443       //将端口改成443
    mode tcp               //使用tcp协议          

#    cookie SESSION_COOKIE insert indirect nocache
    server web01 192.168.161.131:443 check inter 2000 fall 5     //将端口改成443
    server web02 192.168.161.132:443 check inter 2000 fall 5


//重启服务。
systemctl restart haproxy

3.验证。

六、haproxy网页监控

6.1 监控参数详解

1.可以通过haproxy的web页面,查看监听负载均衡集群状态,包括调度器、后端服务器。

2.监控页面参数详解。

复制代码
#####################################################
Queue列表
Cur: current queued requests //当前的队列请求数量
Max:max queued requests     //最大的队列请求数量
Limit:           //队列限制数量
#####################################################
Session rate (每秒的连接回话列表)
scur: current sessions        //每秒的当前回话的限制数量
smax: max sessions           //每秒的新的最大的回话量
slim: sessions limit           //每秒的新回话的限制数量
#####################################################
Sessions 
Total:            //总共回话量
Cur:             //当前的回话
Max:     //最大回话 
Limit:    //回话限制
Lbtot:   //选中一台服务器所用的总时间
#####################################################
Bytes
In: //网络的字节数输入总量  
Out: //网络的字节数输出总量
#####################################################
Denied
Req:  //拒绝请求量
Resp: //拒绝回应
#####################################################
Errors
Req:request errors             //错误请求
Conn:connection errors          //错误的连接
Resp: response errors (among which srv_abrt)  ///错误的回应
#####################################################
Warnings
Retr: retries (warning)                      //重新尝试
Redis:redispatches (warning)               //再次发送
#####################################################
Server列表
Status:状态,包括up(后端机活动)和down(后端机挂掉)两种状态
LastChk:    持续检查后端服务器的时间
Wght: (weight) : 权重
Act: server is active (server), number of active servers (backend) //活动链接数量
Bck: server is backup (server), number of backup servers (backend) //backup:备份的服务器数量
Down:          //后端服务器连接后都是down的数量
Downtime: downtime: total downtime (in seconds)    //总的downtime 时间
Throttle: warm up status                          //设备变热状态

6.2 页面操作

1.查看RS1的httpd服务状态。

2.手动关闭网页上的服务。

3.过一会,页面显示的服务自动起来了。

4.关闭服务器上的服务。

5.查看监控网页状态,显示该服务已宕机。此时即时在网页上启动该服务,也没有效果,只能在服务器上启动服务。

相关推荐
AlfredZhao12 小时前
vi 删除指定范围的行,不用再反复按 dd
linux·vi
用户97183563346618 小时前
银河麒麟 KY10 申威(SW64) 安装 nginx-1.16.1-2.p01.ky10.sw_64.rpm 详细步骤
linux
猪脚踏浪19 小时前
linux 拷贝文件或目录到指定的位置
linux
大树881 天前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
摇滚侠1 天前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
霸道流氓气质1 天前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务
bush41 天前
嵌入式linux学习记录十四、术语
linux·嵌入式
载数而行5202 天前
Linux 11 动态监控指令top
linux
Inhand陈工2 天前
基于台达PLC与映翰通IG502的智慧水产养殖精准投喂与远程运维解决方案
运维·人工智能·物联网·阿里云·信息与通信
酣大智2 天前
ARP代理--工作原理
运维·网络·arp·arp代理