Redis

关注公众号 jb51net

关闭
首页 > 数据库 > Redis > Redis集群搭建

Redis集群搭建(主从模式、哨兵模式、集群模式)

作者:242030

本文主要介绍了Redis集群搭建,主要包括主从模式、哨兵模式、集群模式这三种方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

1、主从模式部署

1.1 环境准备

IP主机名角色
192.168.54.200mastermaster
192.168.54.201slave1slave1
192.168.54.202slave2slave2

1.2 下载

下载地址:http://download.redis.io/releases/

这里选择下载:redis-7.0.3.tar.gz

1.3 上传到服务器并且解压(所有节点)

# 解压
tar -xf redis-7.0.3.tar.gz
cd redis-7.0.3
# 设置环境变量
echo "export REDIS_HOME=/home/zhangshixing/redis/redis-7.0.3">> /etc/profile
source /etc/profile

1.4 编译安装(所有节点)

cd $REDIS_HOME
yum -y install gcc gcc++
make && make install
# 默认安装目录 /usr/local/bin

1.5 配置成服务(所有节点)

cat << EOF > /usr/lib/systemd/system/redis.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/redis.conf --supervised systemd
ExecStop=/usr/libexec/redis-shutdown
Type=forking
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
LimitNOFILE=65536
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

配置描述:

Description: # 描述服务 
After: # 描述服务类别 
[Service]: # 服务运行参数的设置 
Type=forking: # 是后台运行的形式 
ExecStart: # 为服务的具体运行命令 
ExecReload: # 为重启命令 
ExecStop: # 为停止命令 
LimitNOFILE=65536: # 打开文件数和进程数有限制,默认限制为1024,如果不设置,或者设置为LimitNOFILE=unlimited(不识别),则得到了1024
PrivateTmp=True: # 表示给服务分配独立的临时空间
#【注意】[Service]的启动、重启、停止命令全部要求使用绝对路径 
[Install]: # 运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3
# 重载系统服务
systemctl daemon-reload

/usr/libexec/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

配置完就可以通过systemctl启停redis了。

1.6 授权启动服务(所有节点)

chmod +x /usr/libexec/redis-shutdown

useradd -s /sbin/nologin redis

mkdir /usr/local/redis ; cp $REDIS_HOME/redis.conf /usr/local/redis/ && chown -R redis:redis /usr/local/redis

mkdir -p /opt/software/redis-7.0.3/data && chown -R redis:redis /opt/software/redis-7.0.3/data

# 命令补全
yum install -y bash-completion && source /etc/profile                 

systemctl daemon-reload

systemctl enable redis

1.7 修改linux内核参数(所有节点)

# 临时生效
sysctl  -w  vm.overcommit_memory=1
# 永久生效
echo 'vm.overcommit_memory=1' >> /etc/sysctl.conf && sysctl -p
# 可选值:0,1,2。
# 0: 表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则,内存申请失败,并把错误返回给应用进程。
# 1: 表示内核允许分配所有的物理内存,而不管当前的内存状态如何。
# 2: 表示内核允许分配超过所有物理内存和交换空间总和的内存。

1.8 配置(redis.conf)

1.8.1 master节点配置

修改配置如下:

vi /usr/local/redis/redis.conf

bind 192.168.54.200               # 监听ip,多个ip用空格分隔
daemonize yes               # 允许后台启动
logfile "/usr/local/redis/redis.log"                # 日志路径
dir /opt/software/redis-7.0.3/data                 # 数据库备份文件存放目录
masterauth 123456               # slave连接master密码,master可省略
requirepass 123456              # 设置master连接密码,slave可省略
appendonly yes                  # 在/opt/software/redis-7.0.3/data目录生成appendonly.aof文件,将每一次写操作请求都追加到appendonly.aof 文件中

1.8.2 slave1节点配置

修改配置如下:

vi /usr/local/redis/redis.conf

bind 192.168.182.201               # 监听ip,多个ip用空格分隔
daemonize yes               # 允许后台启动
logfile "/usr/local/redis/redis.log"                # 日志路径
dir /opt/software/redis-7.0.3/data                 # 数据库备份文件存放目录
#  replicaof用于追随某个节点的redis,被追随的节点为主节点,追随的为从节点,就是设置master节点
replicaof 192.168.182.200 6379
masterauth 123456               # slave连接master密码,master可省略
requirepass 123456              # 设置master连接密码,slave可省略
appendonly yes                  # 在/opt/software/redis-7.0.3/data目录生成appendonly.aof文件,将每一次写操作请求都追加到appendonly.aof 文件中

1.8.3 slave2节点配置

修改配置如下:

vi /usr/local/redis/redis.conf

bind 192.168.182.202               # 监听ip,多个ip用空格分隔
daemonize yes               # 允许后台启动
logfile "/usr/local/redis/redis.log"                # 日志路径
dir /opt/software/redis-7.0.3/data                 # 数据库备份文件存放目录
#  replicaof用于追随某个节点的redis,被追随的节点为主节点,追随的为从节点,就是设置master节点
replicaof 192.168.182.200 6379
masterauth 123456               # slave连接master密码,master可省略
requirepass 123456              # 设置master连接密码,slave可省略
appendonly yes                  # 在/opt/software/redis-7.0.3/data目录生成appendonly.aof文件,将每一次写操作请求都追加到appendonly.aof 文件中

1.9 启动Redis服务(所有节点)

systemctl start redis
systemctl status redis

1.10 查看集群状态

# 交互式
[root@master redis]# redis-cli -h 192.168.54.200 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.54.200:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.54.201,port=6379,state=online,offset=1204,lag=0
slave1:ip=192.168.54.202,port=6379,state=online,offset=1204,lag=1
master_failover_state:no-failover
master_replid:e85f46b61089c38974769a2faa2fb905d4efc66f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1204
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1204
# 非交互式
[root@master redis]# redis-cli -h 192.168.54.200 -a 123456 info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.54.201,port=6379,state=online,offset=1288,lag=1
slave1:ip=192.168.54.202,port=6379,state=online,offset=1288,lag=1
master_failover_state:no-failover
master_replid:e85f46b61089c38974769a2faa2fb905d4efc66f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1288
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1288
# 交互式
[root@master redis]# redis-cli -h 192.168.54.200 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.54.200:6379> auth 123456
OK
192.168.54.200:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.54.201,port=6379,state=online,offset=1372,lag=1
slave1:ip=192.168.54.202,port=6379,state=online,offset=1372,lag=1
master_failover_state:no-failover
master_replid:e85f46b61089c38974769a2faa2fb905d4efc66f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1372
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1372

