轻松搭建redis集群

1. 硬件配置

 硬盘200G

 内存16G

2. 软件版本

软件名称 软件版本

操作系统 CentOS6.5

Redis版本 redis-3.0.2.tar.gz

JDK版本 jdk-8u45-linux-x64.gz

ruby rubygems-2.4.5.tgz

gem redis-3.2.0.gem

3. 服务器信息

主要由三台服务器组成,具体信息如下:

主机名 IP地扯

xxx1 192.168.2.220

xxx2 192.168.2.221

xxx3 192.168.2.222

4. JDK安装

这个比较简单就不介绍,略

4.1. 添加/etc/hosts文件

添加文件如下内容:

192.168.2.220 xxx1 193. 192.168.2.221 xxx2 193. 192.168.2.222 xxx3

4.2. 解压文件

tar -xvf jdk-8u45-linux-x64.gz

4.3. 修改配置文件内容

添加配置文件/etc/profile内容如下:

export JAVA_HOME=/usr/local/share/jdk1.8.0.45

export PATH=$JAVA_HOME/bin

export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

4.4. 检查jdk的版本号

使用命令java –version进行检测,如下内容:

java version "1.8.0_45"

Java(TM) SE Runtime Environment (build 1.8.0_45-b14)

Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

5. redis软件安装

5.1. 解压软件包

tar –xzvf redis-3.0.2.tar.gz

5.2. 编译

make

注:使用make命令安装后,使用make test命令测试make命令执行是否正确

5.3. 创建目录

在三个节点中都进行

mkdir cluster

mkdir 6379 6380 6381 6382

5.4. 编辑配置文件

(1)创建文件redis-common.conf(redis中的通用部分)

具体内容为:

GENERAL

daemonize no

tcp-backlog 511

timeout 0

tcp-keepalive 0

loglevel notice

databases 16

slave-serve-stale-data yes

slave-read-only yes

repl-disable-tcp-nodelay yes

slave-priority 100

appendonly yes

appendfsync everysec

no-appendfsync-on-rewrite yes

auto-aof-rewrite-min-size 64mb

lua-time-limit 5000

cluster-enabled yes

cluster-node-timeout 15000

cluster-migration-barrier 1

slowlog-log-slower-than 10000

slowlog-max-len 128

notify-keyspace-events ""

hash-max-ziplist-entries 512

hash-max-ziplist-value 64

list-max-ziplist-entries 512

list-max-ziplist-value 64

set-max-intset-entries 512

zset-max-ziplist-entries 128

zset-max-ziplist-value 64

activerehashing yes

client-output-buffer-limit normal 0 0 0

client-output-buffer-limit slave 256mb 64mb 60

client-output-buffer-limit pubsub 32mb 8mb 60

hz 10

aof-rewrite-incremental-fsync yes

save 900 1

save 300 10

save 60 10000

stop-writes-on-bgsave-error yes

rdbcompression yes

rdbchecksum yes

repl-diskless-sync no

repl-diskless-sync-delay 5

aof-load-truncated yes

latency-monitor-threshold 0

hll-sparse-max-bytes 3000

maxmemory 256m

maxmemory-policy allkeys-lru

auto-aof-rewrite-percentage 100

(2)创建redis.conf(redis.conf中的通用部分)

具体的内容:

include redis-common.conf

port 6379

dir ./

appendfilename "appendonly-6379.aof"

dbfilename dump-6379.rdb

cluster-config-file nodes-6379.conf

注:将redis.conf复制到6379、6380、6381、6382目录下并分别重命名为redis-6379.conf、

redis-6380conf、redis-6381.conf、redis-6382.conf,再分别编辑redis-6382.conf的内容,

分别修改为6380、6381、6382

5.5. 复制执行文件

cd redis-3.0.2

cp src/serdis-server cluster

cp src/redis-cli cluster

5.6. 启动节点

cd /usr/local/share/redis-3.0.2/cluster

./redis-server 6379/redis-6379.conf &

./redis-server 6380/redis-6380.conf &

./redis-server 6381/redis-6381.conf &

