redis学习笔记(一)redis3.2.5集群安装与测试

redis系列文章目录

[toc]


下载和解包

  1. cd /usr/software
  2. wget http://download.redis.io/releases/redis-3.2.6.tar.gz
  3. tar -zxvf /redis-3.2.6.tar.gz

编译安装

  1. cd redis-3.2.6
  2. make && make install

创建redis节点

测试我们选择2台服务器,分别为:192.168.215.129,192.168.215.130.每分服务器有3个节点。

  • 我先在192.168.215.129创建3个节点:
  1. cd /usr/software
  2. mkdir redis_cluster //创建集群目录
  3. cd redis_cluster
  4. mkdir 7000 7001 7002 //分别代表三个节点 其对应端口 7000 7001 7002
  5. cd ..
  6. //创建7000节点为例,拷贝到7000目录
  7. cp /usr/software/redis-3.2.6/redis.conf ./redis_cluster/7000/
  8. //拷贝到7001目录
  9. cp /usr/software/redis-3.2.6/redis.conf ./redis_cluster/7001/
  10. //拷贝到7002目录
  11. cp /usr/software/redis-3.2.6/redis.conf ./redis_cluster/7002/

分别对7000,7001、7002文件夹中的3个文件修改对应的配置

  1. daemonize yes //redis后台运行
  2. pidfile /var/run/redis_7000.pid //pidfile文件对应7000,7001,7002
  3. port 7000 //端口7000,7002,7003
  4. cluster-enabled yes //开启集群 把注释#去掉
  5. cluster-config-file nodes_7000.conf //集群的配置 配置文件首次启动自动生成 7000,7001,7002
  6. bind 192.168.215.130 //这里要绑定机器的IP
  7. cluster-node-timeout 5000 //请求超时 设置5秒够了
  8. appendonly yes //aof日志开启 有需要就开启,它会每次写操作都记录一条日志</pre>
  • 在192.168.1.238创建3个节点:对应的端口改为7003,7004,7005.配置对应的改一下就可以了。

    4、两台机启动各节点(两台服务器方式一样)

  1. cd /usr/software
  2. redis-server redis_cluster/7000/redis.conf
  3. redis-server redis_cluster/7001/redis.conf
  4. redis-server redis_cluster/7002/redis.conf
  5. redis-server redis_cluster/7003/redis.conf
  6. redis-server redis_cluster/7004/redis.conf
  7. redis-server redis_cluster/7005/redis.conf

查看服务

  1. ps -ef | grep redis #查看是否启动成功
  2. netstat -tnlp | grep redis #可以看到redis监听端口
  1. [root@zk1 software]# ps -ef | grep redis
  2. root 10601 1 0 06:42 ? 00:00:00 redis-server 192.168.215.129:7000 [cluster]
  3. root 10606 1 0 06:42 ? 00:00:00 redis-server 192.168.215.129:7001 [cluster]
  4. root 10610 1 0 06:42 ? 00:00:00 redis-server 192.168.215.129:7002 [cluster]
  5. root 10615 4548 0 06:42 pts/2 00:00:00 grep --color=auto redis
  6. [root@zk1 software]#
  7. [root@zk1 software]#
  8. [root@zk1 software]#
  9. [root@zk1 software]# netstat -tnlp | grep redis
  10. tcp 0 0 192.168.215.129:17002 0.0.0.0:* LISTEN 10610/redis-server
  11. tcp 0 0 192.168.215.129:7000 0.0.0.0:* LISTEN 10601/redis-server
  12. tcp 0 0 192.168.215.129:7001 0.0.0.0:* LISTEN 10606/redis-server
  13. tcp 0 0 192.168.215.129:7002 0.0.0.0:* LISTEN 10610/redis-server
  14. tcp 0 0 192.168.215.129:17000 0.0.0.0:* LISTEN 10601/redis-server
  15. tcp 0 0 192.168.215.129:17001 0.0.0.0:* LISTEN 10606/redis-server

创建集群

