62-Ubuntu-NGINX-TCP负载均衡

本章衔接上期博客内容

Nginx在1.9.0版本开始支持tcp模式的负载均衡,在1.9.13版本开始支持udp协议的负载,udp主要用于DNS的域名解析,其配置方式和指令和http 代理类似,其基于ngx_stream_proxy_module模块实现tcp负载,另外基于模块ngx_stream_upstream_module实现后端服务器分组转发、权重分配、状态监测、调度算法等高级功能。

官方文档

tcp负载均衡配置参数:

stream { #定义stream
    upstream backend { #定义后端服务器
          hash $remote_addr consistent; #定义调度算法

             server backend1.example.com:12345 weight=5; #定义具体server
             server 127.0.0.1:12345 max_fails=3 fail_timeout=30s;
             server unix:/tmp/backend3;
     }
     upstream dns { #定义后端服务器
             server 192.168.0.1:53535; #定义具体server
             server dns.example.com:53;
     }

     server { #定义server
             listen 12345; #监听IP:PORT
             proxy_connect_timeout 1s; #连接超时时间
             proxy_timeout 3s; #转发超时时间
             proxy_pass backend; #转发到具体服务器组
     }

     server {
             listen 127.0.0.1:53 udp reuseport;
             proxy_timeout 20s;
             proxy_pass dns;
     }

     server {
             listen [::1]:12345;
             proxy_pass unix:/tmp/stream.socket;
     }
}

负载均衡实例–Redis:

#后端web服务
✘root@U8-2: ~✘# apt -y install redis
✘root@U8-2: ~✘# vim /etc/redis/redis.conf
#修改该行
bind 0.0.0.0

✘root@U8-2: ~✘# systemctl start redis
✘root@U8-2: ~✘# lsof -i:6379

COMMAND    PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
redis-ser 2739 redis    6u  IPv4  93193      0t0  TCP localhost:6379 (LISTEN)
redis-ser 2739 redis    7u  IPv6  93196      0t0  TCP localhost:6379 (LISTEN)

#NGINX代理服务器
[root@U8: ~]# mkdir /apps/nginx/conf/tcp
[root@U8: ~]# vim /apps/nginx/conf/tcp/tcp.conf

stream {
  upstream redis_server {
    server 192.168.124.32:6379 max_fails=3 fail_timeout=30s;
  }

  server {
    listen 192.168.124.30:6379;
    proxy_connect_timeout 3s; 
    proxy_timeout 3s; 
    proxy_pass redis_server;
  }                                                                                                                      
}

[root@U8: ~]# vim /apps/nginx/conf/nginx.conf
#添加此行
include /apps/nginx/conf/tcp/tcp.conf; #此处的include与http模块平级

在这里插入图片描述

[root@U8: ~]# /apps/nginx/sbin/nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@U8: ~]# /apps/nginx/sbin/nginx -s reload

扫描二维码关注公众号,回复: 8679932 查看本文章

[root@U8: ~]# ss -tnl | grep 6379
LISTEN 0 511 192.168.124.30:6379 0.0.0.0:*

#测试通过nginx 负载连接redis:
✘root@U8-2: ~✘# redis-cli -h 192.168.124.30
192.168.124.30:6379> set name dushansao
OK
192.168.124.30:6379> get name
“dushansao”
192.168.124.30:6379>


负载均衡实例:MySQL

#后端web服务安装mysql
✘root@U8-2: ~✘# apt -y install mariadb-server
✘root@U8-2: ~✘# systemctl start mariadb

#启动安全初始脚本,设置mysql管理员密码:123456
✘root@U8-2: ~✘# mysql_secure_installation

#登录maraidb
✘root@U8-2: ~✘# mysql -uroot -p123456

MariaDB [(none)]> GRANT ALL PRIVILEGES ON . TO ‘root’@’%’ IDENTIFIED BY ‘123456’ WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

#NGINX代理服务器
[root@U8: ~]# vim /apps/nginx/conf/tcp/tcp.conf

stream {                                                                                                                 
  upstream redis_server {
    server 192.168.124.32:6379 max_fails=3 fail_timeout=30s;
  }
  
  upstream mysql_server {
    least_conn;
    server 192.168.124.32:3306 max_fails=3 fail_timeout=30s;
  }

  server {
    listen 192.168.124.30:3306;
    proxy_connect_timeout 6s; 
    proxy_timeout 15s;
    proxy_pass mysql_server;
  }

  server {
    listen 192.168.124.30:6379;
    proxy_connect_timeout 3s; 
    proxy_timeout 3s; 
    proxy_pass redis_server;
  }
}

[root@U8: ~]# /apps/nginx/sbin/nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@U8: ~]# /apps/nginx/sbin/nginx -s reload

#后端web
✘root@U8-2: ~✘# vim /etc/mysql/mariadb.conf.d/50-server.cnf
#找出此行将其注释
#bind-address = 127.0.0.1

#在该行下设置超时时长
[mysqld]
wait_timeout=86400 #8小时

#重启mariadb
✘root@U8-2: ~✘# systemctl restart mariadb

#测试通过nginx负载连接MySQL:
#后端web
✘root@U8-2: ~✘# mysql -uroot -p123456 -h 192.168.124.30

MariaDB [(none)]> create database dushansao;

Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| dushansao          |
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]>

发布了63 篇原创文章 · 获赞 102 · 访问量 3533

猜你喜欢

转载自blog.csdn.net/dushansao/article/details/104015371