Redis Cluster集群模式部署
【摘要】 Redis Cluster模式部署Redis的哨兵模式基本已经可以实现高可用,读写分离 ,但是在这种模式下每台 Redis 服务器都存储相同的数据,很浪费内存,所以在 redis3.0上加入了Cluster 集群模式,实现了 Redis 的分布式存储,也就是说每台 Redis 节点上存储不同的内容。下面是Cluster 集群模式的一些特点:Sentinel模式基本可以满足一般生产的需求,具...
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)。
关于
https://www.oiox.cn/index.php/start-page.html
CSDN、GitHub、51CTO、知乎、开源中国、思否、博客园、掘金、简书、华为云、阿里云、腾讯云、哔哩哔哩、今日头条、新浪微博、个人博客
全网可搜《小陈运维》
文章主要发布于微信公众号
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)