linux redis集群工具,Redis集群部署及常用的操作命令

简单说下自己测试搭建简单的Redis集群的大体步骤:

1.首先你的有6个redis(官方说最少6个,3master,3slave),可以先在一台机器上搭建,搭建到多台上应该只需要改变启动命令即可(可能需要一些ssh无密钥什么的,只是猜测)

在网上随便可以找到的配置多个redis,(总体有两种方式,一种是虚拟的,貌似走的都是一个redis,一种是将配置好的redis复制成六份,配置相应的端口等,我选择的后者显得更真实一些)

然后把他们全部打开,可以写一个脚本,或者一个一个打开,作为一个新世纪的年轻人,我当然选择一个一个打开它们......

[root@localhost ~]# ps -ef | grep redis

root      3427    1  0 7月17 ?      00:02:59 src/redis-server *:6379

root      3454    1  0 7月17 ?      00:06:09 src/redis-server *:7001 [cluster]

root      3460    1  0 7月17 ?      00:06:19 src/redis-server *:7002 [cluster]

root      3467    1  0 7月17 ?      00:05:59 src/redis-server *:7003 [cluster]

root      3473    1  0 7月17 ?      00:05:59 src/redis-server *:7004 [cluster]

root      3477    1  0 7月17 ?      00:05:57 src/redis-server *:7005 [cluster]

root      5867    1  0 12:02 ?      00:00:06 src/redis-server *:7000 [cluster]

root      5938  5913  0 13:17 pts/1    00:00:00 grep --color=auto redis

2.将6个独立redis设置为集群,so easy 一句话搞定。其中 --replicas 参数是将6台redis分别分配了主从关系(master挂掉slave可以顶替,但是还没有具体深入研究)

./redis-trib.rb create --replicas 1 127.0.0.1:7000 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

在开启过程中可能会遇到很多问题,各种奇怪的报错,那就要安装ruby和gem的一些东东,搞不明白是什么,反正就是依赖吧~ ,只能说按照这个来会绕开很多坑。

# yum install ruby-devel.x86_64

# wget http://rubygems.global.ssl.fastly.net/gems/redis-3.2.1.gem

# gem install -l ./redis-3.2.1.gem

执行这些代码之后再去执行上面建立集群的命令,如果成功的话,会看到下面的样子:

>>> Creating cluster

Connecting to node 127.0.0.1:7000: OK

Connecting to node 127.0.0.1:7001: OK

Connecting to node 127.0.0.1:7002: OK

Connecting to node 127.0.0.1:7003: OK

Connecting to node 127.0.0.1:7004: OK

Connecting to node 127.0.0.1:7005: OK

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

Using 3 masters:

127.0.0.1:7000

127.0.0.1:7001

127.0.0.1:7002

Adding replica 127.0.0.1:7003 to 127.0.0.1:7000

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

M: ad52e4f7c14da4b8b1e8e48603c9e5515a4cec7a 127.0.0.1:7000

slots:0-5460 (5461 slots) master

M: d3058a19483d7be5c30c042779e56130f6ebf074 127.0.0.1:7001

slots:5461-10922 (5462 slots) master

M: 5814305e71552ab45c44b9b8233681a63c3a57b1 127.0.0.1:7002

slots:10923-16383 (5461 slots) master

S: e50676757acbe7c6a21e8abf4eda26ababb08285 127.0.0.1:7003

replicates ad52e4f7c14da4b8b1e8e48603c9e5515a4cec7a

S: ab307ba26d6dabe8edb2f2a7287be6f01aa46d88 127.0.0.1:7004

replicates d3058a19483d7be5c30c042779e56130f6ebf074

S: 11174332eb6ad40c0327750536fa776d706caf85 127.0.0.1:7005

replicates 5814305e71552ab45c44b9b8233681a63c3a57b1

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:7000)

M: ad52e4f7c14da4b8b1e8e48603c9e5515a4cec7a 127.0.0.1:7000

slots:0-5460 (5461 slots) master

M: d3058a19483d7be5c30c042779e56130f6ebf074 127.0.0.1:7001

slots:5461-10922 (5462 slots) master

M: 5814305e71552ab45c44b9b8233681a63c3a57b1 127.0.0.1:7002

slots:10923-16383 (5461 slots) master

M: e50676757acbe7c6a21e8abf4eda26ababb08285 127.0.0.1:7003

slots: (0 slots) master

replicates ad52e4f7c14da4b8b1e8e48603c9e5515a4cec7a

