Redis学习笔记7--Redis集群搭建

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis学习笔记7--Redis集群搭建相关的知识,希望对你有一定的参考价值。

在安装redis集群之前,要先安装一些环境

(1)安装zlib

确保系统安装zlib,否则gem install会报(no such file to load -- zlib)

zlib-1.2.6.tar  

./configure

make

make install

 

(2)安装ruby

ruby 使用本地yum安装也能使用(推荐)

编译安装:

这里不能使用make & make install

ruby1.9.2   

./configure -prefix=/usr/local/ruby  

make  

make install  

sudo cp ruby /usr/local/bin  

 

(3)安装rubygem:version(1.8.16)

# rubygems-1.8.24.tgz

cd /path/gem

sudo ruby setup.rb

 

 

(4)安装gem-redis:version(3.0.0)

 将网络放开,切换为淘宝源地址:

# gem source -l

# gem source --remove http://rubygems.org/

# gem sources -a https://ruby.taobao.org/

# gem source -l

gem install redis --version 3.0.0

 

报错信息:

ERROR: RDoc documentation generator not installed: no such file to load -- rdoc/rdoc

解决办法:

yum install ruby-rdoc -y

vi  redis.conf

 

##修改配置文件中的下面选项

port 7000

daemonize yes

cluster-enabled yes

cluster-config-file nodes-对应端口号.conf

cluster-node-timeout 5000

appendonly yes

bind ip地址

 

以配置文件分别启动这6redis实例

redis-server redis.conf指定以配置文件启动

--replicas 1代表一个主节点下面有一个副节点

./redis-trib.rb create --replicas 1 10.10.10.30:6379 10.10.10.30:6380 10.10.10.40:6381 10.10.10.40:6382 10.10.10.50:6383 10.10.10.50:6384

[[email protected] src]# ./redis-trib.rb create --replicas 1 10.10.10.30:6379 10.10.10.30:6380 10.10.10.40:6381 10.10.10.40:6382 10.10.10.50:6383 10.10.10.50:6384
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
10.10.10.50:6383
10.10.10.40:6381
10.10.10.30:6379
Adding replica 10.10.10.40:6382 to 10.10.10.50:6383
Adding replica 10.10.10.50:6384 to 10.10.10.40:6381
Adding replica 10.10.10.30:6380 to 10.10.10.30:6379
M: 89626713538d146b7890e6f47c62c6aad78f53d2 10.10.10.30:6379
   slots:10923-16383 (5461 slots) master
S: 825e93a27442e225204ee30c59691d0d5e29fe26 10.10.10.30:6380
   replicates 89626713538d146b7890e6f47c62c6aad78f53d2
M: 4919bd2396b138160fd10946f762e0b5b7ef755f 10.10.10.40:6381
   slots:5461-10922 (5462 slots) master
S: 5f06981deeb179aee3671143d8dcfd4001896716 10.10.10.40:6382
   replicates f276f424f1807f2d381cd054ac7579e2b790d0cf
M: f276f424f1807f2d381cd054ac7579e2b790d0cf 10.10.10.50:6383
   slots:0-5460 (5461 slots) master
S: fb84aeb17bb628840f19566a0a2d89e60ab540cc 10.10.10.50:6384
   replicates 4919bd2396b138160fd10946f762e0b5b7ef755f
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.10.10.30:6379)
M: 89626713538d146b7890e6f47c62c6aad78f53d2 10.10.10.30:6379
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 825e93a27442e225204ee30c59691d0d5e29fe26 10.10.10.30:6380
   slots: (0 slots) slave
   replicates 89626713538d146b7890e6f47c62c6aad78f53d2
S: 5f06981deeb179aee3671143d8dcfd4001896716 10.10.10.40:6382
   slots: (0 slots) slave
   replicates f276f424f1807f2d381cd054ac7579e2b790d0cf
S: fb84aeb17bb628840f19566a0a2d89e60ab540cc 10.10.10.50:6384
   slots: (0 slots) slave
   replicates 4919bd2396b138160fd10946f762e0b5b7ef755f
M: 4919bd2396b138160fd10946f762e0b5b7ef755f 10.10.10.40:6381
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
M: f276f424f1807f2d381cd054ac7579e2b790d0cf 10.10.10.50:6383
   slots:0-5460 (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.

 

 

连接redis集群必须指定IP地址和端口:

./redis-cli -h 10.10.10.30 -p 6379 -c

10.10.10.30:6379> set name1 qiangge

OK

10.10.10.30:6379> set name2 wanningxi

-> Redirected to slot [742] located at 10.10.10.50:6383

OK

10.10.10.50:6383> set name3 longge

OK

10.10.10.50:6383> set name4 xiaomin

-> Redirected to slot [8736] located at 10.10.10.40:6381

OK

10.10.10.40:6381> get name3

-> Redirected to slot [4807] located at 10.10.10.50:6383

"longge"

10.10.10.50:6383> get name2

"wanningxi"

10.10.10.50:6383> get name1

-> Redirected to slot [12933] located at 10.10.10.30:6379

"qiangge"

10.10.10.30:6379> get name

-> Redirected to slot [5798] located at 10.10.10.40:6381

"wanning"

10.10.10.40:6381>

 

 

 

 

以上是关于Redis学习笔记7--Redis集群搭建的主要内容,如果未能解决你的问题,请参考以下文章

Linux07 /redis

Redis学习笔记7:Redis持久化-RDBAOF

Redis学习笔记7:Redis持久化-RDBAOF

Redis学习笔记 [事务和锁机制持久化机制模拟主从复制集群搭建]

Docker 学习笔记总结

Docker 学习笔记总结