在这里Redis的主从模式就部署完了(一主两从)。

2、Sentinel(哨兵)模式部署

2.1 环境准备

IP主机名角色
192.168.54.200mastermaster,Sentinel
192.168.54.201slave1slave1,Sentinel
192.168.54.202slave2slave2,Sentinel

【温馨提示】 sentinel 最好跟redis部署在不同的机器上,sentinel 端口:26379。

2.2 配置sentinel(所有节点)

【温馨提示】前面已经安装过了redis主从模式了,因为哨兵模式是基于主从模式的,所以redis的配置这里就省略

了,直接修改sentinel配置文件,配置3个哨兵,每个哨兵的配置都是一样的。

# 三个节点创建存储目录
mkdir /opt/software/redis-7.0.3/sentinel; chown -R redis:redis /opt/software/redis-7.0.3/sentinel

cat >/usr/local/redis/sentinel.conf<<EOF
daemonize yes
logfile "/usr/local/redis/sentinel.log"
# sentinel工作目录
dir "/opt/software/redis-7.0.3/sentinel"
# 判断master失效至少需要2个sentinel同意,建议设置为n/2+1,n为sentinel个数
# sentinel monitor <master-name> <ip> <port> <count>
sentinel monitor mymaster 192.168.54.200 6379 2
sentinel auth-pass mymaster 123456
# 判断master主观下线时间,默认30s
sentinel down-after-milliseconds mymaster 30000
EOF

2.3 启动sentinel(所有节点)

nohup /usr/local/bin/redis-sentinel /usr/local/redis/sentinel.conf >> nohup.out 2>&1 &
netstat -tnlp|grep 26379

2.4 模拟故障测试

# 停掉master
systemctl stop redis
redis-cli -h 192.168.54.201 -a 123456 info replication
[root@master redis]# redis-cli -h 192.168.54.201 -a 123456 info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:192.168.54.200
master_port:6379
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_read_repl_offset:20712
slave_repl_offset:20712
master_link_down_since_seconds:18
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:e85f46b61089c38974769a2faa2fb905d4efc66f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:20712
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:20712
[root@master redis]# redis-cli -h 192.168.54.201 -a 123456 info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:192.168.54.202
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_read_repl_offset:29371
slave_repl_offset:29371
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:7a7fe0a8528e0c7913d5d54bc5ba633ab6879a8c
master_replid2:e85f46b61089c38974769a2faa2fb905d4efc66f
master_repl_offset:29371
second_repl_offset:20713
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:29371

发现master节点已经切换到其它节点了,再测试一下读写:

[root@master redis]# redis-cli -h 192.168.54.202 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.54.202:6379> set k2 v2
OK

新的master节点写能力也正常,接下来就是恢复故障了。

# 启动master
systemctl start redis
redis-cli -h 192.168.54.202 -a 123456 info replication
[root@master redis]# redis-cli -h 192.168.54.202 -a 123456 info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.54.201,port=6379,state=online,offset=70444,lag=0
slave1:ip=192.168.54.200,port=6379,state=online,offset=70444,lag=0
master_failover_state:no-failover
master_replid:7a7fe0a8528e0c7913d5d54bc5ba633ab6879a8c
master_replid2:e85f46b61089c38974769a2faa2fb905d4efc66f
master_repl_offset:70587
second_repl_offset:20713
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:869
repl_backlog_histlen:69719

发现就算原先的master节点恢复了,也不会切换到master角色,而是作为slave角色。

3、Cluster模式部署

使用集群,只需要将redis配置文件中的cluster-enable配置打开即可。

3.1 环境准备

IP端口节点
192.168.54.2007001,7002,7003node1
192.168.54.2017001,7002,7003node2
192.168.54.2027001,7002,7003node3

3.2 修改配置(所有节点)

【温馨提示】这里是基于主从模式的配置修改,不是基于主从模式部署,哨兵模式是基于主从模式部署。

# 创建存储目录
mkdir -p /opt/software/redis-7.0.3/cluster/redis_{7001..7003}
cp /usr/local/redis/redis.conf /usr/local/redis/cluster_redis_7001.conf
cp /usr/local/redis/redis.conf /usr/local/redis/cluster_redis_7002.conf
cp /usr/local/redis/redis.conf /usr/local/redis/cluster_redis_7003.conf

chown -R redis:redis /usr/local/redis ;chown -R redis:redis /opt/software/redis-7.0.3/cluster

修改配置文件/usr/local/redis/cluster_redis_7001.conf

# 【注意】节点不一样,IP不一样,记得修改这个bind配置
bind 192.168.54.200
port 7001
daemonize yes
pidfile "/var/run/cluster_redis_7001.pid"
logfile "/usr/local/redis/cluster_redis_7001.log"
dir "/opt/software/redis-7.0.3/cluster/redis_7001"
masterauth "123456"
requirepass "123456"
# replicaof 192.168.54.202 6379
appendonly yes
# 开启集群模式
cluster-enabled yes
# 虽然此配置的名字叫"集群配置文件",但是此配置文件不能人工编辑,它是集群节点自动维护的文件,主要用于记录集群中有哪些节点、他们的状态以及一些持久化参数等,方便在重启时恢复这些状态。通常是在收到请求之后这个文件就会被更新。
cluster-config-file nodes_7001.conf
cluster-node-timeout 15000

修改配置文件/usr/local/redis/cluster_redis_7002.conf

