首页 > 代码库 > redis集群构建过程 linux windows

redis集群构建过程 linux windows

redis集群的构建过程

本人初次构建redis集群时,通过查阅资料整理的文档

目录

Linux Redis cluster集群方案... 1

Windows redis集群方案... 5

 

Linux Redis cluster集群方案

1 安装:

下载地址:http://download.redis.io/releases/

下载版本:redis3.0.7.tar.gz

 

[dev@linux soft]$ tar -zxzf redis-3.0.7.tar.gz

[dev@linux soft]$ cd redis-3.0.7

[dev@linux redis-3.0.7]$ make

 

编译后src目录下生成6个可执行文件

redis-server(启动服务端)、

redis-cli(调用客户端)、

redis-benchmark、

redis-check-aof、

redis-check-dump、

redis-sentinel

 

2 测试redis

[dev@linux redis-3.0.7]$ src/redis-server (启动服务)

[dev@linux redis-3.0.7]$ src/redis-cli (启动客户端)

127.0.0.1:6379> set name lixiaodong

OK

127.0.0.1:6379> get name

"lixiaodong"

127.0.0.1:6379>

 

3 开机自启动可以继续参考博客

http://blog.csdn.net/zhenzhendeblog/article/details/52161515

 

4集群配置 redis3.0以后才支持cluster集群所以要下载正确的redis

创建文件夹redis_cluster,创建三个子文件夹node1,node2,node3作为集群的三个节点

拷贝redis的配置文件redis.conf分别到node1,node2,node3

[dev@linux redis_cluster]$ cp ../soft/redis-3.0.7/redis.conf node1/

[dev@linux redis_cluster]$ cp ../soft/redis-3.0.7/redis.conf node2/

[dev@linux redis_cluster]$ cp ../soft/redis-3.0.7/redis.conf node3/

 

修改配置文件redis.conf 以node1为例子

         修改端口号:6380

         绑定ip: 指的是本机网ip。 bind 172.20.16.158

         修改pid文件位置:新安装的程序在运行后会在/var/run目录下面产生自己的pid文件,目的是为了防止进程启动多个副本等pidfile /var/run/redis_6380.pid

         开启集群: cluster-enabled yes

    指定集群配置文件:cluster-config-file nodes-6380.conf 通过注释了解到这个文件不用手动修改,redis会自己生成

         指定生成文件的目录:dir /home/lixiaodong/redis_cluster/node1 这样redis生成的文件就会到指定目录,不然的话在哪个目录启动redis这些文件就会生成在相应的目录

        

5 创建集群

需要提前安装:

         yum install ruby //安装ruby

    yum install rubygems //安装rubygems

    gem install redis //

如果安装不成功,

下载安装

      wget https://rubygems.global.ssl.fastly.net/gems/redis-3.0.7.gem

  安装命令:gem install -l ./redis-3.0.7.gem

通过命令创建集群,linux下运行如下

[dev@linux lixiaodong]$ soft/redis-3.0.7/src/redis-trib.rb create --replicas 0 172.20.16.158:6380 172.20.16.158:6381 172.20.16.158:6382

>>> Creating cluster

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

Using 3 masters:

172.20.16.158:6380

172.20.16.158:6381

172.20.16.158:6382

M: d276f248a4e0fba1f9e24f19ac97b330b45b2ab3 172.20.16.158:6380

   slots:0-5460 (5461 slots) master

M: 51eb3b27006cac78672344646a50e4ae6f02215f 172.20.16.158:6381

   slots:5461-10922 (5462 slots) master

M: 08612b4236d4f4733157ec45f98bc19ff319118e 172.20.16.158:6382

   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 172.20.16.158:6380)

M: d276f248a4e0fba1f9e24f19ac97b330b45b2ab3 172.20.16.158:6380

   slots:0-5460 (5461 slots) master

M: 51eb3b27006cac78672344646a50e4ae6f02215f 172.20.16.158:6381

   slots:5461-10922 (5462 slots) master

M: 08612b4236d4f4733157ec45f98bc19ff319118e 172.20.16.158:6382

   slots:10923-16383 (5461 slots) master

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

[dev@linux lixiaodong]$

 

6测试集群

启动客户端命令如下, –c的作用就是如果key对应的slot不是当前连接的redis-server,那么自动重定向到其对应的redis-server

[dev@linux lixiaodong]$ soft/redis-3.0.7/src/redis-cli -h 172.20.16.158 -p 638 –c

172.20.16.158:6381> set k1 123

-> Redirected to slot [12706] located at 172.20.16.158:6382

OK

172.20.16.158:6382> get k1

"123"

172.20.16.158:6382>

 

 