M: ab307ba26d6dabe8edb2f2a7287be6f01aa46d88 127.0.0.1:7004

slots: (0 slots) master

replicates d3058a19483d7be5c30c042779e56130f6ebf074

M: 11174332eb6ad40c0327750536fa776d706caf85 127.0.0.1:7005

slots: (0 slots) master

replicates 5814305e71552ab45c44b9b8233681a63c3a57b1

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

3.测试集群。

可以用下面的命令查看集群的状态,从下面可以看出有4个master,2个slave~~~,这个是由于我太年轻kill了7000的redis又加入了进来,原先他的slave 7003 也变成了master顶替了他。

[root@localhost src]# ./redis-trib.rb check 127.0.0.1:7000

Connecting to node 127.0.0.1:7000: OK

Connecting to node 127.0.0.1:7005: OK

Connecting to node 127.0.0.1:7003: OK

Connecting to node 127.0.0.1:7002: OK

Connecting to node 127.0.0.1:7001: OK

Connecting to node 127.0.0.1:7004: OK

>>> Performing Cluster Check (using node 127.0.0.1:7000)

M: 4eb95ab689288dbb3ebf7ed380a0593ec9daa878 127.0.0.1:7000

slots: (0 slots) master

0 additional replica(s)

S: 11174332eb6ad40c0327750536fa776d706caf85 127.0.0.1:7005

slots: (0 slots) slave

replicates 5814305e71552ab45c44b9b8233681a63c3a57b1

M: e50676757acbe7c6a21e8abf4eda26ababb08285 127.0.0.1:7003

slots:0-5460 (5461 slots) master

0 additional replica(s)

M: 5814305e71552ab45c44b9b8233681a63c3a57b1 127.0.0.1:7002

slots:10923-16383 (5461 slots) master

1 additional replica(s)

M: d3058a19483d7be5c30c042779e56130f6ebf074 127.0.0.1:7001

slots:5461-10922 (5462 slots) master

1 additional replica(s)

S: ab307ba26d6dabe8edb2f2a7287be6f01aa46d88 127.0.0.1:7004

slots: (0 slots) slave

replicates d3058a19483d7be5c30c042779e56130f6ebf074

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

可以进入redis中玩一玩:查了一下使用redis-cli命令的时候应该加上 -c ,意思是在集群模式下,否则你会以一种独立的redis进入,然后各种moved报错。

看着存储位置跳来跳去,貌似简单的集群也就搭建完成了。

[root@localhost src]# ./redis-cli -c -p 7000

127.0.0.1:7000> set linuxidc weikang

-> Redirected to slot [1357] located at 127.0.0.1:7003

OK

127.0.0.1:7003> set linuxidc qiang

OK

127.0.0.1:7003> set weikang linuxidc

-> Redirected to slot [12142] located at 127.0.0.1:7002

OK

127.0.0.1:7002> get linuxidc

-> Redirected to slot [1357] located at 127.0.0.1:7003

"linuxidc"

127.0.0.1:7003> get weikang

-> Redirected to slot [12142] located at 127.0.0.1:7002

"linuxidc"

4.遗留的一些问题:

(1)搭建的集群连接上客户端后,所有的redis只有db0可用,不知道是什么原因。(难道是互相之间只连接了db0吗?)

(2)增删改节点还有待继续研究,不能只是单纯的写命令就可以了,要理解在什么场景下应该做如何的处理。

(3)集群管理工具在redis 3.0之后自己出了一个叫做redis-sentinel的,这个需要着重研究。

只有在深入理解的集群的意义和所配置的每个参数的含义之后,才能使用到生产环境中,以上只是个入门,学习redis集群才只是刚刚开始~~

下面关于Redis的文章您也可能喜欢,不妨参考下:

0b1331709591d260c1c78e86d0c51c18.png

