Redis Cluster cluster Linux installation

1. Version information

CentOS:release 6.5 (Final)
GCC:4.4.7
Redis:5.0.9

2. Cluster construction

2.1 Cluster Planning

Master: 11.11.11.160:6379, 11.11.11.161:6379,
11.11.11.162:6379 Slave: 11.11.11.163:6379, 11.11.11.164:6379, 11.11.11.165:6379

  1. If it is a cloud server, external network access is required, and the above IPs are all external network IPs
  2. Open the firewall ports of all servers, 6379 and 16379

2.2 Cluster installation

First create the /data/redis-cluster folder, download the Redis compressed file to this folder, and then compile and install:

mkdir /data/redis-cluster
cd /data/redis-cluster
wget http://download.redis.io/releases/redis-5.0.9.tar.gz
tar -zxvf redis-5.0.9.tar.gz
cd redis-5.0.9
make

Create folders based on ports

mkdir /data/redis-cluster/redis-5.0.9/conf
mkdir /data/redis-cluster/redis-5.0.9/6379
mkdir /data/redis-cluster/redis-5.0.9/6380
mkdir /data/redis-cluster/redis-5.0.9/6381
mkdir /data/redis-cluster/redis-5.0.9/6382
mkdir /data/redis-cluster/redis-5.0.9/6383
mkdir /data/redis-cluster/redis-5.0.9/6384

Copy the configuration file redis.conf to the /data/redis-cluster/redis-5.0.9/conf directory and rename it to redis-6379.conf, redis-6380.conf, redis-6381.conf, redis-6382.conf , redis-6383.conf, redis-6384.conf
redis-6379.conf modify the following parts:

port 6379
#bind 127.0.0.1
cluster-enabled yes
cluster-config-file /data/redis-cluster/redis-5.0.9/6379/nodes.conf
appendfilename "appendonly.aof"
protected-mode no
daemonize yes
appendonly yes
requirepass qaz@123
masterauth qaz@123
logfile "/data/redis-cluster/redis-5.0.9/6379/redis.log"
pidfile /data/redis-cluster/redis-5.0.9/6379/redis.pid
dbfilename dump.rdb
dir /data/redis-cluster/redis-5.0.9/6379

Configuration instructions

port:修改 Redis 端口
#bind 127.0.0.1:注释掉后外网就可以连接 Redis 了
cluster-enabled:设置为 yes 表示开启集群
cluster-config-file: 表示集群节点的配置文件
protected-mode:由于配置了密码登录,这里将 protected-mode 设置为 no 关闭保护模式。
daemonize:设置为 yes 表示允许 Redis 在后台启动。
requirepass:表示登录该 Redis 实例所需的密码
masterauth:由于每个节点都开启了密码认证,因此又增加 masterauth 配置,使得从机可以登录到主机上。
appendonly:开启aof文件

For other configuration items, explain in more detail: Redis configuration file detailed
explanation The configuration files of other ports should also be modified accordingly
Execute the following command on each server to start the Redis instance respectively:

/data/redis-cluster/redis-5.0.9/src/redis-server /data/redis-cluster/redis-5.0.9/conf/redis-6379.conf

Create a redis cluster with redis-cli

/data/redis-cluster/redis-5.0.9/src/redis-cli -a qaz@123 --cluster create 11.11.11.160:6379 11.11.11.161:6379 11.11.11.162:6379 11.11.11.163:6379 11.11.11.164:6379 11.11.11.165:6379 --cluster-replicas 1

Create log:

Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 11.11.11.164:6379 to 11.11.11.160:6379
Adding replica 11.11.11.165:6379 to 11.11.11.161:6379
Adding replica 11.11.11.163:6379 to 11.11.11.162:6379
M: f6bade412603174b67db6b1a470fd673f9a5c314 11.11.11.160:6379
   slots:[0-5460] (5461 slots) master
M: 64026cdbf85335a43d16ccfb9f5e0e335faa2722 11.11.11.161:6379
   slots:[5461-10922] (5462 slots) master
M: 98ca7df6298a84c58f73412f95a398a050ca302e 11.11.11.162:6379
   slots:[10923-16383] (5461 slots) master
S: a3990e0418aa242bb0e39d2d31a1ec8c796de5f3 11.11.11.163:6379
   replicates 98ca7df6298a84c58f73412f95a398a050ca302e
S: c5dcbc92b1e939113d5594f4895340cdec6bf2c1 11.11.11.164:6379
   replicates f6bade412603174b67db6b1a470fd673f9a5c314
S: 5d252fb7a82c3e2b28aa7f573f1516444584e97c 11.11.11.165:6379
   replicates 64026cdbf85335a43d16ccfb9f5e0e335faa2722
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 11.11.11.160:6379)
M: f6bade412603174b67db6b1a470fd673f9a5c314 11.11.11.160:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 98ca7df6298a84c58f73412f95a398a050ca302e 11.11.11.162:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: c5dcbc92b1e939113d5594f4895340cdec6bf2c1 11.11.11.164:6379
   slots: (0 slots) slave
   replicates f6bade412603174b67db6b1a470fd673f9a5c314