# 【注意】节点不一样,IP不一样,记得修改这个bind配置
bind 192.168.54.200
port 7002
daemonize yes
pidfile "/var/run/cluster_redis_7002.pid"
logfile "/usr/local/redis/cluster_redis_7002.log"
dir "/opt/software/redis-7.0.3/cluster/redis_7002"
masterauth "123456"
requirepass "123456"
# replicaof 192.168.54.202 6379
appendonly yes
# 配置yes则开启集群功能,此redis实例作为集群的一个节点,否则,它是一个普通的单一的redis实例。
cluster-enabled yes
# 虽然此配置的名字叫"集群配置文件",但是此配置文件不能人工编辑,它是集群节点自动维护的文件,主要用于记录集群中有哪些节点、他们的状态以及一些持久化参数等,方便在重启时恢复这些状态。通常是在收到请求之后这个文件就会被更新。
cluster-config-file nodes_7002.conf
cluster-node-timeout 15000

修改配置文件/usr/local/redis/cluster_redis_7002.conf

# 【注意】节点不一样,IP不一样,记得修改这个bind配置
bind 192.168.54.200
port 7003
daemonize yes
pidfile "/var/run/cluster_redis_7003.pid"
logfile "/usr/local/redis/cluster_redis_7003.log"
dir "/opt/software/redis-7.0.3/cluster/redis_7003"
masterauth "123456"
requirepass "123456"
# replicaof 192.168.54.202 6379
appendonly yes
# 配置yes则开启集群功能,此redis实例作为集群的一个节点,否则,它是一个普通的单一的redis实例。
cluster-enabled yes
# 虽然此配置的名字叫"集群配置文件",但是此配置文件不能人工编辑,它是集群节点自动维护的文件,主要用于记录集群中有哪些节点、他们的状态以及一些持久化参数等,方便在重启时恢复这些状态。通常是在收到请求之后这个文件就会被更新。
cluster-config-file nodes_7003.conf
cluster-node-timeout 15000

其它两台机器配置与192.168.54.2000一致,只是ip不同,此处省略。

# 将配置copy到另外两个节点
scp -r /usr/local/redis/cluster_redis_{7001..7003}.conf 192.168.54.201:/usr/local/redis/
scp -r /usr/local/redis/cluster_redis_{7001..7003}.conf 192.168.54.202:/usr/local/redis/

# 在node2上执行
sed -i 's/192.168.54.200/192.168.54.201/g' /usr/local/redis/cluster_redis_{7001..7003}.conf

# 在node3上执行
sed -i 's/192.168.54.200/192.168.54.202/g' /usr/local/redis/cluster_redis_{7001..7003}.conf

3.3 启动Redis服务(所有节点)

nohup redis-server /usr/local/redis/cluster_redis_7001.conf >> nohup.out 2>&1 &
netstat -tnlp|grep 7001
nohup redis-server /usr/local/redis/cluster_redis_7002.conf >> nohup.out 2>&1 &
netstat -tnlp|grep 7002
nohup redis-server /usr/local/redis/cluster_redis_7003.conf >> nohup.out 2>&1 &
netstat -tnlp|grep 7003
tail -f /usr/local/redis/cluster_redis_7001.log
tail -f /usr/local/redis/cluster_redis_7002.log
tail -f /usr/local/redis/cluster_redis_7003.log

3.4 创建集群

# --cluster-replicas 2 : 表示集群的一个主节点有2个从节点,就是一主两从模式
# 如果不加--cluster-replicas 2则会创建9个主节点
# --cluster / -c 告知redis-cli命令行工具,后续操作为cluster模式下
# --create 表示创建cluster模式
redis-cli -a 123456 --cluster create \
192.168.54.200:7001 192.168.54.200:7002 192.168.54.200:7003 \
192.168.54.201:7001 192.168.54.201:7002 192.168.54.201:7003 \
192.168.54.202:7001 192.168.54.202:7002 192.168.54.202:7003 \
--cluster-replicas 2
[root@master redis]# redis-cli -a 123456 --cluster create \
> 192.168.54.200:7001 192.168.54.200:7002 192.168.54.200:7003 \
> 192.168.54.201:7001 192.168.54.201:7002 192.168.54.201:7003 \
> 192.168.54.202:7001 192.168.54.202:7002 192.168.54.202: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.54.201:7002 to 192.168.54.200:7001
Adding replica 192.168.54.202:7002 to 192.168.54.200:7001
Adding replica 192.168.54.200:7003 to 192.168.54.201:7001
Adding replica 192.168.54.202:7003 to 192.168.54.201:7001
Adding replica 192.168.54.201:7003 to 192.168.54.202:7001
Adding replica 192.168.54.200:7002 to 192.168.54.202:7001
M: d88cfb0ee3c53547ce7f1f237655fbd3cd6a7a42 192.168.54.200:7001
   slots:[0-5460] (5461 slots) master
S: 6999efc73a2d128d8583139074929fa144f43d78 192.168.54.200:7002
   replicates 41d651cdefd81e8b2249cf9a99d151920a2e793e
S: 0adc0cb28eb662df3f139ee4a6752d187297a443 192.168.54.200:7003
   replicates 358f827f29b05016fb1f57c88cbdeb7dbfc613f5
M: 358f827f29b05016fb1f57c88cbdeb7dbfc613f5 192.168.54.201:7001
   slots:[5461-10922] (5462 slots) master
S: 18c654e7a1df333c456aa119a2cf36a0c8272165 192.168.54.201:7002
   replicates d88cfb0ee3c53547ce7f1f237655fbd3cd6a7a42
S: 9e95afd03e0b3e019ff81e5dcf78f962830bca69 192.168.54.201:7003
   replicates 41d651cdefd81e8b2249cf9a99d151920a2e793e
M: 41d651cdefd81e8b2249cf9a99d151920a2e793e 192.168.54.202:7001
   slots:[10923-16383] (5461 slots) master
S: ee7300d10e1fd6f52b221b639367e9a6d55d85d5 192.168.54.202:7002
   replicates d88cfb0ee3c53547ce7f1f237655fbd3cd6a7a42
S: bdadcef4ac56abebb545c0cb69c509f8a7491bef 192.168.54.202:7003
   replicates 358f827f29b05016fb1f57c88cbdeb7dbfc613f5
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.54.200:7001)
M: d88cfb0ee3c53547ce7f1f237655fbd3cd6a7a42 192.168.54.200:7001
   slots:[0-5460] (5461 slots) master
   2 additional replica(s)
