安装环境
序号 |
项目 |
值 |
1 |
OS版本 |
Red Hat Enterprise Linux Server release 7.1 (Maipo) |
2 |
内核版本 |
3.10.0-229.el7.x86_64 |
3 |
Redis |
redis-4.0.1 |
4 |
Ruby |
ruby 2.3.4p301 (2017-03-30 revision 58214) [x86_64-linux] |
节点信息规划
本来只准备部署7001-7004 4个实例,但是redis集群提示最少需要3个主节点,6个节点才能完成启动,所以后面2个是后来加的。
注意:这里因为是测试环境,所以将所有实例都放在了一台机器上,生产建议主备节点不要放在一台机器上
序号 |
IP地址 |
端口 |
主备 |
节点配置文件 |
1 |
192.168.9.216 |
7001 |
主 |
/opt/redis-cluster/nodes-7001/redis.conf |
2 |
192.168.9.216 |
7002 |
主 |
/opt/redis-cluster/nodes-7002/redis.conf |
3 |
192.168.9.216 |
7003 |
主 |
/opt/redis-cluster/nodes-7003/redis.conf |
4 |
192.168.9.216 |
7004 |
备 |
/opt/redis-cluster/nodes-7004/redis.conf |
5 |
192.168.9.216 |
7005 |
备 |
/opt/redis-cluster/nodes-7005/redis.conf |
6 |
192.168.9.216 |
7006 |
备 |
/opt/redis-cluster/nodes-7006/redis.conf |
环境安装(按照正常安装步骤,碰到问题再解决)
1.下载redis-4.0.1.tar.gz源码包
[root@elk01 opt]# wget http://download.redis.io/releases/redis-4.0.1.tar.gz --2017-10-26 09:33:23-- http://download.redis.io/releases/redis-4.0.1.tar.gz Resolving download.redis.io (download.redis.io)... 109.74.203.151 Connecting to download.redis.io (download.redis.io)|109.74.203.151|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 1711660 (1.6M) [application/x-gzip] Saving to: ‘redis-4.0.1.tar.gz’ 100%[======================================================>] 1,711,660 426KB/s in 3.9s 2017-10-26 09:33:28 (426 KB/s) - ‘redis-4.0.1.tar.gz’ saved [1711660/1711660] [root@elk01 opt]# ll redis-4.0.1.tar.gz -rw-r--r-- 1 root root 1711660 Jul 24 21:59 redis-4.0.1.tar.gz
2.解压并安装
[root@elk01 opt]# pwd /opt [root@elk01 opt]# tar xf redis-4.0.1.tar.gz [root@elk01 opt]# cd redis-4.0.1 [root@elk01 redis-4.0.1]# ll total 272 -rw-rw-r-- 1 root root 127778 Jul 24 21:58 00-RELEASENOTES -rw-rw-r-- 1 root root 53 Jul 24 21:58 BUGS -rw-rw-r-- 1 root root 1815 Jul 24 21:58 CONTRIBUTING -rw-rw-r-- 1 root root 1487 Jul 24 21:58 COPYING drwxrwxr-x 6 root root 117 Jul 24 21:58 deps -rw-rw-r-- 1 root root 11 Jul 24 21:58 INSTALL -rw-rw-r-- 1 root root 151 Jul 24 21:58 Makefile -rw-rw-r-- 1 root root 4223 Jul 24 21:58 MANIFESTO -rw-rw-r-- 1 root root 20530 Jul 24 21:58 README.md -rw-rw-r-- 1 root root 57764 Jul 24 21:58 redis.conf -rwxrwxr-x 1 root root 271 Jul 24 21:58 runtest -rwxrwxr-x 1 root root 280 Jul 24 21:58 runtest-cluster -rwxrwxr-x 1 root root 281 Jul 24 21:58 runtest-sentinel -rw-rw-r-- 1 root root 7606 Jul 24 21:58 sentinel.conf drwxrwxr-x 3 root root 4096 Jul 24 21:58 src drwxrwxr-x 10 root root 4096 Jul 24 21:58 tests drwxrwxr-x 8 root root 4096 Jul 24 21:58 utils #执行make命令 如果没有报错可以执行make test命令 [root@elk01 redis-4.0.1]# make test cd src && make test make[1]: Entering directory `/opt/redis-4.0.1/src' You need tcl 8.5 or newer in order to run the Redis test #提示需要tcl make[1]: *** [test] Error 1 make[1]: Leaving directory `/opt/redis-4.0.1/src' make: *** [test] Error 2 # yum安装tcl [root@elk01 redis-4.0.1]# yum install -y tcl … Installed: tcl.x86_64 1:8.5.13-8.el7 Complete! [root@elk01 redis-4.0.1]# make test … \o/ All tests passed without errors! Cleanup: may take some time... OK make[1]: Leaving directory `/opt/redis-4.0.1/src' #将redis的执行脚本拷贝到PATH变量所在的目录 [root@elk01 redis]# cp -a src/redis-server src/redis-cli src/redis-sentinel src/redis-trib.rb src/redis-check-aof src/redis-check-rdb src/redis-benchmark /usr/local/bin/
3.配置redis集群文件
[root@elk01 redis-4.0.1]# mkdir redis-cluster You have mail in /var/spool/mail/root [root@elk01 redis-4.0.1]# mkdir redis-cluster/nodes-{7001,7002,7003,7004} [root@elk01 redis-4.0.1]# ll redis-cluster/ total 0 drwxr-xr-x 2 root root 6 Oct 26 13:27 nodes-7001 drwxr-xr-x 2 root root 6 Oct 26 13:27 nodes-7002 drwxr-xr-x 2 root root 6 Oct 26 13:27 nodes-7003 drwxr-xr-x 2 root root 6 Oct 26 13:27 nodes-7004 [root@elk01 redis-4.0.1]# mv redis-cluster /opt/ [root@elk01 redis-4.0.1]# cd /opt/ [root@elk01 opt]# ln -s redis-4.0.1 redis #按需修改配置文件(以nodes-7001实例为例) [root@elk01 nodes-7001]# egrep -v "^#|^$" redis.conf bind 192.168.9.216 127.0.0.1 #此处注意,如果redis实例分布在多台上,一定要把网络端口IP放前面,127.0.0.1放后面 protected-mode yes port 7001 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize yes supervised no pidfile redis_7001.pid loglevel notice logfile "/opt/redis-cluster/nodes-7001/redis_7001.log" databases 16 always-show-logo yes save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb dir /opt/redis-cluster/nodes-7001/ slave-serve-stale-data yes slave-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no slave-priority 100 lazyfree-lazy-eviction no lazyfree-lazy-expire no lazyfree-lazy-server-del no slave-lazy-flush no appendonly yes appendfilename "appendonly.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes aof-use-rdb-preamble no lua-time-limit 5000 cluster-enabled yes cluster-config-file nodes-7001.conf cluster-node-timeout 15000 cluster-slave-validity-factor 10 slowlog-log-slower-than 10000 slowlog-max-len 128 latency-monitor-threshold 0 notify-keyspace-events "" hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-size -2 list-compress-depth 0 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 hll-sparse-max-bytes 3000 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
4.启动redis及创建集群
依次启动各个redis实例
#启动redis实例 [root@elk01 redis-cluster]# redis-server /opt/redis-cluster/nodes-7002/redis.conf [root@elk01 redis-cluster]# ll nodes-7002/ #里面新增了一些文件 total 72 -rw-r--r-- 1 root root 0 Oct 26 14:20 appendonly.aof -rw-r--r-- 1 root root 114 Oct 26 14:20 nodes-7002.conf #自动生成的集群配置文件 -rw-r--r-- 1 root root 2555 Oct 26 14:20 redis_7002.log -rw-r--r-- 1 root root 6 Oct 26 14:20 redis_7002.pid -rw-r--r-- 1 root root 57834 Oct 26 14:19 redis.conf [root@elk01 redis-cluster]# ps -ef | grep redis root 15721 1 0 14:04 ? 00:00:01 redis-server *:7001 [cluster] root 19526 1 0 14:20 ? 00:00:00 redis-server *:7002 [cluster] root 20117 1 0 14:23 ? 00:00:00 redis-server *:7003 [cluster] root 20124 1 0 14:23 ? 00:00:00 redis-server *:7004 [cluster] #创建redis集群 创建redis集群使用redis-trib.rb命令,该命令是基于ruby的脚本,需要安装ruby,ruby-gem以及gem-redis [root@elk01 redis-cluster]# redis-trib.rb create --replicas 1 192.168.9.216:7001 192.168.9.216:7002 192.168.9.216:7003 192.168.9.216:7004 /usr/share/rubygems/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- redis (LoadError) from /usr/share/rubygems/rubygems/core_ext/kernel_require.rb:55:in `require' from /usr/local/bin/redis-trib.rb:25:in `<main>' #卸载老版本ruby2.0.0,安装ruby2.2.2以上版本 [root@elk01 ~]# ruby -v ruby 2.0.0p648 (2015-12-16) [x86_64-linux] [root@elk01 ~]# rpm -qa | grep ruby ruby-libs-2.0.0.648-29.el7.x86_64 rubygem-bigdecimal-1.2.0-29.el7.x86_64 rubygems-2.0.14.1-29.el7.noarch rubygem-psych-2.0.0-29.el7.x86_64 rubygem-json-1.7.7-29.el7.x86_64 ruby-irb-2.0.0.648-29.el7.noarch rubygem-io-console-0.4.2-29.el7.x86_64 ruby-2.0.0.648-29.el7.x86_64 rubygem-rdoc-4.0.0-29.el7.noarch [root@elk01 ~]# yum erase ruby … [root@elk01 ~]# rpm -qa | grep ruby ruby-libs-2.0.0.648-29.el7.x86_64 #安装新版本ruby [root@elk01 ruby-2.3.4]# pwd /root/ruby-2.3.4 [root@elk01 ruby-2.3.4]# ./configure … checking for nroff... /usr/bin/nroff .ext/include/x86_64-linux/ruby/config.h unchanged configure: ruby library version = 2.3.0 configure: creating ./config.status config.status: creating GNUmakefile config.status: creating Makefile config.status: creating ruby-2.3.pc [root@elk01 ruby-2.3.4]# make && make install … installing bundle gems: /usr/local/lib/ruby/gems/2.3.0 (build_info, cache, doc, extensions, gems, specifications) did_you_mean 1.0.0 power_assert 0.2.6 test-unit 3.1.5 net-telnet 0.1.1 minitest 5.8.5 rake 10.4.2 installing rdoc: /usr/local/share/ri/2.3.0/system installing capi-docs: /usr/local/share/doc/ruby [root@elk01 ruby-2.3.4]# ruby -v ruby 2.3.4p301 (2017-03-30 revision 58214) [x86_64-linux] #新版本ruby #安装ruby-redis.gem 推荐:如果默认国外的gem源连不上或者很慢,可以试试国内源 添加国内源命令:gem source -a https://gems.ruby-china.org 删除国外源并添加国内源:gem sources --add https://gems.ruby-china.org/ --remove https://rubygems.org/ #检测gem源,注意RubyGems源尽量用新版本,官方建议2.6.X,但是我用2.5.2也没什么问题 [root@elk01 redis-cluster]# gem source -l *** CURRENT SOURCES *** https://rubygems.org/ #国外源 https://gems.ruby-china.org #国内源 [root@elk01 ruby-2.3.4]# gem install redis Fetching: redis-4.0.1.gem (100%) Successfully installed redis-4.0.1 Parsing documentation for redis-4.0.1 Installing ri documentation for redis-4.0.1 Done installing documentation for redis after 1 seconds 1 gem installed [root@elk01 redis-cluster]# redis-trib.rb create --replicas 1 192.168.9.216:7001 192.168.9.216:7002 192.168.9.216:7003 192.168.9.216:7004 >>> Creating cluster *** ERROR: Invalid configuration for cluster creation. #错误表示redis集群至少要3个主,6个节点 *** Redis Cluster requires at least 3 master nodes. *** This is not possible with 4 nodes and 1 replicas per node. *** At least 6 nodes are required. #新增两个节点 [root@elk01 redis-cluster]# mkdir nodes-7005 [root@elk01 redis-cluster]# mkdir nodes-7006 You have mail in /var/spool/mail/root [root@elk01 redis-cluster]# cp nodes-7001/redis.conf nodes-7005/ [root@elk01 redis-cluster]# cp nodes-7001/redis.conf nodes-7006/ [root@elk01 redis-cluster]# vim nodes-7005/redis.conf [root@elk01 redis-cluster]# vim nodes-7006/redis.conf [root@elk01 redis-cluster]# ll total 0 drwxr-xr-x 2 root root 123 Oct 26 17:30 nodes-7001 drwxr-xr-x 2 root root 123 Oct 26 17:31 nodes-7002 drwxr-xr-x 2 root root 123 Oct 26 17:31 nodes-7003 drwxr-xr-x 2 root root 123 Oct 26 17:31 nodes-7004 drwxr-xr-x 2 root root 23 Oct 26 17:33 nodes-7005 drwxr-xr-x 2 root root 23 Oct 26 17:33 nodes-7006 [root@elk01 redis-cluster]# redis-server /opt/redis-cluster/nodes-7005/redis.conf [root@elk01 redis-cluster]# redis-server /opt/redis-cluster/nodes-7006/redis.conf [root@elk01 redis-cluster]# ps -ef | grep redis root 19865 1 0 17:30 ? 00:00:00 redis-server 127.0.0.1:7001 [cluster] root 19963 1 0 17:31 ? 00:00:00 redis-server 127.0.0.1:7002 [cluster] root 20055 1 0 17:31 ? 00:00:00 redis-server 127.0.0.1:7003 [cluster] root 20117 1 0 17:31 ? 00:00:00 redis-server 127.0.0.1:7004 [cluster] root 20831 1 0 17:34 ? 00:00:00 redis-server 127.0.0.1:7005 [cluster] root 20839 1 0 17:34 ? 00:00:00 redis-server 127.0.0.1:7006 [cluster] #创建集群成功 [root@elk01 redis-cluster]# redis-trib.rb create --replicas 1 192.168.9.216:7001 192.168.9.216:7002 192.168.9.216:7003 192.168.9.216:7004 192.168.9.216:7005 192.168.9.216:7006 >>> Creating cluster >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 192.168.9.216:7001 192.168.9.216:7002 192.168.9.216:7003 Adding replica 192.168.9.216:7004 to 192.168.9.216:7001 Adding replica 192.168.9.216:7005 to 192.168.9.216:7002 Adding replica 192.168.9.216:7006 to 192.168.9.216:7003 M: 23df12c2bafde34f5bdd53d3463ad20b8ab507d2 192.168.9.216:7001 slots:0-5460 (5461 slots) master M: 82b493cdc4d76f3ff4dbe91240290e2c3939f1ef 192.168.9.216:7002 slots:5461-10922 (5462 slots) master M: 7b9d9f8378cb0b27134cae105ccd59146d386739 192.168.9.216:7003 slots:10923-16383 (5461 slots) master S: 71a66eff2283d417373e90ed372d2de8703bf226 192.168.9.216:7004 replicates 23df12c2bafde34f5bdd53d3463ad20b8ab507d2 S: 2c80b6ac34a9d6c38ff425e4cb830ba579740831 192.168.9.216:7005 replicates 82b493cdc4d76f3ff4dbe91240290e2c3939f1ef S: 44976e464ce8146f7a6932faad2f4ad3c9bdd06d 192.168.9.216:7006 replicates 7b9d9f8378cb0b27134cae105ccd59146d386739 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 192.168.9.216:7001) M: 23df12c2bafde34f5bdd53d3463ad20b8ab507d2 192.168.9.216:7001 slots:0-5460 (5461 slots) master 1 additional replica(s) M: 82b493cdc4d76f3ff4dbe91240290e2c3939f1ef 127.0.0.1:7002 slots:5461-10922 (5462 slots) master 1 additional replica(s) S: 71a66eff2283d417373e90ed372d2de8703bf226 127.0.0.1:7004 slots: (0 slots) slave replicates 23df12c2bafde34f5bdd53d3463ad20b8ab507d2 S: 44976e464ce8146f7a6932faad2f4ad3c9bdd06d 127.0.0.1:7006 slots: (0 slots) slave replicates 7b9d9f8378cb0b27134cae105ccd59146d386739 S: 2c80b6ac34a9d6c38ff425e4cb830ba579740831 127.0.0.1:7005 slots: (0 slots) slave replicates 82b493cdc4d76f3ff4dbe91240290e2c3939f1ef M: 7b9d9f8378cb0b27134cae105ccd59146d386739 127.0.0.1:7003 slots:10923-16383 (5461 slots) master 1 additional replica(s) [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
5.测试集群
[root@elk01 redis-cluster]# redis-cli -c -p 7001 #登录集群,-c参数是必需的 127.0.0.1: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:115 cluster_stats_messages_pong_sent:125 cluster_stats_messages_sent:240 cluster_stats_messages_ping_received:120 cluster_stats_messages_pong_received:115 cluster_stats_messages_meet_received:5 cluster_stats_messages_received:240 127.0.0.1:7001> CLUSTER nodes 82b493cdc4d76f3ff4dbe91240290e2c3939f1ef 127.0.0.1:7002@17002 master - 0 1509010649120 2 connected 5461-10922 71a66eff2283d417373e90ed372d2de8703bf226 127.0.0.1:7004@17004 slave 23df12c2bafde34f5bdd53d3463ad20b8ab507d2 0 1509010648118 4 connected 44976e464ce8146f7a6932faad2f4ad3c9bdd06d 127.0.0.1:7006@17006 slave 7b9d9f8378cb0b27134cae105ccd59146d386739 0 1509010650123 6 connected 2c80b6ac34a9d6c38ff425e4cb830ba579740831 127.0.0.1:7005@17005 slave 82b493cdc4d76f3ff4dbe91240290e2c3939f1ef 0 1509010651126 5 connected 7b9d9f8378cb0b27134cae105ccd59146d386739 127.0.0.1:7003@17003 master - 0 1509010649000 3 connected 10923-16383 23df12c2bafde34f5bdd53d3463ad20b8ab507d2 192.168.9.216:7001@17001 myself,master - 0 1509010649000 1 connected 0-5460 127.0.0.1:7001> set foo bar -> Redirected to slot [12182] located at 127.0.0.1:7003 OK 127.0.0.1:7003> get foo "bar"