7 主从搭建

现在的redis集群如果有一个节点挂了,集群就挂了,反正我把node2干掉之后就这样了,下面就是干掉之后现场

[dev@linux lixiaodong]$ soft/redis-3.0.7/src/redis-cli -h 172.20.16.158 -p 6380 -c

172.20.16.158:6380> set k1 12

-> Redirected to slot [12706] located at 172.20.16.158:6382

Could not connect to Redis at 172.20.16.158:6382: Connection refused

Could not connect to Redis at 172.20.16.158:6382: Connection refused

not connected> set k2 12

Could not connect to Redis at 172.20.16.158:6382: Connection refused

not connected> set name lixiaodong

Could not connect to Redis at 172.20.16.158:6382: Connection refused

not connected> set key 1

Could not connect to Redis at 172.20.16.158:6382: Connection refused

not connected>

 

 

redis主从的作用显现出来:

添加新的redis node:

         当然新节点可以作为主节点也可以作为从节点,这里主要介绍作为从节点

   a)       添加:

     我准备了三个从节点,分别作为node1, node2, node3的从节点

           首先将集群与待添加节点都启动了,然后执行添加节点的命令,命令如下

    [dev@linux lixiaodong]$soft/redis-3.0.7/src/redis-trib.rb add-node 172.20.16.158:6383 172.20.16.158:6380

 

     通过如下命令查看集群节点信息:

     [dev@linux lixiaodong]$ soft/redis-3.0.7/src/redis-cli -c -p 6380 cluster nodes

      技术分享

      可以看到6383被加进来了,但是并没有分摊slot,这个需要把它变成主节点的时候设置

          这里的IP地址是127.0.0.1而不是172.20.16.158,这是前后修改过几次文档导致的,然而截图无法修改,不要在意这个

    b)       把添加的接点搞成6380(node1)的从接点 命令如下:注意好长一串的字符串是redis节点的id

      [dev@linux lixiaodong]$ soft/redis-3.0.7/src/redis-cli -c -p 6383 cluster replicate 4757c6352105f7ac77f5a9913da659d9e423e4d4

      OK

   c)       查看接点信息

      [dev@linux lixiaodong]$ soft/redis-3.0.7/src/redis-cli -c -p 6380 cluster nodes

      技术分享

      可以看到6383这个redis状态已经是slave了

8 测试从节点是否生效

技术分享        

图一

 技术分享

图二

第一张图貌似没啥意义,不过从图二看,当我停掉三个主redis后,6383这哥们现在翻身做主人了, 他现在是主服务器了, 当然不必停掉三个,只不过是我写了个脚本让三个都停掉了

当我在次把主服务器启动之后发现,6380自动变成6383的从服务器了

Windows redis cluster集群方案

1 首先需要下载redis 3.0以上版本

2 创建几个节点文件夹

 技术分享

其中redis是下载的windows版的redis 3.2.100, 其他六个文件夹是作为集群的各个节点, node1, node2, node3,node4, node5, node6作为主节点

3 将redis的配置文件redis.conf拷贝到各个节点文件夹中,redis的配置文件redis.conf需要修改的几个配置如下

  1)       port 6379 这个是redis的端口, 每个节点不能重复

  2)       bind 127.0.0.1 绑定的ip, 由于现在部署在同一台物理机,所以不用需改,如果是在不同物理机的话,需要填写本机ip

  3)       cluster-config-file nodes-6380.conf 集群配置文件名字修改下

  4)       appendonly yes 持久化策略yes的话, 会优先使用aof文件恢复缓存

  5)       dir node1  默认是redis server的启动目录,redis运行过程中生成的一些文件会放到该路径下, 如appendonly.aof文件,而我是在各个节点同级目录启动redis, 为了避免各个节点相互冲突,修改dir为各个节点的路径

4 接下来使用构建集群的命令 报错如下:

D:\project\Tools\redis cluster\redis>redis-trib.rb create --replicas 1 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 127.0.0.1:6385

