HAProxy-实现四层负载

针对有特殊访问写完的应用场景

MySQL
Redis
Memcache
RabbitMQ

四层负载示例

注意:如果使用frontend和backend,一定在frontend和backedn段中都指定mode tcp

listen redis-port
    bind 10.0.0.7:6379
    mode tcp
    balance leastconn
    server server1 10.0.0.17:6379 check
    server server1 10.0.0.27:6379 check backup

范例:对 MySQL 服务实现四层负载

[root@centos7 ~]#vim /etc/haproxy/haproxy.cfg
listen magedu_mysql
    bind 10.0.0.7:3306
    mode tcp
    balance leastconn
    server mysql1 10.0.0.17:3306 check  
    server mysql2 10.0.0.27 check           #不写端口号,可以转发,但无法check状态

#或者使用frontend和backend实现
frontend mysql
        bind :3306
        mode tcp                            #必须指定tcp模式
        default_backend mysqlsrvs
backend mysqlsrvs
        mode tcp                            #必须指定tcp模式
        balance leastconn
        server mysql1 10.0.0.17:3306
        server mysql2 10.0.0.27:3306

[root@centos7 ~]#systemctl restart haproxy

#在后端服务器安装和配置mariadb服务
[root@centos7 ~]#yum -y install mariadb-server
[root@centos7 ~]#mysql -e "grant all on *.* to test@'10.0.0.%' identified by '123456'"
[root@centos7 ~]#vim /etc/my.cnf
[mysqld]
server-id=17 #在另一台主机为27
[root@centos7 ~]#systemctl start mariadb

#测试
[root@centos6 ~]#mysql -utest -p123456 -e "show variables like 'hostname'"
+---------------+--------------------------+
| Variable_name | Value                    |
+---------------+--------------------------+
| hostname      | centos17.wangxiaochu.com |
+---------------+--------------------------+
[root@centos6 ~]#mysql -utest -p123456 -e "show variables like 'hostname'"
+---------------+--------------------------+
| Variable_name | Value                    |
+---------------+--------------------------+
| hostname      | centos27.wangxiaochu.com |
+---------------+--------------------------+

[root@centos6 ~]#mysql -utest -p123456 -h10.0.0.7 -e 'select @@server_id'
+-------------+
| @@server_id |
+-------------+
|          17 |
+-------------+
[root@centos6 ~]#mysql -utest -p123456 -h10.0.0.7 -e 'select @@server_id'
+-------------+
| @@server_id |
+-------------+
|          27 |
+-------------+

HAProxy-实现四层负载插图

ACL示例-四层访问控制

frontend  web_host
  bind 10.0.0.7:80
  mode http
  balance  roundrobin
  log global
  option httplog
###################### acl setting ###############################  
  acl static_path  path_beg  -i  /static /images /javascript
  acl invalid_src src 192.168.1.0/24 10.0.0.8
###################### acl hosts #################################
  use_backend static_path_host if  HTTP_1.1 TRUE static_path
  tcp-request connection reject if invalid_src     #四层ACL控制  
  default_backend default_web 
################### backend hosts ################################
backend php_server_host
  mode http
  server web1 10.0.0.17 check inter 2000 fall 3 rise 5

backend static_path_host
  mode http
  server web1 10.0.0.27 check inter 2000 fall 3 rise 5

backend default_web
  mode http
  server web1 10.0.0.37:80 check inter 2000 fall 3 rise 5

本文链接:http://www.yunweipai.com/35312.html

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/52708.html

(0)
上一篇 2021年8月6日
下一篇 2021年8月6日

相关推荐

发表回复

登录后才能评论