M: 64026cdbf85335a43d16ccfb9f5e0e335faa2722 11.11.11.161:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 5d252fb7a82c3e2b28aa7f573f1516444584e97c 11.11.11.165:6379
   slots: (0 slots) slave
   replicates 64026cdbf85335a43d16ccfb9f5e0e335faa2722
S: a3990e0418aa242bb0e39d2d31a1ec8c796de5f3 11.11.11.163:6379
   slots: (0 slots) slave
   replicates 98ca7df6298a84c58f73412f95a398a050ca302e
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

2.3 Cluster verification

2.3.1 Setting environment variables

vim /etc/profile
#添加
export REDIS_HOME=/data/redis-cluster/redis-5.0.9/src
export PATH=${JAVA_HOME}/bin:$PATH:$REDIS_HOME

source /etc/profile

2.3.2 Connect client

redis-cli -c -a qaz@123 -h 11.11.11.160 -p 6379

Tip: -a access server password, -c means cluster mode, specify ip address and port number

Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
11.11.11.160:6379>
11.11.11.160:6379>
11.11.11.160:6379> keys *
(empty list or set)
11.11.11.160:6379> set name my name is lihy
-> Redirected to slot [5798] located at 11.11.11.161:6379
(error) ERR syntax error
11.11.11.161:6379> keys *
(empty list or set)
11.11.11.161:6379> set name 'my name is lihy'
OK
11.11.11.161:6379> keys *
1) "name"
11.11.11.161:6379> get name
"my name is lihy"

2.3.3 View cluster information

11.11.11.161:6379>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:2
cluster_stats_messages_ping_sent:1545
cluster_stats_messages_pong_sent:1615
cluster_stats_messages_meet_sent:2
cluster_stats_messages_sent:3162
cluster_stats_messages_ping_received:1612
cluster_stats_messages_pong_received:1547
cluster_stats_messages_meet_received:3
cluster_stats_messages_received:3162

2.3.4 View node list

11.11.11.161:6379> cluster nodes
98ca7df6298a84c58f73412f95a398a050ca302e 11.11.11.162:6379@16379 master - 0 1600765329213 3 connected 10923-16383
a3990e0418aa242bb0e39d2d31a1ec8c796de5f3 11.11.11.163:6379@16379 slave 98ca7df6298a84c58f73412f95a398a050ca302e 0 1600765330215 3 connected
64026cdbf85335a43d16ccfb9f5e0e335faa2722 11.11.11.161:6379@16379 myself,master - 0 1600765331000 2 connected 5461-10922
f6bade412603174b67db6b1a470fd673f9a5c314 11.11.11.160:6379@16379 master - 0 1600765331217 1 connected 0-5460
5d252fb7a82c3e2b28aa7f573f1516444584e97c 11.11.11.165:6379@16379 slave 64026cdbf85335a43d16ccfb9f5e0e335faa2722 0 1600765331000 6 connected
c5dcbc92b1e939113d5594f4895340cdec6bf2c1 11.11.11.164:6379@16379 slave f6bade412603174b67db6b1a470fd673f9a5c314 0 1600765332221 1 connected

2.3.5 Shutting down the cluster

To shut down the cluster, you need to shut down one by one, use the command:

redis-cli -a qaz@123 -c -h 11.11.11.160 -p 6379 shutdown

2.3.6 Manually manage redis cluster

3、将 ip 和 port 所指定的节点添加到集群中

CLUSTER MEET <ip> <port>

4、从集群中移除 node_id 指定的节点

CLUSTER FORGET <node_id>

5、将当前节点设置为 node_id 指定的节点的从节点

CLUSTER REPLICATE <node_id>

6、将节点的配置文件保存到硬盘里面

CLUSTER SAVECONFIG

7、将一个或多个槽(slot)指派(assign)给当前节点

CLUSTER ADDSLOTS <slot> [slot ...]

8、移除一个或多个槽对当前节点的指派

CLUSTER DELSLOTS <slot> [slot ...]

9、 移除指派给当前节点的所有槽,让当前节点变成一个没有指派任何槽的节点

CLUSTER FLUSHSLOTS

10、将槽 slot 指派给 node_id 指定的节点,如果槽已经指派给另一个节点,那么先让另一个节点删除该槽>,然后再进行指派

CLUSTER SETSLOT <slot> NODE <node_id>

11、将本节点的槽 slot 迁移到 node_id 指定的节点中

CLUSTER SETSLOT <slot> MIGRATING <node_id>

12、从 node_id 指定的节点中导入槽 slot 到本节点

CLUSTER SETSLOT <slot> IMPORTING <node_id>

13、取消对槽 slot 的导入(import)或者迁移(migrate)

CLUSTER SETSLOT <slot> STABLE

14、计算键 key 应该被放置在哪个槽上

CLUSTER KEYSLOT <key>

15、返回槽 slot 目前包含的键值对数量

CLUSTER COUNTKEYSINSLOT <slot>

16、返回 count 个 slot 槽中的键

CLUSTER GETKEYSINSLOT <slot> <count>

Guess you like

Origin blog.csdn.net/lihuayong/article/details/108736726