redis¼¯Èº

redis¼¯ÈºÈýÖÖ·½°¸£º

¿Í»§¶Ë·ÖƬ

´úÀíProxy(twemproxy)

Redis Cluster(ûÓÐÖÐÐĽڵã)

Codis(±È½ÏºÃ)

 

´´½¨redis¼¯Èº(×Ô¶¯·ÖƬ£¬ÔÚ¼¯ÈºÖÐ)

²Î¿¼:http://redisdoc.com/topic/cluster-tutorial.html#id2

[root@node120 opt]# mkdir `seq 7001 7008`

[root@node120 opt]# ls

7001  7002  7003 7004  7005  7006 7007  7008  rh

[root@node120 opt]# pwd

/opt

 

´´½¨ÅäÖÃÎļþ£º

cp /etc/redis326/6379.conf  /opt/

vim 6379.conf

port 6379

cluster-enabled yes

cluster-config-file nodes.conf

cluster-node-timeout 5000

 

[root@node120 opt]# sed 's/6379/7001/g' 6379.conf >> 7001/redis.conf

sed 's/6379/7002/g' 6379.conf >> 7002/redis.conf

sed 's/6379/7003/g' 6379.conf >> 7003/redis.conf

sed 's/6379/7004/g' 6379.conf >> 7004/redis.conf

sed 's/6379/7005/g' 6379.conf >> 7005/redis.conf

sed 's/6379/7006/g' 6379.conf >> 7006/redis.conf

sed 's/6379/7007/g' 6379.conf >> 7007/redis.conf

sed 's/6379/7008/g' 6379.conf >> 7008/redis.conf

 

ÅúÁ¿Æô¶¯redis

[root@node120 opt]# for i in `seq 7001 7008`;do cd/opt/$i && /usr/local/redis326/bin/redis-server redis.conf;done

²é¿´Æô¶¯µÄ½ø³Ì£º

[root@node120 7008]# ps -ef | grep redis

root      7244     1  015:15 ?        00:00:02/usr/local/redis326/bin/redis-server 192.168.2.120:6379

root      7248     1  015:15 ?        00:00:02/usr/local/redis326/bin/redis-server 192.168.2.120:6380

root      7307  6985  015:21 pts/2    00:00:00/usr/local/redis326/bin/redis-cli -h 192.168.2.120 -p 6380

root      7371  6687  015:55 pts/1    00:00:00 vim redis.conf

root      7415  6687  016:01 pts/1    00:00:00 vim7008/redis.conf

root     7420     1  016:05 ?        00:00:00/usr/local/redis326/bin/redis-server 192.168.2.120:7001 [cluster]

root     7422     1  0 16:05 ?        00:00:00/usr/local/redis326/bin/redis-server 192.168.2.120:7002 [cluster]

root     7426     1  0 16:05 ?        00:00:00/usr/local/redis326/bin/redis-server 192.168.2.120:7003 [cluster]

root     7430     1  0 16:05 ?        00:00:00/usr/local/redis326/bin/redis-server 192.168.2.120:7004 [cluster]

root     7434     1  0 16:05 ?        00:00:00 /usr/local/redis326/bin/redis-server192.168.2.120:7005 [cluster]

root     7440     1  0 16:05 ?        00:00:00/usr/local/redis326/bin/redis-server 192.168.2.120:7006 [cluster]

root     7442     1  0 16:05 ?        00:00:00/usr/local/redis326/bin/redis-server 192.168.2.120:7007 [cluster]

root     7446     1  0 16:05 ?        00:00:00/usr/local/redis326/bin/redis-server 192.168.2.120:7008 [cluster]

root     7452  6687  0 16:05 pts/1    00:00:00 grep --color=auto redis

 

²âÊÔ¼¯ÈºÊÇ·ñÕý³££º

[root@node120 opt]# /usr/local/redis326/bin/redis-cli-h 192.168.2.120 -p 7001