S: 18c654e7a1df333c456aa119a2cf36a0c8272165 192.168.54.201:7002
   slots: (0 slots) slave
   replicates d88cfb0ee3c53547ce7f1f237655fbd3cd6a7a42
S: 0adc0cb28eb662df3f139ee4a6752d187297a443 192.168.54.200:7003
   slots: (0 slots) slave
   replicates 358f827f29b05016fb1f57c88cbdeb7dbfc613f5
S: bdadcef4ac56abebb545c0cb69c509f8a7491bef 192.168.54.202:7003
   slots: (0 slots) slave
   replicates 358f827f29b05016fb1f57c88cbdeb7dbfc613f5
M: 358f827f29b05016fb1f57c88cbdeb7dbfc613f5 192.168.54.201:7001
   slots:[5461-10922] (5462 slots) master
   2 additional replica(s)
M: 41d651cdefd81e8b2249cf9a99d151920a2e793e 192.168.54.202:7001
   slots:[10923-16383] (5461 slots) master
   2 additional replica(s)
S: 9e95afd03e0b3e019ff81e5dcf78f962830bca69 192.168.54.201:7003
   slots: (0 slots) slave
   replicates 41d651cdefd81e8b2249cf9a99d151920a2e793e
S: 6999efc73a2d128d8583139074929fa144f43d78 192.168.54.200:7002
   slots: (0 slots) slave
   replicates 41d651cdefd81e8b2249cf9a99d151920a2e793e
S: ee7300d10e1fd6f52b221b639367e9a6d55d85d5 192.168.54.202:7002
   slots: (0 slots) slave
   replicates d88cfb0ee3c53547ce7f1f237655fbd3cd6a7a42
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

会自动生成nodes.conf文件

[root@master redis]# ll /opt/software/redis-7.0.3/cluster/redis_{7001..7003}
/opt/software/redis-7.0.3/cluster/redis_7001:
总用量 4
drwxr-xr-x. 2 root root  103 12月 20 16:48 appendonlydir
-rw-r--r--. 1 root root 1237 12月 20 16:49 nodes_7001.conf

/opt/software/redis-7.0.3/cluster/redis_7002:
总用量 8
drwxr-xr-x. 2 root root  103 12月 20 16:49 appendonlydir
-rw-r--r--. 1 root root  171 12月 20 16:49 dump.rdb
-rw-r--r--. 1 root root 1237 12月 20 16:49 nodes_7002.conf

/opt/software/redis-7.0.3/cluster/redis_7003:
总用量 8
drwxr-xr-x. 2 root root  103 12月 20 16:49 appendonlydir
-rw-r--r--. 1 root root  171 12月 20 16:49 dump.rdb
-rw-r--r--. 1 root root 1237 12月 20 16:49 nodes.conf

3.5 集群操作

3.5.1 登录集群

redis-cli -c -h 192.168.54.200 -p 7001

3.5.2 查看集群信息

> auth 123456
> CLUSTER INFO

3.5.3 列出节点信息

> CLUSTER NODES
[root@master redis]# redis-cli -c -h 192.168.54.200 -p 7001
192.168.54.200:7001> auth 123456
OK
192.168.54.200: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:233
cluster_stats_messages_pong_sent:247
cluster_stats_messages_sent:480
cluster_stats_messages_ping_received:239
cluster_stats_messages_pong_received:233
cluster_stats_messages_meet_received:8
cluster_stats_messages_received:480
total_cluster_links_buffer_limit_exceeded:0
192.168.54.200:7001> CLUSTER NODES
18c654e7a1df333c456aa119a2cf36a0c8272165 192.168.54.201:7002@17002 slave d88cfb0ee3c53547ce7f1f237655fbd3cd6a7a42 0 1671526417098 1 connected
d88cfb0ee3c53547ce7f1f237655fbd3cd6a7a42 192.168.54.200:7001@17001 myself,master - 0 1671526415000 1 connected 0-5460
0adc0cb28eb662df3f139ee4a6752d187297a443 192.168.54.200:7003@17003 slave 358f827f29b05016fb1f57c88cbdeb7dbfc613f5 0 1671526419115 4 connected
bdadcef4ac56abebb545c0cb69c509f8a7491bef 192.168.54.202:7003@17003 slave 358f827f29b05016fb1f57c88cbdeb7dbfc613f5 0 1671526418107 4 connected
358f827f29b05016fb1f57c88cbdeb7dbfc613f5 192.168.54.201:7001@17001 master - 0 1671526417000 4 connected 5461-10922
41d651cdefd81e8b2249cf9a99d151920a2e793e 192.168.54.202:7001@17001 master - 0 1671526418000 7 connected 10923-16383
9e95afd03e0b3e019ff81e5dcf78f962830bca69 192.168.54.201:7003@17003 slave 41d651cdefd81e8b2249cf9a99d151920a2e793e 0 1671526419000 7 connected
6999efc73a2d128d8583139074929fa144f43d78 192.168.54.200:7002@17002 slave 41d651cdefd81e8b2249cf9a99d151920a2e793e 0 1671526418000 7 connected
ee7300d10e1fd6f52b221b639367e9a6d55d85d5 192.168.54.202:7002@17002 slave d88cfb0ee3c53547ce7f1f237655fbd3cd6a7a42 0 1671526416000 1 connected
192.168.54.200:7001>

3.5.4 增加节点

node1上增加一节点

【1】配置

# copy配置
cp /usr/local/redis/cluster_redis_7003.conf /usr/local/redis/cluster_redis_7004.conf
# 创建存储目录
mkdir /opt/software/redis-7.0.3/cluster/redis_7004

# 修改配置
vi /usr/local/redis/cluster_redis_7004.conf

bind 192.168.54.200
port 7004
daemonize yes
pidfile "/var/run/redis_7004.pid"
logfile "/usr/local/redis/cluster_redis_7004.log"
dir "/opt/software/redis-7.0.3/cluster/redis_7004"
#replicaof 192.168.182.110 6379
masterauth "123456"
requirepass "123456"
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 /opt/software/redis-7.0.3/cluster/redis_7004

【2】启动服务

nohup redis-server /usr/local/redis/cluster_redis_7004.conf >> nohup.out 2>&1 &
netstat -tnlp|grep 7004

