Redis Cluster叢集模式部署

蜜獾互联网發表於2024-12-09

Redis Cluster模式部署

Redis的哨兵模式基本已經可以實現高可用,讀寫分離 ,但是在這種模式下每臺 Redis 伺服器都儲存相同的資料,很浪費記憶體,所以在 redis3.0上加入了Cluster 叢集模式,實現了 Redis 的分散式儲存,也就是說每臺 Redis 節點上儲存不同的內容。下面是Cluster 叢集模式的一些特點:

  • Sentinel模式基本可以滿足一般生產的需求,具備高可用性。但是當資料量過大到一臺伺服器存放不下的情況時,主從模式或sentinel模式就不能滿足需求了,這個時候需要對儲存的資料進行分片,將資料儲存到多個Redis例項中。cluster模式的出現就是為了解決單機Redis容量有限的問題,將Redis的資料根據一定的規則分配到多臺機器。
  • Cluster可以說是sentinel+主從模式的結合體,透過cluster可以實現主從和master重選功能,所以如果配置兩個副本三個分片的話,就需要六個Redis例項。因為Redis的資料是根據一定規則分配到cluster的不同機器的,當資料量過大時,可以新增機器進行擴容。
  • 使用叢集,只需要將redis配置檔案中的cluster-enable配置開啟即可,每個叢集中至少需要三個主資料庫才能正常執行,新增節點非常方便。

Cluster叢集模式有如下一些特點:

  • 多個redis節點網路互聯,資料共享;
  • 所有的節點都是一主一從(也可以是一主多從),其中從不提供服務,僅作為備用;
  • 不支援同時處理多個key(如MSET/MGET),因為redis需要把key均勻分佈在各個節點上,併發量很高的情況下同時建立key-value會降低效能並導致不可預測的行為;
  • 支援線上增加、刪除節點;
  • 客戶端可以連線任何一個主節點進行讀寫。

環境

IP節點
192.168.1.21 7001, 70002, 7003 node1
192.168.1.22 7001, 70002, 7003 node2
192.168.1.23 7001, 70002, 7003 node3

安裝編譯環境

# ubuntu
apt install make gcc
# centos
yum install make gcc

安裝 Redis

# 檢視 Redis 版本
http://download.redis.io/releases/

# 下載 Redis
wget http://download.redis.io/releases/redis-7.2.5.tar.gz

# 解壓
tar xvf redis-7.2.5.tar.gz 
cd redis-7.2.5/

# 進行編譯
make && make install

配置服務

# Redis-1 服務
cat << EOF > /usr/lib/systemd/system/redis-1.service
[Unit]
Description=Redis persistent key-value database
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
ExecStart=/usr/local/bin/redis-server /usr/local/redis/cluster_redis_7001.conf --supervised systemd
ExecStop=/usr/local/redis/redis-shutdown
Type=forking
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
LimitNOFILE=65536
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

# Redis-2 服務
cat << EOF > /usr/lib/systemd/system/redis-2.service
[Unit]
Description=Redis persistent key-value database
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
ExecStart=/usr/local/bin/redis-server /usr/local/redis/cluster_redis_7002.conf --supervised systemd
ExecStop=/usr/local/redis/redis-shutdown
Type=forking
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
LimitNOFILE=65536
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

# Redis-3 服務
cat << EOF > /usr/lib/systemd/system/redis-3.service
[Unit]
Description=Redis persistent key-value database
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
ExecStart=/usr/local/bin/redis-server /usr/local/redis/cluster_redis_7003.conf --supervised systemd
ExecStop=/usr/local/redis/redis-shutdown
Type=forking
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
LimitNOFILE=65536
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

配置停止指令碼

mkdir /usr/local/redis
vim /usr/local/redis/redis-shutdown
#!/bin/bash
#
# Wrapper to close properly redis and sentinel
test x"$REDIS_DEBUG" != x && set -x
REDIS_CLI=/usr/local/bin/redis-cli
# Retrieve service name
SERVICE_NAME="$1"
if [ -z "$SERVICE_NAME" ]; then
   SERVICE_NAME=redis