[root@node120 opt]# /usr/local/redis326/bin/redis-cli-h 192.168.2.120 -p 7002

[root@node120 opt]# /usr/local/redis326/bin/redis-cli-h 192.168.2.120 -p 7003

[root@node120 opt]# /usr/local/redis326/bin/redis-cli-h 192.168.2.120 -p 7004

[root@node120 opt]# /usr/local/redis326/bin/redis-cli-h 192.168.2.120 -p 7005

[root@node120 opt]# /usr/local/redis326/bin/redis-cli-h 192.168.2.120 -p 7006

[root@node120 opt]# /usr/local/redis326/bin/redis-cli-h 192.168.2.120 -p 7007

[root@node120 opt]# /usr/local/redis326/bin/redis-cli-h 192.168.2.120 -p 7008

 

°²×°Ò»¸öruby¼¯Èº¹ÜÀí¹¤¾ß£º

[root@node120 opt]# yum install ruby rubygems  -y

[root@node120 opt]# gem sources -a https://ruby.taobao.org/

https://ruby.taobao.org/ added to sources

[root@node120 opt]# gem install redis

Successfully installed redis-3.3.2

1 gem installed

Installing ri documentation for redis-3.3.2...

Installing RDoc documentation for redis-3.3.2...

 

ÔÚredisÔ´´úÂë°²×°Îļþ¸´ÖÆÎļþµ½Ö¸¶¨µÄĿ¼£º

[root@node120 src]# cp /usr/local/src/redis-3.2.6/src/redis-trib.rb/etc/redis326/redis-trib.rb

[root@node120 src]# ll /usr/local/bin/redis-trib.rb

lrwxrwxrwx 1 root root 27 Dec 27 16:21/usr/local/bin/redis-trib.rb -> /etc/redis326/redis-trib.rb

 

[root@node120 src]# redis-trib.rb help

Usage: redis-trib <command> <options><arguments ...>

 

  fix             host:port

                 --timeout <arg>

  check           host:port

  help            (show this help)

  call            host:port command arg arg .. arg

  add-node        new_host:new_portexisting_host:existing_port

                 --master-id <arg>

                 --slave

  rebalance       host:port

                 --auto-weights

                 --threshold <arg>

                 --simulate

                  --pipeline <arg>

                 --weight <arg>

                 --timeout <arg>

                 --use-empty-masters

  import          host:port

                 --copy

                 --replace

                 --from <arg>

  set-timeout     host:port milliseconds

  reshard         host:port

                 --slots <arg>

                 --pipeline <arg>

                 --timeout <arg>

                 --to <arg>

                 --yes

                 --from <arg>

  create          host1:port1 ... hostN:portN

                 --replicas <arg>

  info            host:port

  del-node        host:port node_id

 

For check, fix, reshard, del-node, set-timeout you canspecify the host and port of any working node in the cluster.

 

ͨ¹ý¹¤¾ß´´½¨Ö÷´Ó¼¯Èº£º

[root@node120 src]# redis-trib.rb create --replicas 1192.168.2.120:7001 192.168.2.120:7002 192.168.2.120:7003 192.168.2.120:7004192.168.2.120:7005 192.168.2.120:7006

>>> Creating cluster

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

Using 3 masters:

192.168.2.120:7001

192.168.2.120:7002

192.168.2.120:7003

Adding replica 192.168.2.120:7004 to192.168.2.120:7001

Adding replica 192.168.2.120:7005 to192.168.2.120:7002

Adding replica 192.168.2.120:7006 to192.168.2.120:7003

M: b6172dd5ac64780847fd6660b96ac019ae2cd5a4192.168.2.120:7001

   slots:0-5460(5461 slots) master

M: 87ac8d54f76414e1c12a7eec9286a52328eb64d8192.168.2.120:7002

  slots:5461-10922 (5462 slots) master