【3】集群中增加节点

[root@local-168-182-110 ~]# redis-cli -c -h 192.168.54.200 -p 7001
> auth 123456
# 添加节点
> CLUSTER MEET 192.168.54.200 7004
# 查看节点信息
> CLUSTER NODES
[root@master redis]# redis-cli -c -h 192.168.54.200 -p 7001
192.168.54.200:7001> auth 123456
OK
192.168.54.200:7001> CLUSTER MEET 192.168.54.200 7004
OK
192.168.54.200:7001> CLUSTER NODES
a9b489648b4acc38bfc04499767f808b7208cb1f 192.168.54.200:7004@17004 master - 0 1671527132044 0 connected
18c654e7a1df333c456aa119a2cf36a0c8272165 192.168.54.201:7002@17002 slave d88cfb0ee3c53547ce7f1f237655fbd3cd6a7a42 0 1671527134063 1 connected
d88cfb0ee3c53547ce7f1f237655fbd3cd6a7a42 192.168.54.200:7001@17001 myself,master - 0 1671527131000 1 connected 0-5460
0adc0cb28eb662df3f139ee4a6752d187297a443 192.168.54.200:7003@17003 slave 358f827f29b05016fb1f57c88cbdeb7dbfc613f5 0 1671527132000 4 connected
bdadcef4ac56abebb545c0cb69c509f8a7491bef 192.168.54.202:7003@17003 slave 358f827f29b05016fb1f57c88cbdeb7dbfc613f5 0 1671527132000 4 connected
358f827f29b05016fb1f57c88cbdeb7dbfc613f5 192.168.54.201:7001@17001 master - 0 1671527133000 4 connected 5461-10922
41d651cdefd81e8b2249cf9a99d151920a2e793e 192.168.54.202:7001@17001 master - 0 1671527133567 7 connected 10923-16383
9e95afd03e0b3e019ff81e5dcf78f962830bca69 192.168.54.201:7003@17003 slave 41d651cdefd81e8b2249cf9a99d151920a2e793e 0 1671527132000 7 connected
6999efc73a2d128d8583139074929fa144f43d78 192.168.54.200:7002@17002 slave 41d651cdefd81e8b2249cf9a99d151920a2e793e 0 1671527133051 7 connected
ee7300d10e1fd6f52b221b639367e9a6d55d85d5 192.168.54.202:7002@17002 slave d88cfb0ee3c53547ce7f1f237655fbd3cd6a7a42 0 1671527131000 1 connected

可以看到,新增的节点都是以master身份加入集群的

【4】更换节点身份

将新增的192.168.54.200:7004节点身份改为192.168.54.200:7001slave

redis-cli -c -h 192.168.54.200 -p 7004
> auth 123456
# 改变节点类型
> cluster replicate d88cfb0ee3c53547ce7f1f237655fbd3cd6a7a42
> CLUSTER NODES
[root@master redis]# redis-cli -c -h 192.168.54.200 -p 7004
192.168.54.200:7004> auth 123456
OK
192.168.54.200:7004> CLUSTER NODES
18c654e7a1df333c456aa119a2cf36a0c8272165 192.168.54.201:7002@17002 slave d88cfb0ee3c53547ce7f1f237655fbd3cd6a7a42 0 1671527454000 1 connected
6999efc73a2d128d8583139074929fa144f43d78 192.168.54.200:7002@17002 slave 41d651cdefd81e8b2249cf9a99d151920a2e793e 0 1671527451000 7 connected
358f827f29b05016fb1f57c88cbdeb7dbfc613f5 192.168.54.201:7001@17001 master - 0 1671527452371 4 connected 5461-10922
d88cfb0ee3c53547ce7f1f237655fbd3cd6a7a42 192.168.54.200:7001@17001 master - 0 1671527455399 1 connected 0-5460
9e95afd03e0b3e019ff81e5dcf78f962830bca69 192.168.54.201:7003@17003 slave 41d651cdefd81e8b2249cf9a99d151920a2e793e 0 1671527454000 7 connected
41d651cdefd81e8b2249cf9a99d151920a2e793e 192.168.54.202:7001@17001 master - 0 1671527453000 7 connected 10923-16383
0adc0cb28eb662df3f139ee4a6752d187297a443 192.168.54.200:7003@17003 slave 358f827f29b05016fb1f57c88cbdeb7dbfc613f5 0 1671527453883 4 connected
a9b489648b4acc38bfc04499767f808b7208cb1f 192.168.54.200:7004@17004 myself,master - 0 1671527448000 0 connected
bdadcef4ac56abebb545c0cb69c509f8a7491bef 192.168.54.202:7003@17003 slave 358f827f29b05016fb1f57c88cbdeb7dbfc613f5 0 1671527451000 4 connected
ee7300d10e1fd6f52b221b639367e9a6d55d85d5 192.168.54.202:7002@17002 slave d88cfb0ee3c53547ce7f1f237655fbd3cd6a7a42 0 1671527455000 1 connected
192.168.54.200:7004> cluster replicate d88cfb0ee3c53547ce7f1f237655fbd3cd6a7a42
OK
192.168.54.200:7004> CLUSTER NODES
18c654e7a1df333c456aa119a2cf36a0c8272165 192.168.54.201:7002@17002 slave d88cfb0ee3c53547ce7f1f237655fbd3cd6a7a42 0 1671527488000 1 connected
6999efc73a2d128d8583139074929fa144f43d78 192.168.54.200:7002@17002 slave 41d651cdefd81e8b2249cf9a99d151920a2e793e 0 1671527490723 7 connected
358f827f29b05016fb1f57c88cbdeb7dbfc613f5 192.168.54.201:7001@17001 master - 0 1671527489000 4 connected 5461-10922
d88cfb0ee3c53547ce7f1f237655fbd3cd6a7a42 192.168.54.200:7001@17001 master - 0 1671527489713 1 connected 0-5460
9e95afd03e0b3e019ff81e5dcf78f962830bca69 192.168.54.201:7003@17003 slave 41d651cdefd81e8b2249cf9a99d151920a2e793e 0 1671527486675 7 connected
41d651cdefd81e8b2249cf9a99d151920a2e793e 192.168.54.202:7001@17001 master - 0 1671527489598 7 connected 10923-16383
0adc0cb28eb662df3f139ee4a6752d187297a443 192.168.54.200:7003@17003 slave 358f827f29b05016fb1f57c88cbdeb7dbfc613f5 0 1671527487000 4 connected
a9b489648b4acc38bfc04499767f808b7208cb1f 192.168.54.200:7004@17004 myself,slave d88cfb0ee3c53547ce7f1f237655fbd3cd6a7a42 0 1671527486000 1 connected
bdadcef4ac56abebb545c0cb69c509f8a7491bef 192.168.54.202:7003@17003 slave 358f827f29b05016fb1f57c88cbdeb7dbfc613f5 0 1671527487683 4 connected
ee7300d10e1fd6f52b221b639367e9a6d55d85d5 192.168.54.202:7002@17002 slave d88cfb0ee3c53547ce7f1f237655fbd3cd6a7a42 0 1671527488691 1 connected

