在一台机器上搭建redis集群 redis版本3.2php
我这也是须要搭建而后网上找文档然最后总结的
参考:http://www.javashuo.com/article/p-xubrzzuy-ck.htmlnode
1,解压源码包安装redisredis
#安装须要的一些包 yum install ruby rubygem openssl #此步安装时候,是没有输出的,只要不报错慢慢等待就行了或者加 -V 参数看过程 gem install redis --version 3.0.0 #解压安装 tar xf redis-3.2.9.tar.gz cd redis-3.2.9 make make install PREFIX=/usr/local/redis #把编译好的redis命令安装到这个目录下面
2,配置redis节点;redis3.x 最少须要6个节点[3主3从的模式]</p>vim
#配置节点目录 mkdir -p /opt/redis/ && cd /opt/redis mkdir 7000 7001 7002 7003 7004 7005 cp -rf /usr/local/redis/bin 7000 cp -rf /usr/local/redis/bin 7001 cp -rf /usr/local/redis/bin 7002 cp -rf /usr/local/redis/bin 7003 cp -rf /usr/local/redis/bin 7004 cp -rf /usr/local/redis/bin 7005 #节点配置文件 cd /opt/redis/7000 vim redis.conf bind 192.168.16.186 127.0.0.1 #这里能够多个ip用空格分开 port 7000 daemonize yes cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 cluster-require-full-coverage no appendonly yes pidfile /var/run/redis_7000.pid logfile /var/log/redis/redis_7000.log dbfilename dump_7000.rdb appendfilename "appendonly_7000.aof" 其余几个节点的配置文件和这个同样就行,须要注意须要改动对应端口和一些文件名称 #建立日志目录 mkdir /var/log/redis #启动各节点 cd /opt/redis/7000 ./bin/redis-server ./redis.conf cd /opt/redis/7001 ./bin/redis-server ./redis.conf cd /opt/redis/7002 ./bin/redis-server ./redis.conf cd /opt/redis/7003 ./bin/redis-server ./redis.conf cd /opt/redis/7004 ./bin/redis-server ./redis.conf cd /opt/redis/7005 ./bin/redis-server ./redis.conf
3,建立集群ruby
#进去源码包目录下src目录下面,执行集群建立 cd redis-3.2.9/src/ ./redis-trib.rb create --replicas 1 192.168.16.186:7000 192.168.16.186:7001 192.168.16.186:7002 192.168.16.186:7003 192.168.16.186:7004 192.168.16.186:7005 >>> Creating cluster /usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: wrong element type nil at 0 (expected array) /usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: ignoring wrong elements is deprecated, remove them explicitly /usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: this causes ArgumentError in the next release /usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: wrong element type nil at 19 (expected array) /usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: ignoring wrong elements is deprecated, remove them explicitly /usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: this causes ArgumentError in the next release /usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: wrong element type nil at 20 (expected array) /usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: ignoring wrong elements is deprecated, remove them explicitly /usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: this causes ArgumentError in the next release /usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: wrong element type nil at 25 (expected array) /usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: ignoring wrong elements is deprecated, remove them explicitly /usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: this causes ArgumentError in the next release /usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: wrong element type nil at 26 (expected array) ......此处省略...... /usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: ignoring wrong elements is deprecated, remove them explicitly /usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: this causes ArgumentError in the next release /usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: wrong element type nil at 104 (expected array) /usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: ignoring wrong elements is deprecated, remove them explicitly /usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: this causes ArgumentError in the next release /usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: wrong element type nil at 105 (expected array) /usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: ignoring wrong elements is deprecated, remove them explicitly /usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: this causes ArgumentError in the next release >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 192.168.16.186:7000 192.168.16.186:7001 192.168.16.186:7002 Adding replica 192.168.16.186:7003 to 192.168.16.186:7000 Adding replica 192.168.16.186:7004 to 192.168.16.186:7001 Adding replica 192.168.16.186:7005 to 192.168.16.186:7002 M: 346226a5653bfe33f6d887920e99609b9bbe94ec 192.168.16.186:7000 slots:0-5460 (5461 slots) master M: 5dc8c8fae8addd7321b9b613123bfc4b25608bbc 192.168.16.186:7001 slots:5461-10922 (5462 slots) master M: be357c14ac96528f86b5a6d5457086e307d05b7e 192.168.16.186:7002 slots:10923-16383 (5461 slots) master S: 99106ae5121c1127b53374e0d678b543cd54026b 192.168.16.186:7003 replicates 346226a5653bfe33f6d887920e99609b9bbe94ec S: 4b788f70b19969fb9f41a4b2b77265c02df3718e 192.168.16.186:7004 replicates 5dc8c8fae8addd7321b9b613123bfc4b25608bbc S: 9e9df80f856b6dd0e54da6f1c01bc4ea9f131ff2 192.168.16.186:7005 replicates be357c14ac96528f86b5a6d5457086e307d05b7e 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.. /usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: wrong element type nil at 0 (expected array) /usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: ignoring wrong elements is deprecated, remove them explicitly /usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: this causes ArgumentError in the next release /usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: wrong element type nil at 19 (expected array) /usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: ignoring wrong elements is deprecated, remove them explicitly /usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: this causes ArgumentError in the next release /usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: wrong element type nil at 20 (expected array) ......此处省略...... /usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: ignoring wrong elements is deprecated, remove them explicitly /usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: this causes ArgumentError in the next release /usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: wrong element type nil at 105 (expected array) /usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: ignoring wrong elements is deprecated, remove them explicitly /usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: this causes ArgumentError in the next release >>> Performing Cluster Check (using node 192.168.16.186:7000) M: 346226a5653bfe33f6d887920e99609b9bbe94ec 192.168.16.186:7000 slots:0-5460 (5461 slots) master 1 additional replica(s) S: 4b788f70b19969fb9f41a4b2b77265c02df3718e 192.168.16.186:7004 slots: (0 slots) slave replicates 5dc8c8fae8addd7321b9b613123bfc4b25608bbc M: be357c14ac96528f86b5a6d5457086e307d05b7e 192.168.16.186:7002 slots:10923-16383 (5461 slots) master 1 additional replica(s) M: 5dc8c8fae8addd7321b9b613123bfc4b25608bbc 192.168.16.186:7001 slots:5461-10922 (5462 slots) master 1 additional replica(s) S: 9e9df80f856b6dd0e54da6f1c01bc4ea9f131ff2 192.168.16.186:7005 slots: (0 slots) slave replicates be357c14ac96528f86b5a6d5457086e307d05b7e S: 99106ae5121c1127b53374e0d678b543cd54026b 192.168.16.186:7003 slots: (0 slots) slave replicates 346226a5653bfe33f6d887920e99609b9bbe94ec [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
输入 yes
建立成功!
4,链接测试bash
[root@localhost ~]# redis-cli -c -p 7000 -h 192.168.16.186 192.168.16.186:7000> set testkey 123456 OK 192.168.16.186:7000> get testkey "123456" 192.168.16.186:7000> exit [root@localhost ~]# redis-cli -c -p 7005 -h 192.168.16.186 192.168.16.186:7005> get testkey -> Redirected to slot [4757] located at 192.168.16.186:7000 "123456" 192.168.16.186:7000> exit #查看节点信息 [root@localhost ~]# redis-cli -c -p 7000 -h 192.168.16.186 192.168.16.186:7000> CLUSTER NODES 4b788f70b19969fb9f41a4b2b77265c02df3718e 192.168.16.186:7004 slave 5dc8c8fae8addd7321b9b613123bfc4b25608bbc 0 1522393285325 5 connected be357c14ac96528f86b5a6d5457086e307d05b7e 192.168.16.186:7002 master - 0 1522393286828 3 connected 10923-16383 5dc8c8fae8addd7321b9b613123bfc4b25608bbc 192.168.16.186:7001 master - 0 1522393284824 2 connected 5461-10922 9e9df80f856b6dd0e54da6f1c01bc4ea9f131ff2 192.168.16.186:7005 slave be357c14ac96528f86b5a6d5457086e307d05b7e 0 1522393286828 6 connected 346226a5653bfe33f6d887920e99609b9bbe94ec 192.168.16.186:7000 myself,master - 0 0 1 connected 0-5460 99106ae5121c1127b53374e0d678b543cd54026b 192.168.16.186:7003 slave 346226a5653bfe33f6d887920e99609b9bbe94ec 0 1522393286328 4 connected
5,redis 集群的启动、中止[使用脚本]app
启动 [root@localhost redis]# cat start-redis.sh #!/bin/bash #7000 cd /opt/redis/7000 ./bin/redis-server ./redis.conf #7001 cd /opt/redis/7001 ./bin/redis-server ./redis.conf #7002 cd /opt/redis/7002 ./bin/redis-server ./redis.conf #7003 cd /opt/redis/7003 ./bin/redis-server ./redis.conf #7004 cd /opt/redis/7004 ./bin/redis-server ./redis.conf #7005 cd /opt/redis/7005 ./bin/redis-server ./redis.conf #查看redis进程 ps -ef |grep redis #中止 [root@localhost redis]# cat stop-redis.sh #!/bin/bash /opt/redis/7000/bin/redis-cli -p 7000 -h 192.168.16.186 shutdown /opt/redis/7001/bin/redis-cli -p 7001 -h 192.168.16.186 shutdown /opt/redis/7002/bin/redis-cli -p 7002 -h 192.168.16.186 shutdown /opt/redis/7003/bin/redis-cli -p 7003 -h 192.168.16.186 shutdown /opt/redis/7004/bin/redis-cli -p 7004 -h 192.168.16.186 shutdown /opt/redis/7005/bin/redis-cli -p 7005 -h 192.168.16.186 shutdown ps -ef |grep redis
2台机器搭建集群
每台3个节点;配置大同小异这里就不在贴出过程了
1,一样道理安装不变,配置节点每台为3个就行,2台机器6个节点便可;端口同样不同均可以
2,配置文件bind的IP地址修改,两台机器要互通
3,建立集群时在2台机器任意一台便可,注意节点地址和端口的变化ide