redis-安装
linux下安装单机版redis
在虚拟机10.3.13.197上安装
redis-5.0.5.tar.gz # root @ localhost in /usr/local/redis/redis-5.0.5 make make install
启动之前,需要先做一个简单的配置:修改redis.conf文件,将里面的daemonize no 改成 yes,让服务在后台启动
# root @ localhost in /usr/local/redis/redis-5.0.5 [9:04:57] $ vim redis.conf
启动
# root @ localhost in /usr/local/redis/redis-5.0.5 [9:07:14] $ redis-server redis.conf
开启客户端
# root @ localhost in /usr/local/redis/redis-5.0.5 [9:07:47] $ redis-cli 127.0.0.1:6379>
关闭实例
# root @ localhost in /usr/local/redis/redis-5.0.5 [9:07:47] $ redis-cli 127.0.0.1:6379> shutdown not connected> exit
redis 主备模式集群安装
10.3.13.213 node1 主节点
10.3.13.197 node2 从节点
10.3.13.194 node3 从节点
解压后的到源文件,make后的到安装好的redis
# tar -xvf redis-5.0.5.tar.gz # root @ localhost in /usr/local/redis/redis-5.0.5 make make install
在主节点上,创建redis-6379.conf redis-6379.log
# root @ localhost in /usr/local/redis/redis-5.0.5 [16:02:41] $ cat redis-6379.conf daemonize yes port 6379 logfile ./redis-6379.log dir ./ bind 127.0.0.1 10.3.13.213
启动主节点
# root @ localhost in /usr/local/redis/redis-5.0.5 [16:01:23] $ ./src/redis-server redis-6379.conf # root @ localhost in /usr/local/redis/redis-5.0.5 [16:01:58] $ netstat -tulpn | grep redis tcp 0 0 10.3.13.213:6379 0.0.0.0:* LISTEN 4614/./src/redis-se tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 4614/./src/redis-se
配置从节点 10.3.13.197 10.3.13.194
同样创建文件redis-6379.conf redis-6379.log
# root @ localhost in /usr/local/redis/redis-5.0.5 [16:25:43] $ cat redis-6379.conf daemonize yes port 6379 logfile ./redis-6379.log dir ./ bind 127.0.0.1 10.3.13.197 slaveof 10.3.13.213 6379 slave-read-only yes
启动从节点
在主节点上查看
# root @ localhost in /usr/local/redis/redis-5.0.5 [16:09:28] $ redis-cli -p 6379 127.0.0.1:6379> info replication # 查看集群的各个节点的信息 # Replication role:master connected_slaves:2 slave0:ip=10.3.13.197,port=6379,state=online,offset=1274,lag=1 slave1:ip=10.3.13.194,port=6379,state=online,offset=1274,lag=1 master_replid:42147154de89ceb5da09a89d6b703d7a4822b65a master_replid2:0000000000000000000000000000000000000000 master_repl_offset:1274 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:1274 127.0.0.1:6379>
redis Sentinel哨兵模式集群
在主备模式的基础上
主节点 10.3.13.213 服务端口:6379 哨兵sentinel 端口:26379
从节点 10.3.13.197 服务端口:6379 哨兵sentinel 端口:26379
从节点 10.3.13.194 服务端口:6379 哨兵sentinel 端口:26379
每个节点下redis-6379.conf 与主备模式相同
每个节点下新建 sentinel-26379.conf
$ cat sentinel-26379.conf port 26379 daemonize yes logfile "26379.log" dir "./" #sentinel监控的IP 端口号 sentinel通过投票后认为mater宕机的数量,此处为至少2个 sentinel monitor mymaster 10.3.13.213 6379 2 #20秒ping不通主节点的信息,主观认为master宕机 sentinel down-after-milliseconds mymaster 30000 #故障转移后重新主从复制,1表示串行,>1并行 sentinel parallel-syncs mymaster 1 #故障转移开始,60秒内没有完成,则认为转移失败 sentinel failover-timeout mymaster 600000 bind 0.0.0.0 # root @ localhost in /usr/local/redis/redis-5.0.5 [9:41:14] $ cat sentinel-26379.conf port 26380 daemonize yes logfile "26380.log" dir "./" sentinel monitor mymaster 10.3.13.213 6379 2 sentinel down-after-milliseconds mymaster 30000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 600000 bind 0.0.0.0 # root @ localhost in /usr/local/redis/redis-5.0.5 [9:43:00] $ cat sentinel-26379.conf port 26381 daemonize yes logfile "26381.log" dir "./" sentinel monitor mymaster 10.3.13.213 6379 2 sentinel down-after-milliseconds mymaster 30000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 600000 bind 0.0.0.0
下面有一部分内容是后面运行时生成的。
port 26379 daemonize yes logfile "26379.log" dir "/usr/local/redis/redis-5.0.5" #sentinel监控的IP 端口号 sentinel通过投票后认为mater宕机的数量,此处为至少2个 sentinel myid 9c8174cd0159bd183bc61d897057bbc5b4147ac9 #20秒ping不通主节点的信息,主观认为master宕机 sentinel deny-scripts-reconfig yes #故障转移后重新主从复制,1表示串行,>1并行 sentinel monitor mymaster 10.3.13.213 6379 2 #故障转移开始,60秒内没有完成,则认为转移失败 sentinel failover-timeout mymaster 600000 bind 0.0.0.0 # Generated by CONFIG REWRITE protected-mode no sentinel config-epoch mymaster 0 sentinel leader-epoch mymaster 0 sentinel known-replica mymaster 10.3.13.194 6379 sentinel known-replica mymaster 10.3.13.197 6379 sentinel known-sentinel mymaster 10.3.13.197 26379 00c9ae391c9fbf9c07d0a700edbdde04ee53dbf5 sentinel current-epoch 0
在每个机器上启动服务节点(与前面主备模式一样)
# root @ localhost in /usr/local/redis/redis-5.0.5 [9:44:46] $ ./src/redis-server redis-6379.conf
在每个机器上启动sentinel
# root @ localhost in /usr/local/redis/redis-5.0.5 [19:24:18] $ ./src/redis-sentinel sentinel-26379.conf
查看集群的一些信息
# root @ localhost in /usr/local/redis/redis-5.0.5 [19:25:26] $ ./src/redis-cli -p 26379 127.0.0.1:26379> info replication 127.0.0.1:26379> sentinel masters
查看集群的一些信息
# root @ localhost in /usr/local/redis/redis-5.0.5 [19:25:26] $ ./src/redis-cli -p 26379 127.0.0.1:26379> info replication 127.0.0.1:26379> sentinel masters 1) 1) "name" 2) "mymaster" 3) "ip" 4) "10.3.13.213" 5) "port" 6) "6379" 7) "runid" 8) "757f6e772c4680c9705fe8eee5662f6287adcb3b" 9) "flags" 10) "master" 11) "link-pending-commands" 12) "0" 13) "link-refcount" 14) "1" 15) "last-ping-sent" 16) "0" 17) "last-ok-ping-reply" 18) "990" 19) "last-ping-reply" 20) "990" 21) "down-after-milliseconds" 22) "30000" 23) "info-refresh" 24) "9403" 25) "role-reported" 26) "master" 27) "role-reported-time" 28) "240442" 29) "config-epoch" 30) "0" 31) "num-slaves" 32) "2" 33) "num-other-sentinels" 34) "1" 35) "quorum" 36) "2" 37) "failover-timeout" 38) "600000" 39) "parallel-syncs" 40) "1" 127.0.0.1:26379> exit