3.5.5 删除节点

redis-cli -c -h 192.168.54.200 -p 7001
> auth 123456
# 查看节点
> CLUSTER NODES
# 删除节点
> CLUSTER FORGET a9b489648b4acc38bfc04499767f808b7208cb1f
# 检查节点信息
> CLUSTER NODES
[root@master redis]# redis-cli -c -h 192.168.54.200 -p 7001
192.168.54.200:7001> auth 123456
OK
192.168.54.200:7001> CLUSTER NODES
a9b489648b4acc38bfc04499767f808b7208cb1f 192.168.54.200:7004@17004 slave d88cfb0ee3c53547ce7f1f237655fbd3cd6a7a42 0 1671527601411 1 connected
18c654e7a1df333c456aa119a2cf36a0c8272165 192.168.54.201:7002@17002 slave d88cfb0ee3c53547ce7f1f237655fbd3cd6a7a42 0 1671527598000 1 connected
d88cfb0ee3c53547ce7f1f237655fbd3cd6a7a42 192.168.54.200:7001@17001 myself,master - 0 1671527595000 1 connected 0-5460
0adc0cb28eb662df3f139ee4a6752d187297a443 192.168.54.200:7003@17003 slave 358f827f29b05016fb1f57c88cbdeb7dbfc613f5 0 1671527600403 4 connected
bdadcef4ac56abebb545c0cb69c509f8a7491bef 192.168.54.202:7003@17003 slave 358f827f29b05016fb1f57c88cbdeb7dbfc613f5 0 1671527599000 4 connected
358f827f29b05016fb1f57c88cbdeb7dbfc613f5 192.168.54.201:7001@17001 master - 0 1671527599393 4 connected 5461-10922
41d651cdefd81e8b2249cf9a99d151920a2e793e 192.168.54.202:7001@17001 master - 0 1671527598000 7 connected 10923-16383
9e95afd03e0b3e019ff81e5dcf78f962830bca69 192.168.54.201:7003@17003 slave 41d651cdefd81e8b2249cf9a99d151920a2e793e 0 1671527597374 7 connected
6999efc73a2d128d8583139074929fa144f43d78 192.168.54.200:7002@17002 slave 41d651cdefd81e8b2249cf9a99d151920a2e793e 0 1671527597000 7 connected
ee7300d10e1fd6f52b221b639367e9a6d55d85d5 192.168.54.202:7002@17002 slave d88cfb0ee3c53547ce7f1f237655fbd3cd6a7a42 0 1671527600000 1 connected
192.168.54.200:7001> CLUSTER FORGET a9b489648b4acc38bfc04499767f808b7208cb1f
OK
192.168.54.200:7001>
192.168.54.200:7001> CLUSTER NODES
18c654e7a1df333c456aa119a2cf36a0c8272165 192.168.54.201:7002@17002 slave d88cfb0ee3c53547ce7f1f237655fbd3cd6a7a42 0 1671527636743 1 connected
d88cfb0ee3c53547ce7f1f237655fbd3cd6a7a42 192.168.54.200:7001@17001 myself,master - 0 1671527636000 1 connected 0-5460
0adc0cb28eb662df3f139ee4a6752d187297a443 192.168.54.200:7003@17003 slave 358f827f29b05016fb1f57c88cbdeb7dbfc613f5 0 1671527635733 4 connected
bdadcef4ac56abebb545c0cb69c509f8a7491bef 192.168.54.202:7003@17003 slave 358f827f29b05016fb1f57c88cbdeb7dbfc613f5 0 1671527637753 4 connected
358f827f29b05016fb1f57c88cbdeb7dbfc613f5 192.168.54.201:7001@17001 master - 0 1671527635000 4 connected 5461-10922
41d651cdefd81e8b2249cf9a99d151920a2e793e 192.168.54.202:7001@17001 master - 0 1671527632000 7 connected 10923-16383
9e95afd03e0b3e019ff81e5dcf78f962830bca69 192.168.54.201:7003@17003 slave 41d651cdefd81e8b2249cf9a99d151920a2e793e 0 1671527634724 7 connected
6999efc73a2d128d8583139074929fa144f43d78 192.168.54.200:7002@17002 slave 41d651cdefd81e8b2249cf9a99d151920a2e793e 0 1671527635000 7 connected
ee7300d10e1fd6f52b221b639367e9a6d55d85d5 192.168.54.202:7002@17002 slave d88cfb0ee3c53547ce7f1f237655fbd3cd6a7a42 0 1671527632206 1 connected

3.5.6 保存配置