fi
# Get the proper config file based on service name
CONFIG_FILE="/usr/local/redis/$SERVICE_NAME.conf"
# Use awk to retrieve host, port from config file
HOST=`awk '/^[[:blank:]]*bind/ { print $2 }' $CONFIG_FILE | tail -n1`
PORT=`awk '/^[[:blank:]]*port/ { print $2 }' $CONFIG_FILE | tail -n1`
PASS=`awk '/^[[:blank:]]*requirepass/ { print $2 }' $CONFIG_FILE | tail -n1`
SOCK=`awk '/^[[:blank:]]*unixsocket\s/ { print $2 }' $CONFIG_FILE | tail -n1`
# Just in case, use default host, port
HOST=${HOST:-127.0.0.1}
if [ "$SERVICE_NAME" = redis ]; then
    PORT=${PORT:-6379}
else
    PORT=${PORT:-26739}
fi
# Setup additional parameters
# e.g password-protected redis instances
[ -z "$PASS"  ] || ADDITIONAL_PARAMS="-a $PASS"
# shutdown the service properly
if [ -e "$SOCK" ] ; then
        $REDIS_CLI -s $SOCK $ADDITIONAL_PARAMS shutdown
else
        $REDIS_CLI -h $HOST -p $PORT $ADDITIONAL_PARAMS shutdown
fi

授權啟動服務

chmod +x /usr/local/redis/redis-shutdown
useradd -s /sbin/nologin redis


mkdir -p /usr/local/redis/cluster/redis_7001
mkdir -p /usr/local/redis/cluster/redis_7002
mkdir -p /usr/local/redis/cluster/redis_7003

cp /root/redis-7.2.5/redis.conf /usr/local/redis/cluster_redis_7001.conf
cp /root/redis-7.2.5/redis.conf /usr/local/redis/cluster_redis_7002.conf
cp /root/redis-7.2.5/redis.conf /usr/local/redis/cluster_redis_7003.conf

chown -R redis:redis /usr/local/redis ;chown -R redis:redis /usr/local/redis/cluster

修改配置

vim /usr/local/redis/cluster_redis_7001.conf
bind 0.0.0.0 -::1
port 7001
daemonize yes
pidfile /var/run/cluster_redis_7001.pid
logfile /usr/local/redis/cluster_redis_7001.log
dir /usr/local/redis/cluster/redis_7001
masterauth 123123
requirepass 123123
appendonly yes
# 開啟叢集模式
cluster-enabled yes
# 雖然此配置的名字叫"叢集配置檔案",但是此配置檔案不能人工編輯,它是叢集節點自動維護的檔案,主要用於記錄叢集中有哪些節點、他們的狀態以及一些持久化引數等,方便在重啟時恢復這些狀態。通常是在收到請求之後這個檔案就會被更新。
cluster-config-file nodes_7001.conf
cluster-node-timeout 15000

vim /usr/local/redis/cluster_redis_7002.conf
bind 0.0.0.0 -::1
port 7002
daemonize yes
pidfile /var/run/cluster_redis_7002.pid
logfile /usr/local/redis/cluster_redis_7002.log
dir /usr/local/redis/cluster/redis_7002
masterauth 123123
requirepass 123123
appendonly yes
# 配置yes則開啟叢集功能,此redis例項作為叢集的一個節點,否則,它是一個普通的單一的redis例項。
cluster-enabled yes
# 雖然此配置的名字叫"叢集配置檔案",但是此配置檔案不能人工編輯,它是叢集節點自動維護的檔案,主要用於記錄叢集中有哪些節點、他們的狀態以及一些持久化引數等,方便在重啟時恢復這些狀態。通常是在收到請求之後這個檔案就會被更新。
cluster-config-file nodes_7002.conf
cluster-node-timeout 15000

vim /usr/local/redis/cluster_redis_7003.conf
bind 0.0.0.0 -::1
port 7003
daemonize yes
pidfile /var/run/cluster_redis_7003.pid
logfile /usr/local/redis/cluster_redis_7003.log
dir /usr/local/redis/cluster/redis_7003
masterauth 123123
requirepass 123123
appendonly yes
# 配置yes則開啟叢集功能,此redis例項作為叢集的一個節點,否則,它是一個普通的單一的redis例項。
cluster-enabled yes
# 雖然此配置的名字叫"叢集配置檔案",但是此配置檔案不能人工編輯,它是叢集節點自動維護的檔案,主要用於記錄叢集中有哪些節點、他們的狀態以及一些持久化引數等,方便在重啟時恢復這些狀態。通常是在收到請求之後這個檔案就會被更新。
cluster-config-file nodes_7003.conf
cluster-node-timeout 15000

