nginx四层负载均衡:redis和mysql
Posted y_zilong
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx四层负载均衡:redis和mysql相关的知识,希望对你有一定的参考价值。
如果编译安装,需要指定 --with-stream 选项才能支持ngx_stream_proxy_module模块
负载均衡:redis
后端服务器安装redis
#安装两台redis服务器
[root@cent8_yzl_10 ~]# yum -y install redis
[root@cent8_yzl_10 ~]# grep '^bind' /etc/redis.conf
bind 127.0.0.1
[root@cent8_yzl_10 ~]# sed -i '/^bind /c bind 0.0.0.0' /etc/redis.conf
[root@cent8_yzl_10 ~]# grep '^bind' /etc/redis.conf
bind 0.0.0.0
[root@cent8_yzl_10 ~]# systemctl enable --now redis
Created symlink /etc/systemd/system/multi-user.target.wants/redis.service → /usr/lib/systemd/system/redis.service.
[root@cent8_yzl_10 ~]# ss -ntl |grep 6379
LISTEN 0 128 0.0.0.0:6379 0.0.0.0:*
[root@cent8_yzl_10 ~]#
nginx配置
[root@nginx ~]# vim /apps/nginx/conf/nginx.conf
include /apps/nginx/conf/tcp/tcp.conf; #注意此处的include与http模块平级
[root@nginx ~]# mkdir /apps/nginx/conf/tcp
[root@nginx ~]# cat /apps/nginx/conf/tcp/tcp.conf
stream {
upstream redis_server {
server 10.0.0.10:6379 max_fails=3 fail_timeout=30s;
server 10.0.0.20:6379 max_fails=3 fail_timeout=30s;
}
server {
listen 10.0.0.30:6379;
proxy_connect_timeout 3s;
proxy_timeout 3s;
proxy_pass redis_server;
}
}
[root@nginx ~]#
#重启nginx并访问测试
[root@nginx ~]# systemctl restart nginx
[root@nginx ~]# ss -ntl |grep 6379
LISTEN 0 128 10.0.0.30:6379 0.0.0.0:*
[root@nginx ~]#
#测试通过nginx负载连接redis
[root@cent8_yzl_10 ~]# redis-cli
127.0.0.1:6379> set class yzl_10
OK
127.0.0.1:6379> get class
"yzl_10"
127.0.0.1:6379> exit
[root@cent8_yzl_10 ~]#
[root@web ~]# redis-cli -h 10.0.0.30 get class
"yzl_10"
[root@web ~]# redis-cli -h 10.0.0.30 get class
(nil)
[root@web ~]# redis-cli -h 10.0.0.30 get class
"yzl_10"
[root@web ~]#
负载均衡:mysql
后端服务器安装mysql
#先修改后端两个服务器的主机名,方便测试
[root@cent8_yzl_10 ~]# hostnamectl set-hostname mysql-server1.example.com
[root@cent8_yzl_20 ~]# hostnamectl set-hostname mysql-server2.example.com
#安装mysql服务
[root@mysql-server1 ~]# yum install -y mysql-server
[root@mysql-server1 ~]# systemctl enable --now mysqld
[root@mysql-server1 ~]# mysql
mysql> create user yzl@'10.0.0.%' identified by 'redhat';
mysql> flush privileges;
mysql> exit;
Bye
[root@mysql-server1 ~]#
nginx配置
[root@nginx ~]# cat /apps/nginx/conf/tcp/tcp.conf
stream {
upstream redis_server {
server 10.0.0.10:6379 max_fails=3 fail_timeout=30s;
server 10.0.0.20:6379 max_fails=3 fail_timeout=30s;
}
upstream mysql_server {
server 10.0.0.10:3306 max_fails=3 fail_timeout=30s;
server 10.0.0.20:3306 max_fails=3 fail_timeout=30s;
}
server {
listen 10.0.0.30:6379;
proxy_connect_timeout 3s;
proxy_timeout 3s;
proxy_pass redis_server;
}
server {
listen 10.0.0.30:3306;
proxy_pass mysql_server;
}
}
[root@nginx ~]#
#重启nginx并访问测试
[root@nginx ~]# systemctl restart nginx
#测试通过nginx负载连接mysql
[root@web ~]# mysql -uyzl -predhat -h 10.0.0.30 -e 'show variables like "hostname"'
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+---------------------------+
| Variable_name | Value |
+---------------+---------------------------+
| hostname | mysql-server1.example.com |
+---------------+---------------------------+
[root@web ~]# mysql -uyzl -predhat -h 10.0.0.30 -e 'show variables like "hostname"'
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+---------------------------+
| Variable_name | Value |
+---------------+---------------------------+
| hostname | mysql-server2.example.com |
+---------------+---------------------------+
[root@web ~]#
#在10.0.0.20上停止mysql服务
[root@mysql-server2 ~]# systemctl stop mysqld
#再次测试访问,只会看到mysql-server1.example.com进行响应
[root@web ~]# mysql -uyzl -predhat -h 10.0.0.30 -e 'show variables like "hostname"'
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+---------------------------+
| Variable_name | Value |
+---------------+---------------------------+
| hostname | mysql-server1.example.com |
+---------------+---------------------------+
[root@web ~]# mysql -uyzl -predhat -h 10.0.0.30 -e 'show variables like "hostname"'
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+---------------------------+
| Variable_name | Value |
+---------------+---------------------------+
| hostname | mysql-server1.example.com |
+---------------+---------------------------+
[root@web ~]#
以上是关于nginx四层负载均衡:redis和mysql的主要内容,如果未能解决你的问题,请参考以下文章