redis-cli -c -h 192.168.54.200 -p 7001
> auth 123456
# 将节点的配置文件保存到硬盘里面
> CLUSTER SAVECONFIG
> CLUSTER NODES
[root@master redis]# redis-cli -c -h 192.168.54.200 -p 7001
192.168.54.200:7001> auth 123456
OK
192.168.54.200:7001> CLUSTER SAVECONFIG
OK
192.168.54.200:7001> CLUSTER NODES
a9b489648b4acc38bfc04499767f808b7208cb1f 192.168.54.200:7004@17004 slave d88cfb0ee3c53547ce7f1f237655fbd3cd6a7a42 0 1671527763975 1 connected
18c654e7a1df333c456aa119a2cf36a0c8272165 192.168.54.201:7002@17002 slave d88cfb0ee3c53547ce7f1f237655fbd3cd6a7a42 0 1671527763000 1 connected
d88cfb0ee3c53547ce7f1f237655fbd3cd6a7a42 192.168.54.200:7001@17001 myself,master - 0 1671527762000 1 connected 0-5460
0adc0cb28eb662df3f139ee4a6752d187297a443 192.168.54.200:7003@17003 slave 358f827f29b05016fb1f57c88cbdeb7dbfc613f5 0 1671527760000 4 connected
bdadcef4ac56abebb545c0cb69c509f8a7491bef 192.168.54.202:7003@17003 slave 358f827f29b05016fb1f57c88cbdeb7dbfc613f5 0 1671527759929 4 connected
358f827f29b05016fb1f57c88cbdeb7dbfc613f5 192.168.54.201:7001@17001 master - 0 1671527759000 4 connected 5461-10922
41d651cdefd81e8b2249cf9a99d151920a2e793e 192.168.54.202:7001@17001 master - 0 1671527761000 7 connected 10923-16383
9e95afd03e0b3e019ff81e5dcf78f962830bca69 192.168.54.201:7003@17003 slave 41d651cdefd81e8b2249cf9a99d151920a2e793e 0 1671527762964 7 connected
6999efc73a2d128d8583139074929fa144f43d78 192.168.54.200:7002@17002 slave 41d651cdefd81e8b2249cf9a99d151920a2e793e 0 1671527762000 7 connected
ee7300d10e1fd6f52b221b639367e9a6d55d85d5 192.168.54.202:7002@17002 slave d88cfb0ee3c53547ce7f1f237655fbd3cd6a7a42 0 1671527761956 1 connected

可以看到,之前删除的节点又恢复了,这是因为对应的配置文件没有删除,执行CLUSTER SAVECONFIG恢复。

3.5.7 模拟master节点挂掉

netstat -lntp |grep :7001|awk '{print $NF}'|cut -d '/' -f 1|xargs kill -9
redis-cli -c -h 192.168.54.201 -p 7001 -a 123456 CLUSTER NODES
[root@master redis]# redis-cli -c -h 192.168.54.201 -p 7001 -a 123456 CLUSTER NODES
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
6999efc73a2d128d8583139074929fa144f43d78 192.168.54.200:7002@17002 slave 41d651cdefd81e8b2249cf9a99d151920a2e793e 0 1671528090000 7 connected
bdadcef4ac56abebb545c0cb69c509f8a7491bef 192.168.54.202:7003@17003 slave 358f827f29b05016fb1f57c88cbdeb7dbfc613f5 0 1671528090000 4 connected
d88cfb0ee3c53547ce7f1f237655fbd3cd6a7a42 192.168.54.200:7001@17001 master,fail - 1671528024512 1671528017000 1 disconnected
9e95afd03e0b3e019ff81e5dcf78f962830bca69 192.168.54.201:7003@17003 slave 41d651cdefd81e8b2249cf9a99d151920a2e793e 0 1671528094858 7 connected
ee7300d10e1fd6f52b221b639367e9a6d55d85d5 192.168.54.202:7002@17002 slave a9b489648b4acc38bfc04499767f808b7208cb1f 0 1671528093000 10 connected
18c654e7a1df333c456aa119a2cf36a0c8272165 192.168.54.201:7002@17002 slave a9b489648b4acc38bfc04499767f808b7208cb1f 0 1671528093843 10 connected
41d651cdefd81e8b2249cf9a99d151920a2e793e 192.168.54.202:7001@17001 master - 0 1671528091000 7 connected 10923-16383
358f827f29b05016fb1f57c88cbdeb7dbfc613f5 192.168.54.201:7001@17001 myself,master - 0 1671528093000 4 connected 5461-10922
0adc0cb28eb662df3f139ee4a6752d187297a443 192.168.54.200:7003@17003 slave 358f827f29b05016fb1f57c88cbdeb7dbfc613f5 0 1671528092000 4 connected
a9b489648b4acc38bfc04499767f808b7208cb1f 192.168.54.200:7004@17004 master - 0 1671528092000 10 connected 0-5460

对应192.168.54.200:7001的一行可以看到,master fail,状态为disconnected;而对应

192.168.54.200:7004的一行,slave已经变成master。

【故障恢复】重新启动192.168.54.200:7001节点

nohup redis-server /usr/local/redis/cluster_redis_7001.conf >> nohup.out 2>&1 &
redis-cli -c -h 192.168.54.201 -p 7001 -a 123456 CLUSTER NODES
[root@master redis]# redis-cli -c -h 192.168.54.201 -p 7001 -a 123456 CLUSTER NODES
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
6999efc73a2d128d8583139074929fa144f43d78 192.168.54.200:7002@17002 slave 41d651cdefd81e8b2249cf9a99d151920a2e793e 0 1671528205550 7 connected
bdadcef4ac56abebb545c0cb69c509f8a7491bef 192.168.54.202:7003@17003 slave 358f827f29b05016fb1f57c88cbdeb7dbfc613f5 0 1671528206457 4 connected
d88cfb0ee3c53547ce7f1f237655fbd3cd6a7a42 192.168.54.200:7001@17001 slave a9b489648b4acc38bfc04499767f808b7208cb1f 0 1671528206000 10 connected
9e95afd03e0b3e019ff81e5dcf78f962830bca69 192.168.54.201:7003@17003 slave 41d651cdefd81e8b2249cf9a99d151920a2e793e 0 1671528206000 7 connected
ee7300d10e1fd6f52b221b639367e9a6d55d85d5 192.168.54.202:7002@17002 slave a9b489648b4acc38bfc04499767f808b7208cb1f 0 1671528208475 10 connected
18c654e7a1df333c456aa119a2cf36a0c8272165 192.168.54.201:7002@17002 slave a9b489648b4acc38bfc04499767f808b7208cb1f 0 1671528206000 10 connected
41d651cdefd81e8b2249cf9a99d151920a2e793e 192.168.54.202:7001@17001 master - 0 1671528206558 7 connected 10923-16383
358f827f29b05016fb1f57c88cbdeb7dbfc613f5 192.168.54.201:7001@17001 myself,master - 0 1671528205000 4 connected 5461-10922
0adc0cb28eb662df3f139ee4a6752d187297a443 192.168.54.200:7003@17003 slave 358f827f29b05016fb1f57c88cbdeb7dbfc613f5 0 1671528207466 4 connected
a9b489648b4acc38bfc04499767f808b7208cb1f 192.168.54.200:7004@17004 master - 0 1671528206000 10 connected 0-5460

