ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

haproxy配置负载均衡

2021-10-18 02:31:06  阅读:142  来源: 互联网

标签:haproxy 负载 service root etc 均衡 0.0 DR


haproxy配置负载均衡

haproxy介绍

HAProxy 提供高可用性、负载均衡以及基于 TCP 和 HTTP 应用的代理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案.HAProxy 特别适用于那些负载特大的 web 站点, 这些站点通常又需要会话保持或七层处理.HAProxy 运行在当前的硬件上,完全可以支持数以万计的并发连接.并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的 web 服务器不被暴露到网络上.

haproxy安装

在RS配置httpd服务
[root@RS1 ~]# systemctl disable --now firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@RS1 ~]# getenforce 
Disabled
[root@RS1 ~]# yum -y install httpd
[root@RS1 ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@RS1 ~]# echo "123" > /var/www/html/index.html

[root@RS2 ~]# systemctl disable --now firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@RS2 ~]# getenforce 
Disabled
[root@RS2 ~]# yum -y install httpd
[root@RS2 ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@RS2 ~]# echo "456" > /var/www/html/index.html

配置DR
[root@DR ~]# systemctl disable --now firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@DR ~]# getenforce 
Disabled

安装服务
[root@DR ~]# yum -y install openssl make gcc pcre-devel bzip2-devel openssl-devel systemd-devel

创建用户
[root@DR ~]# useradd -r -M -s /sbin/nologin haproxy

上传压缩包
[root@DR ~]# ls
公共  视频  文档  音乐  anaconda-ks.cfg       initial-setup-ks.cfg
模板  图片  下载  桌面  haproxy-2.4.0.tar.gz
解压压缩包
[root@DR ~]# tar xf haproxy-2.4.0.tar.gz 
[root@DR ~]# ls
公共  视频  文档  音乐  anaconda-ks.cfg  haproxy-2.4.0.tar.gz
模板  图片  下载  桌面  haproxy-2.4.0    initial-setup-ks.cfg

[root@DR haproxy-2.4.0]# ls
addons    CHANGELOG     doc       include  MAINTAINERS  reg-tests  src      VERDATE
admin     CONTRIBUTING  examples  INSTALL  Makefile     ROADMAP    SUBVERS  VERSION
BRANCHES  dev           haproxy   LICENSE  README       scripts    tests
[root@DR haproxy-2.4.0]# make clean
[root@DR haproxy-2.4.0]# make -j $(nproc) TARGET=linux-glibc USE_OPENSSL=1 USE_PCRE=1 USE_SYSTEMD=1         //过程省略
[root@DR haproxy-2.4.0]# make install PREFIX=/usr/local/haproxy

配置内核参数
[root@DR ~]# echo 'net.ipv4.ip_nonlocal_bind = 1' >>  /etc/sysctl.conf
[root@DR ~]# echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
[root@DR ~]# sysctl  -p
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1

提供配置文件
[root@DR ~]# mkdir /etc/haproxy
[root@DR ~]# cd /etc/haproxy/
[root@DR 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 192.168.145.170:80 check inter 2000 fall 5
    #server web02 192.168.145.171:80 cookie web01 check inter 2000 fall 5
EOF

haproxy.service文件编写
[root@DR ~]# cat > /usr/lib/systemd/system/haproxy.service <<EOF
[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target

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

[Install]
WantedBy=multi-user.target
EOF
[root@DR ~]# systemctl daemon-reload

开启日志
[root@DR ~]# vim /etc/rsyslog.conf
local0.* /var/log/haproxy.log       加入此行

设置开机自启
[root@DR ~]# systemctl enable --now haproxy
Created symlink /etc/systemd/system/multi-user.target.wants/haproxy.service → /usr/lib/systemd/system/haproxy.service.
[root@DR ~]# systemctl status haproxy.service 
● haproxy.service - HAProxy Load Balancer
   Loaded: loaded (/usr/lib/systemd/system/haproxy.service; enabled; vendor preset: >
   Active: active (running) since Mon 2021-10-18 02:01:59 CST; 5s ago
  Process: 106583 ExecStartPre=/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/hapro>
 Main PID: 106585 (haproxy)
    Tasks: 2 (limit: 23862)
   Memory: 7.7M
   CGroup: /system.slice/haproxy.service
           ├─106585 /usr/local/haproxy/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg >
           └─106588 /usr/local/haproxy/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg >

10月 18 02:01:59 DR systemd[1]: Starting HAProxy Load Balancer...
10月 18 02:01:59 DR systemd[1]: Started HAProxy Load Balancer.
10月 18 02:01:59 DR haproxy[106585]: [NOTICE]   (106585) : New worker #1 (106588) fo>
[root@DR ~]# ss -antl
State     Recv-Q     Send-Q          Local Address:Port         Peer Address:Port    
LISTEN    0          32              192.168.122.1:53                0.0.0.0:*       
LISTEN    0          128                   0.0.0.0:22                0.0.0.0:*       
LISTEN    0          5                   127.0.0.1:631               0.0.0.0:*       
LISTEN    0          128                   0.0.0.0:8189              0.0.0.0:*       
LISTEN    0          128                   0.0.0.0:111               0.0.0.0:*       
LISTEN    0          128                   0.0.0.0:80                0.0.0.0:*       
LISTEN    0          128                      [::]:22                   [::]:*       
LISTEN    0          5                       [::1]:631                  [::]:*       
LISTEN    0          128                      [::]:111                  [::]:*    

 

 


标签:haproxy,负载,service,root,etc,均衡,0.0,DR
来源: https://www.cnblogs.com/Aimmi/p/15419001.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有