nginx四层负载均衡实践の实现MySQL负载均衡

nginx四层负载均衡实现主要就是stream模块

1)stream模块官方说明:
http://nginx.org/en/docs/stream/ngx_stream_core_module.html#stream
The ngx_stream_core_module module is available since version 1.9.0. This module is not built by default, it should be enabled with the --with-stream configuration parameter.
2)检查nginx支持--with-stream情况
nginx -V 2>&1 |grep -w "\-\-with-stream"
3)stream模块语法
Syntax:    stream { ... }
Default:    —
Context:    main

nginx四层负载均衡实践:

1)环境
tcp-lb01 10.0.0.15
tcp-lb02 10.0.0.16
2)安装nginx+keepalived
3)生产场景1:实现MySQL数据库负载均衡
四层代理:10.0.0.15   172.16.1.15
DB节点:172.16.1.7  172.16.1.51 
步骤:
1.克隆10.0.0.15
2.检查负载的节点正常172.16.1.7  172.16.1.51 
3.配置tcp-lb01 10.0.0.15
4.测试
[root@tcp-lb01 ~]# vim /etc/nginx/nginx.conf +12
注意要在http标签外面加上如下几行
stream {
    include /etc/nginx/stream_conf.d/*.conf;
}
mkdir /etc/nginx/stream_conf.d/

[root@tcp-lb01 nginx]# cd /etc/nginx/stream_conf.d/
[root@tcp-lb01 stream_conf.d]# cat 01_lb4-mysql.conf 
        upstream mysql{
            server 172.16.1.51:3306;
            server 172.16.1.8:3306;  #开启mariadb
        }
        server {
            listen 3306;  #监听的机器本地不能有3306端口。
            proxy_pass mysql;
        }
nginx -t
systemctl restart nginx

注意:此场景用于代理各类tcp应用,比如,k8s集群,数据库集群,中间件应用服务器集群。
注意:
1.nginx.conf里不允许多个stream标签。
2.stream标签配置为主配置文件nginx.conf main区段。
3.可以在主配置的stream标签里嵌入include /etc/nginx/stream_conf.d/*.conf;

#安装Navicat for MySQL或使用Phpmyadmin进行测试


分享到:
关键词:NginxLinux运维MySQL

网友留言(0 条)

发表评论