阿里云ECS部署Redis主备哨兵集群遇到的问题
一、部署
详细部署步骤:https://blog.csdn.net/lihongtai/article/details/82826809
Redis5.0版本需要注意的参数配置:https://www.cnblogs.com/ibethfy/p/9965902.html
二、遇到的问题
1、bind公网IP地址时,会出现异常:【Cannot assign requested address】
首先理解bind的含义:https://blog.csdn.net/cw_hello1/article/details/83444013
原因:这里无法bind公网IP地址,是因为ECS的本地IP是通过NAT技术转换成了公网IP,即公网IP不是ECS任意一个网卡上的IP
解决方案:bind 0.0.0.0,这样不论阿里云私网还是公网均可以访问
2、SpringBoot配置Redis主备哨兵集群后,无法连接
原因:ECS实例既绑定了私网IP地址,也绑定了公网IP地址,哨兵会将监控的Redis实例IP地址收集管理起来,设置bind 0.0.0.0后,使得哨兵会收集这两个IP地址,而连接时使用的是私网IP地址,故无法连接
解决方案:修改哨兵配置文件sentinel.conf,【sentinel announce-ip 哨兵所在的公网IP】
announce-ip配置项的官方解释如下:
# sentinel announce-ip <ip> # sentinel announce-port <port> # # The above two configuration directives are useful in environments where, # because of NAT, Sentinel is reachable from outside via a non-local address. # # When announce-ip is provided, the Sentinel will claim the specified IP address # in HELLO messages used to gossip its presence, instead of auto-detecting the # local address as it usually does. # # Similarly when announce-port is provided and is valid and non-zero, Sentinel # will announce the specified TCP port. # # The two options don‘t need to be used together, if only announce-ip is # provided, the Sentinel will announce the specified IP and the server port # as specified by the "port" option. If only announce-port is provided, the # Sentinel will announce the auto-detected local IP and the specified port.
大致意思是:考虑到NAT网络的存在,设置非本地IP后,哨兵可以正常地被外部访问
同时,哨兵启动后,通过观察配置文件sentinel.conf,发现已不会收集Redis实例的私网IP
最后SpringBoot可以正常连接Redis主备哨兵集群
注:以上场景为一主三备两哨兵,一主一备一哨兵在同一个ECS实例上,两备一哨兵在另一个ECS实例上,这两个ECS实例在同一个区域,属于同一个私有网段,且均绑定了公网IP