C:/Ruby23-x64/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require: cannot load such file -- redis (LoadError)

        from C:/Ruby23-x64/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require
        from D:/project/Tools/redis cluster/redis/redis-trib.rb:25:in `<main>

 

别慌,先安装个rubyinstaller-2.3.3-x64.exe,能勾选的都勾选了, 不包括第三方插件等等,完后命令行测试安装是否成功ruby –v, gem –v

提示版本号信息,说明安装成功

接下来 安装下redis

D:\project\Tools\redis cluster\redis>gem install redis

Fetching: redis-3.3.3.gem (100%)

Successfully installed redis-3.3.3

Parsing documentation for redis-3.3.3

Installing ri documentation for redis-3.3.3

Done installing documentation for redis after 3 seconds

1 gem installed

 

再次执行构建集群命令,妥妥成功了,注意命令中的replicas后的1是自动分配主从关系的意思

以下提供几个本人用的脚本

linux一键构建集群

#!/bin/sh

echo "backup data..."
rm -rf redis_cluster/node1/backup
rm -rf redis_cluster/node2/backup
rm -rf redis_cluster/node3/backup
rm -rf redis_cluster/slaveof-node1/backup
rm -rf redis_cluster/slaveof-node2/backup
rm -rf redis_cluster/slaveof-node3/backup

mkdir redis_cluster/node1/backup
mkdir redis_cluster/node2/backup
mkdir redis_cluster/node3/backup
mkdir redis_cluster/slaveof-node1/backup
mkdir redis_cluster/slaveof-node2/backup
mkdir redis_cluster/slaveof-node3/backup

mv redis_cluster/node1/dump.rdb redis_cluster/node1/backup
mv redis_cluster/node2/dump.rdb redis_cluster/node2/backup
mv redis_cluster/node3/dump.rdb redis_cluster/node3/backup
mv redis_cluster/slaveof-node1/dump.rdb redis_cluster/slaveof-node1/backup
mv redis_cluster/slaveof-node2/dump.rdb redis_cluster/slaveof-node2/backup
mv redis_cluster/slaveof-node3/dump.rdb redis_cluster/slaveof-node3/backup


mv redis_cluster/node1/nodes-6380.conf redis_cluster/node1/backup
mv redis_cluster/node2/nodes-6381.conf redis_cluster/node2/backup
mv redis_cluster/node3/nodes-6382.conf redis_cluster/node3/backup
mv redis_cluster/slaveof-node1/nodes-6383.conf redis_cluster/slaveof-node1/backup
mv redis_cluster/slaveof-node2/nodes-6384.conf redis_cluster/slaveof-node2/backup
mv redis_cluster/slaveof-node3/nodes-6385.conf redis_cluster/slaveof-node3/backup

echo "start redis................................................"
nohup soft/redis-3.0.7/src/redis-server redis_cluster/node1/redis.conf & 
nohup soft/redis-3.0.7/src/redis-server redis_cluster/node2/redis.conf &
nohup soft/redis-3.0.7/src/redis-server redis_cluster/node3/redis.conf &
nohup soft/redis-3.0.7/src/redis-server redis_cluster/slaveof-node1/redis.conf &
nohup soft/redis-3.0.7/src/redis-server redis_cluster/slaveof-node2/redis.conf &
nohup soft/redis-3.0.7/src/redis-server redis_cluster/slaveof-node3/redis.conf &
echo "start redis done!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"


echo "create cluster begin........................................."
echo yes| soft/redis-3.0.7/src/redis-trib.rb create --replicas 0 172.20.16.158:6380 172.20.16.158:6381 172.20.16.158:6382
echo "create cluster done!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"

echo "add slave nodes ............................................"
sleep 5
soft/redis-3.0.7/src/redis-trib.rb add-node 172.20.16.158:6383 172.20.16.158:6380
soft/redis-3.0.7/src/redis-trib.rb add-node 172.20.16.158:6384 172.20.16.158:6381
soft/redis-3.0.7/src/redis-trib.rb add-node 172.20.16.158:6385 172.20.16.158:6382

sleep 5
node1Id=$(soft/redis-3.0.7/src/redis-cli -c -h 172.20.16.158 -p 6380 cluster nodes|grep 6380|cut -d " " -f 1)
node2Id=$(soft/redis-3.0.7/src/redis-cli -c -h 172.20.16.158 -p 6381 cluster nodes|grep 6381|cut -d " " -f 1)
node3Id=$(soft/redis-3.0.7/src/redis-cli -c -h 172.20.16.158 -p 6382 cluster nodes|grep 6382|cut -d " " -f 1)

echo "node 1 Id =" $node1Id
echo "node 2 Id =" $node2Id
echo "node 3 Id =" $node3Id

 

window 构建集群脚本

create.bat

@echo off
echo start redis...
call start.bat

echo wait 3 second...
ping -n 3 127.0>nul

echo create redis cluster...
start redis/redis-trib.rb create --replicas 1 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 127.0.0.1:6385

echo done!

pause

 

start.bat

start redis/redis-server node1/redis.windows.conf
start redis/redis-server node2/redis.windows.conf
start redis/redis-server node3/redis.windows.conf
start redis/redis-server node4/redis.windows.conf
start redis/redis-server node5/redis.windows.conf
start redis/redis-server node6/redis.windows.conf

 

redis集群构建过程 linux windows