前面已经准备好了搭建集群的redis节点,接下来我们要把这些节点都串连起来搭建集群。官方提供了一个工具:redis-trib.rb(/usr/local/redis-3.2.1/src/redis-trib.rb) 看后缀就知道这鸟东西不能直接执行,它是用ruby写的一个程序,所以我们还得安装ruby.再用 gem 这个命令来安装 redis接口, gem是ruby的一个工具包. 
为了方便,两台机器都安装.

  1. yum -y install ruby ruby-devel rubygems rpm-build
  2. gem install redis

注意:这里需要修改gem源,并且淘宝的gem源已经不能用了,坑爹啊,现在要使用ruby-china。 
https://gems.ruby-china.org/

添加ruby-china源:

  1. [root@zk1 software]# gem sources --add https://gems.ruby-china.org/ --remove https://rubygems.org/
  2. [root@zk1 software]# gem sources -l
  3. *** CURRENT SOURCES ***
  4. https://gems.ruby-china.org/

redis-trib.rb

进入/usr/software/redis-3.2.5/src运行一下redis-trib.rb

  1. [root@zk2 src]# ./redis-trib.rb
  2. Usage: redis-trib <command> <options> <arguments ...>
  3. create host1:port1 ... hostN:portN
  4. --replicas <arg>
  5. check host:port
  6. info host:port
  7. fix host:port
  8. --timeout <arg>
  9. reshard host:port
  10. --from <arg>
  11. --to <arg>
  12. --slots <arg>
  13. --yes
  14. --timeout <arg>
  15. --pipeline <arg>
  16. rebalance host:port
  17. --weight <arg>
  18. --auto-weights
  19. --use-empty-masters
  20. --timeout <arg>
  21. --simulate
  22. --pipeline <arg>
  23. --threshold <arg>
  24. add-node new_host:new_port existing_host:existing_port
  25. --slave
  26. --master-id <arg>
  27. del-node host:port node_id
  28. set-timeout host:port milliseconds
  29. call host:port command arg arg .. arg
  30. import host:port
  31. --from <arg>
  32. --copy
  33. --replace
  34. help (show this help)
  35. For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.

看到这,应该明白了吧, 就是靠上面这些操作 完成redis集群搭建的.

确认所有的节点都启动,接下来使用参数create 创建 (在192.168.215.129中来创建)

  1. [root@zk1 src]# ./redis-trib.rb create --replicas 1 192.168.215.129:7000 192.168.215.129:7001 192.168.215.129:7002 192.168.215.130:7003 192.168.215.130:7004 192.168.215.130:7005
  2. >>> Creating cluster
  3. >>> Performing hash slots allocation on 6 nodes...
  4. Using 3 masters:
  5. 192.168.215.129:7000
  6. 192.168.215.130:7003
  7. 192.168.215.129:7001
  8. Adding replica 192.168.215.130:7004 to 192.168.215.129:7000
  9. Adding replica 192.168.215.129:7002 to 192.168.215.130:7003
  10. Adding replica 192.168.215.130:7005 to 192.168.215.129:7001
  11. M: 16518afbfcbd961aeb76ef1592007a3e7fe24b1b 192.168.215.129:7000
  12. slots:0-5460 (5461 slots) master
  13. M: 524219969118a57ceaac753ecef7585f634cdf26 192.168.215.129:7001
  14. slots:10923-16383 (5461 slots) master
  15. S: ea4519ff0083a13cef8262490ee9e61e5a4b14b1 192.168.215.129:7002
  16. replicates 82c0e591b9bc7a289026dff2873a254d1c49d285
  17. M: 82c0e591b9bc7a289026dff2873a254d1c49d285 192.168.215.130:7003
  18. slots:5461-10922 (5462 slots) master
  19. S: baf74dd89c0605d2a71a8d1d3706005ff668563b 192.168.215.130:7004
  20. replicates 16518afbfcbd961aeb76ef1592007a3e7fe24b1b
  21. S: f8192314d2232e12ba9f558e9ecbfcc890f4fb73 192.168.215.130:7005
  22. replicates 524219969118a57ceaac753ecef7585f634cdf26
  23. Can I set the above configuration? (type 'yes' to accept): yes
  24. >>> Nodes configuration updated
  25. >>> Assign a different config epoch to each node
  26. >>> Sending CLUSTER MEET messages to join the cluster
  27. Waiting for the cluster to join.....
  28. >>> Performing Cluster Check (using node 192.168.215.129:7000)
  29. M: 16518afbfcbd961aeb76ef1592007a3e7fe24b1b 192.168.215.129:7000
  30. slots:0-5460 (5461 slots) master
  31. 1 additional replica(s)
  32. S: ea4519ff0083a13cef8262490ee9e61e5a4b14b1 192.168.215.129:7002
  33. slots: (0 slots) slave
  34. replicates 82c0e591b9bc7a289026dff2873a254d1c49d285
  35. S: f8192314d2232e12ba9f558e9ecbfcc890f4fb73 192.168.215.130:7005
  36. slots: (0 slots) slave
  37. replicates 524219969118a57ceaac753ecef7585f634cdf26
  38. S: baf74dd89c0605d2a71a8d1d3706005ff668563b 192.168.215.130:7004
  39. slots: (0 slots) slave
  40. replicates 16518afbfcbd961aeb76ef1592007a3e7fe24b1b
  41. M: 524219969118a57ceaac753ecef7585f634cdf26 192.168.215.129:7001
  42. slots:10923-16383 (5461 slots) master
  43. 1 additional replica(s)
  44. M: 82c0e591b9bc7a289026dff2873a254d1c49d285 192.168.215.130:7003
  45. slots:5461-10922 (5462 slots) master
  46. 1 additional replica(s)
  47. [OK] All nodes agree about slots configuration.
  48. >>> Check for open slots...
  49. >>> Check slots coverage...
  50. [OK] All 16384 slots covered.