修改linux核心引數

# 臨時生效
sysctl  -w  vm.overcommit_memory=1
# 永久生效
echo 'vm.overcommit_memory=1' >> /etc/sysctl.conf && sysctl -p
### 可選值:0,1,2。
# 0,:表示核心將檢查是否有足夠的可用記憶體供應用程序使用;如果有足夠的可用記憶體,記憶體申請允許;否則,記憶體申請失敗,並把錯誤返回給應用程序。
# 1:表示核心允許分配所有的實體記憶體,而不管當前的記憶體狀態如何。
# 2: 表示核心允許分配超過所有實體記憶體和交換空間總和的記憶體。

啟動 Redis

systemctl daemon-reload
systemctl enable redis-1
systemctl stop redis-1
systemctl start redis-1
systemctl status redis-1

systemctl enable redis-2
systemctl stop redis-2
systemctl start redis-2
systemctl status redis-2

systemctl enable redis-3
systemctl stop redis-3
systemctl start redis-3
systemctl status redis-3

root@cby:~/redis-7.2.5# netstat -anpt|grep 7001
tcp        0      0 0.0.0.0:7001            0.0.0.0:*               LISTEN      9392/redis-server 0 
tcp        0      0 0.0.0.0:17001           0.0.0.0:*               LISTEN      9392/redis-server 0 
tcp6       0      0 ::1:7001                :::*                    LISTEN      9392/redis-server 0 
tcp6       0      0 ::1:17001               :::*                    LISTEN      9392/redis-server 0 
root@cby:~/redis-7.2.5# 
root@cby:~/redis-7.2.5# 
root@cby:~/redis-7.2.5# netstat -anpt|grep 7002
tcp        0      0 0.0.0.0:7002            0.0.0.0:*               LISTEN      9459/redis-server 0 
tcp        0      0 0.0.0.0:17002           0.0.0.0:*               LISTEN      9459/redis-server 0 
tcp6       0      0 ::1:7002                :::*                    LISTEN      9459/redis-server 0 
tcp6       0      0 ::1:17002               :::*                    LISTEN      9459/redis-server 0 
root@cby:~/redis-7.2.5# 
root@cby:~/redis-7.2.5# 
root@cby:~/redis-7.2.5# netstat -anpt|grep 7003
tcp        0      0 0.0.0.0:7003            0.0.0.0:*               LISTEN      9516/redis-server 0 
tcp        0      0 0.0.0.0:17003           0.0.0.0:*               LISTEN      9516/redis-server 0 
tcp6       0      0 ::1:7003                :::*                    LISTEN      9516/redis-server 0 
tcp6       0      0 ::1:17003               :::*                    LISTEN      9516/redis-server 0 
root@cby:~/redis-7.2.5# 
root@cby:~/redis-7.2.5# 

建立叢集

# –cluster-replicas 2 : 表示叢集的一個主節點有2個從節點,就是一主兩從模式
redis-cli -a 123123 --cluster create \
192.168.1.21:7001 192.168.1.21:7002 192.168.1.21:7003 \
192.168.1.22:7001 192.168.1.22:7002 192.168.1.22:7003 \
192.168.1.23:7001 192.168.1.23:7002 192.168.1.23:7003 \
--cluster-replicas 2


root@cby:~/redis-7.2.5# redis-cli -a 123123 --cluster create \
192.168.1.21:7001 192.168.1.21:7002 192.168.1.21:7003 \
192.168.1.22:7001 192.168.1.22:7002 192.168.1.22:7003 \
192.168.1.23:7001 192.168.1.23:7002 192.168.1.23:7003 \
--cluster-replicas 2
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 9 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.1.22:7002 to 192.168.1.21:7001
Adding replica 192.168.1.23:7002 to 192.168.1.21:7001
Adding replica 192.168.1.21:7003 to 192.168.1.22:7001
Adding replica 192.168.1.23:7003 to 192.168.1.22:7001
Adding replica 192.168.1.22:7003 to 192.168.1.23:7001
Adding replica 192.168.1.21:7002 to 192.168.1.23:7001
M: 2f7f45c8ed5d0de27d106e1712d4d5362aaeb78e 192.168.1.21:7001
   slots:[0-5460] (5461 slots) master
