[root@master bin]# ./redis-cli --cluster create 192.168.230.21:7001 192.168.230.21:7002 192.168.230.21:7003 192.168.230.21:8001 192.168.230.21:8002 192.168.230.21:8003 --cluster-replicas 1 -a 123456
上面的命令只能在新創(chuàng)健集群的時候執(zhí)行一次,目的是為了建立內(nèi)部各個節(jié)點的對應關(guān)系,比如主從關(guān)系,這些關(guān)系僅且只能在一個集群中初始化時對應一次;
如果再此執(zhí)行,則會出現(xiàn)如下錯誤:
[root@master bin]# ./redis-cli --cluster create 192.168.230.21:7001 192.168.230.21:7002 192.168.230.21:7003 192.168.230.21:8001 192.168.230.21:8002 192.168.230.21:8003 --cluster-replicas 1 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
[ERR] Node 192.168.230.21:7001 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
集群關(guān)閉直接將各個節(jié)點的進程kill掉即可;
[root@master bin]# ps -ef | grep redis root 11516 1 0 16:15 ? 00:00:10 ./redis-server 192.168.230.21:7002 [cluster] root 11521 1 0 16:15 ? 00:00:09 ./redis-server 192.168.230.21:7003 [cluster] root 11526 1 0 16:15 ? 00:00:10 ./redis-server 192.168.230.21:8001 [cluster] root 11531 1 0 16:15 ? 00:00:10 ./redis-server 192.168.230.21:8002 [cluster] root 11536 1 0 16:15 ? 00:00:10 ./redis-server 192.168.230.21:8003 [cluster] root 11869 1 0 16:33 ? 00:00:07 ./redis-server 192.168.230.21:7001 [cluster] root 12528 9737 0 17:39 pts/7 00:00:00 grep --color=auto redis
[root@master bin]# kill -9 11516 [root@master bin]# kill -9 11521 [root@master bin]# kill -9 11526 [root@master bin]# kill -9 11531 [root@master bin]# kill -9 11536 [root@master bin]# kill -9 11869
[root@master bin]# ps -ef | grep redis root 12552 9737 0 17:40 pts/7 00:00:00 grep --color=auto redis
(1)集群開啟
[root@master bin]# ./redis-server /opt/softWare/redis-cluster/redis01/redis.conf 12553:C 31 Mar 2020 17:40:59.875 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 12553:C 31 Mar 2020 17:40:59.875 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=12553, just started 12553:C 31 Mar 2020 17:40:59.875 # Configuration loaded [root@master bin]# ./redis-server /opt/softWare/redis-cluster/redis02/redis.conf 12558:C 31 Mar 2020 17:41:03.697 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 12558:C 31 Mar 2020 17:41:03.697 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=12558, just started 12558:C 31 Mar 2020 17:41:03.697 # Configuration loaded [root@master bin]# ./redis-server /opt/softWare/redis-cluster/redis03/redis.conf 12563:C 31 Mar 2020 17:41:06.702 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 12563:C 31 Mar 2020 17:41:06.702 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=12563, just started 12563:C 31 Mar 2020 17:41:06.702 # Configuration loaded [root@master bin]# ./redis-server /opt/softWare/redis-cluster/redis04/redis.conf 12568:C 31 Mar 2020 17:41:09.742 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 12568:C 31 Mar 2020 17:41:09.742 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=12568, just started 12568:C 31 Mar 2020 17:41:09.742 # Configuration loaded [root@master bin]# ./redis-server /opt/softWare/redis-cluster/redis05/redis.conf 12574:C 31 Mar 2020 17:41:12.760 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 12574:C 31 Mar 2020 17:41:12.760 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=12574, just started 12574:C 31 Mar 2020 17:41:12.760 # Configuration loaded [root@master bin]# ./redis-server /opt/softWare/redis-cluster/redis06/redis.conf 12580:C 31 Mar 2020 17:41:16.406 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 12580:C 31 Mar 2020 17:41:16.406 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=12580, just started 12580:C 31 Mar 2020 17:41:16.406 # Configuration loaded
[root@master bin]# ps -ef | grep redis root 12554 1 0 17:40 ? 00:00:00 ./redis-server 192.168.230.21:7001 [cluster] root 12559 1 0 17:41 ? 00:00:00 ./redis-server 192.168.230.21:7002 [cluster] root 12564 1 0 17:41 ? 00:00:00 ./redis-server 192.168.230.21:7003 [cluster] root 12569 1 0 17:41 ? 00:00:00 ./redis-server 192.168.230.21:8001 [cluster] root 12575 1 0 17:41 ? 00:00:00 ./redis-server 192.168.230.21:8002 [cluster] root 12581 1 0 17:41 ? 00:00:00 ./redis-server 192.168.230.21:8003 [cluster] root 12587 9737 0 17:41 pts/7 00:00:00 grep --color=auto redis
(2)集群連接
[root@master bin]# ./redis-cli -p 7001 -a 123456 -h 192.168.230.21 -a 123456 -c Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.230.21:7001> DBSIZE (integer) 2 192.168.230.21:7001> keys * 1) "aa" 2) "ss" 192.168.230.21:7001> set str STR -> Redirected to slot [6928] located at 192.168.230.21:7002 OK 192.168.230.21:7002>
redis有三種集群方式:主從復制,哨兵模式和集群。
主從復制原理:
主從復制優(yōu)缺點:
優(yōu)點:
缺點:
當主服務器中斷服務后,可以將一個從服務器升級為主服務器,以便繼續(xù)提供服務,但是這個過程需要人工手動來操作。 為此,Redis 2.8中提供了哨兵工具來實現(xiàn)自動化的系統(tǒng)監(jiān)控和故障恢復功能。
哨兵的作用就是監(jiān)控Redis系統(tǒng)的運行狀況。它的功能包括以下兩個。
(1)監(jiān)控主服務器和從服務器是否正常運行。
(2)主服務器出現(xiàn)故障時自動將從服務器轉(zhuǎn)換為主服務器。
哨兵的工作方式:
哨兵模式的優(yōu)缺點
優(yōu)點:
缺點:
redis的哨兵模式基本已經(jīng)可以實現(xiàn)高可用,讀寫分離 ,但是在這種模式下每臺redis服務器都存儲相同的數(shù)據(jù),很浪費內(nèi)存,所以在redis3.0上加入了cluster模式,實現(xiàn)的redis的分布式存儲,也就是說每臺redis節(jié)點上存儲不同的內(nèi)容。
Redis-Cluster采用無中心結(jié)構(gòu),它的特點如下:
工作方式:
在redis的每一個節(jié)點上,都有這么兩個東西,一個是插槽(slot),它的的取值范圍是:0-16383。還有一個就是cluster,可以理解為是一個集群管理的插件。當我們的存取的key到達的時候,redis會根據(jù)crc16的算法得出一個結(jié)果,然后把結(jié)果對 16384 求余數(shù),這樣每個 key 都會對應一個編號在 0-16383 之間的哈希槽,通過這個值,去找到對應的插槽所對應的節(jié)點,然后直接自動跳轉(zhuǎn)到這個對應的節(jié)點上進行存取操作。
為了保證高可用,redis-cluster集群引入了主從模式,一個主節(jié)點對應一個或者多個從節(jié)點,當主節(jié)點宕機的時候,就會啟用從節(jié)點。當其它主節(jié)點ping一個主節(jié)點A時,如果半數(shù)以上的主節(jié)點與A通信超時,那么認為主節(jié)點A宕機了。如果主節(jié)點A和它的從節(jié)點A1都宕機了,那么該集群就無法再提供服務了。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。