解释下, –replicas 1 表示 自动为每一个master节点分配一个slave节点 上面有6个节点,程序会按照一定规则生成 3个master(主)3个slave(从) 
前面已经提醒过的 防火墙一定要开放监听的端口,否则会创建失败。

检查集群状态:

  1. [root@zk1 src]# ./redis-trib.rb check 192.168.215.129:7002
  2. >>> Performing Cluster Check (using node 192.168.215.129:7002)
  3. S: ea4519ff0083a13cef8262490ee9e61e5a4b14b1 192.168.215.129:7002
  4. slots: (0 slots) slave
  5. replicates 82c0e591b9bc7a289026dff2873a254d1c49d285
  6. M: 82c0e591b9bc7a289026dff2873a254d1c49d285 192.168.215.130:7003
  7. slots:5461-10922 (5462 slots) master
  8. 1 additional replica(s)
  9. S: baf74dd89c0605d2a71a8d1d3706005ff668563b 192.168.215.130:7004
  10. slots: (0 slots) slave
  11. replicates 16518afbfcbd961aeb76ef1592007a3e7fe24b1b
  12. M: 524219969118a57ceaac753ecef7585f634cdf26 192.168.215.129:7001
  13. slots:10923-16383 (5461 slots) master
  14. 1 additional replica(s)
  15. M: 16518afbfcbd961aeb76ef1592007a3e7fe24b1b 192.168.215.129:7000
  16. slots:0-5460 (5461 slots) master
  17. 1 additional replica(s)
  18. S: f8192314d2232e12ba9f558e9ecbfcc890f4fb73 192.168.215.130:7005
  19. slots: (0 slots) slave
  20. replicates 524219969118a57ceaac753ecef7585f634cdf26
  21. [OK] All nodes agree about slots configuration.
  22. >>> Check for open slots...
  23. >>> Check slots coverage...
  24. [OK] All 16384 slots covered.

redis集群中数据分片是通过hash slot的方式实现的

