redis 单机多实例主从+哨兵模式

在生产环境中,如果想要使用Redis的哨兵模式,也会尽量使用Redis的2.8版本之后的版本。无论是主从模式,还是哨兵模式,这两个模式都有一个问题,不能水平扩容,并且这两个模式的高可用特性都会受到Master主节点内存的限制。还有一点,实现哨兵模式的配置也不简单,甚至可以说有些繁琐,所以在工业场景里这两个模式都不建议使用,如果要使用必须有相关的问题的解决方案,以免后续带来的问题。
安装步骤:
下载安装包:

[root@localhost ~]# cd /usr/local/src/
[root@localhost src]# wget http://download.redis.io/releases/redis-5.0.2.tar.gz

安装依赖

[root@localhost src]# yum -y install gcc gcc-c++

解压安装

[root@localhost src]# tar zxf redis-5.0.2.tar.gz 
[root@localhost src]# cd redis-5.0.2
[root@localhost redis-5.0.2]# make MALLOC=libc && make install

配置redis一主两从

[root@localhost redis-5.0.2]# cp redis.conf /etc/redis_6379.conf
[root@localhost redis-5.0.2]# cp redis.conf /etc/redis_6380.conf
[root@localhost redis-5.0.2]# cp redis.conf /etc/redis_6381.conf

修改配置文件

#=========主============
[root@localhost redis-5.0.2]# vim /etc/redis_6379.conf
-----------------------------------------
bind 192.168.33.143      #IP地址 
daemonize yes            #后台守护
port 6379		         #端口号
-----------------------------------------
#==========从===========
[root@localhost redis-5.0.2]# vim /etc/redis_6380.conf
----------------------------------------
bind 192.168.33.143     #IP地址
daemonize yes  后台守护	#后台守护
port 6380				#端口号
pidfile /var/run/redis_6380.pid
replicaof 192.168.33.143 6379  #主节点的IP和端口   
replica-priority 100    #优先级
----------------------------------------
[root@localhost redis-5.0.2]# vim /etc/redis_6381.conf
---------------------------------------
bind 192.168.33.143     #IP地址
daemonize yes  后台守护	#后台守护
port 6381				#端口号
pidfile /var/run/redis_6381.pid
replicaof 192.168.33.143 6379  #主节点的IP和端口   
replica-priority 90    #优先级
---------------------------------------

开启服务

[root@localhost ~]# redis-server /etc/redis_6379.conf
[root@localhost ~]# redis-server /etc/redis_6380.conf
[root@localhost ~]# redis-server /etc/redis_6381.conf
#查看端口
[root@localhost ~]# netstat -anpt |grep 63

测试主从

[root@localhost ~]# redis-cli -p 6379 -h 192.168.33.143
192.168.33.143:6379> set ljh 888
OK
192.168.33.143:6379> exit

从节点提取key的值

[root@localhost ~]# redis-cli -p 6380 -h 192.168.33.143
192.168.33.143:6380> get ljh
"888"

配置哨兵模式

[root@localhost ~]# cd /usr/local/src/redis-5.0.2
[root@localhost redis-5.0.2]# cp sentinel.conf /etc/sentinel_26379.conf
[root@localhost redis-5.0.2]# cp sentinel.conf /etc/sentinel_26380.conf
[root@localhost redis-5.0.2]# cp sentinel.conf /etc/sentinel_26381.conf

修改配置文件

#==========主==============
[root@localhost redis-5.0.2]# vim /etc/sentinel_26379.conf
-------------------------
bind 127.0.0.1 192.168.33.143
port 26379
daemonize yes
pidfile /var/run/redis-sentinel_26379.pid
sentinel monitor mymaster 192.168.33.143 6379 2
--------------------------
#==========从==============
[root@localhost redis-5.0.2]# vim /etc/sentinel_26380.conf 
-------------------------
bind 127.0.0.1 192.168.33.143
port 26380
daemonize yes
pidfile /var/run/redis-sentinel_26380.pid
sentinel monitor mymaster 192.168.33.143 6379 2
-------------------------
[root@localhost redis-5.0.2]# vim /etc/sentinel_26381.conf
-------------------------
bind 127.0.0.1 192.168.33.143
port 26381
daemonize yes
pidfile /var/run/redis-sentinel_26381.pid
sentinel monitor mymaster 192.168.33.143 6379 2
-------------------------

创建存放日志目录

[root@localhost redis-5.0.2]# redis-server /etc/sentinel_26379.conf --sentinel
[root@localhost redis-5.0.2]# redis-server /etc/sentinel_26380.conf --sentinel
[root@localhost redis-5.0.2]# redis-server /etc/sentinel_26381.conf --sentinel
[root@localhost redis-5.0.2]# netstat -anpt |grep 263

验证

[root@localhost redis-5.0.2]# redis-cli -p 26379
127.0.0.1:26379> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.33.143:6379,slaves=2,sentinels=3

查看、kill掉6379端口

[root@localhost ~]# netstat -anpt |grep -w 6379
tcp        0      0 192.168.33.143:6379     0.0.0.0:*               LISTEN      12384/redis-server
##或者
[root@localhost ~]# cat /var/run/redis_6379.pid 
12384
[root@localhost ~]# kill -9 12384

主机成功转移

[root@localhost ~]# redis-cli -p 26379
127.0.0.1:26379> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.33.143:6381,slaves=2,sentinels=3

你可能感兴趣的:(redis)