S: 770d27130c62f82fb1cdd1deb7d76bf78a20fa8c 192.168.1.21:7002
   replicates 86edbada046b5b4820681ba992d4ad5ad8ca45c3
S: 056192acaef5a5dd2df4fea45fb6a010c797f6bd 192.168.1.21:7003
   replicates f95e114d1ef8f4bf1af93ae3406b920e290c6df8
M: f95e114d1ef8f4bf1af93ae3406b920e290c6df8 192.168.1.22:7001
   slots:[5461-10922] (5462 slots) master
S: d4d4ec9ae6d6b264cd5582b43cd2b4fd5d80e766 192.168.1.22:7002
   replicates 2f7f45c8ed5d0de27d106e1712d4d5362aaeb78e
S: 516859cd11dd45b8d1e3609a60ea0064a1e28c9e 192.168.1.22:7003
   replicates 86edbada046b5b4820681ba992d4ad5ad8ca45c3
M: 86edbada046b5b4820681ba992d4ad5ad8ca45c3 192.168.1.23:7001
   slots:[10923-16383] (5461 slots) master
S: 0d3b7feb2554e51093f1756a1852be9933f011d7 192.168.1.23:7002
   replicates 2f7f45c8ed5d0de27d106e1712d4d5362aaeb78e
S: 9847e18aa388829a27510c3e252e9d9732d04089 192.168.1.23:7003
   replicates f95e114d1ef8f4bf1af93ae3406b920e290c6df8
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.21:7001)
M: 2f7f45c8ed5d0de27d106e1712d4d5362aaeb78e 192.168.1.21:7001
   slots:[0-5460] (5461 slots) master
   2 additional replica(s)
S: d4d4ec9ae6d6b264cd5582b43cd2b4fd5d80e766 192.168.1.22:7002
   slots: (0 slots) slave
   replicates 2f7f45c8ed5d0de27d106e1712d4d5362aaeb78e
S: 9847e18aa388829a27510c3e252e9d9732d04089 192.168.1.23:7003
   slots: (0 slots) slave
   replicates f95e114d1ef8f4bf1af93ae3406b920e290c6df8
M: 86edbada046b5b4820681ba992d4ad5ad8ca45c3 192.168.1.23:7001
   slots:[10923-16383] (5461 slots) master
   2 additional replica(s)
S: 516859cd11dd45b8d1e3609a60ea0064a1e28c9e 192.168.1.22:7003
   slots: (0 slots) slave
   replicates 86edbada046b5b4820681ba992d4ad5ad8ca45c3
S: 0d3b7feb2554e51093f1756a1852be9933f011d7 192.168.1.23:7002
   slots: (0 slots) slave
   replicates 2f7f45c8ed5d0de27d106e1712d4d5362aaeb78e
M: f95e114d1ef8f4bf1af93ae3406b920e290c6df8 192.168.1.22:7001
   slots:[5461-10922] (5462 slots) master
   2 additional replica(s)
S: 770d27130c62f82fb1cdd1deb7d76bf78a20fa8c 192.168.1.21:7002
   slots: (0 slots) slave
   replicates 86edbada046b5b4820681ba992d4ad5ad8ca45c3
S: 056192acaef5a5dd2df4fea45fb6a010c797f6bd 192.168.1.21:7003
   slots: (0 slots) slave
   replicates f95e114d1ef8f4bf1af93ae3406b920e290c6df8
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
root@cby:~/redis-7.2.5# 


root@cby:~/redis-7.2.5# ll /usr/local/redis/cluster/redis_{7001..7003}
/usr/local/redis/cluster/redis_7001:
total 16
drwxr-xr-x 3 redis redis 4096 Jun 16 11:29 ./
drwxr-xr-x 5 redis redis 4096 Jun 16 11:13 ../
drwxr-xr-x 2 redis redis 4096 Jun 16 11:25 appendonlydir/
-rw-r--r-- 1 redis redis 1777 Jun 16 11:29 nodes_7001.conf

/usr/local/redis/cluster/redis_7002:
total 20
drwxr-xr-x 3 redis redis 4096 Jun 16 11:29 ./
drwxr-xr-x 5 redis redis 4096 Jun 16 11:13 ../
drwxr-xr-x 2 redis redis 4096 Jun 16 11:29 appendonlydir/
-rw-r--r-- 1 redis redis  171 Jun 16 11:29 dump.rdb
-rw-r--r-- 1 redis redis 1777 Jun 16 11:29 nodes_7002.conf