可以看到,192.168.54.200:7001节点启动后为slave节点,并且是192.168.54.200:7004的slave节点。即

master节点如果挂掉,它的slave节点变为新master节点继续对外提供服务,而原来的master节点如果重启,则

变为新master节点的slave节点。

【温馨提示】cluster不能选择db,只能默认db为0,所以select切库相当于是不能使用的。

4、简单使用

4.1 集群常用操作

4.1.1 查看集群信息

# 打印集群的信息
cluster info
# 列出集群当前已知的所有节点(node),以及这些节点的相关信息
cluster nodes

4.1.2 节点操作

# 将ip和port所指定的节点添加到集群当中,让它成为集群的一份子
cluster meet <ip> <port>
# 从集群中移除node_id指定的节点
cluster forget <node_id>
# 将当前节点设置为node_id指定的节点的从节点
cluster replicate <node_id>
# 将节点的配置文件保存到硬盘里面
cluster saveconfig

4.1.3 槽(slot)

# 将一个或多个槽(slot)指派(assign)给当前节点
cluster addslots <slot> [slot ...]
# 移除一个或多个槽对当前节点的指派
cluster delslots <slot> [slot ...]
# 移除指派给当前节点的所有槽,让当前节点变成一个没有指派任何槽的节点
cluster flushslots
# 将槽slot指派给node_id指定的节点,如果槽已经指派给另一个节点
# 那么先让另一个节点删除该槽,然后再进行指派
cluster setslot <slot> node <node_id>
# 将本节点的槽slot迁移到node_id指定的节点中
cluster setslot <slot> migrating <node_id>
# 从node_id指定的节点中导入槽slot到本节点
cluster setslot <slot> importing <node_id>
# 取消对槽slot的导入(import)或者迁移(migrate)
cluster setslot <slot> stable

4.1.4 键

# 计算键key应该被放置在哪个槽上
cluster keyslot <key>
# 返回槽slot目前包含的键值对数量
cluster countkeysinslot <slot>
# 返回count个slot槽中的键
cluster getkeysinslot <slot> <count>

4.2 redis 常用操作

4.2.1 字符串(String)

# 登录master节点,slave节点是只读的
redis-cli -c -h 192.168.182.112 -p 7001
192.168.182.112:7001> auth 123456
OK
192.168.182.112:7001> set k1 v1
OK
192.168.182.112:7001> get k1
"v1"
# 获取多个值
192.168.182.112:7001> mget k1 k2 k3
1) "v1"
2) "v2"
3) "v3"

4.2.2 哈希(Hash)

Redis hash 是一个 string 类型的 field(字段) 和 value(值) 的映射表,hash 特别适合用于存储对象。

# 登录master节点,slave节点是只读的
redis-cli -c -h 192.168.182.112 -p 7001
192.168.182.112:7001> auth 123456
# 设置
192.168.182.112:7001> HSET myhash field1 "foo"
(integer) 1
# 获取
192.168.182.112:7001> hget myhash field1
"foo"

4.2.3 列表(List)

Redis列表是简单的字符串列表,按照插入顺序排序。你可以添加一个元素到列表的头部(左边)或者尾部(右

边)。

# 登录master节点,slave节点是只读的
redis-cli -c -h 192.168.182.112 -p 7001
192.168.182.112:7001> auth 123456
192.168.182.112:7001> LPUSH larr v1
(integer) 1
192.168.182.112:7001> LPUSH larr v2
(integer) 2
192.168.182.112:7001> LPUSH larr v3
(integer) 3
# 获取0到10个元素
192.168.182.112:7001> LRANGE larr  0 10
1) "v3"
2) "v2"
3) "v1"

4.2.4 集合(Set)

Redis 的 Set 是 String 类型的无序集合。集合成员是唯一的,这就意味着集合中不能出现重复的数据。

# 登录master节点,slave节点是只读的
redis-cli -c -h 192.168.182.112 -p 7001
192.168.182.112:7001> auth 123456
192.168.182.112:7001> SADD myset "hello"
(integer) 1
192.168.182.112:7001> SADD myset "foo"
(integer) 1
# 已经存在,则不能添加该元素
192.168.182.112:7001> SADD myset "hello"
(integer) 0
# 获取
192.168.182.112:7001> SMEMBERS myset
1) "foo"
2) "hello"
192.168.182.112:6379>

4.2.5 有序集合(sorted set)

Redis 有序集合和集合一样也是 string 类型元素的集合,且不允许重复的成员。不同的是每个元素都会关联一个

double 类型的分数。redis 正是通过分数来为集合中的成员进行从小到大的排序。有序集合的成员是唯一的,但

分数(score)却可以重复。

# 登录master节点,slave节点是只读的
redis-cli -c -h 192.168.182.112 -p 7001
192.168.182.112:7001> auth 123456
192.168.182.112:7001> ZADD key1 1 redis
(integer) 1
192.168.182.112:7001> ZADD key1 2 mongodb
(integer) 1
192.168.182.112:7001> ZADD key1 3 mysql
(integer) 1
# 已经存在,不能添加
192.168.182.112:7001> ZADD key1  3 mysql
(integer) 0
# 已经存在,不能添加
192.168.182.112:7001> ZADD key1  4 mysql
(integer) 0
# 获取数据
192.168.182.112:7001> ZRANGE key1  0 10 WITHSCORES
1) "redis"
2) "1"
3) "mongodb"
4) "2"
5) "mysql"
6) "4"

到此这篇关于Redis集群搭建(主从模式、哨兵模式、集群模式)的文章就介绍到这了,更多相关Redis集群搭建内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文