redis 集群配置

redis 集群配置

①,查看gcc是否安装

gcc -v

没有就安装gcc

yum install gcc

②加压redis文件并安装

解压 tar -zxvf redis-xxx.tar.gz

进入 cd redis-3.0.6

键入 make 进行安装

将 redis-cli,redis-sentinel,redis-server 放入/usr/local/bin

这样在任意目录下就能启动redis了

③新建redis_cluster 目录

在redis_cluster 目录下新建 7001,7002,7003,7004,7005,7006文件夹

cp redis.conf /redis_cluster/redis7001.conf

vim redis7001.conf

修改如下:

daemonize yes #后台启动

port 700x #修改端口号,从7001到7006

cluster-enabled yes #开启cluster,去掉注释

cluster-config-file nodes.conf

cluster-node-timeout 15000

appendonly yes

dir /redis-3.0.6/redis_cluster/700x #修改目录,从7001到7006

bind 127.0.0.1

④redis-server redis700x.conf 启动对应的redis

ps -ef|grep redis 若能看到下图,说明前面配置成功

[root@localhost src]# ps -ef|grep redis
root      2304     1  0 20:08 ?        00:00:01 redis-server 127.0.0.1:7002 [cluster]
root      2328     1  0 20:15 ?        00:00:01 redis-server 127.0.0.1:7001 [cluster]
root      2334     1  0 20:15 ?        00:00:01 redis-server 127.0.0.1:7003 [cluster]
root      2341     1  0 20:15 ?        00:00:01 redis-server 127.0.0.1:7004 [cluster]
root      2349     1  0 20:17 ?        00:00:01 redis-server 127.0.0.1:7006 [cluster]
root      2355     1  0 20:17 ?        00:00:00 redis-server 127.0.0.1:7005 [cluster]
root      4576  2272  0 20:47 pts/0    00:00:00 grep redis
 

⑤ 安装ruby环境:

yum install ruby

yum install rubygems

下载redis-3.2.2.gem(可以到这篇博客里下载)

放入 redis_cluster目录,执行如下命令

gem install redis-3.2.2.gem

⑥使用redis-trib.rb创建集群

  1. ./redis-trib.rb create --replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006

  2. 看到如下情况,表示成功

[root@localhost src]# ./redis-trib.rb create --replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
127.0.0.1:7001
127.0.0.1:7002
127.0.0.1:7003
Adding replica 127.0.0.1:7004 to 127.0.0.1:7001
Adding replica 127.0.0.1:7005 to 127.0.0.1:7002
Adding replica 127.0.0.1:7006 to 127.0.0.1:7003
M: 0a0bfd616dd785ef341388979b2090ab43280c92 127.0.0.1:7001
   slots:0-5460 (5461 slots) master
M: efd75010158f56f965f4f61b74a0cfff0109fa89 127.0.0.1:7002
   slots:5461-10922 (5462 slots) master
M: a0a7672fbb88a6dac2e05bffb2a5853bff8cf70f 127.0.0.1:7003
   slots:10923-16383 (5461 slots) master
S: df907bd1d011436651bb6b199b537d267e7e49f2 127.0.0.1:7004
   replicates 0a0bfd616dd785ef341388979b2090ab43280c92
S: 79dc86a9ed26fc14cff1b74ad2428b848d18e892 127.0.0.1:7005
   replicates efd75010158f56f965f4f61b74a0cfff0109fa89
S: 4aa14389b41103f446227fbe13155a0729231960 127.0.0.1:7006
   replicates a0a7672fbb88a6dac2e05bffb2a5853bff8cf70f
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 127.0.0.1:7001)
M: 0a0bfd616dd785ef341388979b2090ab43280c92 127.0.0.1:7001
   slots:0-5460 (5461 slots) master
M: efd75010158f56f965f4f61b74a0cfff0109fa89 127.0.0.1:7002
   slots:5461-10922 (5462 slots) master
M: a0a7672fbb88a6dac2e05bffb2a5853bff8cf70f 127.0.0.1:7003
   slots:10923-16383 (5461 slots) master
M: df907bd1d011436651bb6b199b537d267e7e49f2 127.0.0.1:7004
   slots: (0 slots) master
   replicates 0a0bfd616dd785ef341388979b2090ab43280c92
M: 79dc86a9ed26fc14cff1b74ad2428b848d18e892 127.0.0.1:7005
   slots: (0 slots) master
   replicates efd75010158f56f965f4f61b74a0cfff0109fa89
M: 4aa14389b41103f446227fbe13155a0729231960 127.0.0.1:7006
   slots: (0 slots) master
   replicates a0a7672fbb88a6dac2e05bffb2a5853bff8cf70f
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
 

⑦连接redis集群客户端

  1. [root@localhost redis01]# ./redis-cli -c -p 7001

  2. 127.0.0.1:7001> set name andy

  3. -> Redirected to slot [5798] located at 127.0.0.1:7002

  4. OK

  5. 127.0.0.1:7002> get name

  6. "andy"

  7. 127.0.0.1:7002>

参考https://www.cnblogs.com/yingchen/p/6763524.html

猜你喜欢

转载自my.oschina.net/u/3574106/blog/1809792