主机 | 端口1 | 端口2 |
---|---|---|
192.168.15.231 | 7001 | 7002 |
192.168.15.232 | 7001 | 7002 |
192.168.15.233 | 7001 | 7002 |
1、下载redis最新稳定版(redis-5.0.4.tar.gz)
wget http://download.redis.io/releases/redis-5.0.4.tar.gz
2、解压,创建相关目录
tar zxvf redis-5.0.4.tar.gz
mv redis-5.0.4 redis
cd redis && mkdir {bin,conf,data,log}
3、在conf下创建配置文件(启动时候指定即可)
[lakala@master1 conf]$ cat redis-common.conf
#GENERAL
daemonize yes
tcp-backlog 100
timeout 0
tcp-keepalive 0
#log级别 notice info warning error
loglevel warning
databases 16
save 900 1
save 300 10
save 60 10000
#最大可用内存 根据实际修改
maxmemory 2048m
dir /home/lakala/redis/data
loglevel warning
slave-serve-stale-data yes
#slave只读
slave-read-only yes
#not use default
repl-disable-tcp-nodelay yes
slave-priority 100
#打开aof持久化
appendonly yes
#每秒一次aof写
appendfsync everysec
#关闭在aof rewrite的时候对新的写操作进行fsync
no-appendfsync-on-rewrite yes
auto-aof-rewrite-min-size 64mb
lua-time-limit 5000
#打开redis集群
cluster-enabled yes
#节点互连超时的阀值
cluster-node-timeout 15000
cluster-migration-barrier 1
slowlog-log-slower-than 10000
slowlog-max-len 128
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
[lakala@master1 conf]$ cat redis-7001.conf
#包含通用配置
include /home/lakala/redis/conf/redis-common.conf
#监听tcp端口
bind 192.168.15.231
port 7001
logfile "/home/lakala/redis/log/redis-7001.log"
#内存耗尽时采用的淘汰策略:
# volatile-lru -> remove the key with an expire set using an LRU algorithm
# allkeys-lru -> remove any key accordingly to the LRU algorithm
# volatile-random -> remove a random key with an expire set
# allkeys-random -> remove a random key, any key
# volatile-ttl -> remove the key with the nearest expire time (minor TTL)
# noeviction -> don't expire at all, just return an error on write operations
maxmemory-policy allkeys-lru
#aof存储文件
appendfilename "appendonly-7001.aof"
#不开启rdb存储,只用于添加slave过程
dbfilename dump-7001.rdb
#cluster配置文件(启动自动生成)
cluster-config-file nodes-7001.conf
#部署在同一机器的redis实例,把auto-aof-rewrite搓开,因为cluster环境下内存占用基本一致.
#防止同意机器下瞬间fork所有redis进程做aof rewrite,占用大量内存(ps:cluster必须开启aof)
auto-aof-rewrite-percentage 80-100
[lakala@master1 conf]$ cat redis-7002.conf
#包含通用配置
include /home/lakala/redis/conf/redis-common.conf
#监听tcp端口
bind 192.168.15.231
port 7002
logfile "/home/lakala/redis/log/redis-7002.log"
#内存耗尽时采用的淘汰策略:
# volatile-lru -> remove the key with an expire set using an LRU algorithm
# allkeys-lru -> remove any key accordingly to the LRU algorithm
# volatile-random -> remove a random key with an expire set
# allkeys-random -> remove a random key, any key
# volatile-ttl -> remove the key with the nearest expire time (minor TTL)
# noeviction -> don't expire at all, just return an error on write operations
maxmemory-policy allkeys-lru
#aof存储文件
appendfilename "appendonly-7002.aof"
#不开启rdb存储,只用于添加slave过程
dbfilename dump-7002.rdb
#cluster配置文件(启动自动生成)
cluster-config-file nodes-7002.conf
#部署在同一机器的redis实例,把auto-aof-rewrite搓开,因为cluster环境下内存占用基本一致.
#防止同意机器下瞬间fork所有redis进程做aof rewrite,占用大量内存(ps:cluster必须开启aof)
auto-aof-rewrite-percentage 80-100
4、在bin目录下创建启停脚本
[lakala@master1 bin]$ cat start-redis.sh
#!/bin/bash
/home/lakala/redis/src/redis-server /home/lakala/redis/conf/redis-7001.conf
/home/lakala/redis/src/redis-server /home/lakala/redis/conf/redis-7002.conf
[lakala@master1 bin]$ cat stop-redis.sh
#!/bin/bash
ps -ef |grep redis |grep -v grep |awk '{print $2}' |xargs kill
5、以上步骤在另外两台机器上面重复做一遍,注意修改ip即可,最后把redis都启动起来
6、在其中一台机器上启动redis集群
/home/lakala/redis/src/redis-cli --cluster create 192.168.15.231:7001 192.168.15.232:7001 192.168.15.233:7001 192.168.15.231:7002 192.168.15.232:7002 192.168.15.233:7002 --cluster-replicas 1
7、查看集群情况
[lakala@master1 redis]$ ./src/redis-cli -c -h 192.168.15.231 -p 7001
192.168.15.231:7001> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:18
cluster_my_epoch:18
cluster_stats_messages_ping_sent:1263
cluster_stats_messages_pong_sent:1277
cluster_stats_messages_sent:2540
cluster_stats_messages_ping_received:1277
cluster_stats_messages_pong_received:1262
cluster_stats_messages_received:2539
192.168.15.231:7001> cluster nodes
5857256d73532aaf02a3788c80332f6d38563807 192.168.15.231:7001@17001 myself,slave eaf36b2c8e1618cd6abbd2ca87ec38e0552f386c 0 1555406223000 9 connected
1dd073577ae8b27e43555345c3706e6e3e1cf54d 192.168.15.231:7002@17002 slave b2878a4d06b38580e107f5b04b1561620c8af175 0 1555406221000 16 connected
3a98f61b8a9c92ccb819cf4883576f1bea16638b 192.168.15.233:7002@17002 slave 4419323fa73993873ad0212f3645cc6b77632683 0 1555406224781 15 connected
b2878a4d06b38580e107f5b04b1561620c8af175 192.168.15.233:7001@17001 master - 0 1555406223779 16 connected 10923-16383
eaf36b2c8e1618cd6abbd2ca87ec38e0552f386c 192.168.15.232:7002@17002 master - 0 1555406222775 18 connected 0-5460
4419323fa73993873ad0212f3645cc6b77632683 192.168.15.232:7001@17001 master - 0 1555406224000 15 connected 5461-10922
8、输入和获取键值对
192.168.15.231:7001> set hello world
-> Redirected to slot [866] located at 192.168.15.232:7002
OK
192.168.15.232:7002> get hello
"world"
9、集群故障转移测试
先断掉集群中的一台master节点,可以看到集群依旧是正常,且会自动将这故障master对应的slave节点顶替上,变为master
192.168.15.232:7001> cluster nodes
4419323fa73993873ad0212f3645cc6b77632683 192.168.15.232:7001@17001 myself,master - 0 1555406865000 15 connected 5461-10922
5857256d73532aaf02a3788c80332f6d38563807 192.168.15.231:7001@17001 slave eaf36b2c8e1618cd6abbd2ca87ec38e0552f386c 0 1555406863000 18 connected
1dd073577ae8b27e43555345c3706e6e3e1cf54d 192.168.15.231:7002@17002 slave b2878a4d06b38580e107f5b04b1561620c8af175 0 1555406865647 16 connected
b2878a4d06b38580e107f5b04b1561620c8af175 192.168.15.233:7001@17001 master - 0 1555406864645 16 connected 10923-16383
3a98f61b8a9c92ccb819cf4883576f1bea16638b 192.168.15.233:7002@17002 slave 4419323fa73993873ad0212f3645cc6b77632683 0 1555406864000 15 connected
eaf36b2c8e1618cd6abbd2ca87ec38e0552f386c 192.168.15.232:7002@17002 master - 0 1555406862637 18 connected 0-5460
192.168.15.232:7001>
[lakala@master2 redis]$ ps aux | grep redis
lakala 8143 0.3 0.1 164636 15164 ? Ssl 15:17 0:27 /home/lakala/redis/src/redis-server 192.168.15.232:7001 [cluster]
lakala 8148 0.3 0.1 161052 14260 ? Ssl 15:17 0:27 /home/lakala/redis/src/redis-server 192.168.15.232:7002 [cluster]
lakala 16709 0.0 0.0 112708 980 pts/0 S+ 17:27 0:00 grep --color=auto redis
[lakala@master2 redis]$ kill 8148
[lakala@master2 redis]$ ./src/redis-cli -c -h 192.168.15.232 -p 7001
192.168.15.232:7001> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:19
cluster_my_epoch:15
cluster_stats_messages_ping_sent:78503
cluster_stats_messages_pong_sent:6919
cluster_stats_messages_fail_sent:15
cluster_stats_messages_auth-req_sent:5
cluster_stats_messages_auth-ack_sent:3
cluster_stats_messages_update_sent:2
cluster_stats_messages_sent:85447
cluster_stats_messages_ping_received:6916
cluster_stats_messages_pong_received:7559
cluster_stats_messages_fail_received:10
cluster_stats_messages_auth-req_received:4
cluster_stats_messages_auth-ack_received:2
cluster_stats_messages_received:14491
192.168.15.232:7001> cluster nodes
4419323fa73993873ad0212f3645cc6b77632683 192.168.15.232:7001@17001 myself,master - 0 1555406915000 15 connected 5461-10922
5857256d73532aaf02a3788c80332f6d38563807 192.168.15.231:7001@17001 master - 0 1555406915825 19 connected 0-5460
1dd073577ae8b27e43555345c3706e6e3e1cf54d 192.168.15.231:7002@17002 slave b2878a4d06b38580e107f5b04b1561620c8af175 0 1555406915000 16 connected
b2878a4d06b38580e107f5b04b1561620c8af175 192.168.15.233:7001@17001 master - 0 1555406916829 16 connected 10923-16383
3a98f61b8a9c92ccb819cf4883576f1bea16638b 192.168.15.233:7002@17002 slave 4419323fa73993873ad0212f3645cc6b77632683 0 1555406914000 15 connected
eaf36b2c8e1618cd6abbd2ca87ec38e0552f386c 192.168.15.232:7002@17002 master,fail - 1555406892929 1555406892000 18 disconnected
测试一台机器上面断掉某个master节点后,在此节点上的数据有无丢失,从下面可以看出master节点断掉后,从集群中读取该节点下的数据会自动切换到该master节点的slave节点下,而slave节点已经替代原master节点成为新的master节点
192.168.15.231:7001> keys *
1) "b"
2) "f"
3) "hello"
[lakala@master1 redis]$ ps aux | grep redis
lakala 7834 0.2 0.1 168732 15024 ? Ssl Apr16 2:09 /home/lakala/redis/src/redis-server 192.168.15.231:7001 [cluster]
lakala 7839 0.2 0.1 161052 14208 ? Ssl Apr16 2:05 /home/lakala/redis/src/redis-server 192.168.15.231:7002 [cluster]
lakala 11289 0.0 0.0 112708 980 pts/0 S+ 09:30 0:00 grep --color=auto redis
[lakala@master1 redis]$ kill 7834
[lakala@master1 redis]$ ./src/redis-cli -c -h 192.168.15.231 -p 7002
192.168.15.231:7002> get b
-> Redirected to slot [3300] located at 192.168.15.232:7002
"wei"
192.168.15.232:7002> get f
"ling"
192.168.15.232:7002> get hello
"world"