/usr/local/redis/cluster/redis_7003:
total 20
drwxr-xr-x 3 redis redis 4096 Jun 16 11:29 ./
drwxr-xr-x 5 redis redis 4096 Jun 16 11:13 ../
drwxr-xr-x 2 redis redis 4096 Jun 16 11:29 appendonlydir/
-rw-r--r-- 1 redis redis  171 Jun 16 11:29 dump.rdb
-rw-r--r-- 1 redis redis 1777 Jun 16 11:29 nodes_7003.conf
root@cby:~/redis-7.2.5#

檢視叢集

redis-cli -c -h 192.168.1.21 -p 7001
192.168.1.21:7001> auth 123123
OK
192.168.1.21:7001> 
192.168.1.21:7001> CLUSTER INFO
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:9
cluster_size:3
cluster_current_epoch:9
cluster_my_epoch:1
cluster_stats_messages_ping_sent:203
cluster_stats_messages_pong_sent:222
cluster_stats_messages_sent:425
cluster_stats_messages_ping_received:214
cluster_stats_messages_pong_received:203
cluster_stats_messages_meet_received:8
cluster_stats_messages_received:425
total_cluster_links_buffer_limit_exceeded:0
192.168.1.21:7001> 
192.168.1.21:7001> CLUSTER NODES
d4d4ec9ae6d6b264cd5582b43cd2b4fd5d80e766 192.168.1.22:7002@17002 slave 2f7f45c8ed5d0de27d106e1712d4d5362aaeb78e 0 1718537548032 1 connected
2f7f45c8ed5d0de27d106e1712d4d5362aaeb78e 192.168.1.21:7001@17001 myself,master - 0 1718537548000 1 connected 0-5460
9847e18aa388829a27510c3e252e9d9732d04089 192.168.1.23:7003@17003 slave f95e114d1ef8f4bf1af93ae3406b920e290c6df8 0 1718537550000 4 connected
86edbada046b5b4820681ba992d4ad5ad8ca45c3 192.168.1.23:7001@17001 master - 0 1718537548000 7 connected 10923-16383
516859cd11dd45b8d1e3609a60ea0064a1e28c9e 192.168.1.22:7003@17003 slave 86edbada046b5b4820681ba992d4ad5ad8ca45c3 0 1718537551000 7 connected
0d3b7feb2554e51093f1756a1852be9933f011d7 192.168.1.23:7002@17002 slave 2f7f45c8ed5d0de27d106e1712d4d5362aaeb78e 0 1718537552056 1 connected
f95e114d1ef8f4bf1af93ae3406b920e290c6df8 192.168.1.22:7001@17001 master - 0 1718537550000 4 connected 5461-10922
770d27130c62f82fb1cdd1deb7d76bf78a20fa8c 192.168.1.21:7002@17002 slave 86edbada046b5b4820681ba992d4ad5ad8ca45c3 0 1718537550000 7 connected
056192acaef5a5dd2df4fea45fb6a010c797f6bd 192.168.1.21:7003@17003 slave f95e114d1ef8f4bf1af93ae3406b920e290c6df8 0 1718537551049 4 connected
192.168.1.21:7001> 

增加節點

# copy配置
cp /usr/local/redis/cluster_redis_7003.conf /usr/local/redis/cluster_redis_7004.conf
# 建立儲存目錄
mkdir /usr/local/redis/cluster/redis_7004

# 修改配置
vim /usr/local/redis/cluster_redis_7004.conf
bind 0.0.0.0 -::1
port 7004
daemonize yes
pidfile /var/run/redis_7004.pid
logfile /usr/local/redis/cluster_redis_7004.log
dir /usr/local/redis/cluster/redis_7004
masterauth 123123
requirepass 123123
appendonly yes
cluster-enabled yes
cluster-config-file nodes_7004.conf
cluster-node-timeout 15000

# 授權
chown -R redis:redis /usr/local/redis && chown -R redis:redis /usr/local/redis/cluster/redis_7004

# 啟動
redis-server /usr/local/redis/cluster_redis_7004.conf
netstat -tnlp|grep :7004
tcp        0      0 0.0.0.0:7004            0.0.0.0:*               LISTEN      8893/redis-server 0 
tcp6       0      0 ::1:7004                :::*                    LISTEN      8893/redis-server 0 