./redis-server 6382/redis-6382.conf &

6. 集群部署

6.1. 安装ruby

sudo gem install redis-3.2.0.gem --local

tar -xzvf rubygems-2.4.5.tgz ruby setup.rb

6.2. 创建集群

一)创建集群这个操作仅需在一个节点中操作即可

[root@xxx1 src]# ./redis-trib.rb create --replicas 3 192.168.2.220:6379 192.168. 2.221:6379 192.168.2.222:6379 192.168.2.220:6380 192.168.2.221:6380 192.168.2.22 3. 2:6380 192.168.2.220:6381 192.168.2.221:6381 192.168.2.222:6381 192.168.2.220:6

382 192.168.2.221:6382 192.168.2.222:6382

Creating cluster

二)查看集群节点

[root@xxx1 src]# ./redis-cli -h 127.0.0.1 cluster nodes

[root@xxx share]# cd redis-3.0.2/

[root@xxx redis-3.0.2]# cd src

[root@xxx src]# ./redis-trib.rb --replicas 3 10.10.8.17:6379 10.10.8.18:6379 10.10.8.19:6379 10.10.8.17:6380 10.10.8.18:6380 10.10.8.19:6380 10.10.8.17:6381 10.10.8.18:6381 10.10.8.19:6381 10.10.8.17:6382 10.10.8.18:6382 10.10.8.19:6382 ./redis-trib.rb:24:in `require': no such file to load -- rubygems (LoadError) from ./redis-trib.rb:24

解决方案:

[root@xxx share]# cd rubygems-2.4.5 [root@xxx rubygems-2.4.5]# ruby setup.rb RubyGems 2.4.5 installed ERROR: While executing gem ... (Gem::DocumentError) RDoc is not installed: no such file to load -- rdoc/rdoc [root@xxx rubygems-2.4.5]#

rpm -ivh ruby*.rpm --force --nodeps

[root@xxx share]# sudo gem install redis-3.2.0.gem --local

Successfully installed redis-3.2.0

Installing ri documentation for redis-3.2.0

/usr/lib/ruby/1.8/rdoc/rdoc.rb:280: warning: conflicting chdir during another chdir block

/usr/lib/ruby/1.8/rdoc/rdoc.rb:287: warning: conflicting chdir during another chdir block

Done installing documentation for redis after 6 seconds

1 gem installed

[root@xxx share]#

[root@xxx share]# cd rubygems-2.4.5

[root@xxx rubygems-2.4.5]# ruby setup.rb

[root@xxx src]# ./redis-trib.rb create --replicas 3 10.10.8.17:6379 10.10.8.18:6379

10.10.8.19:6379 10.10.8.17:6380 10.10.8.18:6380 10.10.8.19:6380 10.10.8.17:6381

10.10.8.18:6381 10.10.8.19:6381 10.10.8.17:6382 10.10.8.18:6382 10.10.8.19:6382

Creating cluster Connecting to node 10.10.8.17:6379: OK Connecting to node 10.10.8.18:6379: OK Connecting to node 10.10.8.19:6379: OK Connecting to node 10.10.8.17:6380: OK Connecting to node 10.10.8.18:6380: OK Connecting to node 10.10.8.19:6380: OK Connecting to node 10.10.8.17:6381: OK Connecting to node 10.10.8.18:6381: OK Connecting to node 10.10.8.19:6381: OK Connecting to node 10.10.8.17:6382: OK Connecting to node 10.10.8.18:6382: OK Connecting to node 10.10.8.19:6382: OK Performing hash slots allocation on 12 nodes... ...... [root@xxx src]#

6.4. 查看集群状态

./redis-cli -h 192.168.2.221 cluster nodes

6.5. 操作数据

/usr/local/share/redis-3.0.2/cluster

./redis-cli -c

set test value

get test

7. 高可用性配置

7.1. 复制文件

cp src/redis-sentinel cluster/

cp sentinel.conf cluster/

7.2. 编辑配置文件

sentinel.conf配置文件如下内容:

port 26379

dir "/tmp"

sentinel monitor master1 192.168.2.220 6379 2