M: 532f4a7eea37e338e15c56d417e7370e65e037b5 192.168.2.120:7003

  slots:10923-16383 (5461 slots) master

S: dd190db89ed8db54a53a5e1ce9624a60a36ca2d8192.168.2.120:7004

   replicatesb6172dd5ac64780847fd6660b96ac019ae2cd5a4

S: f1eddccbd05ee6c67b0c7e8a8c83897ae4c59c37192.168.2.120:7005

   replicates87ac8d54f76414e1c12a7eec9286a52328eb64d8

S: 6a86ea5f88cb0e8b577c2aa7396b727a587b005f192.168.2.120:7006

   replicates532f4a7eea37e338e15c56d417e7370e65e037b5

Can I set the above configuration? (type 'yes' toaccept): yes   

>>> Nodes configuration updated

>>> Assign a different config epoch to eachnode

>>> Sending CLUSTER MEET messages to join thecluster

Waiting for the cluster to join.

>>> Performing Cluster Check (using node192.168.2.120:7001)

M: b6172dd5ac64780847fd6660b96ac019ae2cd5a4192.168.2.120:7001

   slots:0-5460(5461 slots) master

   1 additionalreplica(s)

S: 6a86ea5f88cb0e8b577c2aa7396b727a587b005f192.168.2.120:7006

   slots: (0slots) slave

   replicates532f4a7eea37e338e15c56d417e7370e65e037b5

S: f1eddccbd05ee6c67b0c7e8a8c83897ae4c59c37192.168.2.120:7005

   slots: (0slots) slave

   replicates87ac8d54f76414e1c12a7eec9286a52328eb64d8

M: 87ac8d54f76414e1c12a7eec9286a52328eb64d8192.168.2.120:7002

  slots:5461-10922 (5462 slots) master

   1 additionalreplica(s)

M: 532f4a7eea37e338e15c56d417e7370e65e037b5192.168.2.120:7003

  slots:10923-16383 (5461 slots) master

   1 additionalreplica(s)

S: dd190db89ed8db54a53a5e1ce9624a60a36ca2d8192.168.2.120:7004

   slots: (0slots) slave

   replicatesb6172dd5ac64780847fd6660b96ac019ae2cd5a4

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

 

Ñé Ö¤£º

[root@node120 src]# /usr/local/redis326/bin/redis-cli-c -h 192.168.2.120 -p 7001

192.168.2.120:7001> info

# Replication

role:master

connected_slaves:1

slave0:ip=192.168.2.120,port=7004,state=online,offset=393,lag=0

master_repl_offset:393

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:2

repl_backlog_histlen:392

192.168.2.120:7001> set key1  1

-> Redirected to slot [9189] located at192.168.2.120:7002         #±¾µØ²»±£ÁôÊý¾Ý,½«Êý¾ÝÍƵ½7002ÉÏ

OK

192.168.2.120:7002>

 

 

[root@node120 redis]#/usr/local/redis326/bin/redis-cli -c -h 192.168.2.120 -p 7002

192.168.2.120:7002> keys *

1) "key1"

192.168.2.120:7002>

 

 

²é¿´¼¯Èº½á½Ú״̬£º

192.168.2.120:7003> CLUSTER nodes

dd190db89ed8db54a53a5e1ce9624a60a36ca2d8192.168.2.120:7004 slave b6172dd5ac64780847fd6660b96ac019ae2cd5a4 01482830788754 1 connected

532f4a7eea37e338e15c56d417e7370e65e037b5192.168.2.120:7003 myself,master - 0 0 3 connected 10923-16383

f1eddccbd05ee6c67b0c7e8a8c83897ae4c59c37192.168.2.120:7005 slave 87ac8d54f76414e1c12a7eec9286a52328eb64d8 01482830789259 2 connected

b6172dd5ac64780847fd6660b96ac019ae2cd5a4192.168.2.120:7001 master - 0 1482830788252 1 connected 0-5460

