redis集群
1、环境
master节点 192.168.56.11 端口:7001,7002 slave节点 192.168.56.12 端口:7003,7004 slave节点 192.168.56.13 端口:7005,7006
2.创建目录
192.168.56.11 mkdir /data/redis/cluster/redis_{7001,7002} -p vim /data/redis/cluster/redis_7001/redis_7001.conf bind 192.168.56.11 port 7001 daemonize yes pidfile "/data/redis/cluster/redis_7001/redis_7001.pid" logfile "/data/redis/cluster/redis_7001/redis_7001.log" dir /data/redis/cluster/redis_7001/ masterauth 123456 requirepass 123456 appendonly yes cluster-enabled yes cluster-config-file nodes-7001.conf cluster-node-timeout 15000 vim /data/redis/cluster/redis_7002/redis_7002.conf bind 192.168.56.11 port 7002 daemonize yes pidfile "/data/redis/cluster/redis_7002/redis_7002.pid" logfile "/data/redis/cluster/redis_7002/redis_7002.log" dir /data/redis/cluster/redis_7002/ masterauth 123456 requirepass 123456 appendonly yes cluster-enabled yes cluster-config-file nodes-7002.conf cluster-node-timeout 15000 192.168.56.12 /192.168.56.13参考11都一样的 mkdir /data/redis/cluster/redis_{7003,7004} -p 192.168.56.13 mkdir /data/redis/cluster/redis_{7006,7005} -p
3、启动redis
redis-server /data/redis/cluster/redis_7001/redis_7001.conf redis-server /data/redis/cluster/redis_7002/redis_7002.conf
4.创建集群
低版本需要安装ruby环境
# yum -y groupinstall "Development Tools" # yum install -y gdbm-devel libdb4-devel libffi-devel libyaml libyaml-devel ncurses-devel openssl-devel readline-devel tcl-devel # mkdir -p ~/rpmbuild/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS} # wget http://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.3.tar.gz -P ~/rpmbuild/SOURCES # wget http://raw.githubusercontent.com/tjinjin/automate-ruby-rpm/master/ruby22x.spec -P ~/rpmbuild/SPECS # rpmbuild -bb ~/rpmbuild/SPECS/ruby22x.spec # rpm -ivh ~/rpmbuild/RPMS/x86_64/ruby-2.2.3-1.el7.x86_64.rpm # gem install redis #目的是安装这个,用于配置集群 #cp /usr/local/redis/src/redis-trib.rb /usr/bin/ #redis-trib.rb create --replicas 1 192.168.56.11:7001 192.168.56.11:7002 192.168.56.12:7003 192.168.56.12:7004 192.168.56.13:7005 192.168.56.13:7006
5、5.0的不用可以直接创建
redis-cli -a 123456 --cluster create 192.168.56.11:7001 192.168.56.11:7002 192.168.56.12:7003 192.168.56.12:7004 192.168.56.13:7005 192.168.56.13:7006 --cluster-replicas 1 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. >>> Performing hash slots allocation on 6 nodes... Master[0] -> Slots 0 - 5460 Master[1] -> Slots 5461 - 10922 Master[2] -> Slots 10923 - 16383 Adding replica 192.168.56.12:7004 to 192.168.56.11:7001 Adding replica 192.168.56.13:7006 to 192.168.56.12:7003 Adding replica 192.168.56.11:7002 to 192.168.56.13:7005 M: 9db1a6bd175f51b03d706cb5921ddc0f0ecb53df 192.168.56.11:7001 slots:[0-5460] (5461 slots) master S: c244f42b9cb9f35ab7f13342af3b683820ec7b4f 192.168.56.11:7002 replicates 3570580694cb31afd9f9a483dc1189c26b629419 M: 895222913dc3e98d2c86b36d4f20b09975a4e7fc 192.168.56.12:7003 slots:[5461-10922] (5462 slots) master S: b1e2739478199fe583906fcbec427f3a3339921b 192.168.56.12:7004 replicates 9db1a6bd175f51b03d706cb5921ddc0f0ecb53df M: 3570580694cb31afd9f9a483dc1189c26b629419 192.168.56.13:7005 slots:[10923-16383] (5461 slots) master S: f1176743f2469e44d91e800f5cb303d506b172d6 192.168.56.13:7006 replicates 895222913dc3e98d2c86b36d4f20b09975a4e7fc 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 [OK] All 16384 slots covered.
查看日志
[root@linux-node1 redis_7001]# cat nodes-7001.conf 895222913dc3e98d2c86b36d4f20b09975a4e7fc 192.168.56.12:7003@17003 master - 0 1587472901829 3 connected 5461-10922 3570580694cb31afd9f9a483dc1189c26b629419 192.168.56.13:7005@17005 master - 0 1587472903837 5 connected 10923-16383 f1176743f2469e44d91e800f5cb303d506b172d6 192.168.56.13:7006@17006 slave 895222913dc3e98d2c86b36d4f20b09975a4e7fc 0 1587472904879 6 connected 9db1a6bd175f51b03d706cb5921ddc0f0ecb53df 192.168.56.11:7001@17001 myself,master - 0 1587472905000 1 connected 0-5460 c244f42b9cb9f35ab7f13342af3b683820ec7b4f 192.168.56.11:7002@17002 slave 3570580694cb31afd9f9a483dc1189c26b629419 0 1587472905883 5 connected b1e2739478199fe583906fcbec427f3a3339921b 192.168.56.12:7004@17004 slave 9db1a6bd175f51b03d706cb5921ddc0f0ecb53df 0 1587472906887 4 connected vars currentEpoch 6 lastVoteEpoch 0
6、查看集群状态
[root@linux-node1 ~]# redis-cli -c -h 192.168.56.11 -p 7001 -a 123456 192.168.56.11: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:6 cluster_my_epoch:1 cluster_stats_messages_ping_sent:61 cluster_stats_messages_pong_sent:65 cluster_stats_messages_sent:126 cluster_stats_messages_ping_received:60 cluster_stats_messages_pong_received:61 cluster_stats_messages_meet_received:5 cluster_stats_messages_received:126 192.168.56.11:7001> CLUSTER NODES #列出节点信息 895222913dc3e98d2c86b36d4f20b09975a4e7fc 192.168.56.12:7003@17003 master - 0 1587472964315 3 connected 5461-10922 3570580694cb31afd9f9a483dc1189c26b629419 192.168.56.13:7005@17005 master - 0 1587472963000 5 connected 10923-16383 f1176743f2469e44d91e800f5cb303d506b172d6 192.168.56.13:7006@17006 slave 895222913dc3e98d2c86b36d4f20b09975a4e7fc 0 1587472963311 6 connected 9db1a6bd175f51b03d706cb5921ddc0f0ecb53df 192.168.56.11:7001@17001 myself,master - 0 1587472961000 1 connected 0-5460 c244f42b9cb9f35ab7f13342af3b683820ec7b4f 192.168.56.11:7002@17002 slave 3570580694cb31afd9f9a483dc1189c26b629419 0 1587472965318 5 connected b1e2739478199fe583906fcbec427f3a3339921b 192.168.56.12:7004@17004 slave 9db1a6bd175f51b03d706cb5921ddc0f0ecb53df 0 1587472966324 4 connected 192.168.56.11:7001> set key111 aaa -> Redirected to slot [13680] located at 192.168.56.13:7005 OK 192.168.56.13:7005> set key222 bbb -> Redirected to slot [2320] located at 192.168.56.11:7001 OK 192.168.56.11:7001> set key333 ccc -> Redirected to slot [7472] located at 192.168.56.12:7003 OK 192.168.56.12:7003> get key111 -> Redirected to slot [13680] located at 192.168.56.13:7005 "aaa" 192.168.56.13:7005> get key333 -> Redirected to slot [7472] located at 192.168.56.12:7003 "ccc" #分别写到不同的切片上了