sentinel monitor master2 192.168.2.221 6379 2

sentinel monitor master3 192.168.2.222 6379 2

Default is 30 seconds

sentinel down-after-milliseconds master1 30000

sentinel down-after-milliseconds master2 30000

sentinel down-after-milliseconds master3 30000

sentinel parallel-syncs master1 1

sentinel parallel-syncs master2 1

sentinel parallel-syncs master3 1

Defult is 3 minutes

sentinel failover-timeout master1 180000

sentinel failover-timeout master2 180000

sentinel failover-timeout master3 180000

7.3. 启动sentinel

在三个节点上均需要进行操作

nohup ./redis-sentinel sentinel.conf &

7.4. 节点验证

(1)使用命令查找一个mater节点

[root@xxx1 src]# ./redis-trib.rb check 192.168.2.221:6379

Connecting to node 192.168.2.221:6379: OK Connecting to node 192.168.2.222:6379: OK Connecting to node 192.168.2.222:6382: OK Connecting to node 192.168.2.221:6381: OK Connecting to node 192.168.2.221:6380: OK Connecting to node 192.168.2.221:6382: OK Connecting to node 192.168.2.220:6382: OK Connecting to node 192.168.2.222:6380: OK Connecting to node 192.168.2.220:6379: OK Connecting to node 192.168.2.222:6381: OK Connecting to node 192.168.2.220:6381: OK Connecting to node 192.168.2.220:6380: OK

7.5. 故障模拟

[root@xxx1 src]# ./redis-cli -h 192.168.2.221 -p 6379 shutdown

  [root@xxx1 share]# sudo gem install redis-3.2.0.gem --local Successfully installed redis-3.2.0 Installing ri documentation for redis-3.2.0 /usr/lib/ruby/1.8/rdoc/rdoc.rb:280: warning: conflicting chdir during another ch dir block /usr/lib/ruby/1.8/rdoc/rdoc.rb:287: warning: conflicting chdir during another ch dir block Done installing documentation for redis after 4 seconds 1 gem installed [root@xxx1 share]#

[root@xxx1 src]# ./redis-trib.rb check 192.168.2.221:6379

Connecting to node 192.168.2.221:6379: [ERR] Sorry, can't connect to node 192.16 8.2.221:6379 [root@xxx1 src]#

[root@xxx1 src]# ./redis-trib.rb check 192.168.2.222:6379

Connecting to node 192.168.2.222:6379: OK Connecting to node 192.168.2.222:6382: OK Connecting to node 192.168.2.222:6381: OK Connecting to node 192.168.2.222:6380: OK Connecting to node 192.168.2.221:6380: OK Connecting to node 192.168.2.221:6381: OK Connecting to node 192.168.2.220:6381: OK Connecting to node 192.168.2.221:6382: OK Connecting to node 192.168.2.220:6380: OK Connecting to node 192.168.2.220:6382: OK Connecting to node 192.168.2.220:6379: OK

8. 集群维护

8.1. 4.1 添加新节点

对新节点执行1.1—1.5的操作

在已有集群的某节点上执行:

cd redis-3.0.2

cd src

./redis-trib.rb add-node IP:6379(未修改过) 127.0.0.1:6379(未修改过)

其中,127.0.0.1:6379是任意一个已有集群中的某节点。

如果提示:Send CLUSTER MEET to node IP: 6379(未修改过)to make it join the cluster.

在已有集群的某节点上,使用CLUSTER MEET命令使该节点生效:

cd redis-3.0.2

cd cluster

./redis-cli -p 6379(未修改过)-c

CLUSTER MEET IP 6379(未修改过)

设置为master节点:

通过命令查看新添加的节点ID:

cd redis-3.0.2

cd src

./redis-trib.rb check IP:6379(未修改过)

重新分配slot:

cd redis-3.0.2

cd src

./redis-trib.rb reshard IP:6379(未修改过)

注:过程中需要输入移动的槽数、目的(新添加的)节点的ID、源节点的ID(可以直接输入all)。

使用命令,重新检查分片情况:

cd redis-3.0.2

cd src

./redis-trib.rb check IP:6379(未修改过)

设置为slave节点:

通过命令查看要加入到的master的节点ID:

cd redis-3.0.2

cd cluster

./redis-cli -h IP -p 6379(未修改过) cluster nodes

连接新添加的节点:

cd redis-3.0.2

cd cluster

./redis-cli -h IP -p 6379(未修改过)-c

cluster replicate master的节点ID

使用命令,重新检查分片情况:

cd redis-3.0.2

cd cluster

./redis-cli -h IP -p 6379(未修改过) cluster nodes

8.2. 4.2 删除节点

删除slave节点:

通过命令查看要删除的slave的节点ID:

cd redis-3.0.2

cd cluster

./redis-cli -h IP -p 6379(未修改过) cluster nodes

使用命令,删除节点:

cd redis-3.0.2

cd src

./redis-trib.rb del-node IP: 6379(未修改过) ‘节点的ID’

删除master节点:

首先要使用reshard移除master的全部slot,然后再删除当前节点(目前只能把被删除master的slot迁

移到一个节点上)。 在已有集群的某节点(非要删除的master节点)上执行:

cd redis-3.0.2

cd src

./redis-trib.rb reshard IP(要删除的节点的IP): 6379(未修改过)

以下是交互信息:

How many slots do you want to move (from 1 to 16384)? 被删除master的所有slot数量

What is the receiving node ID?接受slot的master节点的ID

Please enter all the source node IDs.

Type 'all' to use all the nodes as source nodes for the hash slots.

Type 'done' once you entered all the source nodes IDs.

Source node #1:被删除的master节点的ID

Source node #2:done

Do you want to proceed with the proposed reshard plan (yes/no)? yes

使用3.1删除该master节点的所有slave节点。

最终,删除master节点

./redis-trib.rb del-node IP: 6379(未修改过) ‘要删除的master节点的ID’

8.3. 4.3 在线reshard数据

对于负载/数据均匀的情况,可以在线reshard slot来解决,方法与添加新master的
reshard一样。

8.4. 4.4 关闭节点

使用命令关闭节点:

./redis-cli -h IP -p 6379(未修改过) shutdown 常用命令

参考地址:

http://www.cnblogs.com/tankaixiong/articles/4022646.html

集群

CLUSTER INFO 打印集群的信息

CLUSTER NODES 列出集群当前已知的所有节点(node),以及这些节点的相关信息。

节点

CLUSTER MEET 将 ip 和 port 所指定的节点添加到集群当中,让它成为集群的一份子。

CLUSTER FORGET 从集群中移除 node_id 指定的节点。

CLUSTER REPLICATE 将当前节点设置为 node_id 指定的节点的从节点。

CLUSTER SAVECONFIG 将节点的配置文件保存到硬盘里面。

槽(slot)

CLUSTER ADDSLOTS [slot ...] 将一个或多个槽(slot)指派(assign)给当前节点。

CLUSTER DELSLOTS [slot ...] 移除一个或多个槽对当前节点的指派。

CLUSTER FLUSHSLOTS 移除指派给当前节点的所有槽,让当前节点变成一个没有指派任何槽的节点。 CLUSTER SETSLOT NODE 将槽 slot 指派给 node_id 指定的节点,如果槽已经指

派给另一个节点,那么先让另一个节点删除该槽>,然后再进行指派。

CLUSTER SETSLOT MIGRATING 将本节点的槽 slot 迁移到 node_id 指定的节点

中。 CLUSTER SETSLOT IMPORTING 从 node_id 指定的节点中导入槽 slot 到本节

点。 CLUSTER SETSLOT STABLE 取消对槽 slot 的导入(import)或者迁移(migrate)。

CLUSTER KEYSLOT 计算键 key 应该被放置在哪个槽上。

CLUSTER COUNTKEYSINSLOT 返回槽 slot 目前包含的键值对数量。

CLUSTER GETKEYSINSLOT 返回 count 个 slot 槽中的键。

发表评论

发表评论

*

沙发空缺中,还不快抢~