87ac8d54f76414e1c12a7eec9286a52328eb64d8192.168.2.120:7002 master - 0 1482830789763 2 connected 5461-10922

6a86ea5f88cb0e8b577c2aa7396b727a587b005f192.168.2.120:7006 slave 532f4a7eea37e338e15c56d417e7370e65e037b5 01482830788252 3 connected

 

192.168.2.120:7003> CLUSTER INFO

cluster_state:ok

cluster_slots_assigned:16384

cluster_slots_ok:16384

cluster_slots_pfail:0

cluster_slots_fail:0

cluster_known_nodes:6

cluster_size:3

cluster_current_epoch:6

cluster_my_epoch:3

cluster_stats_messages_sent:3184

cluster_stats_messages_received:3184

 

Ïò¼¯ÈºÖÐÌí¼ÓÒ»¸ö½áµã£º

[root@node120 redis326]# redis-trib.rb add-node192.168.2.120:7007 192.168.2.120:7001                       #нáµã£º192.168.2.120:7007   ¼¯ÈºÖÐÈÎÒâ½Úµã£º192.168.2.120:7001          

>>> Adding node 192.168.2.120:7007 to cluster192.168.2.120:7001

>>> Performing Cluster Check (using node192.168.2.120:7001)

M: b6172dd5ac64780847fd6660b96ac019ae2cd5a4192.168.2.120:7001

   slots:0-5460(5461 slots) master

   1 additionalreplica(s)

S: 6a86ea5f88cb0e8b577c2aa7396b727a587b005f192.168.2.120:7006

   slots: (0slots) slave

   replicates532f4a7eea37e338e15c56d417e7370e65e037b5

S: f1eddccbd05ee6c67b0c7e8a8c83897ae4c59c37192.168.2.120:7005

   slots: (0slots) slave

   replicates87ac8d54f76414e1c12a7eec9286a52328eb64d8

M: 87ac8d54f76414e1c12a7eec9286a52328eb64d8192.168.2.120:7002

  slots:5461-10922 (5462 slots) master

   1 additionalreplica(s)

M: 532f4a7eea37e338e15c56d417e7370e65e037b5192.168.2.120:7003

  slots:10923-16383 (5461 slots) master

   1 additionalreplica(s)

S: dd190db89ed8db54a53a5e1ce9624a60a36ca2d8192.168.2.120:7004

   slots: (0slots) slave

   replicatesb6172dd5ac64780847fd6660b96ac019ae2cd5a4

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

>>> Send CLUSTER MEET to node192.168.2.120:7007 to make it join the cluster.

[OK] New node added correctly.

 

192.168.2.120:7003> cluster nodes

dd190db89ed8db54a53a5e1ce9624a60a36ca2d8192.168.2.120:7004 slave b6172dd5ac64780847fd6660b96ac019ae2cd5a4 0 14828310415361 connected

532f4a7eea37e338e15c56d417e7370e65e037b5192.168.2.120:7003 myself,master - 0 0 3 connected 10923-16383

f1eddccbd05ee6c67b0c7e8a8c83897ae4c59c37192.168.2.120:7005 slave 87ac8d54f76414e1c12a7eec9286a52328eb64d8 01482831040531 2 connected

b6172dd5ac64780847fd6660b96ac019ae2cd5a4192.168.2.120:7001 master - 0 1482831042042 1 connected 0-5460

87ac8d54f76414e1c12a7eec9286a52328eb64d8192.168.2.120:7002 master - 0 1482831042548 2 connected 5461-10922

f411d72ebe01ff1825ef2ea938f8c04d09f26acb192.168.2.120:7007 master - 0 1482831041033 0 connected                        #ÐÂÌí¼ÓµÄ½Úµã£¬¿ªÊ¼µ±×÷Ö÷½Úµã.Ϊ£¬ ûÓвÛλ

6a86ea5f88cb0e8b577c2aa7396b727a587b005f192.168.2.120:7006 slave 532f4a7eea37e338e15c56d417e7370e65e037b5 01482831041033 3 connected

 

