B7-Haproxy 四层负载均衡

Haproxy 四层负载均衡


拓扑:                 
  app  ---->  mysql_master(write)
    |
 haproxy(read)
    |
mysql_slave*N



一 Haproxy 部分
1 haproxy 代码mysql

  
  
  
  
  1. listen  bbs_slave   10.0.100.82:3306  
  2.         mode tcp          #配置TCP模式  
  3.         maxconn 2000  
  4.         balance roundrobin  
  5.         #option httpchk OPTIONS * HTTP/1.1\r\nHost:\ www 
  6.         server  slave01 10.0.100.75:3306 check port 9120 inter 5000 rise 3 fall 3 weight 3 
  7.         server  slave02 10.0.100.76:3306 check port 9120 inter 5000 rise 3 fall 3 weight 3 
  8.         srvtimeout      20000   



二 Mysql 部分
2.1 mysql replication 监控脚本linux

  
  
  
  
  1. cat /usr/local/bin/mysqlrep_status.sh 
  2. #!/bin/bash 
  3. # /usr/local/bin/mysqlrep_status.sh 
  4. # This script checks if a mysql server is healthy running on localhost. It will 
  5. # return: 
  6. # "HTTP/1.x 200 OK\r" (if mysql is running smoothly) 
  7. # – OR – 
  8. # "HTTP/1.x 503 Internal Server Error\r" (else) 
  9.  
  10. mysql=/usr/local/bin/mysql 
  11. mysql_host="localhost" 
  12. mysql_port="3306" 
  13. mysql_username="root" 
  14. mysql_password="dongnan" 
  15. $mysql -u${mysql_username} -p${mysql_password} -e "show full processlist;" >/tmp/processlist.txt 
  16. $mysql -u${mysql_username} -p${mysql_password} -e "show slave status\G;" >/tmp/rep.txt 
  17. iostat=`awk '/Slave_IO_Running/ {print $2}' /tmp/rep.txt` 
  18. sqlstat=`awk '/Slave_SQL_Running/ {print $2}' /tmp/rep.txt`  
  19.  
  20. if [ "$iostat" = "Yes" ] && [ "$sqlstat" = "Yes" ];then 
  21.     /bin/echo -e "HTTP/1.1 200 OK\r\n" 
  22. else 
  23.     /bin/echo -e "HTTP/1.1 503 Service Unavailable\r\n" 
  24. fi 


2.2 配置 xinetd 
//定义服务,服务名必定要在 /etc/services列出ios

  
  
  
  
  1. tail -n1 /etc/services  

mysql_rep_check 9120/tcp

//定义 super daemon    算法

  
  
  
  
  1. cat >> /etc/xinetd.d/mysql_rep_check << EOF 
  2. # default: off 
  3. service mysql_rep_check 
  4.         flags           = REUSE 
  5.         socket_type     = stream 
  6.         wait            = no 
  7.         user            = nobody 
  8.         server          = /usr/local/bin/mysqlrep_status.sh 
  9.         log_on_failure  += USERID 
  10.         disable         = no 
  11.         
  12.  
  13. EOF 



2.3 重启xinetd sql

  
  
  
  
  1. /etc/init.d/xinetd restart 


2.4 xinetd 日志bash

  
  
  
  
  1. tail -n1 /var/log/messages 

Nov  8 16:44:27 one xinetd[12224]: Started working: 1 available service

2.5 查看自定义服务端口服务器

  
  
  
  
  1. lsof -i :9120 

COMMAND   PID USER   FD   TYPE  DEVICE SIZE NODE NAME
xinetd  12224 root    5u  IPv4 1078320       TCP *:mysql_rep_check (LISTEN)

2.6 测试app

  
  
  
  
  1. telnet 127.0.0.1 9120 

Trying 127.0.0.1...
Connected to one.test.com (127.0.0.1).
Escape character is '^]'.
HTTP/1.1 200 OK

Connection closed by foreign host.

流程
(1)应用服务器
(2)链接HAProxy的10.0.100.82:3306,根据算法,分配到一台mysql slave。
(3)检测slave的9120端口是否返回http 200状态码
(4)返回200 状态码,HAProxy 返回正常,继续服务
(5)返回503,剔除该slave,并将mysql请求转发到另一台slave


参考
基于Keepalived+Haproxy搭建四层负载均衡器

MySQL从库集群方案之HAProxy篇


更多请:
linux 相关  274134275 , 37275208(已满)
vmware 虚拟化相关  166682360负载均衡

相关文章
相关标签/搜索