1、总部署图
我们需要四台虚拟机,代理服务器上安装nginx,服务器上安装redis,最后通过客户端访问以实现负载均衡
2、具体步骤
2.1 代理服务器编译安装nginx
安装完成
2.2 后端服务器需要安装redis
在这里我们选择yum安装
#安装两台redis服务器
[root@centos8 ~]# yum -y install redis
[root@centos8 ~]# sed -i '/^bind /c bind 0.0.0.0' /etc/redis.conf
[root@centos8 ~]# systemctl enable --now redis
[root@centos8 ~]# ss -tnl | grep 6379
LISTEN 0 128 *:6379 *:*
2.3 修改nginx的配置文件
[root@localhost tcp]# vim /apps/nginx/conf/nginx.conf
[root@localhost tcp]# vim tcp.conf
stream{
upstream redis_server{
server 192.168.107.193:6379;
server 192.168.107.192:6379;
}
server {
listen 192.168.107.190:6379;
proxy_pass redis_server;
}
}
刷新配置文件
[root@localhost tcp]# 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@localhost tcp]# nginx -s reload
2.4 测试访问
具体测试操作
在两台redis服务器上的操作
[root@localhost ~]# redis-cli
127.0.0.1:6379> set name def
OK
127.0.0.1:6379> get name
"def"
127.0.0.1:6379>
[root@localhost ~]# redis-cli
127.0.0.1:6379> set name abc
OK
127.0.0.1:6379> get name
"abc"
127.0.0.1:6379>
测试结果:
[root@localhost ~]# redis-cli -h 192.168.107.190
192.168.107.190:6379> get name
"abc"
192.168.107.190:6379> get name
"abc"
192.168.107.190:6379> get name
"abc"
192.168.107.190:6379> quit
[root@localhost ~]# redis-cli -h 192.168.107.190
192.168.107.190:6379> get name
"def"
192.168.107.190:6379>
#负载均衡验证成功