redis-cli -c -h 192.168.1.21 -p 7001
192.168.1.21:7001> auth 123123
OK
192.168.1.21:7001> 
# 新增節點
192.168.1.21:7001> CLUSTER MEET 192.168.1.23 7004
OK
192.168.1.21:7001> 
# 檢視節點
192.168.1.21:7001> CLUSTER NODES
8e4682e8e63998afbdebce591a42a36e6e0545d4 192.168.1.23:7004@17004 master - 0 1718538096216 0 connected   # 這裡
d4d4ec9ae6d6b264cd5582b43cd2b4fd5d80e766 192.168.1.22:7002@17002 slave 2f7f45c8ed5d0de27d106e1712d4d5362aaeb78e 0 1718538095210 1 connected
2f7f45c8ed5d0de27d106e1712d4d5362aaeb78e 192.168.1.21:7001@17001 myself,master - 0 1718538092000 1 connected 0-5460
9847e18aa388829a27510c3e252e9d9732d04089 192.168.1.23:7003@17003 slave f95e114d1ef8f4bf1af93ae3406b920e290c6df8 0 1718538095000 4 connected
86edbada046b5b4820681ba992d4ad5ad8ca45c3 192.168.1.23:7001@17001 master - 0 1718538095000 7 connected 10923-16383
516859cd11dd45b8d1e3609a60ea0064a1e28c9e 192.168.1.22:7003@17003 slave 86edbada046b5b4820681ba992d4ad5ad8ca45c3 0 1718538095000 7 connected
0d3b7feb2554e51093f1756a1852be9933f011d7 192.168.1.23:7002@17002 slave 2f7f45c8ed5d0de27d106e1712d4d5362aaeb78e 0 1718538091000 1 connected
f95e114d1ef8f4bf1af93ae3406b920e290c6df8 192.168.1.22:7001@17001 master - 0 1718538095000 4 connected 5461-10922
770d27130c62f82fb1cdd1deb7d76bf78a20fa8c 192.168.1.21:7002@17002 slave 86edbada046b5b4820681ba992d4ad5ad8ca45c3 0 1718538094207 7 connected
056192acaef5a5dd2df4fea45fb6a010c797f6bd 192.168.1.21:7003@17003 slave f95e114d1ef8f4bf1af93ae3406b920e290c6df8 0 1718538095000 4 connected
192.168.1.21:7001> 

互換身份

