redis多机集群部署

2018/08 作者:ihunter 0 0

redis多机集群部署文档

(要让集群正常工作至少需要3个主节点,在这里我们要创建6个redis节点,其中三个为主节点,三个为从节点,对应的redis节点的ip和端口对应关系如下)

10.168.32.116:6379

10.168.32.117:6379

10.168.32.118:6379

10.168.32.119:6379

10.168.32.120:6379

10.168.32.121:6379

 

在安装集群之前,需要在服务器上安装ruby环境,

yum install -y ruby rubygems

gem install redis

 

下载redis版本,

#git clone https://github.com/antirez/redis.git

#mv redis /usr/local/redis

#cd  /usr/local/redis

#make && make install

 

修改配置集群需要的基本配置,其余部分,根据实际需要配置

#vi /usr/local/redis/redis.conf

bind 10.168.32.116

port 6379

daemonize yes

cluster-enabled yes

cluster-config-file nodes.conf

appendonly yes

修改完这几行,把redis.conf分发到其余的服务器,所有服务器的配置可以完全一致,bind是主机ip(如果是在同一台服务器进行实验,需要修改port,每个redis使用独立的端口)

 

配置同步之后,启动所有服务器的redis

redis-server /usr/local/redis/redis.conf

 

执行redis的创建集群命令创建集群:

#./redis-trib.rb create --replicas 1 10.168.32.116:6379 10.168.32.117:6379 10.168.32.118:6379 10.168.32.119:6379 10.168.32.120:6379 10.168.32.121:6379

 

>>> Creating cluster

Connecting to node 10.168.32.116:6379: OK

Connecting to node 10.168.32.117:6379: OK

Connecting to node 10.168.32.118:6379: OK

Connecting to node 10.168.32.119:6379: OK

Connecting to node 10.168.32.120:6379: OK

Connecting to node 10.168.32.121:6379: OK

>>> Performing hash slots allocation on 6 nodes...

Using 3 masters:

10.168.32.121:6379

10.168.32.120:6379

10.168.32.119:6379

Adding replica 10.168.32.118:6379 to 10.168.32.121:6379

Adding replica 10.168.32.117:6379 to 10.168.32.120:6379

Adding replica 10.168.32.116:6379 to 10.168.32.119:6379

S: f3314ecda2ef4ea0ec140e06de3e4bf656a5e71b 10.168.32.116:6379

   replicates a5a22ba976c05b1d495812241a8dd10a5d4a5b42

S: 314f957e1021b0c262e635ad4feeb340dfe5955c 10.168.32.117:6379

   replicates 9880ecc98cde59e0d6341ba7c54bbcae9b27c877

S: 07634642c88fdf1b886d3db87dedc4b9857fce7c 10.168.32.118:6379

   replicates b7f33a1b733fe4a51447a788e334fab625a984bc

M: a5a22ba976c05b1d495812241a8dd10a5d4a5b42 10.168.32.119:6379

   slots:10923-16383 (5461 slots) master

M: 9880ecc98cde59e0d6341ba7c54bbcae9b27c877 10.168.32.120:6379

   slots:5461-10922 (5462 slots) master

M: b7f33a1b733fe4a51447a788e334fab625a984bc 10.168.32.121:6379

   slots:0-5460 (5461 slots) master

Can I set the above configuration? (type 'yes' to accept): yes 此次输入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.168.32.116:6379)

M: f3314ecda2ef4ea0ec140e06de3e4bf656a5e71b 10.168.32.116:6379

   slots: (0 slots) master

   replicates a5a22ba976c05b1d495812241a8dd10a5d4a5b42

M: 314f957e1021b0c262e635ad4feeb340dfe5955c 10.168.32.117:6379

   slots: (0 slots) master

   replicates 9880ecc98cde59e0d6341ba7c54bbcae9b27c877

M: 07634642c88fdf1b886d3db87dedc4b9857fce7c 10.168.32.118:6379

   slots: (0 slots) master

   replicates b7f33a1b733fe4a51447a788e334fab625a984bc

M: a5a22ba976c05b1d495812241a8dd10a5d4a5b42 10.168.32.119:6379

   slots:10923-16383 (5461 slots) master

M: 9880ecc98cde59e0d6341ba7c54bbcae9b27c877 10.168.32.120:6379

   slots:5461-10922 (5462 slots) master

M: b7f33a1b733fe4a51447a788e334fab625a984bc 10.168.32.121:6379

   slots:0-5460 (5461 slots) master

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

 

至此redis集群即搭建成功!

执行命令redis-cli -c -p 6379 cluster nodes,查看集群节点

[root@puppetmaster1 src]# redis-cli -c -h 10.168.32.116 -p 6379 cluster nodes

314f957e1021b0c262e635ad4feeb340dfe5955c 10.168.32.117:6379 slave 9880ecc98cde59e0d6341ba7c54bbcae9b27c877 0 1425114266105 5 connected

