redis安装
#官方下载安装包:
wget http://download.redis.io/releases/redis-5.0.8.tar.gz
#解压:
tar -xvzf redis-5.0.8.tar.gz
#安装:
cd redis-5.0.8
make && make install
sentinel(哨兵)模式
- redis的sentinel(哨兵)模式可是实现当主节点出现异常之后,自动推举一个从节点升级为主节点,当某一个主节点出现异常后,并且没有可以使用的从节点,整个redis集群就会报错。
- 所以一般情况下,redis集群都会为单个主节点添加从节点,防止因为一个主节点出现异常后,没有从节点可以升级为主节点导致整个集群报错。
修改配置文件
cp -a redis.conf redis_30000.conf
cp -a redis.conf redis_30001.conf
cp -a redis.conf redis_30002.conf
cp -a redis.conf redis_30003.conf
cp -a redis.conf redis_30004.conf
cp -a redis.conf redis_30005.conf
#主要修改:
以redis_30000.conf 文件举例
bind 0.0.0.0 //ip配置
port 30000 //端口配置
//rdb模式
dbfilename dump_30000.rdb //数据持久化文件
//aof模式
appendonly yes
appendfilename "appendonly_30000.aof" //数据持久化文件
dir /root/lzc/redis-5.0.8/ //文件目录
cluster-enabled yes //开启持久化
cluster-config-file nodes-30000.conf //集群node文件
cluster-node-timeout 5000
daemonize yes //后台启动
pidfile /var/run/redis_30000.pid //pid文件
启动redis各个节点
./src/redis-server ./redis_30000.conf
./src/redis-server ./redis_30001.conf
./src/redis-server ./redis_30002.conf
./src/redis-server ./redis_30003.conf
./src/redis-server ./redis_30004.conf
./src/redis-server ./redis_30005.conf
创建redis集群
./src/redis-trib.rb create --replicas 1 127.0.0.1:30000 127.0.0.1:30001 127.0.0.1:30002 127.0.0.1:30003 127.0.0.1:30004 127.0.0.1:30005
如果没有安装 ruby 则会提示ruby不存在
安装ruby
yum install -y ruby
再次执行出现
ARNING: redis-trib.rb is not longer available!
You should use redis-cli instead.
All commands and features belonging to redis-trib.rb have been moved
to redis-cli.
In order to use them you should call redis-cli with the --cluster
option followed by the subcommand name, arguments and options.
Use the following syntax:
redis-cli --cluster SUBCOMMAND [ARGUMENTS] [OPTIONS]
Example:
redis-cli --cluster create 127.0.0.1:30000 127.0.0.1:30001 127.0.0.1:30002 127.0.0.1:30003 127.0.0.1:30004 127.0.0.1:30005 --cluster-replicas 1
To get help about all subcommands, type:
redis-cli --cluster help
意思是 mysql 5.0以后不在支持使用redis-trib.rb
执行示例:
redis-cli --cluster create 127.0.0.1:30000 127.0.0.1:30001 127.0.0.1:30002 127.0.0.1:30003 127.0.0.1:30004 127.0.0.1:30005 --cluster-replicas 1
出现
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 127.0.0.1:30004 to 127.0.0.1:30000
Adding replica 127.0.0.1:30005 to 127.0.0.1:30001
Adding replica 127.0.0.1:30003 to 127.0.0.1:30002
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: a95de95b4ccfb543099230a516d3de8825ef7bff 127.0.0.1:30000
slots:[0-5460] (5461 slots) master
M: ed196c6c6354507d88a5c265ddb23729a600ea76 127.0.0.1:30001
slots:[5461-10922] (5462 slots) master
M: 54e9738f87d82675ddaf2c135216971d24f8b559 127.0.0.1:30002
slots:[10923-16383] (5461 slots) master
S: e8c1bdf3d6825a8fbc2f0498f80a89a39731163a 127.0.0.1:30003
replicates 54e9738f87d82675ddaf2c135216971d24f8b559
S: 2cfdef6891a271c3e865e74b5668ed3b1a787d10 127.0.0.1:30004
replicates a95de95b4ccfb543099230a516d3de8825ef7bff
S: 68c4bc9dc38108690b12932b4dd7bc00611a3069 127.0.0.1:30005
replicates ed196c6c6354507d88a5c265ddb23729a600ea76
Can I set the above configuration? (type 'yes' to accept):
输入yes
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 127.0.0.1:30004 to 127.0.0.1:30000
Adding replica 127.0.0.1:30005 to 127.0.0.1:30001
Adding replica 127.0.0.1:30003 to 127.0.0.1:30002
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: a95de95b4ccfb543099230a516d3de8825ef7bff 127.0.0.1:30000
slots:[0-5460] (5461 slots) master
M: ed196c6c6354507d88a5c265ddb23729a600ea76 127.0.0.1:30001
slots:[5461-10922] (5462 slots) master
M: 54e9738f87d82675ddaf2c135216971d24f8b559 127.0.0.1:30002
slots:[10923-16383] (5461 slots) master
S: e8c1bdf3d6825a8fbc2f0498f80a89a39731163a 127.0.0.1:30003
replicates 54e9738f87d82675ddaf2c135216971d24f8b559
S: 2cfdef6891a271c3e865e74b5668ed3b1a787d10 127.0.0.1:30004
replicates a95de95b4ccfb543099230a516d3de8825ef7bff
S: 68c4bc9dc38108690b12932b4dd7bc00611a3069 127.0.0.1:30005
replicates ed196c6c6354507d88a5c265ddb23729a600ea76
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
....
>>> Performing Cluster Check (using node 127.0.0.1:30000)
M: a95de95b4ccfb543099230a516d3de8825ef7bff 127.0.0.1:30000
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 54e9738f87d82675ddaf2c135216971d24f8b559 127.0.0.1:30002
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
M: ed196c6c6354507d88a5c265ddb23729a600ea76 127.0.0.1:30001
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 68c4bc9dc38108690b12932b4dd7bc00611a3069 127.0.0.1:30005
slots: (0 slots) slave
replicates ed196c6c6354507d88a5c265ddb23729a600ea76
S: 2cfdef6891a271c3e865e74b5668ed3b1a787d10 127.0.0.1:30004
slots: (0 slots) slave
replicates a95de95b4ccfb543099230a516d3de8825ef7bff
S: e8c1bdf3d6825a8fbc2f0498f80a89a39731163a 127.0.0.1:30003
slots: (0 slots) slave
replicates 54e9738f87d82675ddaf2c135216971d24f8b559
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
redis集群创建成功
链接redis
./src/redis-cli -c -p 30000 (注意-c参数 以集群方式连接)
查看集群状态
cluster nodes
出现
a95de95b4ccfb543099230a516d3de8825ef7bff 127.0.0.1:30000@40000 master - 0 1593402443128 1 connected 0-5460
2cfdef6891a271c3e865e74b5668ed3b1a787d10 127.0.0.1:30004@40004 slave a95de95b4ccfb543099230a516d3de8825ef7bff 0 1593402442527 5 connected
ed196c6c6354507d88a5c265ddb23729a600ea76 127.0.0.1:30001@40001 master - 0 1593402443000 2 connected 5461-10922
54e9738f87d82675ddaf2c135216971d24f8b559 127.0.0.1:30002@40002 master - 0 1593402442126 3 connected 10923-16383
e8c1bdf3d6825a8fbc2f0498f80a89a39731163a 127.0.0.1:30003@40003 myself,slave 54e9738f87d82675ddaf2c135216971d24f8b559 0 1593402441000 4 connected
68c4bc9dc38108690b12932b4dd7bc00611a3069 127.0.0.1:30005@40005 slave ed196c6c6354507d88a5c265ddb23729a600ea76 0 1593402443000 6 connected
可以看到 master 为主节点 slave为从节点
再任意一个节点执行set操作,其他节点均可以使用get操作获取到内容。
a95de95b4ccfb543099230a516d3de8825ef7bff 127.0.0.1:30000@40000 master - 0 1593402635611 1 connected 0-5460
2cfdef6891a271c3e865e74b5668ed3b1a787d10 127.0.0.1:30004@40004 slave a95de95b4ccfb543099230a516d3de8825ef7bff 0 1593402635510 5 connected
ed196c6c6354507d88a5c265ddb23729a600ea76 127.0.0.1:30001@40001 master - 0 1593402635000 2 connected 5461-10922
54e9738f87d82675ddaf2c135216971d24f8b559 127.0.0.1:30002@40002 master,fail - 1593402628075 1593402627000 3 disconnected
e8c1bdf3d6825a8fbc2f0498f80a89a39731163a 127.0.0.1:30003@40003 myself,master - 0 1593402634000 7 connected 10923-16383
68c4bc9dc38108690b12932b4dd7bc00611a3069 127.0.0.1:30005@40005 slave ed196c6c6354507d88a5c265ddb23729a600ea76 0 1593402634000 6 connected
当一个节点出现异常以后,再次执行 cluster nodes
会看到127.0.0.1:30002@40002 master,fail fail代表当前机器出现问题
127.0.0.1:30003@40003 myself,master 原本30003的从节点自动被推举为主节点。
当30002与30003均出现问题,执行get操作会出现
(error) CLUSTERDOWN The cluster is down 集群出现异常(主从节点均异常)
此时再次启动30002 及 30003会恢复正常。