redis-cli -c -h 192.168.1.23 -p 7004
192.168.1.23:7004> auth 123123
OK
192.168.1.23:7004> 
192.168.1.23:7004> CLUSTER NODES
86edbada046b5b4820681ba992d4ad5ad8ca45c3 192.168.1.23:7001@17001 master - 0 1718538347000 7 connected 10923-16383
d4d4ec9ae6d6b264cd5582b43cd2b4fd5d80e766 192.168.1.22:7002@17002 slave 2f7f45c8ed5d0de27d106e1712d4d5362aaeb78e 0 1718538347330 1 connected
8e4682e8e63998afbdebce591a42a36e6e0545d4 192.168.1.23:7004@17004 myself,master # 這裡 - 0 1718538344000 0 connected
056192acaef5a5dd2df4fea45fb6a010c797f6bd 192.168.1.21:7003@17003 slave f95e114d1ef8f4bf1af93ae3406b920e290c6df8 0 1718538345320 4 connected
2f7f45c8ed5d0de27d106e1712d4d5362aaeb78e 192.168.1.21:7001@17001 master - 0 1718538346000 1 connected 0-5460
0d3b7feb2554e51093f1756a1852be9933f011d7 192.168.1.23:7002@17002 slave 2f7f45c8ed5d0de27d106e1712d4d5362aaeb78e 0 1718538343309 1 connected
770d27130c62f82fb1cdd1deb7d76bf78a20fa8c 192.168.1.21:7002@17002 slave 86edbada046b5b4820681ba992d4ad5ad8ca45c3 0 1718538343000 7 connected
f95e114d1ef8f4bf1af93ae3406b920e290c6df8 192.168.1.22:7001@17001 master - 0 1718538347000 4 connected 5461-10922
9847e18aa388829a27510c3e252e9d9732d04089 192.168.1.23:7003@17003 slave f95e114d1ef8f4bf1af93ae3406b920e290c6df8 0 1718538348337 4 connected
516859cd11dd45b8d1e3609a60ea0064a1e28c9e 192.168.1.22:7003@17003 slave 86edbada046b5b4820681ba992d4ad5ad8ca45c3 0 1718538347000 7 connected
192.168.1.23:7004> 
192.168.1.23:7004> 
192.168.1.23:7004> 
# 互換身份
192.168.1.23:7004> cluster replicate 86edbada046b5b4820681ba992d4ad5ad8ca45c3
OK
192.168.1.23:7004> 
192.168.1.23:7004> 
192.168.1.23:7004> CLUSTER NODES
86edbada046b5b4820681ba992d4ad5ad8ca45c3 192.168.1.23:7001@17001 master - 0 1718538388000 7 connected 10923-16383
d4d4ec9ae6d6b264cd5582b43cd2b4fd5d80e766 192.168.1.22:7002@17002 slave 2f7f45c8ed5d0de27d106e1712d4d5362aaeb78e 0 1718538389571 1 connected
8e4682e8e63998afbdebce591a42a36e6e0545d4 192.168.1.23:7004@17004 myself,slave # 這裡 86edbada046b5b4820681ba992d4ad5ad8ca45c3 0 1718538389000 7 connected
056192acaef5a5dd2df4fea45fb6a010c797f6bd 192.168.1.21:7003@17003 slave f95e114d1ef8f4bf1af93ae3406b920e290c6df8 0 1718538387000 4 connected
2f7f45c8ed5d0de27d106e1712d4d5362aaeb78e 192.168.1.21:7001@17001 master - 0 1718538388000 1 connected 0-5460
0d3b7feb2554e51093f1756a1852be9933f011d7 192.168.1.23:7002@17002 slave 2f7f45c8ed5d0de27d106e1712d4d5362aaeb78e 0 1718538389000 1 connected
770d27130c62f82fb1cdd1deb7d76bf78a20fa8c 192.168.1.21:7002@17002 slave 86edbada046b5b4820681ba992d4ad5ad8ca45c3 0 1718538386000 7 connected
f95e114d1ef8f4bf1af93ae3406b920e290c6df8 192.168.1.22:7001@17001 master - 0 1718538390597 4 connected 5461-10922
9847e18aa388829a27510c3e252e9d9732d04089 192.168.1.23:7003@17003 slave f95e114d1ef8f4bf1af93ae3406b920e290c6df8 0 1718538388582 4 connected
516859cd11dd45b8d1e3609a60ea0064a1e28c9e 192.168.1.22:7003@17003 slave 86edbada046b5b4820681ba992d4ad5ad8ca45c3 0 1718538386567 7 connected
192.168.1.23:7004> 
192.168.1.23:7004> 
192.168.1.23:7004> 

刪除節點