ÖØзÖÅä²Ûλ¸øÐÂÌí¼ÓµÄ½Úµã£º(Êý¾Ý»á½øÐÐǨÒÆ,·ÖÅä²Ûλ)

#redis-trib.rb reshard 192.168.2.120:7007

 

ǨÒÆÍêºó²é¿´nodes:

192.168.2.120:7003> cluster nodes

dd190db89ed8db54a53a5e1ce9624a60a36ca2d8192.168.2.120:7004 slave b6172dd5ac64780847fd6660b96ac019ae2cd5a4 01482831317705 1 connected

532f4a7eea37e338e15c56d417e7370e65e037b5192.168.2.120:7003 myself,master - 0 0 3 connected 11089-16383

f1eddccbd05ee6c67b0c7e8a8c83897ae4c59c37192.168.2.120:7005 slave 87ac8d54f76414e1c12a7eec9286a52328eb64d8 01482831317705 2 connected

b6172dd5ac64780847fd6660b96ac019ae2cd5a4192.168.2.120:7001 master - 0 1482831316196 1 connected 166-5460

87ac8d54f76414e1c12a7eec9286a52328eb64d8192.168.2.120:7002 master - 0 1482831317204 2 connected 5628-10922

f411d72ebe01ff1825ef2ea938f8c04d09f26acb192.168.2.120:7007 master - 0 1482831316700 7 connected 0-165 5461-562710923-11088

6a86ea5f88cb0e8b577c2aa7396b727a587b005f192.168.2.120:7006 slave 532f4a7eea37e338e15c56d417e7370e65e037b5 01482831318207 3 connected

 

 

ÔÙÌí¼ÓÒ»¸ö½Úµã£º

[root@node120 redis326]# redis-trib.rb add-node192.168.2.120:7008 192.168.2.120:7001

>>> Adding node 192.168.2.120:7008 to cluster192.168.2.120:7001

>>> Performing Cluster Check (using node192.168.2.120:7001)

M: b6172dd5ac64780847fd6660b96ac019ae2cd5a4192.168.2.120:7001

  slots:166-5460 (5295 slots) master

   1 additionalreplica(s)

S: 6a86ea5f88cb0e8b577c2aa7396b727a587b005f192.168.2.120:7006

   slots: (0slots) slave

   replicates532f4a7eea37e338e15c56d417e7370e65e037b5

S: f1eddccbd05ee6c67b0c7e8a8c83897ae4c59c37192.168.2.120:7005

   slots: (0slots) slave

   replicates87ac8d54f76414e1c12a7eec9286a52328eb64d8

M: 87ac8d54f76414e1c12a7eec9286a52328eb64d8192.168.2.120:7002

  slots:5628-10922 (5295 slots) master

   1 additionalreplica(s)

M: 532f4a7eea37e338e15c56d417e7370e65e037b5192.168.2.120:7003

  slots:11089-16383 (5295 slots) master

   1 additionalreplica(s)

M: f411d72ebe01ff1825ef2ea938f8c04d09f26acb192.168.2.120:7007

  slots:0-165,5461-5627,10923-11088 (499 slots) master

   0 additionalreplica(s)

S: dd190db89ed8db54a53a5e1ce9624a60a36ca2d8192.168.2.120:7004

   slots: (0slots) slave

   replicatesb6172dd5ac64780847fd6660b96ac019ae2cd5a4

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

>>> Send CLUSTER MEET to node192.168.2.120:7008 to make it join the cluster.

[OK] New node added correctly.

 

ÐÂÌí¼ÓµÄ7008ĬÈÏÊÇmaster,µ«ÊÇûÓвÛλ£º

192.168.2.120:7003> cluster nodes

dd190db89ed8db54a53a5e1ce9624a60a36ca2d8192.168.2.120:7004 slave b6172dd5ac64780847fd6660b96ac019ae2cd5a4 01482831462224 1 connected

