当前位置:首页 > 技术知识 > 正文内容

使用haproxy实现负载均衡_负载均衡hash rr

maynowei7个月前 (09-14)技术知识98

场景描述

后端服务有三个节点(node1,node2,node3),需要通过haproxy实现负载均衡,服务器具采用ubuntu操作系统。

部署步骤

1、安装haproxy

apt-get install haproxy

2、修改配置文件

vi /etc/haproxy/haproxy.cfg

global
        log /dev/log    local0
        log /dev/log    local1 notice
        chroot /var/lib/haproxy
        stats socket /run/haproxy/admin.sock mode 660 level admin expose-fd listeners
        stats timeout 30s
        user haproxy
        group haproxy
        daemon
 
        # Default SSL material locations
        ca-base /etc/ssl/certs
        crt-base /etc/ssl/private
 
        # Default ciphers to use on SSL-enabled listening sockets.
        # For more information, see ciphers(1SSL). This list is from:
        #  https://hynek.me/articles/hardening-your-web-servers-ssl-ciphers/
        # An alternative list with additional directives can be obtained from
        #  https://mozilla.github.io/server-side-tls/ssl-config-generator/?server=haproxy
        ssl-default-bind-ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:RSA+AESGCM:RSA+AES:!aNULL:!MD5:!DSS
        ssl-default-bind-options no-sslv3
 
defaults
        log     global
        mode    http
        option  httplog
        option  dontlognull
        timeout connect 5000
        timeout client  50000
        timeout server  50000
        errorfile 400 /etc/haproxy/errors/400.http
        errorfile 403 /etc/haproxy/errors/403.http
        errorfile 408 /etc/haproxy/errors/408.http
        errorfile 500 /etc/haproxy/errors/500.http
        errorfile 502 /etc/haproxy/errors/502.http
        errorfile 503 /etc/haproxy/errors/503.http
        errorfile 504 /etc/haproxy/errors/504.http
 
listen stats
        bind 0.0.0.0:1080  
        stats refresh 30s 
        stats uri /stats 
        stats realm Haproxy Manager  
        stats auth admin:admin
 
frontend main
        bind 0.0.0.0:80
        acl url_static path_beg -i /static /images /javascript /stylesheets
        acl url_static path_end -i .jpg .gif .png .css .js 
        default_backend webservice
 
backend webservice
        balance leastconn
        server web1 node1:80 check
        server web2 node2:80 check
        server web3 node3:80 check

3、加载配置文件使生效

/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg

配置说明:

  • 1、frontend main
    bind 0.0.0.0:80
    acl url_static path_beg -i /static /images /javascript /stylesheets
    acl url_static path_end -i .jpg .gif .png .css .js 
    default_backend
    webservice

这里default_backend webservice需要和后端backend 保持一致

  • 2、backend webservice
    balance leastconn
    server web1
    node1:80 check
    server web2
    node2:80 check
    server web3
    node3:80 check

HAProxy还提供了其他类型的负载均衡:

oleastconn:每次将服务转发至连接数最少的服务器。

osource:对源IP地址进行哈希处理,用运行中服务器的总权重除以哈希值,即可决定哪台服务器将接收请求。

ouri:URI的左边部分(问号前面)经哈希处理,用运行中服务器的总权重除以哈希值。所得结果决定哪台服务器将接收请求。

ourl_param:变量中指定的URL参数将在每个HTTP GET请求的查询串中进行查询。你基本上可以将使用蓄意制作的URL(crafted URL)的请求锁定于特定的负载均衡节点。

ohdr(name):HTTP头<name> 将在每个HTTP请求中进行查询,被定向到特定节点。

这里配置后端服务,端口可在具体每个server 上再用nginx做反向代理

相关文章

伪装成抖音国际版Tiktok的短信蠕虫

概述近期监测到一款仿冒Tiktok的短信蠕虫,该短信蠕虫最明显的特点就是针对Android系统版本高于6.0以上的设备,由于Android版本的更新迭代,现在大部分设备已经更新到较高的版本,通过不完全...

掌握C语言多线程:高效并发编程指南

一、多线程基础概念介绍多线程编程是现代软件开发中提高程序性能和响应性的重要技术。在C语言中,pthread(POSIX Threads)库是实现多线程编程的标准工具。本节将通俗易懂地介绍多线程的核心概...

从 async/await 到虚拟线程:Python 并发的再思考

演进之路:从async/await到线程的反思首先必须明确的是,async/await对Python并非全无裨益:它最大的价值,是让更多人接触到了并发编程。通过在编程语言中嵌入语法元素,并发编程的门槛...

大厂 Go 编程规范(二):mutex(编程大厂是什么意思)

mutex 是golang 的互斥锁,可以保障在多协程的情况下,数据访问的安全。1、零值有效我们并不需要mutex指针mu := new(sync.Mutex) mu.Lock()直接可以使用mute...

如何在Go中同步线程(go语言同步锁)

单线程代码已经带来头痛。添加第二个线程,就是从基础头痛升级了。解决方案?互斥锁:线程和数据的交通警察。一旦你理解了它们,线程同步就变成了第二本能,语言无关。在C++和Go中工作,我遇到过所有常见的混乱...

python-oracledb——利用python连接Oracle数据库的好用方法

这篇文章最早发布在CSDN了,最近想尝试使用一下头条,重新转移过来了。背景介绍之前使用的数据库一直是MySql,偶尔使用PostgreSQL,都是利用的数据库连接池使用;最近需要在Oracle数据库取...