a5a22ba976c05b1d495812241a8dd10a5d4a5b42 10.168.32.119:6379 master - 0 1425114267205 4 connected 10923-16383

07634642c88fdf1b886d3db87dedc4b9857fce7c 10.168.32.118:6379 slave b7f33a1b733fe4a51447a788e334fab625a984bc 0 1425114265605 6 connected

b7f33a1b733fe4a51447a788e334fab625a984bc 10.168.32.121:6379 master - 0 1425114266205 6 connected 0-5460

f3314ecda2ef4ea0ec140e06de3e4bf656a5e71b 10.168.32.116:6379 myself,slave a5a22ba976c05b1d495812241a8dd10a5d4a5b42 0 0 1 connected

9880ecc98cde59e0d6341ba7c54bbcae9b27c877 10.168.32.120:6379 master - 0 1425114266505 5 connected 5461-10922

 

7:使用redis-cli命令进入集群环境

redis-cli -c -h 10.168.32.117 -p 6379

 

Redis集群操作节点

1:新增一个节点,IP为10.168.32.122,端口为6379

首先把需要添加的节点启动

cd /usr/local/redis/

#vi /usr/local/redis/redis.conf

bind 10.168.32.122

port 6379

启动redis

redis-server /usr/local/redis/redis.conf

 

将新节点添加到集群

cd /usr/local/redis/src

[root@puppetmaster1 src]# ./redis-trib.rb add-node 10.168.32.122:6379 10.168.32.116:6379

>>> Adding node 10.168.32.122:6379 to cluster 10.168.32.116:6379

Connecting to node 10.168.32.116:6379: OK

Connecting to node 10.168.32.117:6379: OK

Connecting to node 10.168.32.119:6379: OK

Connecting to node 10.168.32.118:6379: OK

Connecting to node 10.168.32.121:6379: OK

Connecting to node 10.168.32.120:6379: OK

>>> Performing Cluster Check (using node 10.168.32.116:6379)

S: f3314ecda2ef4ea0ec140e06de3e4bf656a5e71b 10.168.32.116:6379

   slots: (0 slots) slave

   replicates a5a22ba976c05b1d495812241a8dd10a5d4a5b42

S: 314f957e1021b0c262e635ad4feeb340dfe5955c 10.168.32.117:6379

   slots: (0 slots) slave

   replicates 9880ecc98cde59e0d6341ba7c54bbcae9b27c877

M: a5a22ba976c05b1d495812241a8dd10a5d4a5b42 10.168.32.119:6379

   slots:10923-16383 (5461 slots) master

   1 additional replica(s)

S: 07634642c88fdf1b886d3db87dedc4b9857fce7c 10.168.32.118:6379

   slots: (0 slots) slave

   replicates b7f33a1b733fe4a51447a788e334fab625a984bc

M: b7f33a1b733fe4a51447a788e334fab625a984bc 10.168.32.121:6379

   slots:0-5460 (5461 slots) master

   1 additional replica(s)

M: 9880ecc98cde59e0d6341ba7c54bbcae9b27c877 10.168.32.120:6379

   slots:5461-10922 (5462 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.

Connecting to node 10.168.32.122:6379: OK

>>> Send CLUSTER MEET to node 10.168.32.122:6379 to make it join the cluster.

[OK] New node added correctly.

 

查看集群节点

[root@puppetmaster1 src]# redis-cli -c -h 10.168.32.116 -p 6379 cluster nodes

314f957e1021b0c262e635ad4feeb340dfe5955c 10.168.32.117:6379 slave 9880ecc98cde59e0d6341ba7c54bbcae9b27c877 0 1425114360107 5 connected

38c2f060533c24698334a16b26f678ae6bd7ec41 10.168.32.122:6379 master - 0 1425114361107 0 connected

a5a22ba976c05b1d495812241a8dd10a5d4a5b42 10.168.32.119:6379 master - 0 1425114361507 4 connected 10923-16383

07634642c88fdf1b886d3db87dedc4b9857fce7c 10.168.32.118:6379 slave b7f33a1b733fe4a51447a788e334fab625a984bc 0 1425114362507 6 connected

b7f33a1b733fe4a51447a788e334fab625a984bc 10.168.32.121:6379 master - 0 1425114360507 6 connected 0-5460

f3314ecda2ef4ea0ec140e06de3e4bf656a5e71b 10.168.32.116:6379 myself,slave a5a22ba976c05b1d495812241a8dd10a5d4a5b42 0 0 1 connected

9880ecc98cde59e0d6341ba7c54bbcae9b27c877 10.168.32.120:6379 master - 0 1425114362107 5 connected 5461-10922

 

删除redis集群节点

redis-trib.rb  del-node        host:port node_id

./redis-trib.rb del-node 10.168.32.122:6379 38c2f060533c24698334a16b26f678ae6bd7ec41


赞(0) 更多分享

上篇: Mysql分库分表技术难题之分布式全局唯一id解决方案
下篇: MySQL千万级的大表要怎么优化(读写分离、水平拆分、垂直拆分)