532f4a7eea37e338e15c56d417e7370e65e037b5192.168.2.120:7003 myself,master - 0 0 3 connected 11089-16383

f1eddccbd05ee6c67b0c7e8a8c83897ae4c59c37192.168.2.120:7005 slave 87ac8d54f76414e1c12a7eec9286a52328eb64d8 01482831462725 2 connected

b6172dd5ac64780847fd6660b96ac019ae2cd5a4192.168.2.120:7001 master - 0 1482831462224 1 connected 166-5460

87ac8d54f76414e1c12a7eec9286a52328eb64d8192.168.2.120:7002 master - 0 1482831463728 2 connected 5628-10922

7dae246693bc4963040a731f65d0fab27676bdf3192.168.2.120:7008 master - 0 1482831462725 0 connected

f411d72ebe01ff1825ef2ea938f8c04d09f26acb 192.168.2.120:7007master - 0 1482831463226 7 connected 0-165 5461-5627 10923-11088

6a86ea5f88cb0e8b577c2aa7396b727a587b005f192.168.2.120:7006 slave 532f4a7eea37e338e15c56d417e7370e65e037b5 01482831463728 3 connected

 

 

½«7008×÷Ϊ7007µÄ´Ó¿â£º

[root@node120 redis326]#/usr/local/redis326/bin/redis-cli -c -h 192.168.2.120 -p 7008

192.168.2.120:7008> CLUSTER replicate f411d72ebe01ff1825ef2ea938f8c04d09f26acb      #Ϊ7007 masterµÄid

OK

192.168.2.120:7008>

 

M: f411d72ebe01ff1825ef2ea938f8c04d09f26acb 192.168.2.120:7007      

  slots:0-165,5461-5627,10923-11088 (499 slots) master

   0 additionalreplica(s)

²é¿´¼¯Èº¹Øϵ±í£º

192.168.2.120:7008> CLUSTER NODES

f411d72ebe01ff1825ef2ea938f8c04d09f26acb192.168.2.120:7007 master - 0 1482831719171 7 connected 0-165 5461-5627 10923-11088     #Ö÷¿â

6a86ea5f88cb0e8b577c2aa7396b727a587b005f192.168.2.120:7006 slave 532f4a7eea37e338e15c56d417e7370e65e037b5 01482831718667 3 connected

87ac8d54f76414e1c12a7eec9286a52328eb64d8192.168.2.120:7002 master - 0 1482831719676 2 connected 5628-10922

f1eddccbd05ee6c67b0c7e8a8c83897ae4c59c37192.168.2.120:7005 slave 87ac8d54f76414e1c12a7eec9286a52328eb64d8 01482831719676 2 connected

7dae246693bc4963040a731f65d0fab27676bdf3192.168.2.120:7008 myself,slave f411d72ebe01ff1825ef2ea938f8c04d09f26acb 0 0 0connected            #´Ó¿â

532f4a7eea37e338e15c56d417e7370e65e037b5192.168.2.120:7003 master - 0 1482831720684 3 connected 11089-16383

dd190db89ed8db54a53a5e1ce9624a60a36ca2d8192.168.2.120:7004 slave b6172dd5ac64780847fd6660b96ac019ae2cd5a4 01482831719676 1 connected

b6172dd5ac64780847fd6660b96ac019ae2cd5a4192.168.2.120:7001 master - 0 1482831720180 1 connected 166-5460

 

 

 

 

 

 

 

 

 

 

·Ö¸îÏß
¸Ðл´òÉÍ
½­Î÷Êý¿âÐÅÏ¢¼¼ÊõÓÐÏÞ¹«Ë¾
YWSOS.COM ƽ̨´úÔËά½â¾ö·½°¸
 ÆÀÂÛ
 ·¢±íÆÀÂÛ
ÐÕ   Ãû£º

Powered by AKCMS