redis-cli -c -h 192.168.1.21 -p 7001
192.168.1.21:7001>  auth 123123
OK
192.168.1.21:7001> 
192.168.1.21:7001> CLUSTER NODES
8e4682e8e63998afbdebce591a42a36e6e0545d4 192.168.1.23:7004@17004 slave 86edbada046b5b4820681ba992d4ad5ad8ca45c3 0 1718538571929 7 connected
d4d4ec9ae6d6b264cd5582b43cd2b4fd5d80e766 192.168.1.22:7002@17002 slave 2f7f45c8ed5d0de27d106e1712d4d5362aaeb78e 0 1718538569000 1 connected
2f7f45c8ed5d0de27d106e1712d4d5362aaeb78e 192.168.1.21:7001@17001 myself,master - 0 1718538566000 1 connected 0-5460
9847e18aa388829a27510c3e252e9d9732d04089 192.168.1.23:7003@17003 slave f95e114d1ef8f4bf1af93ae3406b920e290c6df8 0 1718538568000 4 connected
86edbada046b5b4820681ba992d4ad5ad8ca45c3 192.168.1.23:7001@17001 master - 0 1718538570921 7 connected 10923-16383
516859cd11dd45b8d1e3609a60ea0064a1e28c9e 192.168.1.22:7003@17003 slave 86edbada046b5b4820681ba992d4ad5ad8ca45c3 0 1718538569000 7 connected
0d3b7feb2554e51093f1756a1852be9933f011d7 192.168.1.23:7002@17002 slave 2f7f45c8ed5d0de27d106e1712d4d5362aaeb78e 0 1718538566000 1 connected
f95e114d1ef8f4bf1af93ae3406b920e290c6df8 192.168.1.22:7001@17001 master - 0 1718538568907 4 connected 5461-10922
770d27130c62f82fb1cdd1deb7d76bf78a20fa8c 192.168.1.21:7002@17002 slave 86edbada046b5b4820681ba992d4ad5ad8ca45c3 0 1718538569000 7 connected
056192acaef5a5dd2df4fea45fb6a010c797f6bd 192.168.1.21:7003@17003 slave f95e114d1ef8f4bf1af93ae3406b920e290c6df8 0 1718538569914 4 connected
192.168.1.21:7001> 
# 刪除節點
192.168.1.21:7001>  CLUSTER FORGET  8e4682e8e63998afbdebce591a42a36e6e0545d4
OK
192.168.1.21:7001> 
192.168.1.21:7001> CLUSTER NODES
d4d4ec9ae6d6b264cd5582b43cd2b4fd5d80e766 192.168.1.22:7002@17002 slave 2f7f45c8ed5d0de27d106e1712d4d5362aaeb78e 0 1718538581988 1 connected
2f7f45c8ed5d0de27d106e1712d4d5362aaeb78e 192.168.1.21:7001@17001 myself,master - 0 1718538580000 1 connected 0-5460
9847e18aa388829a27510c3e252e9d9732d04089 192.168.1.23:7003@17003 slave f95e114d1ef8f4bf1af93ae3406b920e290c6df8 0 1718538579000 4 connected
86edbada046b5b4820681ba992d4ad5ad8ca45c3 192.168.1.23:7001@17001 master - 0 1718538579000 7 connected 10923-16383
516859cd11dd45b8d1e3609a60ea0064a1e28c9e 192.168.1.22:7003@17003 slave 86edbada046b5b4820681ba992d4ad5ad8ca45c3 0 1718538580000 7 connected
0d3b7feb2554e51093f1756a1852be9933f011d7 192.168.1.23:7002@17002 slave 2f7f45c8ed5d0de27d106e1712d4d5362aaeb78e 0 1718538582993 1 connected
f95e114d1ef8f4bf1af93ae3406b920e290c6df8 192.168.1.22:7001@17001 master - 0 1718538580000 4 connected 5461-10922
770d27130c62f82fb1cdd1deb7d76bf78a20fa8c 192.168.1.21:7002@17002 slave 86edbada046b5b4820681ba992d4ad5ad8ca45c3 0 1718538580000 7 connected
056192acaef5a5dd2df4fea45fb6a010c797f6bd 192.168.1.21:7003@17003 slave f95e114d1ef8f4bf1af93ae3406b920e290c6df8 0 1718538580983 4 connected
192.168.1.21:7001> 
192.168.1.21:7001> 
192.168.1.21:7001> 
192.168.1.21:7001> 

常用命令

檢視叢集資訊
  • cluster info :列印叢集的資訊
  • cluster nodes :列出叢集當前已知的所有節點( node),以及這些節點的相關資訊。
節點操作
  • cluster meet :將 ip 和 port 所指定的節點新增到叢集當中,讓它成為叢集的一份子。
  • cluster forget <node_id> :從叢集中移除 node_id 指定的節點。
  • cluster replicate <node_id> :將當前節點設定為 node_id 指定的節點的從節點。
  • cluster saveconfig :將節點的配置檔案儲存到硬碟裡面。
槽(slot)操作
  • cluster addslots [slot …] :將一個或多個槽( slot)指派( assign)給當前節點。
  • cluster delslots [slot …] :移除一個或多個槽對當前節點的指派。
  • cluster flushslots :移除指派給當前節點的所有槽,讓當前節點變成一個沒有指派任何槽的節點。
  • cluster setslot node <node_id> :將槽 slot 指派給 node_id 指定的節點,如果槽已經指派給另一個節點,那麼先讓另一個節點刪除該槽>,然後再進行指派。
  • cluster setslot migrating <node_id> :將本節點的槽 slot 遷移到 node_id 指定的節點中。
  • cluster setslot importing <node_id> :從 node_id 指定的節點中匯入槽 slot 到本節點。
  • cluster setslot stable :取消對槽 slot 的匯入( import)或者遷移( migrate)。

相關文章