测试集群

  • 链接服务器
  1. [root@zk1 src]# ./redis-cli -c -p 7000 -h 192.168.215.129
  2. 192.168.215.129:7000>
  3. 192.168.215.129:7000> cluster info
  4. cluster_state:ok
  5. cluster_slots_assigned:16384
  6. cluster_slots_ok:16384
  7. cluster_slots_pfail:0
  8. cluster_slots_fail:0
  9. cluster_known_nodes:6
  10. cluster_size:3
  11. cluster_current_epoch:6
  12. cluster_my_epoch:1
  13. cluster_stats_messages_sent:1502
  14. cluster_stats_messages_received:1502
  • set值
  1. 192.168.215.129:7000> set name lbl
  2. -> Redirected to slot [5798] located at 192.168.215.130:7003
  3. OK
  4. 192.168.215.130:7003> get name
  5. "lbl"

可见,重定向到了130节点7003端口。 
原因是redis采用hash槽的方式分发key到不同节点,算法是crc(16)%16384。详细描述后续会单独写文章描述。 
而且你会发现,当一次重定向以后,这个客户端就连接到了130:7003这个节点。

测试一个master宕机

将上面设置的name所在的130:7003kill掉,只剩了两个master和3个slave。你会发现cluster_current_epoch相比之前加了1,这是因为redis的主从关系,重新选了一次主。 
然后get name发现,重定向了129:7002这个节点。

  1. [root@zk1 src]# ./redis-trib.rb check 192.168.215.129:7002
  2. >>> Performing Cluster Check (using node 192.168.215.129:7002)
  3. M: ea4519ff0083a13cef8262490ee9e61e5a4b14b1 192.168.215.129:7002
  4. slots:5461-10922 (5462 slots) master
  5. 0 additional replica(s)
  6. S: baf74dd89c0605d2a71a8d1d3706005ff668563b 192.168.215.130:7004
  7. slots: (0 slots) slave
  8. replicates 16518afbfcbd961aeb76ef1592007a3e7fe24b1b
  9. M: 524219969118a57ceaac753ecef7585f634cdf26 192.168.215.129:7001
  10. slots:10923-16383 (5461 slots) master
  11. 1 additional replica(s)
  12. M: 16518afbfcbd961aeb76ef1592007a3e7fe24b1b 192.168.215.129:7000
  13. slots:0-5460 (5461 slots) master
  14. 1 additional replica(s)
  15. S: f8192314d2232e12ba9f558e9ecbfcc890f4fb73 192.168.215.130:7005
  16. slots: (0 slots) slave
  17. replicates 524219969118a57ceaac753ecef7585f634cdf26
  18. [OK] All nodes agree about slots configuration.
  19. >>> Check for open slots...
  20. >>> Check slots coverage...
  21. [OK] All 16384 slots covered.
  22. [root@zk1 src]# ./redis-cli -c -p 7000 -h 192.168.215.129
  23. 192.168.215.129:7000> cluster info
  24. cluster_state:ok
  25. cluster_slots_assigned:16384
  26. cluster_slots_ok:16384
  27. cluster_slots_pfail:0
  28. cluster_slots_fail:0
  29. cluster_known_nodes:6
  30. cluster_size:3
  31. cluster_current_epoch:7
  32. cluster_my_epoch:1
  33. cluster_stats_messages_sent:2883
  34. cluster_stats_messages_received:2675
  35. 192.168.215.129:7000> get name
  36. -> Redirected to slot [5798] located at 192.168.215.129:7002
  37. "lbl"

之所以会重定向到129:7002这个节点,是因为在kill之前129:7002130:7003的slave: 
下面这是在kill之前拷贝的./redis-trib.rb check的数据,注意replicates后的值

  1. 1. `S: ea4519ff0083a13cef8262490ee9e61e5a4b14b1 192.168.215.129:7002`
  2. 2. `replicates 82c0e591b9bc7a289026dff2873a254d1c49d285`
  3. 3. `M: 82c0e591b9bc7a289026dff2873a254d1c49d285 192.168.215.130:7003`
  4. 4. `slots:5461-10922 (5462 slots) master`

好了,今天就到这里,redis具体实现原理后续再讲。

©️2020 CSDN 皮肤主题: Age of Ai 设计师: meimeiellie 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值