一、安装redis
1、安装gcc环境
安装 gcc-c++
2、下载源码包并解压
wget http://download.redis.io/releases/redis-3.2.4.tar.gz
tar -zxvf redis-3.2.4.tar.gz
cd redis-3.2.4
3、安装ruby2.4.1并编译(一定要安装大于2.2.2的ruby,要不然redis在make的时候会报错)
wget http://ftp.ruby-lang.org/pub/ruby/2.4/ruby-2.4.1.tar.gz tar -zxvf ruby-2.4.1.tar.gz cd ruby-2.4.1 mkdir -p /usr/local/ruby ./configure --prefix=/usr/local/ruby make && make install ln -s /usr/local/ruby/bin/ruby /usr/bin/ruby
4.安装
make make install PREFIX=/usr/local/redis cp redis.conf /usr/local/redis/bin/redis.conf chmod /usr/local/redis/bin/redis.conf (也可以直接在解压包进行操作不建议)
5.启动redis
./redis-server redis.conf
6、验证
[root@host-172-16-80-177 bin]# ./redis-cli -p 6379 127.0.0.1:6379> get (error) ERR wrong number of arguments for 'get' command 127.0.0.1:6379> set aa hzb OK 127.0.0.1:6379> get aa "hzb"
二、搭建redis集群(集群模式此处安装的属于三主节点无备用节点)
1、需要几个节点配置几分配置文件
daemonize yes
Port 7001
logfile "./redis-7001.log"
protected-mode no
pidfile /var/run/redis_7001.pid
cluster-enabled yes
2.将redis-trib.rb复到制redis目录里面
3.安装gem
sudo apt-get install rubygems
4.安装ruby的redis包
gem install redis
[root@host-172-16-80-177 include]# 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
5.分别启动redis1 reids2 reids3 .........
./redis-server redis-7001.conf
ps -ef|grep redis
6.用redis-trib.rb构建集群
cd /usr/local/redis-cluster/ [root@host-172-16-80-177 redis-cluster]# ./redis-trib.rb create --replicas 1 172.16.80.177:7001 172.16.80.177:7002 172.16.80.177:7003 172.16.80.177:7004 172.16.80.177:7005 172.16.80.177:7006 >>> Creating cluster [ERR] Sorry, can't connect to node 172.16.80.177:7001 以上错误原因是redis.conf里面未开起远程访问 打开每个redis里面的.conf文件,找到 # JUST COMMENT THE FOLLOWING LINE. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ bind 127.0.0.1 # Protected mode is a layer of security protection, in order to avoid that # Redis instances left open on the internet are accessed and exploited.
将上面的bind 127.0.0.1改成bind 172.16.80.177(外部可以访问的ip),然后重启各服务
再用redis-trib.rb创建集群:
./redis-trib.rb create --replicas 1 172.16.80.177:7001 172.16.80.177:7002 172.16.80.177:7003 172.16.80.177:7004 172.16.80.177:7005 172.16.80.177:7006
redis 集权必须最少六个节点 如果配置三节点 去掉 --replica
./redis-trib.rb create 192.168.28.223:5551 192.168.28.223:6661 192.168.28.223:7771
[root@host-172-16-80-177 redis-cluster]# ./redis-trib.rb create --replicas 1 172.16.80.177:7001 172.16.80.177:7002 172.16.80.177:7003 172.16.80.177:7004 172.16.80.177:7005 172.16.80.177:7006 >>> Creating cluster >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 172.16.80.177:7001 172.16.80.177:7002 172.16.80.177:7003 Adding replica 172.16.80.177:7004 to 172.16.80.177:7001 Adding replica 172.16.80.177:7005 to 172.16.80.177:7002 Adding replica 172.16.80.177:7006 to 172.16.80.177:7003 M: 40bbe8a979fe39f1f0941cc8e118443436fc71d8 172.16.80.177:7001 slots:0-5460 (5461 slots) master M: e6b76ee1540c9be6c44abc3dec42378f8cfd0191 172.16.80.177:7002 slots:5461-10922 (5462 slots) master M: fdc248e24bd9f8690c652213a5781f4e567208e8 172.16.80.177:7003 slots:10923-16383 (5461 slots) master S: 38faede9dd348983b1b8b80edc975e8d34105e5c 172.16.80.177:7004 replicates 40bbe8a979fe39f1f0941cc8e118443436fc71d8 S: 38faede9dd348983b1b8b80edc975e8d34105e5c 172.16.80.177:7005 replicates e6b76ee1540c9be6c44abc3dec42378f8cfd0191 S: 38faede9dd348983b1b8b80edc975e8d34105e5c 172.16.80.177:7006 replicates fdc248e24bd9f8690c652213a5781f4e567208e8 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 172.16.80.177:7001) M: 40bbe8a979fe39f1f0941cc8e118443436fc71d8 172.16.80.177:7001 slots:0-5460 (5461 slots) master additional replica(s) M: e6b76ee1540c9be6c44abc3dec42378f8cfd0191 172.16.80.177:7002 slots:5461-10922 (5462 slots) master additional replica(s) S: 38faede9dd348983b1b8b80edc975e8d34105e5c 172.16.80.177:7004 slots: (0 slots) slave replicates 40bbe8a979fe39f1f0941cc8e118443436fc71d8 M: fdc248e24bd9f8690c652213a5781f4e567208e8 172.16.80.177:7003 slots:10923-16383 (5461 slots) master additional replica(s) [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
至此,redis集群创建成功
验证集群是否成功
[root@host-172-16-80-177 redis2]# ./redis-cli -h 172.16.80.177 -c -p 7002 172.16.80.177:7002> set hello helloword -> Redirected to slot [866] located at 172.16.80.177:7001 OK 172.16.80.177:7006> get hello -> Redirected to slot [866] located at 172.16.80.177:7001 "helloword"
搭建redis集群遇到的问题收集
一、集群搭建主要配置如下:
P61 bind 127.0.0.1//默认ip为127.0.0.1改为其他节点机器可访问 的ip 注释掉bind;可以监听连接当前服务的所有-h 后的ip;例如;绑定了 127.0.0.1,但是登录时使用-h 10.9.17.153 P80 protected-mode no //yes修改为no P84 port 8000 //端口8000-8005 P128 daemonize yes //后台运行 P150 pidfile /var/run/redis_8000.pid P163 logfile 8000/redis.log P237 dump dump8000.rdb P593 appendonly yes P597 appendfilename “appendonly.aof” P721 cluster-enabled yes P729 cluster-config-file nodes_8000.conf P735 cluster-node-timeout 15000 //请求超时 默认15秒,可 自行设置
二、搭建完成后依次启动各个实例
redis-server 8000/redis.conf redis-server 8001/redis.conf redis-server 8002/redis.conf redis-server 8003/redis.conf redis-server 8004/redis.conf redis-server 8005/redis.conf
三、检查启动结果:
[root@cenos01 redis-3.2.11]# ps -ef|grep 800 root 19360 1 0 23:19 ? 00:00:01 redis-server *:8001 [cluster] root 19364 1 0 23:19 ? 00:00:01 redis-server *:8002 [cluster] root 19368 1 0 23:19 ? 00:00:01 redis-server *:8003 [cluster] root 19372 1 0 23:19 ? 00:00:01 redis-server *:8004 [cluster] root 19378 1 0 23:19 ? 00:00:01 redis-server *:8005 [cluster] root 19433 1 0 23:30 ? 00:00:01 redis-server *:8000 [cluster]
四、以上启动后并不是一个真正的集群,以下开始创建集群
在redis的根目录下的src目录下执行***/redis-3.2.11/src,报错信息如下 [root@cenos01 src]# ./redis-trib.rb create 10.211.55.5:8000 10.211.55.5:8001 10.211.55.5:8002
Creating cluster Performing hash slots allocation on 3 nodes… Using 3 masters: 10.211.55.5:8000 10.211.55.5:8001 10.211.55.5:8002 M: 8ce851252f32fcab268e77d3b3ed976d57d2f498 10.211.55.5:8000 slots:0-5460 (5461 slots) master M: 6f4a31fc322725fc4ed453a588f344fed4f261bf 10.211.55.5:8001 slots:5461-10922 (5462 slots) master M: da7d09090520a0e6287ea12fe7f48225d6665f68 10.211.55.5:8002 slots:10923-16383 (5461 slots) master Can I set the above configuration? (type ‘yes’ to accept): yes /usr/local/lib/ruby/gems/2.3.0/gems/redis-4.0.2/lib/redis/client.rb:119:in
call': ERR Slot 1180 is already busy (Redis::CommandError) from /usr/local/lib/ruby/gems/2.3.0/gems/redis-4.0.2/lib/redis.rb:2854:in
block in method_missing’ from /usr/local/lib/ruby/gems/2.3.0/gems/redis-4.0.2/lib/redis.rb:45:inblock in synchronize' from /usr/local/lib/ruby/2.3.0/monitor.rb:214:in
mon_synchronize’ from /usr/local/lib/ruby/gems/2.3.0/gems/redis-4.0.2/lib/redis.rb:45:insynchronize' from /usr/local/lib/ruby/gems/2.3.0/gems/redis-4.0.2/lib/redis.rb:2853:in
method_missing’ from ./redis-trib.rb:212:inflush_node_config' from ./redis-trib.rb:776:in
block in flush_nodes_config’ from ./redis-trib.rb:775:ineach' from ./redis-trib.rb:775:in
flush_nodes_config’ from ./redis-trib.rb:1296:increate_cluster_cmd' from ./redis-trib.rb:1701:in
’
五、针对以上问题的解决方法:
所有redis服务器都执行以下命令:
$redis-cli -p 端口号 $flushall $cluster reset soft $exit
六、解决后再次创建,就成功了
[root@cenos01 src]# ./redis-trib.rb create 10.211.55.5:8000 10.211.55.5:8001 10.211.55.5:8002
Creating cluster Performing hash slots allocation on 3 nodes… Using 3 masters: 10.211.55.5:8000 10.211.55.5:8001 10.211.55.5:8002 M: 8ce851252f32fcab268e77d3b3ed976d57d2f498 10.211.55.5:8000 slots:0-5460 (5461 slots) master M: 6f4a31fc322725fc4ed453a588f344fed4f261bf 10.211.55.5:8001 slots:5461-10922 (5462 slots) master M: da7d09090520a0e6287ea12fe7f48225d6665f68 10.211.55.5:8002 slots:10923-16383 (5461 slots) master 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 10.211.55.5:8000) M: 8ce851252f32fcab268e77d3b3ed976d57d2f498 10.211.55.5:8000 slots:0-5460 (5461 slots) master 0 additional replica(s) M: 6f4a31fc322725fc4ed453a588f344fed4f261bf 10.211.55.5:8001 slots:5461-10922 (5462 slots) master 0 additional replica(s) M: da7d09090520a0e6287ea12fe7f48225d6665f68 10.211.55.5:8002 slots:10923-16383 (5461 slots) master 0 additional replica(s) [OK] All nodes agree about slots configuration.
Check for open slots… Check slots coverage… [OK] All 16384 slots covered.
**
七、查看集群状态
** [root@cenos01 src]# redis-cli -c -p 8000 127.0.0.1:8000> cluster info cluster_state:ok cluster_slots_assigned:16384 cluster_slots_ok:16384 cluster_slots_pfail:0 cluster_slots_fail:0 cluster_known_nodes:3 cluster_size:3 cluster_current_epoch:3 cluster_my_epoch:1 cluster_stats_messages_sent:2059 cluster_stats_messages_received:2059
cluster_current_epoch:6 代表当前集群的最新逻辑计算时间,数字 越大,表示操作或者配置越新,整个集群的这个值是保持一致的 cluster_my_epoch:1代表当前节点的逻辑计算时间
八、查看槽道分配情况和集群节点信息
127.0.0.1:8000> cluster nodes 8ce851252f32fcab268e77d3b3ed976d57d2f498 10.211.55.5:8000 myself,master - 0 0 1 connected 0-5460 6f4a31fc322725fc4ed453a588f344fed4f261bf 10.211.55.5:8001 master - 0 1538582282356 2 connected 5461-10922 da7d09090520a0e6287ea12fe7f48225d6665f68 10.211.55.5:8002 master - 0 1538582283359 3 connected 10923-16383
感谢参考链接 https://www.cnblogs.com/boshen-hzb/p/7699783.html
http://www.itbox.info/article/70623.html