Redis集羣安裝javascript
4版本須要ruby 5版本不須要ruby就能集羣html
1集羣機器分佈java
192.168.1.133 redis1node
192.168.1.134 redis2linux
192.168.1.135 redis3c++
2 免密登陸git
ssh-keygengithub
ssh-copy-id 192.168.1.133redis
ssh-copy-id 192.168.1.134sql
ssh-copy-id 192.168.1.135
3 關閉防火牆
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i ‘s/SELINUX=enforcing/SELINUX=disabled/g’ /etc/selinux/config
4設置yum源
這裏添加阿里源
cd /etc/yum.repos.d/
wget https://mirrors.aliyun.com/repo/Centos-7.repo
wget https://mirrors.aliyun.com/repo/epel-7.repo
5修改系統參數
vi /etc/security/limits.conf
* soft nofile 65535
* hard nofile 655350
* soft nproc 65535
* hard nproc 655350
安裝依賴
yum install gcc make tcl –y
yum install -y gcc-c++ patch readline readline-devel zlib zlib-devel libyaml-devel libffi-devel make bzip2 autoconf automake libtool bison sqlite-devel iconv-devel
6 安裝redis
mkdir /opt/haizhi/redis
mkdir -p /opt/haizhi/redis
cd /opt/haizhi/redis/
wget http://download.redis.io/releases/redis-4.0.6.tar.gz
tar zxvf redis-4.0.6.tar.gz
cd redis-4.0.6/ #編譯安裝完成後會生成bin目錄
make & make PREFIX=/opt/haizhi/redis install
cp -a /opt/haizhi/redis/redis-4.0.6/src/redis-trib.rb /opt/haizhi/redis/bin/
#上面是複製集羣命令 到bin目錄
注意:若是make出錯 使用make MALLOC=libc
----------------------
5版本
----------------------
wget http://download.redis.io/releases/redis-5.0.2.tar.gz
tar xzf redis-5.0.2.tar.gz
cd redis-5.0.2
make & make install ====================================
7 建立集羣須要的目錄和配置
建立3個實例目錄
cd /opt/haizhi/redis/
mkdir -p cluster-6379/{conf,data,log,pid}
mkdir -p cluster-6380/{conf,data,log,pid}
mkdir -p cluster-6381/{conf,data,log,pid}
爲每一個實例建立配置文件(以6379爲例)
[root@localhost redis]# cat cluster-6379/conf/redis_6379.conf
bind 0.0.0.0
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /opt/haizhi/redis/cluster-6379/conf/redis_6379.pid
loglevel notice
logfile "/opt/haizhi/redis/cluster-6379/log/redis_6379.log"
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /opt/haizhi/redis/cluster-6379/data
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite yes
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no
lua-time-limit 5000
cluster-enabled yes
cluster-config-file /opt/haizhi/redis/cluster-6379/conf/nodes-6379.conf
cluster-node-timeout 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
其餘實例只需修改6379爲6380 6381 便可使用
8 啓動各實例
./redis-server /opt/haizhi/redis/cluster-6379/conf/redis_6379.conf
./redis-server /opt/haizhi/redis/cluster-6380/conf/redis_6380.conf
./redis-server /opt/haizhi/redis/cluster-6381/conf/redis_6381.conf
[root@localhost redis]# netstat -lnupt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 17736/./redis-serve
tcp 0 0 0.0.0.0:6380 0.0.0.0:* LISTEN 17741/./redis-serve
tcp 0 0 0.0.0.0:6381 0.0.0.0:* LISTEN 17746/./redis-serve
以上步驟 三個節點都要執行安裝操做
安裝啓動後再選一臺機器設置集羣
9 設置集羣(選一臺機器安裝)
redis官方4版本提供了redis-trib.rb工具,可是在使用以前須要安裝ruby,以及redis和ruby鏈接
參考https://blog.csdn.net/woxingwosu0100/article/details/77840528
----------------------------------------------------------------------
5 版本集羣管理工具redis-trib.rb已經被廢棄,因此不用安裝ruby環境,redis-trib.rb的功能,如今已經集成到了redis-cli中,而且能夠在有認證的狀況執行了,能夠經過./redis-cli --cluster help查看使用方式。
----------------------------------------------------------------------
9.1安裝工具依賴環境(前面安裝過)
yum install -y gcc-c++ gcc* patch readline readline-devel zlib zlib-devel libyaml-devel libffi-devel openssl-devel make bzip2 autoconf automake libtool bison sqlite-devel iconv-devel
9.2 安裝rvm(ruby管理工具)
執行下面語句
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
或者
gpg2 --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
而後
執行 curl -L get.rvm.io | bash -s stable
#若是上面的鏈接失敗,能夠嘗試:
curl -Lhttps://raw.githubusercontent.com/wayneeseguin/rvm/master/binscripts/rvm-installer| bash -s stable
執行 source /usr/local/rvm/scripts/rvm
rvm install 2.3.3
[root@server bin]# ruby --version
ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-linux]
最後執行 gem install redis 安裝
10 建立集羣
4版本命令
cd /opt/haizhi/redis/bin
./redis-trib.rb create --replicas 2 192.168.1.133:6379 192.168.1.133:6380 192.168.1.133:6381 192.168.1.134:6379 192.168.1.134:6380 192.168.1.134:6381 192.168.1.135:6379 192.168.1.135:6380 192.168.1.135:6381
[root@server bin]# ./redis-trib.rb create --replicas 2 192.168.1.133:6379 192.168.1.133:6380 192.168.1.133:6381 192.168.1.134:6379 192.168.1.134:6380 192.168.1.134:6381 192.168.1.135:6379 192.168.1.135:6380 192.168.1.135:6381 >>> Creating cluster >>> Performing hash slots allocation on 9 nodes... Using 3 masters: 192.168.1.133:6379 192.168.1.134:6379 192.168.1.135:6379 Adding replica 192.168.1.134:6380 to 192.168.1.133:6379 Adding replica 192.168.1.135:6380 to 192.168.1.133:6379 Adding replica 192.168.1.133:6380 to 192.168.1.134:6379 Adding replica 192.168.1.133:6381 to 192.168.1.134:6379 Adding replica 192.168.1.134:6381 to 192.168.1.135:6379 Adding replica 192.168.1.135:6381 to 192.168.1.135:6379 M: 19fd62cb41e794f6478eff3d209d7c9a8587f45e 192.168.1.133:6379 slots:0-5460 (5461 slots) master S: b5a28ba32c3f96bfad569d1a12fdc84068882bed 192.168.1.133:6380 replicates 4f0f85289ad961682e995b3f9e547b16e3251268 S: 8fef8364429c82e7f5ddf2c6888a98651495a66f 192.168.1.133:6381 replicates 4f0f85289ad961682e995b3f9e547b16e3251268 M: 4f0f85289ad961682e995b3f9e547b16e3251268 192.168.1.134:6379 slots:5461-10922 (5462 slots) master S: efc9d969a0c86ae8c30b1b1e555523234d9c4d61 192.168.1.134:6380 replicates 19fd62cb41e794f6478eff3d209d7c9a8587f45e S: 6ad292b2129ed62ec0d49c079f38e4697161da14 192.168.1.134:6381 replicates 8b3e3748a106b2bf0483dc5bfee5f378edc7f933 M: 8b3e3748a106b2bf0483dc5bfee5f378edc7f933 192.168.1.135:6379 slots:10923-16383 (5461 slots) master S: 06e899228e65b2b6a16fb38968699d853b71c851 192.168.1.135:6380 replicates 19fd62cb41e794f6478eff3d209d7c9a8587f45e S: 7bf167e33b2060506f0945d0945d4e029ad95606 192.168.1.135:6381 replicates 8b3e3748a106b2bf0483dc5bfee5f378edc7f933 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.1.133:6379) M: 19fd62cb41e794f6478eff3d209d7c9a8587f45e 192.168.1.133:6379 slots:0-5460 (5461 slots) master 2 additional replica(s) S: 6ad292b2129ed62ec0d49c079f38e4697161da14 192.168.1.134:6381 slots: (0 slots) slave replicates 8b3e3748a106b2bf0483dc5bfee5f378edc7f933 M: 8b3e3748a106b2bf0483dc5bfee5f378edc7f933 192.168.1.135:6379 slots:10923-16383 (5461 slots) master 2 additional replica(s) S: efc9d969a0c86ae8c30b1b1e555523234d9c4d61 192.168.1.134:6380 slots: (0 slots) slave replicates 19fd62cb41e794f6478eff3d209d7c9a8587f45e S: b5a28ba32c3f96bfad569d1a12fdc84068882bed 192.168.1.133:6380 slots: (0 slots) slave replicates 4f0f85289ad961682e995b3f9e547b16e3251268 S: 7bf167e33b2060506f0945d0945d4e029ad95606 192.168.1.135:6381 slots: (0 slots) slave replicates 8b3e3748a106b2bf0483dc5bfee5f378edc7f933 M: 4f0f85289ad961682e995b3f9e547b16e3251268 192.168.1.134:6379 slots:5461-10922 (5462 slots) master 2 additional replica(s) S: 06e899228e65b2b6a16fb38968699d853b71c851 192.168.1.135:6380 slots: (0 slots) slave replicates 19fd62cb41e794f6478eff3d209d7c9a8587f45e S: 8fef8364429c82e7f5ddf2c6888a98651495a66f 192.168.1.133:6381 slots: (0 slots) slave replicates 4f0f85289ad961682e995b3f9e547b16e3251268 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
------------------------------------------------
5版本建立集羣命令
./src/redis-cli --cluster create 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 --cluster-replicas 1
./src/redis-cli --cluster create --replicas 2 192.168.1.133:6379 192.168.1.133:6380 192.168.1.133:6381 192.168.1.134:6379 192.168.1.134:6380 192.168.1.134:6381 192.168.1.135:6379 192.168.1.135:6380 192.168.1.135:6381
[root@localhost redis-5.0.2]# ./src/redis-cli -c -p 6380 127.0.0.1:6380> CLUSTER nodes 2abb1028cd21a4f8534f939e7656a292d4304eb1 127.0.0.1:6381@16381 master - 0 1555319145000 2 connected 5461-10922 b4caf8de09a68f2ded00194fe1c6d526165d1b8f 127.0.0.1:6380@16380 myself,master - 0 1555319143000 1 connected 0-5460 e7aee0ee4cdcac44526bf4bd28096801e178f6dc 127.0.0.1:6384@16384 slave 2abb1028cd21a4f8534f939e7656a292d4304eb1 0 1555319145518 5 connected 192c60ed7b583696d1b99e6ead634b97f1abb56b 127.0.0.1:6385@16385 slave eead61f9b5e2188b28d8f6c4339be0c5357377be 0 1555319145821 6 connected 4518d97ad24ebd6e7127f6c89a58b9654202decf 127.0.0.1:6383@16383 slave b4caf8de09a68f2ded00194fe1c6d526165d1b8f 0 1555319146325 4 connected eead61f9b5e2188b28d8f6c4339be0c5357377be 127.0.0.1:6382@16382 master - 0 1555319145317 3 connected 10923-16383 127.0.0.1:6380> 127.0.0.1:6380> 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:381 cluster_stats_messages_pong_sent:232 cluster_stats_messages_sent:613 cluster_stats_messages_ping_received:227 cluster_stats_messages_pong_received:381 cluster_stats_messages_meet_received:5 cluster_stats_messages_received:613 127.0.0.1:6380>
------------------------------------------------
查看集羣
[root@localhost bin]# ./redis-cli -c -p 6379
127.0.0.1:6379> CLUSTER nodes 4f0f85289ad961682e995b3f9e547b16e3251268 192.168.1.134:6379@16379 myself,master - 0 1534227574000 4 connected 5461-10922 7bf167e33b2060506f0945d0945d4e029ad95606 192.168.1.135:6381@16381 slave 8b3e3748a106b2bf0483dc5bfee5f378edc7f933 0 1534227576097 9 connected efc9d969a0c86ae8c30b1b1e555523234d9c4d61 192.168.1.134:6380@16380 slave 19fd62cb41e794f6478eff3d209d7c9a8587f45e 0 1534227575595 5 connected 6ad292b2129ed62ec0d49c079f38e4697161da14 192.168.1.134:6381@16381 slave 8b3e3748a106b2bf0483dc5bfee5f378edc7f933 0 1534227575094 7 connected 06e899228e65b2b6a16fb38968699d853b71c851 192.168.1.135:6380@16380 slave 19fd62cb41e794f6478eff3d209d7c9a8587f45e 0 1534227575595 8 connected 8b3e3748a106b2bf0483dc5bfee5f378edc7f933 192.168.1.135:6379@16379 master - 0 1534227576000 7 connected 10923-16383 8fef8364429c82e7f5ddf2c6888a98651495a66f 192.168.1.133:6381@16381 slave 4f0f85289ad961682e995b3f9e547b16e3251268 0 1534227575000 4 connected 19fd62cb41e794f6478eff3d209d7c9a8587f45e 192.168.1.133:6379@16379 master - 0 1534227575000 1 connected 0-5460 b5a28ba32c3f96bfad569d1a12fdc84068882bed 192.168.1.133:6380@16380 slave 4f0f85289ad961682e995b3f9e547b16e3251268 0 1534227575595 4 connected 127.0.0.1:6379>
-------------------------------------------------------
5版本 集羣節點的增長與刪除
以單機器建立redis集羣爲例
./src/redis-cli --cluster create 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 --cluster-replicas 1
在本機器上新增節點
[root@localhost redis-5.0.2]# cd /usr/local/redis/redis-5.0.2 [root@localhost redis-5.0.2]# ll 總用量 252 -rw-rw-r-- 1 root root 85327 11月 22 18:26 00-RELEASENOTES drwxr-xr-x 2 root root 122 4月 15 17:03 6380 drwxr-xr-x 2 root root 122 4月 15 17:03 6381 drwxr-xr-x 2 root root 122 4月 15 17:03 6382 drwxr-xr-x 2 root root 122 4月 15 17:03 6383 drwxr-xr-x 2 root root 122 4月 15 17:03 6384 drwxr-xr-x 2 root root 122 4月 15 17:03 6385 -rw-r--r-- 1 root root 0 4月 15 16:14 appendonly.aof -rw-rw-r-- 1 root root 53 11月 22 18:26 BUGS -rw-rw-r-- 1 root root 1894 11月 22 18:26 CONTRIBUTING -rw-rw-r-- 1 root root 1487 11月 22 18:26 COPYING drwxrwxr-x 6 root root 4096 4月 15 15:18 deps -rw-r--r-- 1 root root 92 4月 15 16:14 dump.rdb -rw-rw-r-- 1 root root 11 11月 22 18:26 INSTALL -rw-rw-r-- 1 root root 151 11月 22 18:26 Makefile -rw-rw-r-- 1 root root 4223 11月 22 18:26 MANIFESTO -rw-rw-r-- 1 root root 20555 11月 22 18:26 README.md -rw-rw-r-- 1 root root 62143 4月 15 16:14 redis.conf -rwxrwxr-x 1 root root 275 11月 22 18:26 runtest -rwxrwxr-x 1 root root 280 11月 22 18:26 runtest-cluster -rwxrwxr-x 1 root root 281 11月 22 18:26 runtest-sentinel -rw-rw-r-- 1 root root 9710 11月 22 18:26 sentinel.conf drwxrwxr-x 3 root root 8192 4月 15 15:22 src drwxrwxr-x 10 root root 4096 11月 22 18:26 tests drwxrwxr-x 8 root root 4096 11月 22 18:26 utils [root@localhost redis-5.0.2]#
cd /usr/local/redis/redis-5.0.2
mkdir 6386 6387 建立目錄並調整配置
cp 6380/redis-6380.conf 6386/
cp 6380/redis-6380.conf 6387/
mv 6386/redis-6380.conf 6386/redis-6386.conf
mv 6387/redis-6380.conf 6387/redis-6387.conf
sed -i 's/6380/6386/g' 6386/redis-6386.conf
sed -i 's/6380/6387/g' 6387/redis-6387.conf
啓動服務
./src/redis-server 6386/redis-6386.conf
./src/redis-server 6387/redis-6387.conf
[root@localhost redis-5.0.2]# ps -ef |grep redis root 11154 1 0 16:47 ? 00:00:07 ./src/redis-server 0.0.0.0:6380 [cluster] root 11156 1 0 16:47 ? 00:00:05 ./src/redis-server 0.0.0.0:6381 [cluster] root 11161 1 0 16:47 ? 00:00:05 ./src/redis-server 0.0.0.0:6382 [cluster] root 11169 1 0 16:47 ? 00:00:05 ./src/redis-server 0.0.0.0:6383 [cluster] root 11174 1 0 16:47 ? 00:00:05 ./src/redis-server 0.0.0.0:6384 [cluster] root 11179 1 0 16:47 ? 00:00:06 ./src/redis-server 0.0.0.0:6385 [cluster] root 12569 1 0 17:31 ? 00:00:00 ./src/redis-server 0.0.0.0:6386 [cluster] root 12579 1 1 17:31 ? 00:00:00 ./src/redis-server 0.0.0.0:6387 [cluster] root 12588 2865 0 17:31 pts/0 00:00:00 grep --color=auto redis
添加節點到集羣 6386爲主 6387爲從
先添加主節點6386到集羣中
./src/redis-cli --cluster add-node 127.0.0.1:6386 127.0.0.1:6381
##redis-cli --cluster add-node {新節點IP}:{新節點端口} {任意集羣節點IP}:{對應端口} #若是添加集羣中的主節點,則新添加的就是主節點,若是是從節點則是從節點
[root@localhost redis-5.0.2]# ./src/redis-cli --cluster add-node 127.0.0.1:6386 127.0.0.1:6381 >>> Adding node 127.0.0.1:6386 to cluster 127.0.0.1:6381 >>> Performing Cluster Check (using node 127.0.0.1:6381) M: 2abb1028cd21a4f8534f939e7656a292d4304eb1 127.0.0.1:6381 slots:[5461-10922] (5462 slots) master 1 additional replica(s) S: e7aee0ee4cdcac44526bf4bd28096801e178f6dc 127.0.0.1:6384 slots: (0 slots) slave replicates 2abb1028cd21a4f8534f939e7656a292d4304eb1 S: 192c60ed7b583696d1b99e6ead634b97f1abb56b 127.0.0.1:6385 slots: (0 slots) slave replicates eead61f9b5e2188b28d8f6c4339be0c5357377be M: eead61f9b5e2188b28d8f6c4339be0c5357377be 127.0.0.1:6382 slots:[10923-16383] (5461 slots) master 1 additional replica(s) S: 4518d97ad24ebd6e7127f6c89a58b9654202decf 127.0.0.1:6383 slots: (0 slots) slave replicates b4caf8de09a68f2ded00194fe1c6d526165d1b8f M: b4caf8de09a68f2ded00194fe1c6d526165d1b8f 127.0.0.1:6380 slots:[0-5460] (5461 slots) master 1 additional replica(s) [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. >>> Send CLUSTER MEET to node 127.0.0.1:6386 to make it join the cluster. [OK] New node added correctly.
而後咱們爲新節點手工分配hash槽
使用redis-cli命令爲6386分配hash槽,找到集羣中的任意一個主節點(6381),對其進行從新分片工做。
[root@localhost redis-5.0.2]# /usr/local/redis/redis-5.0.2/src/redis-cli --cluster reshard 127.0.0.1:6381
>>> Performing Cluster Check (using node 127.0.0.1:6381)
M: 2abb1028cd21a4f8534f939e7656a292d4304eb1 127.0.0.1:6381
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: e7aee0ee4cdcac44526bf4bd28096801e178f6dc 127.0.0.1:6384
slots: (0 slots) slave
replicates 2abb1028cd21a4f8534f939e7656a292d4304eb1
S: 192c60ed7b583696d1b99e6ead634b97f1abb56b 127.0.0.1:6385
slots: (0 slots) slave
replicates eead61f9b5e2188b28d8f6c4339be0c5357377be
M: eead61f9b5e2188b28d8f6c4339be0c5357377be 127.0.0.1:6382
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
M: 564673ea144dadd7b964395c74284f057e4ea6d7 127.0.0.1:6386
slots: (0 slots) master
S: 4518d97ad24ebd6e7127f6c89a58b9654202decf 127.0.0.1:6383
slots: (0 slots) slave
replicates b4caf8de09a68f2ded00194fe1c6d526165d1b8f
M: b4caf8de09a68f2ded00194fe1c6d526165d1b8f 127.0.0.1:6380
slots:[0-5460] (5461 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)? 3000
##分配3000個slots給下面的ID
What is the receiving node ID? 564673ea144dadd7b964395c74284f057e4ea6d7
##紅色的集羣ID號就是6386的ID號
而後有兩種方式,一種是all,以將全部節點用做散列槽的源節點,一種是done,這種是你本身選擇從哪一個節點上拿出來節點分給8007
Please enter all the source node IDs. 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 #1:
all是隨機的,好比說咱們要分出1000個,則3個主節點分別拿出333個,333個,334個節點分給6386,這裏咱們選擇all,從6380 6381 6382總共拿出3000個slots給6386
How many slots do you want to move (from 1 to 16384)? 3000 What is the receiving node ID? 564673ea144dadd7b964395c74284f057e4ea6d7 Please enter all the source node IDs. 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 #1: all 選擇全部的主機器
完成後再查看節點 發現已經分配了slot
[root@localhost redis-5.0.2]# ./src/redis-cli -c -p 6380 127.0.0.1:6380> CLUSTER nodes 2abb1028cd21a4f8534f939e7656a292d4304eb1 127.0.0.1:6381@16381 master - 0 1555322875361 2 connected 6462-10922 b4caf8de09a68f2ded00194fe1c6d526165d1b8f 127.0.0.1:6380@16380 myself,master - 0 1555322872000 1 connected 999-5460 564673ea144dadd7b964395c74284f057e4ea6d7 127.0.0.1:6386@16386 master - 0 1555322875000 7 connected 0-998 5461-6461 10923-11921 e7aee0ee4cdcac44526bf4bd28096801e178f6dc 127.0.0.1:6384@16384 slave 2abb1028cd21a4f8534f939e7656a292d4304eb1 0 1555322874000 5 connected 192c60ed7b583696d1b99e6ead634b97f1abb56b 127.0.0.1:6385@16385 slave eead61f9b5e2188b28d8f6c4339be0c5357377be 0 1555322874554 6 connected 4518d97ad24ebd6e7127f6c89a58b9654202decf 127.0.0.1:6383@16383 slave b4caf8de09a68f2ded00194fe1c6d526165d1b8f 0 1555322873341 4 connected eead61f9b5e2188b28d8f6c4339be0c5357377be 127.0.0.1:6382@16382 master - 0 1555322874351 3 connected 11922-16383 127.0.0.1:6380>
添加從節點6387到集羣中去並查看集羣狀態
/usr/local/redis/redis-5.0.2/src/redis-cli --cluster add-node 127.0.0.1:6387 127.0.0.1:6386
[root@localhost redis-5.0.2]# /usr/local/redis/redis-5.0.2/src/redis-cli --cluster add-node 127.0.0.1:6387 127.0.0.1:6386 >>> Adding node 127.0.0.1:6387 to cluster 127.0.0.1:6386 >>> Performing Cluster Check (using node 127.0.0.1:6386) M: 564673ea144dadd7b964395c74284f057e4ea6d7 127.0.0.1:6386 slots:[0-998],[5461-6461],[10923-11921] (2999 slots) master M: eead61f9b5e2188b28d8f6c4339be0c5357377be 127.0.0.1:6382 slots:[11922-16383] (4462 slots) master 1 additional replica(s) M: 2abb1028cd21a4f8534f939e7656a292d4304eb1 127.0.0.1:6381 slots:[6462-10922] (4461 slots) master 1 additional replica(s) S: e7aee0ee4cdcac44526bf4bd28096801e178f6dc 127.0.0.1:6384 slots: (0 slots) slave replicates 2abb1028cd21a4f8534f939e7656a292d4304eb1 S: 4518d97ad24ebd6e7127f6c89a58b9654202decf 127.0.0.1:6383 slots: (0 slots) slave replicates b4caf8de09a68f2ded00194fe1c6d526165d1b8f M: b4caf8de09a68f2ded00194fe1c6d526165d1b8f 127.0.0.1:6380 slots:[999-5460] (4462 slots) master 1 additional replica(s) S: 192c60ed7b583696d1b99e6ead634b97f1abb56b 127.0.0.1:6385 slots: (0 slots) slave replicates eead61f9b5e2188b28d8f6c4339be0c5357377be [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. >>> Send CLUSTER MEET to node 127.0.0.1:6387 to make it join the cluster. [OK] New node added correctly. [root@localhost redis-5.0.2]# ./src/redis-cli -c -p 6380 127.0.0.1:6380> CLUSTER nodes 2abb1028cd21a4f8534f939e7656a292d4304eb1 127.0.0.1:6381@16381 master - 0 1555323610107 2 connected 6462-10922 b4caf8de09a68f2ded00194fe1c6d526165d1b8f 127.0.0.1:6380@16380 myself,master - 0 1555323608000 1 connected 999-5460 564673ea144dadd7b964395c74284f057e4ea6d7 127.0.0.1:6386@16386 master - 0 1555323609501 7 connected 0-998 5461-6461 10923-11921 e7aee0ee4cdcac44526bf4bd28096801e178f6dc 127.0.0.1:6384@16384 slave 2abb1028cd21a4f8534f939e7656a292d4304eb1 0 1555323609000 5 connected 192c60ed7b583696d1b99e6ead634b97f1abb56b 127.0.0.1:6385@16385 slave eead61f9b5e2188b28d8f6c4339be0c5357377be 0 1555323609000 6 connected 2998c800138fa6bc0611c75b19f60aafad025724 127.0.0.1:6387@16387 master - 0 1555323610208 0 connected 4518d97ad24ebd6e7127f6c89a58b9654202decf 127.0.0.1:6383@16383 slave b4caf8de09a68f2ded00194fe1c6d526165d1b8f 0 1555323609000 4 connected eead61f9b5e2188b28d8f6c4339be0c5357377be 127.0.0.1:6382@16382 master - 0 1555323609400 3 connected 11922-16383 127.0.0.1:6380>
此時6387是一個master節點,沒有被分配任何的hash槽。
咱們須要執行replicate命令來指定當前節點(從節點)的主節點id爲哪一個,首先須要鏈接新加的6387節點的客戶端,
而後使用集羣命令進行操做,把當前的6387(slave)節點指定到一個主節點下
命令示例 CLUSTER REPLICATE 564673ea144dadd7b964395c74284f057e4ea6d7 ##指向到master節點6386的ID
[root@localhost redis-5.0.2]# ./src/redis-cli -c -p 6387 127.0.0.1:6387> CLUSTER nodes 192c60ed7b583696d1b99e6ead634b97f1abb56b 127.0.0.1:6385@16385 slave eead61f9b5e2188b28d8f6c4339be0c5357377be 0 1555323889761 3 connected 4518d97ad24ebd6e7127f6c89a58b9654202decf 127.0.0.1:6383@16383 slave b4caf8de09a68f2ded00194fe1c6d526165d1b8f 0 1555323889561 1 connected b4caf8de09a68f2ded00194fe1c6d526165d1b8f 127.0.0.1:6380@16380 master - 0 1555323890773 1 connected 999-5460 2abb1028cd21a4f8534f939e7656a292d4304eb1 127.0.0.1:6381@16381 master - 0 1555323890571 2 connected 6462-10922 eead61f9b5e2188b28d8f6c4339be0c5357377be 127.0.0.1:6382@16382 master - 0 1555323890000 3 connected 11922-16383 2998c800138fa6bc0611c75b19f60aafad025724 127.0.0.1:6387@16387 myself,master - 0 1555323888000 0 connected 564673ea144dadd7b964395c74284f057e4ea6d7 127.0.0.1:6386@16386 master - 0 1555323889257 7 connected 0-998 5461-6461 10923-11921 e7aee0ee4cdcac44526bf4bd28096801e178f6dc 127.0.0.1:6384@16384 slave 2abb1028cd21a4f8534f939e7656a292d4304eb1 0 1555323889000 2 connected 127.0.0.1:6387> CLUSTER REPLICATE 564673ea144dadd7b964395c74284f057e4ea6d7 ##指向到master節點ID OK 127.0.0.1:6387> CLUSTER nodes 192c60ed7b583696d1b99e6ead634b97f1abb56b 127.0.0.1:6385@16385 slave eead61f9b5e2188b28d8f6c4339be0c5357377be 0 1555323925000 3 connected 4518d97ad24ebd6e7127f6c89a58b9654202decf 127.0.0.1:6383@16383 slave b4caf8de09a68f2ded00194fe1c6d526165d1b8f 0 1555323926102 1 connected b4caf8de09a68f2ded00194fe1c6d526165d1b8f 127.0.0.1:6380@16380 master - 0 1555323926102 1 connected 999-5460 2abb1028cd21a4f8534f939e7656a292d4304eb1 127.0.0.1:6381@16381 master - 0 1555323926103 2 connected 6462-10922 eead61f9b5e2188b28d8f6c4339be0c5357377be 127.0.0.1:6382@16382 master - 0 1555323926000 3 connected 11922-16383 2998c800138fa6bc0611c75b19f60aafad025724 127.0.0.1:6387@16387 myself,slave 564673ea144dadd7b964395c74284f057e4ea6d7 0 1555323924000 0 connected 564673ea144dadd7b964395c74284f057e4ea6d7 127.0.0.1:6386@16386 master - 0 1555323926506 7 connected 0-998 5461-6461 10923-11921 e7aee0ee4cdcac44526bf4bd28096801e178f6dc 127.0.0.1:6384@16384 slave 2abb1028cd21a4f8534f939e7656a292d4304eb1 0 1555323926102 2 connected 127.0.0.1:6387>
刪除從節點
[root@localhost redis-5.0.2]# /usr/local/redis/redis-5.0.2/src/redis-cli --cluster del-node 127.0.0.1:6387 2998c800138fa6bc0611c75b19f60aafad025724
>>> Removing node 2998c800138fa6bc0611c75b19f60aafad025724 from cluster 127.0.0.1:6387
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
[root@localhost redis-5.0.2]# ./src/redis-cli -c -p 6387
Could not connect to Redis at 127.0.0.1:6387: Connection refused
not connected>
刪除主節點 3686(由於剛纔刪除的從節點就是6386的從節點)
1)若是主節點有從節點,將從節點轉移到其餘主節點
2)若是主節點有slot,去掉分配的slot,而後在刪除主節點
#./redis-cli --cluster reshard 127.0.0.1:6386 //取消分配的slot
How many slots do you want to move (from 1 to 16384)? 3000//被刪除master的全部slot數量
What is the receiving node ID? 2abb1028cd21a4f8534f939e7656a292d4304eb1 //接收slot的master節點6381
Please enter all the source node IDs.
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 #1:564673ea144dadd7b964395c74284f057e4ea6d7 //被刪除master的node-id
Source node #2:done
Do you want to proceed with the proposed reshard plan (yes/no)? yes //取消slot
[root@localhost redis-5.0.2]# ./src/redis-cli -c -p 6381
127.0.0.1:6381> CLUSTER nodes 2abb1028cd21a4f8534f939e7656a292d4304eb1 127.0.0.1:6381@16381 myself,master - 0 1555325338000 8 connected 0-998 5461-11921 e7aee0ee4cdcac44526bf4bd28096801e178f6dc 127.0.0.1:6384@16384 slave 2abb1028cd21a4f8534f939e7656a292d4304eb1 0 1555325342000 8 connected 192c60ed7b583696d1b99e6ead634b97f1abb56b 127.0.0.1:6385@16385 slave eead61f9b5e2188b28d8f6c4339be0c5357377be 0 1555325344348 6 connected eead61f9b5e2188b28d8f6c4339be0c5357377be 127.0.0.1:6382@16382 master - 0 1555325343341 3 connected 11922-16383 564673ea144dadd7b964395c74284f057e4ea6d7 127.0.0.1:6386@16386 master - 0 1555325342000 7 connected 4518d97ad24ebd6e7127f6c89a58b9654202decf 127.0.0.1:6383@16383 slave b4caf8de09a68f2ded00194fe1c6d526165d1b8f 0 1555325342000 4 connected b4caf8de09a68f2ded00194fe1c6d526165d1b8f 127.0.0.1:6380@16380 master - 0 1555325342333 1 connected 999-5460 127.0.0.1:6381> cluster info
[root@localhost redis-5.0.2]# /usr/local/redis/redis-5.0.2/src/redis-cli --cluster del-node 127.0.0.1:6386 564673ea144dadd7b964395c74284f057e4ea6d7
>>> Removing node 564673ea144dadd7b964395c74284f057e4ea6d7 from cluster 127.0.0.1:6386 >>> Sending CLUSTER FORGET messages to the cluster... >>> SHUTDOWN the node.
[root@localhost redis-5.0.2]# ./src/redis-cli -c -p 6381 127.0.0.1:6381> CLUSTER nodes 2abb1028cd21a4f8534f939e7656a292d4304eb1 127.0.0.1:6381@16381 myself,master - 0 1555325387000 8 connected 0-998 5461-11921 e7aee0ee4cdcac44526bf4bd28096801e178f6dc 127.0.0.1:6384@16384 slave 2abb1028cd21a4f8534f939e7656a292d4304eb1 0 1555325387768 8 connected 192c60ed7b583696d1b99e6ead634b97f1abb56b 127.0.0.1:6385@16385 slave eead61f9b5e2188b28d8f6c4339be0c5357377be 0 1555325385000 6 connected eead61f9b5e2188b28d8f6c4339be0c5357377be 127.0.0.1:6382@16382 master - 0 1555325387000 3 connected 11922-16383 4518d97ad24ebd6e7127f6c89a58b9654202decf 127.0.0.1:6383@16383 slave b4caf8de09a68f2ded00194fe1c6d526165d1b8f 0 1555325388776 4 connected b4caf8de09a68f2ded00194fe1c6d526165d1b8f 127.0.0.1:6380@16380 master - 0 1555325386758 1 connected 999-5460 127.0.0.1:6381>
增長刪除節點能夠參考下面文章
https://www.jianshu.com/p/6355d0827aea
https://cloud.tencent.com/developer/article/1412672
http://www.javashuo.com/article/p-rfkaqrng-a.html
集羣原理