創建集群
集群環境
拓撲結構
IP規劃
工作原理
工作原理
無論有多少臺redis服務器,一共都會有0-16383共16384個槽 slot的作用:用來確定存儲變量時,變量存儲在哪一臺redis服務器上的 存取數據的過程:變量名與內置的crc16算法做一個hash計算,得到的數字再與16384進行取余,根據余數的結果確定在哪一臺服務器節點 0-5460 5461-10922 10923-16383 能夠存多少變量取決于本機的內存的大小
創建集群
== == == == == == == == == == == == == == == == == == == == = 1.確保每一臺redis都是空的,由于前次試驗在host51上操作,所以先將host51還原至空的狀態,一定要保證redis是空的狀態!!!
[ root@host51 ~
]
Stopping
.. .
Redis stopped
[ root@host51 ~
] 501
[ root@host51 ~
]
Starting Redis server
.. .
[ root@host51 ~
]
192.168.4.51:6351
> exit
如果此時重啟報錯,可能是pid沖突,可以進行如下操作
[ root@host51 ~
]
/var/run/redis_6379.pid exists, process is already running or crashed
[ root@host51 ~
]
[ root@host51 ~
]
Starting Redis server
.. .
******************************2.接下來在host52-56主機分別準備好redis服務器,我們可以寫一些簡單的不能再簡單的腳本,以host52主機為例
[ root@host52 ~
]
[ root@host52 opt
]
> do
> scp /opt/redis-4.0.8.tar.gz root@192.168.4.
$i :/opt
> done [ root@host52 opt
]
yum -y
install gcc
tar -zxvf redis-4.0.8.tar.gz
cd redis-4.0.8/
make && make install ./utils/install_server.sh
[ root@host52 opt
]
> do
> scp /opt/redis.sh root@192.168.4.
$i :/opt
> done
[ root@host52 opt
]
[ root@host52 opt
]
[ root@host52 opt
]
[ root@host52 opt
] 70 bind 192.168.4.52
[ root@host52 opt
] 93 port 6352
[ root@host52 opt
]
[ root@host52 opt
]
*****
檢查準備工作:
[ root@host51 ~
]
tcp LISTEN 0 128 192.168.4.51:6351 *:* users:
(( "redis-server" ,pid
= 1841,fd
= 6
))
[ root@host52 opt
]
tcp LISTEN 0 128 127.0.0.1:6352 *:* users:
(( "redis-server" ,pid
= 4753,fd
= 6
))
[ root@host53 opt
]
tcp LISTEN 0 128 127.0.0.1:6353 *:* users:
(( "redis-server" ,pid
= 4721,fd
= 6
))
[ root@host54 opt
]
tcp LISTEN 0 128 127.0.0.1:6354 *:* users:
(( "redis-server" ,pid
= 4722,fd
= 6
))
[ root@host55 opt
]
tcp LISTEN 0 128 127.0.0.1:6355 *:* users:
(( "redis-server" ,pid
= 4749,fd
= 6
))
[ root@host56 opt
]
tcp LISTEN 0 128 127.0.0.1:6356 *:* users:
(( "redis-server" ,pid
= 4710,fd
= 6
)) 每臺redis服務器都必須是空的
redis-trib腳本
用法: [root@host57 ~]# redis-trib.rb <arguments…>
命令描述 create 創建集群 check 檢查集群 info 查看集群信息 reshard 重新分片 del-node 刪除主機 add-node --slave 添加slave主機 add-node 添加master主機 rebalance 平均分配hash slots
== == == == == == == == == == == == == == == == == == == == == == == = 1.配置管理主機host57,也可以部署在任意一臺redis服務器上
[ root@host57 opt
]
未安裝軟件包 ruby
[ root@host57 opt
]
/usr/bin/which: no gem
in ( /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
)
[ root@host57 opt
]
[ root@host57 opt
] redis-4.0.8.tar.gz redis-3.2.1.gem
[ root@host57 opt
]
Successfully installed redis-3.2.1
Parsing documentation
for redis-3.2.1
Installing ri documentation
for redis-3.2.1
1 gem installed
[ root@host57 opt
]
[ root@host57 opt
]
[ root@host57 redis-4.0.8
]
00-RELEASENOTES COPYING Makefile redis.conf runtest-sentinel tests
BUGS deps MANIFESTO runtest sentinel.conf utils
CONTRIBUTING INSTALL README.md runtest-cluster src
[ root@host57 redis-4.0.8
]
src/redis-trib.rb
[ root@host57 redis-4.0.8
]
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[ root@host57 redis-4.0.8
]
[ root@host57 redis-4.0.8
]
[ root@host57 redis-4.0.8
]
[ root@host57 redis-4.0.8
]
[ root@host57 redis-4.0.8
]
Usage: redis-trib
< command
> < options
> < arguments
.. .
> create host1:port1
.. . hostN:portN--replicas
< arg
> check host:portinfo host:portfix host:port--timeout
< arg
> reshard host:port--from
< arg
> --to
< arg
> --slots
< arg
> --yes--timeout
< arg
> --pipeline
< arg
> rebalance host:port--weight
< arg
> --auto-weights--use-empty-masters--timeout
< arg
> --simulate--pipeline
< arg
> --threshold
< arg
> add-node new_host:new_port existing_host:existing_port--slave--master-id
< arg
> del-node host:port node_idset-timeout host:port millisecondscall host:port
command arg arg
.. arg
import host:port--from
< arg
> --copy--replace
help ( show this help
) For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node
in the cluster.*************************************2.開啟集群,以host51為例,每臺redis集群都要做
[ root@host51 ~
]
192.168.4.51:6351
> cluster info
ERR This instance has cluster support disabled
[ root@host51 ~
] 815 cluster-enabled
yes 823 cluster-config-file nodes-6379.conf 829 cluster-node-timeout 5000
[ root@host51 ~
]
[ root@host51 ~
]
$CLIEXEC -h 192.168.4.51 -p 6351
shutdown
:wq
[ root@host51 ~
]
Starting Redis server
.. [ root@host51 ~
]
192.168.4.51:6351
> cluster info
cluster_state:fail
cluster_slots_assigned:0
cluster_slots_ok:0
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:1
cluster_size:0
cluster_current_epoch:0
cluster_my_epoch:0
cluster_stats_messages_sent:0
cluster_stats_messages_received:0
[ root@host51 ~
]
tcp 0 0 192.168.4.51:16351 0.0.0.0:* LISTEN 1964/redis-server 1
tcp 0 0 192.168.4.51:6351 0.0.0.0:* LISTEN 1964/redis-server 1
---------------------------------------------------------------
或者也可以寫個腳本
[ root@host51 ~
]
sed -i
'815s/# //' /etc/redis/6379.conf
sed -i
'823s/# //' /etc/redis/6379.conf
sed -i
'829s/# //' /etc/redis/6379.conf
sed -i
'829s/15000/5000/' /etc/redis/6379.conf
redis-cli -h 192.168.4.
$1 -p 63
$1 shutdown
/etc/init.d/redis_6379 start
netstat -antulp
| grep redis-server
-----------------------------------------------------------------
如果查不到pid號,可以進行如下操作
[ root@host51 ~
]
auditd.pid dbus initramfs mysqld setrans tuned
chrony dmeventd-client lock netreport sshd.pid udev
chronyd.pid dmeventd-server log NetworkManager
sudo user
console ebtables.lock lvm plymouth syslogd.pid utmp
crond.pid faillock lvmetad.pid redis_6379.pid systemd vmware
cron.reboot firewalld
mount sepermit tmpfiles.d xtables.lock
[ root@host51 ~
]
tcp 0 0 192.168.4.51:16351 0.0.0.0:* LISTEN 1964/redis-server 1
tcp 0 0 192.168.4.51:6351 0.0.0.0:* LISTEN 1964/redis-server 1
[ root@host51 ~
]
1964
或者先停止redis服務,再重新啟動
[ root@host51 ~
]
[ root@host51 ~
]
Starting Redis server
.. .
[ root@host51 ~
] ****************************************3.在管理主機57創建集群(記得關防火墻)
[ root@host57 ~
]
>> > Creating cluster
>> > Performing
hash slots allocation on 6 nodes
.. .
Using 3 masters:
192.168.4.51:6351
192.168.4.52:6352
192.168.4.53:6353
Adding replica 192.168.4.55:6355 to 192.168.4.51:6351
Adding replica 192.168.4.56:6356 to 192.168.4.52:6352
Adding replica 192.168.4.54:6354 to 192.168.4.53:6353
M: 25991ad67a2e5b7c570ca87eed3140432cc78eee 192.168.4.51:6351 slots:0-5460
( 5461 slots
) master
M: 23b46dfa5997798b02837b365122d0315a48a84e 192.168.4.52:6352slots:5461-10922
( 5462 slots
) master
M: 7747411ab79e16a5493e66d4a46515bd110ce822 192.168.4.53:6353slots:10923-16383
( 5461 slots
) master
S: 8da07552c93155e4ac83c63bf1ee717cb15610a0 192.168.4.54:6354 replicates 7747411ab79e16a5493e66d4a46515bd110ce822
S: b69bbe093c47927c88fa3eaf10cce898e4593cc5 192.168.4.55:6355replicates 25991ad67a2e5b7c570ca87eed3140432cc78eee
S: 75917bffed4334444e2cf50165b9b03652f6bdc2 192.168.4.56:6356replicates 23b46dfa5997798b02837b365122d0315a48a84e
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 192.168.4.51:6351
)
M: 25991ad67a2e5b7c570ca87eed3140432cc78eee 192.168.4.51:6351slots:0-5460
( 5461 slots
) master1 additional replica
( s
)
S: 8da07552c93155e4ac83c63bf1ee717cb15610a0 192.168.4.54:6354slots:
( 0 slots
) slavereplicates 7747411ab79e16a5493e66d4a46515bd110ce822
S: 75917bffed4334444e2cf50165b9b03652f6bdc2 192.168.4.56:6356slots:
( 0 slots
) slavereplicates 23b46dfa5997798b02837b365122d0315a48a84e
S: b69bbe093c47927c88fa3eaf10cce898e4593cc5 192.168.4.55:6355slots:
( 0 slots
) slavereplicates 25991ad67a2e5b7c570ca87eed3140432cc78eee
M: 7747411ab79e16a5493e66d4a46515bd110ce822 192.168.4.53:6353slots:10923-16383
( 5461 slots
) master1 additional replica
( s
)
M: 23b46dfa5997798b02837b365122d0315a48a84e 192.168.4.52:6352slots:5461-10922
( 5462 slots
) master1 additional replica
( s
)
[ OK
] All nodes agree about slots configuration.
>> > Check
for open slots
.. .
>> > Check slots coverage
.. .
[ OK
] All 16384 slots covered. *********************************************
4.在任意一臺redis服務器本機,查看集群信息
[ root@host51 ~
]
192.168.4.51:6351
> 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:1
cluster_stats_messages_ping_sent:487
cluster_stats_messages_pong_sent:587
cluster_stats_messages_sent:1074
cluster_stats_messages_ping_received:582
cluster_stats_messages_pong_received:487
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:1074
查看集群中的節點信息成員列表
192.168.4.51:6351
> cluster nodes
8da07552c93155e4ac83c63bf1ee717cb15610a0 192.168.4.54:6354@16354 slave 7747411ab79e16a5493e66d4a46515bd110ce822 0 1582890996432 4 connected
75917bffed4334444e2cf50165b9b03652f6bdc2 192.168.4.56:6356@16356 slave 23b46dfa5997798b02837b365122d0315a48a84e 0 1582890997000 6 connected
b69bbe093c47927c88fa3eaf10cce898e4593cc5 192.168.4.55:6355@16355 slave 25991ad67a2e5b7c570ca87eed3140432cc78eee 0 1582890998000 5 connected
7747411ab79e16a5493e66d4a46515bd110ce822 192.168.4.53:6353@16353 master - 0 1582890998449 3 connected 10923-16383
25991ad67a2e5b7c570ca87eed3140432cc78eee 192.168.4.51:6351@16351 myself,master - 0 1582890998000 1 connected 0-5460
23b46dfa5997798b02837b365122d0315a48a84e 192.168.4.52:6352@16352 master - 0 1582890997442 2 connected 5461-10922
[ root@host51 ~
]
8da07552c93155e4ac83c63bf1ee717cb15610a0 192.168.4.54:6354@16354 slave 7747411ab79e16a5493e66d4a46515bd110ce822 0 1582890552000 4 connected
75917bffed4334444e2cf50165b9b03652f6bdc2 192.168.4.56:6356@16356 slave 23b46dfa5997798b02837b365122d0315a48a84e 0 1582890552000 6 connected
b69bbe093c47927c88fa3eaf10cce898e4593cc5 192.168.4.55:6355@16355 slave 25991ad67a2e5b7c570ca87eed3140432cc78eee 0 1582890551442 5 connected
7747411ab79e16a5493e66d4a46515bd110ce822 192.168.4.53:6353@16353 master - 0 1582890552550 3 connected 10923-16383
25991ad67a2e5b7c570ca87eed3140432cc78eee 192.168.4.51:6351@16351 myself,master - 0 1582890551000 1 connected 0-5460
23b46dfa5997798b02837b365122d0315a48a84e 192.168.4.52:6352@16352 master - 0 1582890552450 2 connected 5461-10922
vars currentEpoch 6 lastVoteEpoch 0在管理主機上查看集群統計信息
[ root@host57 ~
]
192.168.4.51:6351
( 25991ad6
.. .
) -
> 0 keys
| 5461 slots
| 1 slaves.
192.168.4.53:6353
( 7747411a
.. .
) -
> 0 keys
| 5461 slots
| 1 slaves.
192.168.4.52:6352
( 23b46dfa
.. .
) -
> 0 keys
| 5462 slots
| 1 slaves.
[ OK
] 0 keys
in 3 masters.
0.00 keys per slot on average.在管理主機上看集群的詳細信息
[ root@host57 ~
]
>> > Performing Cluster Check
( using node 192.168.4.51:6351
)
M: 25991ad67a2e5b7c570ca87eed3140432cc78eee 192.168.4.51:6351slots:0-5460
( 5461 slots
) master1 additional replica
( s
)
S: 8da07552c93155e4ac83c63bf1ee717cb15610a0 192.168.4.54:6354slots:
( 0 slots
) slavereplicates 7747411ab79e16a5493e66d4a46515bd110ce822
S: 75917bffed4334444e2cf50165b9b03652f6bdc2 192.168.4.56:6356slots:
( 0 slots
) slavereplicates 23b46dfa5997798b02837b365122d0315a48a84e
S: b69bbe093c47927c88fa3eaf10cce898e4593cc5 192.168.4.55:6355slots:
( 0 slots
) slavereplicates 25991ad67a2e5b7c570ca87eed3140432cc78eee
M: 7747411ab79e16a5493e66d4a46515bd110ce822 192.168.4.53:6353slots:10923-16383
( 5461 slots
) master1 additional replica
( s
)
M: 23b46dfa5997798b02837b365122d0315a48a84e 192.168.4.52:6352slots:5461-10922
( 5462 slots
) master1 additional replica
( s
)
[ OK
] All nodes agree about slots configuration.
>> > Check
for open slots
.. .
>> > Check slots coverage
.. .
[ OK
] All 16384 slots covered.
如果主服務器壞了,那么對應的從服務器就會自動變成主
*****************************************************還原操作 在每一臺redis服務器上作如下操作:
1)停止redis服務
2)清空數據庫目錄
rm -rf /var/lib/redis/6379/*
3)檢查集群功能是否能啟用
4)啟動redis服務
5)查看端口,進程
6)在管理主機上創建集群
*****************************************************5.訪問集群
在客戶端可以連接集群中任意一臺redis服務器,也可以在redis服務器本機訪問
在任意一臺redis服務器上寫數據,數據都會存入相應的hash槽內,這就是分片式存儲
[ root@host51 ~
]
192.168.4.56:6356
> keys *
( empty list or set
)
192.168.4.56:6356
> set a 456
-
> Redirected to slot
[ 15495
] located at 192.168.4.53:6353
OK
192.168.4.53:6353
> keys *
1
) "a"
192.168.4.53:6353
> exit
[ root@host51 ~
]
192.168.4.54:6354
> keys *
1
) "a"
192.168.4.54:6354
> set b 784
-
> Redirected to slot
[ 3300
] located at 192.168.4.51:6351
OK
192.168.4.51:6351
> exit
[ root@host51 ~
]
192.168.4.55:6355
> keys *
1
) "b"
192.168.4.55:6355
> set x 23
-
> Redirected to slot
[ 16287
] located at 192.168.4.53:6353
OK
192.168.4.53:6353
> keys *
1
) "a"
2
) "x"
192.168.4.53:6353
> get b
-
> Redirected to slot
[ 3300
] located at 192.168.4.51:6351
"784"
192.168.4.51:6351
>
管理集群
1.故障切換測試
先查看當前集群中的主服務器
[ root@host57 ~
]
192.168.4.51:6351
( 25991ad6
.. .
) -
> 1 keys
| 5461 slots
| 1 slaves.
192.168.4.53:6353
( 7747411a
.. .
) -
> 2 keys
| 5461 slots
| 1 slaves.
192.168.4.52:6352
( 23b46dfa
.. .
) -
> 0 keys
| 5462 slots
| 1 slaves.
[ OK
] 3 keys
in 3 masters.
0.00 keys per slot on average.
[ root@host57 ~
]
>> > Performing Cluster Check
( using node 192.168.4.51:6351
)
M: 25991ad67a2e5b7c570ca87eed3140432cc78eee 192.168.4.51:6351slots:0-5460
( 5461 slots
) master1 additional replica
( s
)
S: 8da07552c93155e4ac83c63bf1ee717cb15610a0 192.168.4.54:6354slots:
( 0 slots
) slavereplicates 7747411ab79e16a5493e66d4a46515bd110ce822
M: 7747411ab79e16a5493e66d4a46515bd110ce822 192.168.4.53:6353slots:10923-16383
( 5461 slots
) master1 additional replica
( s
)
S: 75917bffed4334444e2cf50165b9b03652f6bdc2 192.168.4.56:6356slots:
( 0 slots
) slavereplicates 23b46dfa5997798b02837b365122d0315a48a84e
M: 23b46dfa5997798b02837b365122d0315a48a84e 192.168.4.52:6352slots:5461-10922
( 5462 slots
) master1 additional replica
( s
)
S: b69bbe093c47927c88fa3eaf10cce898e4593cc5 192.168.4.55:6355slots:
( 0 slots
) slavereplicates 25991ad67a2e5b7c570ca87eed3140432cc78eee
[ OK
] All nodes agree about slots configuration.
>> > Check
for open slots
.. .
>> > Check slots coverage
.. .
[ OK
] All 16384 slots covered.-----------------------------------------------------------------------停止master主機的redis服務,master宕機后對應的slave自動被選舉為master;
[ root@host51 ~
]
tcp LISTEN 0 128 192.168.4.51:16351 *:* users:
(( "redis-server" ,pid
= 1099,fd
= 8
))
tcp LISTEN 0 128 192.168.4.51:6351 *:* users:
(( "redis-server" ,pid
= 1099,fd
= 6
)) [ root@host51 ~
]
[ root@host51 ~
] [ root@host57 ~
]
192.168.4.52:6352
( 23b46dfa
.. .
) -
> 0 keys
| 5462 slots
| 1 slaves.
192.168.4.55:6355
( b69bbe09
.. .
) -
> 1 keys
| 5461 slots
| 0 slaves.
192.168.4.53:6353
( 7747411a
.. .
) -
> 2 keys
| 5461 slots
| 1 slaves.
[ OK
] 3 keys
in 3 masters.
0.00 keys per slot on average.在客戶端連接任意一臺55主機寫入數據
[ root@host56 ~
]
192.168.4.55:6355
> set ar 66
-
> Redirected to slot
[ 9934
] located at 192.168.4.52:6352
OK
192.168.4.52:6352
> set kk 4444
-
> Redirected to slot
[ 2589
] located at 192.168.4.55:6355
OK
192.168.4.55:6355
> keys *
1
) "kk"
2
) "b"
192.168.4.55:6355
> set pp 4575
-
> Redirected to slot
[ 14030
] located at 192.168.4.53:6353
OK
192.168.4.53:6353
> keys *
1
) "a"
2
) "pp"
3
) "x"
192.168.4.53:6353
> set ii 11123
-
> Redirected to slot
[ 3133
] located at 192.168.4.55:6355
OK
192.168.4.55:6355
> keys *
1
) "ii"
2
) "kk"
3
) "b" -------------------------------------------------------------------------源master啟動后,會自動配置為當前master的slave;
[ root@host51 ~
]
Starting Redis server
.. .
[ root@host51 ~
]
tcp LISTEN 0 128 192.168.4.51:16351 *:* users:
(( "redis-server" ,pid
= 1748,fd
= 8
))
tcp LISTEN 0 128 192.168.4.51:6351 *:* users:
(( "redis-server" ,pid
= 1748,fd
= 6
)) [ root@host57 ~
]
192.168.4.52:6352
( 23b46dfa
.. .
) -
> 1 keys
| 5462 slots
| 1 slaves.
192.168.4.55:6355
( b69bbe09
.. .
) -
> 3 keys
| 5461 slots
| 1 slaves.
192.168.4.53:6353
( 7747411a
.. .
) -
> 3 keys
| 5461 slots
| 1 slaves.
[ OK
] 7 keys
in 3 masters.
0.00 keys per slot on average.
[ root@host57 ~
]
>> > Performing Cluster Check
( using node 192.168.4.52:6352
)
M: 23b46dfa5997798b02837b365122d0315a48a84e 192.168.4.52:6352slots:5461-10922
( 5462 slots
) master1 additional replica
( s
)
M: b69bbe093c47927c88fa3eaf10cce898e4593cc5 192.168.4.55:6355slots:0-5460
( 5461 slots
) master1 additional replica
( s
)
M: 7747411ab79e16a5493e66d4a46515bd110ce822 192.168.4.53:6353slots:10923-16383
( 5461 slots
) master1 additional replica
( s
)
S: 75917bffed4334444e2cf50165b9b03652f6bdc2 192.168.4.56:6356slots:
( 0 slots
) slavereplicates 23b46dfa5997798b02837b365122d0315a48a84e
S: 25991ad67a2e5b7c570ca87eed3140432cc78eee 192.168.4.51:6351slots:
( 0 slots
) slavereplicates b69bbe093c47927c88fa3eaf10cce898e4593cc5
S: 8da07552c93155e4ac83c63bf1ee717cb15610a0 192.168.4.54:6354slots:
( 0 slots
) slavereplicates 7747411ab79e16a5493e66d4a46515bd110ce822
[ OK
] All nodes agree about slots configuration.
>> > Check
for open slots
.. .
>> > Check slots coverage
.. .
[ OK
] All 16384 slots covered.連接51主機的服務器,會發現,在55主機上寫入的數據自動同步到了51主機上
[ root@host51 ~
]
192.168.4.51:6351
> keys *
1
) "b"
2
) "kk"
3
) "ii" 用Redis集群存取數據時一定要確保每組主從服務器至少有一個是活著的,否則整個集群都會崩潰
存儲空間不足,或者當前已有的服務器處理不了客戶并發訪問量時,可以添加新的服務器到集群中
1.添加master服務器
1)部署一臺新redis服務器host58,運行服務并啟用集群配置
[ root@host58 opt
] redis-4.0.8.tar.gz
[ root@host58 opt
]
[ root@host58 opt
]
[ root@host58 opt
]
[ root@host58 redis-4.0.8
]
[ root@host58 redis-4.0.8
]
Port
: 6379
Config
file : /etc/redis/6379.conf
Log
file : /var/log/redis_6379.log
Data
dir : /var/lib/redis/6379
Executable
: /usr/local/bin/redis-server
Cli Executable
: /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf
= > /etc/init.d/redis_6379
[ root@host58 redis-4.0.8
]
Stopping
.. .
Redis stopped
[ root@host58 redis-4.0.8
] 70 bind 192.168.4.5893 port 6358815 cluster-enabled
yes 823 cluster-config-file nodes-6379.conf 829 cluster-node-timeout 5000
[ root@host58 redis-4.0.8
]
Starting Redis server
.. .
[ root@host58 redis-4.0.8
]
tcp LISTEN 0 128 192.168.4.58:16358 *:* users:
(( "redis-server" ,pid
= 4940,fd
= 8
))
tcp LISTEN 0 128 192.168.4.58:6358 *:* users:
(( "redis-server" ,pid
= 4940,fd
= 6
))
[ root@host58 redis-4.0.8
]
192.168.4.58:6358
> ping
PONG
192.168.4.58:6358
> cluster info
cluster_state:fail
cluster_slots_assigned:0
cluster_slots_ok:0
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:1
cluster_size:0
cluster_current_epoch:0
cluster_my_epoch:0
cluster_stats_messages_sent:0
cluster_stats_messages_received:0
192.168.4.58:6358
> exit 2)在管理主機57,執行添加新服務的操作,添加master主機,分配hash槽(slots)
[ root@host57 ~
]
192.168.4.52:6352
( 23b46dfa
.. .
) -
> 1 keys
| 5462 slots
| 1 slaves.
192.168.4.55:6355
( b69bbe09
.. .
) -
> 3 keys
| 5461 slots
| 1 slaves.
192.168.4.53:6353
( 7747411a
.. .
) -
> 3 keys
| 5461 slots
| 1 slaves.
[ OK
] 7 keys
in 3 masters.
[ root@host57 ~
]
[ root@host57 ~
]
192.168.4.52:6352
( 23b46dfa
.. .
) -
> 1 keys
| 5462 slots
| 1 slaves.
192.168.4.55:6355
( b69bbe09
.. .
) -
> 3 keys
| 5461 slots
| 1 slaves.
192.168.4.53:6353
( 7747411a
.. .
) -
> 3 keys
| 5461 slots
| 1 slaves.
192.168.4.58:6358
( 5e0d1807
.. .
) -
> 0 keys
| 0 slots
| 0 slaves.
[ OK
] 7 keys
in 4 masters.
0.00 keys per slot on average.3)重新分片(hash slot)
[ root@host57 ~
]
.. .
How many slots
do you want to move
( from 1 to 16384
) ? 4096
What is the receiving node ID? 5e0d18070c3fecdfcb3d560e4550f200ab8e0d58 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 Do you want to proceed with the proposed reshard plan
( yes/no
) ?
yes [ root@host57 ~
]
192.168.4.52:6352
( 23b46dfa
.. .
) -
> 1 keys
| 4096 slots
| 1 slaves.
192.168.4.55:6355
( b69bbe09
.. .
) -
> 3 keys
| 4096 slots
| 1 slaves.
192.168.4.53:6353
( 7747411a
.. .
) -
> 3 keys
| 4096 slots
| 1 slaves.
192.168.4.58:6358
( 5e0d1807
.. .
) -
> 0 keys
| 4096 slots
| 0 slaves.
[ OK
] 7 keys
in 4 masters.
0.00 keys per slot on average.
[ root@host57 ~
]
>> > Performing Cluster Check
( using node 192.168.4.52:6352
)
M: 23b46dfa5997798b02837b365122d0315a48a84e 192.168.4.52:6352slots:0-1365,8193-10922
( 4096 slots
) master1 additional replica
( s
)
M: b69bbe093c47927c88fa3eaf10cce898e4593cc5 192.168.4.55:6355slots:1366-5461
( 4096 slots
) master1 additional replica
( s
)
M: 7747411ab79e16a5493e66d4a46515bd110ce822 192.168.4.53:6353slots:5462-6826,13653-16383
( 4096 slots
) master1 additional replica
( s
)
M: 5e0d18070c3fecdfcb3d560e4550f200ab8e0d58 192.168.4.58:6358slots:6827-8192,10923-13652
( 4096 slots
) master1 additional replica
( s
)
S: 75917bffed4334444e2cf50165b9b03652f6bdc2 192.168.4.56:6356slots:
( 0 slots
) slavereplicates 23b46dfa5997798b02837b365122d0315a48a84e
S: 25991ad67a2e5b7c570ca87eed3140432cc78eee 192.168.4.51:6351slots:
( 0 slots
) slavereplicates b69bbe093c47927c88fa3eaf10cce898e4593cc5
S: 8da07552c93155e4ac83c63bf1ee717cb15610a0 192.168.4.54:6354slots:
( 0 slots
) slavereplicates 7747411ab79e16a5493e66d4a46515bd110ce822
[ OK
] All nodes agree about slots configuration.
>> > Check
for open slots
.. .
>> > Check slots coverage
.. .
[ OK
] All 16384 slots covered.-------------------------------------------------------------------------2.添加slave角色主機到集群里
1)部署新的redis服務器192.168.4.59,運行redis服務,啟用集群功能
[ root@host59 opt
]
redis-4.0.8.tar.gz
[ root@host59 opt
]
[ root@host59 opt
]
[ root@host59 opt
]
[ root@host59 redis-4.0.8
]
Port
: 6379
Config
file : /etc/redis/6379.conf
Log
file : /var/log/redis_6379.log
Data
dir : /var/lib/redis/6379
Executable
: /usr/local/bin/redis-server
Cli Executable
: /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf
= > /etc/init.d/redis_6379
[ root@host59 redis-4.0.8
]
Stopping
.. .
Redis stopped
[ root@host59 redis-4.0.8
] 70 bind 192.168.4.5993 port 6359815 cluster-enabled
yes 823 cluster-config-file nodes-6379.conf 829 cluster-node-timeout 5000
[ root@host59 redis-4.0.8
]
Starting Redis server
.. .
[ root@host59 redis-4.0.8
]
tcp LISTEN 0 128 192.168.4.59:16359 *:* users:
(( "redis-server" ,pid
= 14657,fd
= 8
))
tcp LISTEN 0 128 192.168.4.59:6359 *:* users:
(( "redis-server" ,pid
= 14657,fd
= 6
)) [ root@host59 redis-4.0.8
]
192.168.4.59:6359
> ping
PONG
192.168.4.59:6359
> cluster info
cluster_state:fail
cluster_slots_assigned:0
cluster_slots_ok:0
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:1
cluster_size:0
cluster_current_epoch:0
cluster_my_epoch:0
cluster_stats_messages_sent:0
cluster_stats_messages_received:02)在管理主機添加slave角色主機
[ root@host57 ~
]
[ root@host57 ~
]
192.168.4.52:6352
( 23b46dfa
.. .
) -
> 1 keys
| 4096 slots
| 1 slaves.
192.168.4.55:6355
( b69bbe09
.. .
) -
> 3 keys
| 4096 slots
| 1 slaves.
192.168.4.53:6353
( 7747411a
.. .
) -
> 3 keys
| 4096 slots
| 1 slaves.
192.168.4.58:6358
( 5e0d1807
.. .
) -
> 0 keys
| 4096 slots
| 1 slaves.
[ OK
] 7 keys
in 4 masters.
0.00 keys per slot on average.
[ root@host57 ~
]
>> > Performing Cluster Check
( using node 192.168.4.52:6352
)
M: 23b46dfa5997798b02837b365122d0315a48a84e 192.168.4.52:6352slots:0-1365,8193-10922
( 4096 slots
) master1 additional replica
( s
)
M: b69bbe093c47927c88fa3eaf10cce898e4593cc5 192.168.4.55:6355slots:1366-5461
( 4096 slots
) master1 additional replica
( s
)
M: 7747411ab79e16a5493e66d4a46515bd110ce822 192.168.4.53:6353slots:5462-6826,13653-16383
( 4096 slots
) master1 additional replica
( s
)
M: 5e0d18070c3fecdfcb3d560e4550f200ab8e0d58 192.168.4.58:6358slots:6827-8192,10923-13652
( 4096 slots
) master1 additional replica
( s
)
S: 75917bffed4334444e2cf50165b9b03652f6bdc2 192.168.4.56:6356slots:
( 0 slots
) slavereplicates 23b46dfa5997798b02837b365122d0315a48a84e
S: a33e7cd079dceefb8d8fc699947e9d17a114515e 192.168.4.59:6359slots:
( 0 slots
) slavereplicates 5e0d18070c3fecdfcb3d560e4550f200ab8e0d58
S: 25991ad67a2e5b7c570ca87eed3140432cc78eee 192.168.4.51:6351slots:
( 0 slots
) slavereplicates b69bbe093c47927c88fa3eaf10cce898e4593cc5
S: 8da07552c93155e4ac83c63bf1ee717cb15610a0 192.168.4.54:6354slots:
( 0 slots
) slavereplicates 7747411ab79e16a5493e66d4a46515bd110ce822
[ OK
] All nodes agree about slots configuration.
>> > Check
for open slots
.. .
>> > Check slots coverage
.. .
[ OK
] All 16384 slots covered.如果reshard分配不平均的話,可以執行以下操作:
[ root@host57 ~
]
1)移除slave主機
從服務器沒有hash槽,直接移除即可,一出事指定從服務器id值
[ root@host57 ~
]
192.168.4.52:6352
( 23b46dfa
.. .
) -
> 1 keys
| 4096 slots
| 1 slaves.
192.168.4.55:6355
( b69bbe09
.. .
) -
> 3 keys
| 4096 slots
| 1 slaves.
192.168.4.53:6353
( 7747411a
.. .
) -
> 3 keys
| 4096 slots
| 1 slaves.
192.168.4.58:6358
( 5e0d1807
.. .
) -
> 0 keys
| 4096 slots
| 1 slaves.
[ OK
] 7 keys
in 4 masters.
0.00 keys per slot on average.
[ root@host57 ~
]
>> > Performing Cluster Check
( using node 192.168.4.52:6352
)
M: 23b46dfa5997798b02837b365122d0315a48a84e 192.168.4.52:6352slots:0-1365,8193-10922
( 4096 slots
) master1 additional replica
( s
)
M: b69bbe093c47927c88fa3eaf10cce898e4593cc5 192.168.4.55:6355slots:1366-5461
( 4096 slots
) master1 additional replica
( s
)
M: 7747411ab79e16a5493e66d4a46515bd110ce822 192.168.4.53:6353slots:5462-6826,13653-16383
( 4096 slots
) master1 additional replica
( s
)
M: 5e0d18070c3fecdfcb3d560e4550f200ab8e0d58 192.168.4.58:6358slots:6827-8192,10923-13652
( 4096 slots
) master1 additional replica
( s
)
S: 75917bffed4334444e2cf50165b9b03652f6bdc2 192.168.4.56:6356slots:
( 0 slots
) slavereplicates 23b46dfa5997798b02837b365122d0315a48a84e
S: a33e7cd079dceefb8d8fc699947e9d17a114515e 192.168.4.59:6359slots:
( 0 slots
) slavereplicates 5e0d18070c3fecdfcb3d560e4550f200ab8e0d58
S: 25991ad67a2e5b7c570ca87eed3140432cc78eee 192.168.4.51:6351slots:
( 0 slots
) slavereplicates b69bbe093c47927c88fa3eaf10cce898e4593cc5
S: 8da07552c93155e4ac83c63bf1ee717cb15610a0 192.168.4.54:6354slots:
( 0 slots
) slavereplicates 7747411ab79e16a5493e66d4a46515bd110ce822
[ OK
] All nodes agree about slots configuration.
>> > Check
for open slots
.. .
>> > Check slots coverage
.. .
[ OK
] All 16384 slots covered.
[ root@host57 ~
]
>> > Removing node 25991ad67a2e5b7c570ca87eed3140432cc78eee from cluster 192.168.4.52:6352
>> > Sending CLUSTER FORGET messages to the cluster
.. .
>> > SHUTDOWN the node.
[ root@host51 ~
]
[ root@host57 ~
]
192.168.4.52:6352
( 23b46dfa
.. .
) -
> 1 keys
| 4096 slots
| 1 slaves.
192.168.4.55:6355
( b69bbe09
.. .
) -
> 3 keys
| 4096 slots
| 0 slaves.
192.168.4.53:6353
( 7747411a
.. .
) -
> 3 keys
| 4096 slots
| 1 slaves.
192.168.4.58:6358
( 5e0d1807
.. .
) -
> 0 keys
| 4096 slots
| 1 slaves.
[ OK
] 7 keys
in 4 masters.
0.00 keys per slot on average.
[ root@host51 ~
]
Starting Redis server
.. .
[ root@host57 ~
]
192.168.4.52:6352
( 23b46dfa
.. .
) -
> 1 keys
| 4096 slots
| 1 slaves.
192.168.4.55:6355
( b69bbe09
.. .
) -
> 3 keys
| 4096 slots
| 0 slaves.
192.168.4.53:6353
( 7747411a
.. .
) -
> 3 keys
| 4096 slots
| 1 slaves.
192.168.4.58:6358
( 5e0d1807
.. .
) -
> 0 keys
| 4096 slots
| 1 slaves.
[ OK
] 7 keys
in 4 masters.
0.00 keys per slot on average.2)把master服務器移除集群
①在管理主機,先刪除master服務器占用的hash槽
[ root@host57 ~
]
How many slots
do you want to move
( from 1 to 16384
) ? 4096
What is the receiving node ID? b69bbe093c47927c88fa3eaf10cce898e4593cc5
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
Source node
Do you want to proceed with the proposed reshard plan
( yes/no
) ?
yes ②在管理主機查看集群信息
[ root@host57 ~
]
>> > Removing node 5e0d18070c3fecdfcb3d560e4550f200ab8e0d58 from cluster 192.168.4.52:6352
>> > Sending CLUSTER FORGET messages to the cluster
.. .
>> > SHUTDOWN the node.
[ root@host57 ~
]
192.168.4.52:6352
( 23b46dfa
.. .
) -
> 1 keys
| 4096 slots
| 1 slaves.
192.168.4.55:6355
( b69bbe09
.. .
) -
> 3 keys
| 8192 slots
| 1 slaves.
192.168.4.53:6353
( 7747411a
.. .
) -
> 3 keys
| 4096 slots
| 1 slaves.
[ OK
] 7 keys
in 3 masters.
0.00 keys per slot on average.
[ root@host57 ~
]
[ root@host57 ~
]
192.168.4.52:6352
( 23b46dfa
.. .
) -
> 2 keys
| 5462 slots
| 1 slaves.
192.168.4.55:6355
( b69bbe09
.. .
) -
> 0 keys
| 5461 slots
| 1 slaves.
192.168.4.53:6353
( 7747411a
.. .
) -
> 5 keys
| 5461 slots
| 1 slaves.
[ OK
] 7 keys
in 3 masters.
0.00 keys per slot on average.
[ root@host57 ~
]
>> > Performing Cluster Check
( using node 192.168.4.52:6352
)
M: 23b46dfa5997798b02837b365122d0315a48a84e 192.168.4.52:6352slots:0-2731,8193-10922
( 5462 slots
) master1 additional replica
( s
)
M: b69bbe093c47927c88fa3eaf10cce898e4593cc5 192.168.4.55:6355slots:4097-5461,6827-8192,10923-13652
( 5461 slots
) master1 additional replica
( s
)
M: 7747411ab79e16a5493e66d4a46515bd110ce822 192.168.4.53:6353slots:2732-4096,5462-6826,13653-16383
( 5461 slots
) master1 additional replica
( s
)
S: 75917bffed4334444e2cf50165b9b03652f6bdc2 192.168.4.56:6356slots:
( 0 slots
) slavereplicates 23b46dfa5997798b02837b365122d0315a48a84e
S: a33e7cd079dceefb8d8fc699947e9d17a114515e 192.168.4.59:6359slots:
( 0 slots
) slavereplicates b69bbe093c47927c88fa3eaf10cce898e4593cc5
S: 8da07552c93155e4ac83c63bf1ee717cb15610a0 192.168.4.54:6354slots:
( 0 slots
) slavereplicates 7747411ab79e16a5493e66d4a46515bd110ce822
[ OK
] All nodes agree about slots configuration.
>> > Check
for open slots
.. .
>> > Check slots coverage
.. .
[ OK
] All 16384 slots covered.
[ root@host57 ~
]
>> > Adding node 192.168.4.58:6358 to cluster 192.168.4.52:6352
>> > Performing Cluster Check
( using node 192.168.4.52:6352
)
M: 23b46dfa5997798b02837b365122d0315a48a84e 192.168.4.52:6352slots:0-2731,8193-10922
( 5462 slots
) master1 additional replica
( s
)
M: b69bbe093c47927c88fa3eaf10cce898e4593cc5 192.168.4.55:6355slots:4097-5461,6827-8192,10923-13652
( 5461 slots
) master1 additional replica
( s
)
M: 7747411ab79e16a5493e66d4a46515bd110ce822 192.168.4.53:6353slots:2732-4096,5462-6826,13653-16383
( 5461 slots
) master1 additional replica
( s
)
S: 75917bffed4334444e2cf50165b9b03652f6bdc2 192.168.4.56:6356slots:
( 0 slots
) slavereplicates 23b46dfa5997798b02837b365122d0315a48a84e
S: a33e7cd079dceefb8d8fc699947e9d17a114515e 192.168.4.59:6359slots:
( 0 slots
) slavereplicates b69bbe093c47927c88fa3eaf10cce898e4593cc5
S: 8da07552c93155e4ac83c63bf1ee717cb15610a0 192.168.4.54:6354slots:
( 0 slots
) slavereplicates 7747411ab79e16a5493e66d4a46515bd110ce822
[ OK
] All nodes agree about slots configuration.
>> > Check
for open slots
.. .
>> > Check slots coverage
.. .
[ OK
] All 16384 slots covered.
[ ERR
] Node 192.168.4.58:6358 is not empty. Either the node already knows other nodes
( check with CLUSTER NODES
) or contains some key
in database 0.
解決辦法:1.可以將/var/lib/redis/6379/nodes-6379.conf刪除,然后將該主機的redis服務關閉重啟
[ root@host58 redis-4.0.8
]
a33e7cd079dceefb8d8fc699947e9d17a114515e 192.168.4.59:6359@16359 slave b69bbe093c47927c88fa3eaf10cce898e4593cc5 0 1582909828964 12 connected
8da07552c93155e4ac83c63bf1ee717cb15610a0 192.168.4.54:6354@16354 slave 7747411ab79e16a5493e66d4a46515bd110ce822 0 1582909828964 14 connected
75917bffed4334444e2cf50165b9b03652f6bdc2 192.168.4.56:6356@16356 slave 23b46dfa5997798b02837b365122d0315a48a84e 0 1582909829000 13 connected
7747411ab79e16a5493e66d4a46515bd110ce822 192.168.4.53:6353@16353 master - 0 1582909828964 14 connected 2732-4096 5462-6826 13653-16383
b69bbe093c47927c88fa3eaf10cce898e4593cc5 192.168.4.55:6355@16355 master - 0 1582909829000 12 connected 4097-5461 6827-8192 10923-13652
5e0d18070c3fecdfcb3d560e4550f200ab8e0d58 192.168.4.58:6358@16358 myself,master - 0 1582909828957 8 connected
23b46dfa5997798b02837b365122d0315a48a84e 192.168.4.52:6352@16352 master - 0 1582909828964 13 connected 0-2731 8193-10922
vars currentEpoch 14 lastVoteEpoch 02.或者可以直接在redis服務器內執行cluster reset重置
[ root@host58 redis-4.0.8
]
192.168.4.58:6358
> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:7
cluster_size:3
cluster_current_epoch:14
cluster_my_epoch:8
cluster_stats_messages_ping_sent:631
cluster_stats_messages_sent:631
cluster_stats_messages_pong_received:631
cluster_stats_messages_received:631
192.168.4.58:6358
> cluster reset
192.168.4.58:6358
> cluster info
cluster_state:fail
cluster_slots_assigned:0
cluster_slots_ok:0
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:1
cluster_size:0
cluster_current_epoch:14
cluster_my_epoch:8
cluster_stats_messages_ping_sent:719
cluster_stats_messages_sent:719
cluster_stats_messages_pong_received:719
cluster_stats_messages_received:719
[ root@host57 ~
]
>> > Adding node 192.168.4.58:6358 to cluster 192.168.4.52:6352
>> > Performing Cluster Check
( using node 192.168.4.52:6352
)
M: 23b46dfa5997798b02837b365122d0315a48a84e 192.168.4.52:6352slots:0-2731,8193-10922
( 5462 slots
) master1 additional replica
( s
)
M: b69bbe093c47927c88fa3eaf10cce898e4593cc5 192.168.4.55:6355slots:4097-5461,6827-8192,10923-13652
( 5461 slots
) master1 additional replica
( s
)
M: 7747411ab79e16a5493e66d4a46515bd110ce822 192.168.4.53:6353slots:2732-4096,5462-6826,13653-16383
( 5461 slots
) master1 additional replica
( s
)
S: 75917bffed4334444e2cf50165b9b03652f6bdc2 192.168.4.56:6356slots:
( 0 slots
) slavereplicates 23b46dfa5997798b02837b365122d0315a48a84e
S: a33e7cd079dceefb8d8fc699947e9d17a114515e 192.168.4.59:6359slots:
( 0 slots
) slavereplicates b69bbe093c47927c88fa3eaf10cce898e4593cc5
S: 8da07552c93155e4ac83c63bf1ee717cb15610a0 192.168.4.54:6354slots:
( 0 slots
) slavereplicates 7747411ab79e16a5493e66d4a46515bd110ce822
[ OK
] All nodes agree about slots configuration.
>> > Check
for open slots
.. .
>> > Check slots coverage
.. .
[ OK
] All 16384 slots covered.
>> > Send CLUSTER MEET to node 192.168.4.58:6358 to
make it
join the cluster.
[ OK
] New node added correctly.
[ root@host57 ~
]
192.168.4.58:6358
( 5e0d1807
.. .
) -
> 0 keys
| 0 slots
| 0 slaves.
192.168.4.53:6353
( 7747411a
.. .
) -
> 5 keys
| 5461 slots
| 1 slaves.
192.168.4.55:6355
( b69bbe09
.. .
) -
> 0 keys
| 5461 slots
| 1 slaves.
192.168.4.52:6352
( 23b46dfa
.. .
) -
> 2 keys
| 5462 slots
| 1 slaves.
[ OK
] 7 keys
in 4 masters.
0.00 keys per slot on average.
停止redis服務 清空數據庫目錄 注釋集群功能 啟動redis服務 查看端口只有服務端口,沒有集群端口 cluster info 顯示enabled
[ root@host51 ~
]
[ root@host51 ~
]
[ root@host51 ~
] 815 823 829
[ root@host51 ~
]
Starting Redis server
.. .
[ root@host51 ~
]
tcp 0 0 192.168.4.51:6351 0.0.0.0:* LISTEN 2005/redis-server 1
[ root@host51 ~
]
192.168.4.51:6351
> cluster info
ERR This instance has cluster support disabled
[ root@host51 ~
]
部署redis腳本
#!/bin/bash
rpm -q gcc
if [ $? -eq 0
] ; then
echo "gcc已經安裝"
else
yum -y
install gcc
expect
fi
redis
( ) { tar -zxf redis-4.0.8.tar.gz
cd redis-4.0.8/
make make install cd utils/
expect << ok
spawn ./install_server.sh
expect "Please select the redis port for this instance" {send "exit\r"}
expect "Please select the redis config file name " {send "exit\r"}
expect "Please select the redis log file name " {send "exit\r"}
expect "select the data directory for this instance " {send "exit\r"}
expect "Please select the redis executable path " {send "exit\r"}
expect "Is this ok? Then press ENTER to go on or Ctrl-C to abort." {send "exit\r"}
expect "##" {send "exit\r"}
expect "##" {send "exit\r"}
ok
}
redis
echo "redis服務已安裝完成"
總結
以上是生活随笔 為你收集整理的NoSQL(二):创建、管理集群 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。