redis-cluster-tool 是一个非常便利的 Redis 集群管理工具。help        Usage: redis-cluster-tool [-?hVds] [-v verbosity level] [-o output file]                  [-c conf file] [-a addr] [-i interval]                  [-p pid file] [-C command] [-r redis role]                  [-t thread number] [-b buffer size]    Options:      -h, --help             : this help      -V, --version          : show version and exit      -d, --daemonize        : run as a daemon      -s, --simple           : show the output not in detail      -v, --verbosity=N      : set logging level (default: 5, min: 0, max: 11)      -o, --output=S         : set logging file (default: stderr)      -c, --conf-file=S      : set configuration file (default: conf/rct.yml)      -a, --addr=S           : set redis cluster address (default: 127.0.0.1:6379)      -i, --interval=N       : set interval in msec (default: 1000 msec)      -p, --pid-file=S       : set pid file (default: off)      -C, --command=S        : set command to execute (default: cluster_state)      -r, --role=S           : set the role of the nodes that command to execute on (default: all, you can input: all, master or slave)      -t, --thread=N         : set how many threads to run the job(default: 8)      -b, --buffer=S         : set buffer size to run the job (default: 1048576 byte, unit:G/M/K)        Commands:        cluster_state                 :Show the cluster state.        cluster_used_memory           :Show the cluster used memory.        cluster_keys_num              :Show the cluster holds keys num.        slots_state                   :Show the slots state.        node_slot_num                 :Show the node hold slots number.        new_nodes_name                :Show the new nodes name that not covered slots.        cluster_rebalance             :Show the cluster how to rebalance.        flushall                      :Flush all the cluster.        cluster_config_get            :Get config from every node in the cluster and check consistency.        cluster_config_set            :Set config to every node in the cluster.        cluster_config_rewrite        :Rewrite every node config to echo node for the cluster.        node_list                     :List the nodes            del_keys                      :Delete keys in the cluster. The keys must match a given glob-style pattern.(This command not block the redis)ExampleGet the cluster state:        $redis-cluster-tool -a 127.0.0.1:34501 -C cluster_state -r master    master[127.0.0.1:34504] cluster_state is ok     master[127.0.0.1:34501] cluster_state is ok     master[127.0.0.1:34502] cluster_state is ok     master[127.0.0.1:34503] cluster_state is ok     all nodes cluster_state is ok    Get the cluster used memory:    $redis-cluster-tool -a 127.0.0.1:34501 -C cluster_used_memory -r master    master[127.0.0.1:34504] used 195 M 25%    master[127.0.0.1:34501] used 195 M 25%    master[127.0.0.1:34502] used 195 M 25%    master[127.0.0.1:34503] used 195 M 25%    cluster used 780 MRebalance the cluster slots:    $redis-cluster-tool -a 127.0.0.1:34501 -C cluster_rebalance    --from e1a4ba9922555bfc961f987213e3d4e6659c9316 --to 785862477453bc6b91765ffba0b5bc803052d70a --slots 2048    --from 437c719f50dc9d0745032f3b280ce7ecc40792ac --to cb8299390ce53cefb2352db34976dd768708bf64 --slots 2048    --from a497fc619d4f6c93bd4afb85f3f8a148a3f35adb --to a0cf6c1f12d295cd80f5811afab713cdc858ea30 --slots 2048    --from 0bdef694d08cb3daab9aac518d3ad6f8035ec896 --to 471eaf98ff43ba9a0aadd9579f5af1609239c5b7 --slots 2048Then you can use "redis-trib.rb reshard --yes --from e1a4ba9922555bfc961f987213e3d4e6659c9316 --to 785862477453bc6b91765ffba0b5bc803052d70a --slots 2048 127.0.0.1:34501" to rebalance the cluster slots     Flushall the cluster:    $redis-cluster-tool -a 127.0.0.1:34501 -C flushall -s    Do you really want to execute the "flushall"?    please input "yes" or "no" :        yes    OKGet a config from every node in cluster:    $redis-cluster-tool -a 127.0.0.1:34501 -C "cluster_config_get maxmemory" -r master    master[127.0.0.1:34501] config maxmemory is 1048576000 (1000MB)    master[127.0.0.1:34502] config maxmemory is 1048576000 (1000MB)    master[127.0.0.1:34503] config maxmemory is 1048576000 (1000MB)    master[127.0.0.1:34504] config maxmemory is 1048576000 (1000MB)    All nodes config are Consistent    cluster total maxmemory: 4194304000 (4000MB)Set a config from every node in cluster:    $redis-cluster-tool -a 127.0.0.1:34501 -C "cluster_config_set maxmemory 10000000" -s    Do you really want to execute the "cluster_config_set"?    please input "yes" or "no" :    yes        OKDelete keys in the cluster:    $redis-cluster-tool -a 127.0.0.1:34501 -C "del_keys abc*"    Do you really want to execute the "del_keys"?    please input "yes" or "no" :    yes    delete keys job is running...    delete keys job finished, deleted: 999999 keys, used: 4 s 标签:redis
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值