2024年8月14日(haproxy)

一、haproxy简介 

官网https://www.haproxy.com/
自由及开放源代码软件

HAProxy是一个使用C语言编写的自由及开放源代码软件,其提供高可用性、负我均衡,以及基TCP和HTTP的应用程序代理。

HAProxy特别适用于那些负载特大的veb站点,这些站点通常又需要会活保或七层处理。HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式一与它可以很简单安全地整合进用户当前的架构中,同时可以保护用户的web服务器不被暴露到网络上。

HAProxy实现了一种事件驱动,单一进程模型,此模型支持非常大的并发连接数。多进程或多线程模型受内存限制、系统调度器限制以及无处不在的锁限制,很少能处理数千并发连接。事件驱动模型因为在有更好的资源和时间管理的用户空间(User-Space)实现所有这些任务,所以没有这些问题。此模型的弊端是,在多核系统上,这些程序通常扩展性较差。这就是为什么他们必须进行优化以使每个CPU时间片(Cycle)做更多的工作。

包括GitHub、Bitbucket、Stack Overflow、Reddit、Tumblr、Twitter5和Tuenti在内的知名网站,及亚马逊网络服务系统都使用了HAProxy。

 1、安装(时间同步软件,haproxy)

[root@haproxy ~]# yum -y install ntpdate

[root@haproxy ~]# ntpdate cn.ntp.org.cn

[root@haproxy ~]# yum -y install ntp
[root@haproxy ~]# systemctl start ntpd
[root@haproxy ~]# systemctl enable ntpd

[root@haproxy ~]# yum -y install haproxy

2、配置

[root@haproxy ~]# rpm -ql haproxy

/etc/haproxy/haproxy.cfg

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg

 63 frontend  main *:80
 67     #use_backend static          if url_static
 68     #default_backend             app
 69     default_backend             web 
 81 backend app
 82     balance     roundrobin
 83     server  app1 127.0.0.1:5001 check
 84     server  app2 127.0.0.1:5002 check
 85     server  app3 127.0.0.1:5003 check
 86     server  app4 127.0.0.1:5004 check
 87     
 88 backend web
 89         balance         roundrobin
 90         server web1     192.168.8.171:80 check  #web01的IP地址
 91         server web2     192.168.8.175:80 check  #web02的IP地址

[root@haproxy ~]# systemctl start haproxy.service
[root@haproxy ~]# systemctl enable haproxy.service

向web主机的静态页面写入东西

[root@web01 ~]# echo "web01" > /usr/share/nginx/html/index.html

[root@web02 ~]# echo "web02" > /usr/share/nginx/html/index.html
[root@haproxy ~]# curl 192.168.8.174
web02
[root@haproxy ~]# curl 192.168.8.174
web01

3、配置DNS服务器测试

[root@dns ~]# vim /var/named/lxe.com.zone 

 13 web01   A       192.168.8.166  #web01IP
 14 web02   A       192.168.8.168  #web02IP
 15 ha      A       192.168.8.174  #haproxy主机的IP地址

[root@haproxy ~]# echo "nameserver 192.168.8.147" > /etc/resolv.conf  #DNS主机IP
[root@haproxy ~]# curl ha.lxe.com
web02
[root@haproxy ~]# curl ha.lxe.com
web01

4、添加统计页面

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg

 92 # 定义web管理界面
 93    listen statistics
 94      bind *:9090                 #定义监听端口
 95      mode http                   #默认使用协议
 96      stats enable                #启用stats
 97      stats uri /hadmin?stats     #自定义统计页面的url 
 98      stats auth admin:admin      #统计页面的账号密码
 99      stats hide-version          #隐藏在统计页面上的haproxy版本信 
100      stats refresh 30s           #统计页面自动刷新时间
101      stats admin if TRUE         #如果认证通过就做管理功能,可以管理后端服务器
102      stats realm hapadmin        #统计页面密码框上提示文件,默认为haproxy\statisti cs

[root@haproxy ~]# systemctl restart haproxy 

5、负载均衡策略--加权

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg

89         balance         static-rr
 90         server web1     192.168.8.171:80 weight 8 check
 91         server web2     192.168.8.175:80 weight 2 check

[root@haproxy ~]# systemctl restart haproxy 

二、使用haproxy对mysql 主从做负载均衡

 43     mode                    tcp
 63 frontend  main *:3306
 70      default_backend             mysql
 94 backend mysql
 95         balance roundrobin
 96         server master   192.168.8.173:3306 check
 97         server slave    192.168.8.172:3306 check

三、mysql读写分离

[root@localhost ~]# pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple/ some-package
[root@localhost ~]# pip3 install pymysql

[root@localhost ~]# python3

>>> import pymysql
>>>master_conn=pymysql.connect(host="10.1.1.11",user="zhangmin",password="zhangmin",database="test",port=3306)
>>>slave_conn=pymysql.connect(host="10.1.1.12",user="zhangmin",password="zhangmin",database="test",port=3310)
>>> updatesql="update user set password='000' where username='aaa'"
>>> master_cursor.execute(updatesql)
>>> master_conn.commit()
>>> delete_sql="delete from user where username='aaa'"
>> master_cursor. execute(delete_sql)
1
>>> master_conn. commit ()

9. 执行查询slave

>>># 执行查询 获得获得slave 游标

...

>>> slave_cursor=slave_conn.cursor()
>>> sql

'select * from user'

>>> slave_cursor.execute(sql)

3

>>> slave_cursor.fetchall()
((2, 'bbb', 'bbbb'), (3, 'ccc', 'cccc'),(1004, 'ddddd', 'ddddddd'))
class rwsplit(object):
        def __init__ (self):
                print("initialized")

        def master_statment(self,sql):
                pass
        def slave_statment(self,sql):
                pass



if __name__ == "__main__":
        demo=rwsplit()
        sql=input("sign sql:")
        if sql[:6]=="select": 
                demo.slave_statment(sql)
        else:
                demo.master_statment(sql)

 

  • 19
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值