名称 | ip | 节点 |
---|---|---|
redis-1 | 192.168.20.130 | 7001 7002 |
redis-2 | 192.168.20.141 | 7003 7004 |
redis-3 | 192.168.20.142 | 7005 7006 |
三台服务器均安装
[root@redis-1 ~]# mkdir -p /data/application ---创建工作目录
[root@redis-1 ~]# wget http://download.redis.io/releases/redis-5.0.10.tar.gz ---下载redis
[root@redis-1 ~]# tar xzf redis-5.0.10.tar.gz -C /data/application/ ---解压
[root@redis-1 ~]# cd /data/application/
[root@redis-1 application]# mv redis-5.0.10/ redis
[root@redis-1 application]# cd redis/
[root@redis-1 redis]# yum install -y gcc make #安装编译工具
[root@redis-1 redis]# make #编译安装
[root@redis-1 redis]# vim redis.conf ---修改如下
bind 192.168.246.202 #只监听内网IP
daemonize yes #开启后台模式将on改为yes
timeout 300 #连接超时时间
port 6379 #端口号
dir /data/application/redis/data #本地数据库存放持久化数据的目录该目录-----需要存在
pidfile /var/run/redis_6379.pid #定义pid文件
logfile /var/log/redis.log #定义log文件
[root@redis-1 redis]# mkdir /data/application/redis/data --创建存放数据的目录
#配置redis为systemctl启动
[root@redis-1 redis]# cd /lib/systemd/system
[root@redis-1 system]# vim redis.service
[Unit]
Description=Redis
After=network.target
[Service]
ExecStart=/data/application/redis/src/redis-server /data/application/redis/redis.conf --daemonize no
ExecStop=/data/application/redis/src/redis-cli -h 127.0.0.1 -p 6379 shutdown
[Install]
WantedBy=multi-user.target
[root@redis-1~]# mkdir /data/application/{7001,7002}
[root@redis-1 ~]# vim /data/application/7001redis.conf
port 7001
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
daemonize yes
bind 0.0.0.0
pidfile /data/application/7001/redis_7001.pid
dir /data/application/7001/data
[root@redis-1 ~]# mkdir /data/application/7001/data
[root@redis-1 ~]# vim /data/application/7002/redis.conf
port 7002
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
daemonize yes
bind 0.0.0.0
pidfile /data/application/7002/redis_7002.pid
dir /data/application/7002/data
[root@redis-1 ~]# mkdir /data/application/7002/data
[root@redis-1 ~]# cd /data/application
[root@redis-1 application]# ./redis/src/redis-server 7001/redis.conf
[root@redis-1 application]# ./redis/src/redis-server 7002/redis.conf
[root@redis-1 application]# ss -nplt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:7001 *:* users:(("redis-server",pid=8921,fd=6))
LISTEN 0 128 *:7002 *:* users:(("redis-server",pid=8927,fd=6))
LISTEN 0 128 *:17001 *:* users:(("redis-server",pid=8921,fd=9))
LISTEN 0 128 *:17002 *:* users:(("redis-server",pid=8927,fd=9))
[root@redis-2~]# mkdir /data/application/{7003,7004}
[root@redis-2 ~]# vim /data/application/7003/redis.conf
port 7003
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
daemonize yes
bind 0.0.0.0
pidfile /data/application/7003/redis_7003.pid
dir /data/application/7003/data
[root@redis-2 ~]# mkdir /data/application/7003/data
[root@redis-2 ~]# vim /data/application/7004/redis.conf
port 7004
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
daemonize yes
bind 0.0.0.0
pidfile /data/application/7004/redis_7004.pid
dir /data/application/7004/data
[root@redis-2 ~]# mkdir /data/application/7004/data
[root@redis-2 ~]# cd /data/application
[root@redis-2 application]# ./redis/src/redis-server 7003/redis.conf
[root@redis-2 application]# ./redis/src/redis-server 7004/redis.conf
[root@redis-2 application]# ss -nplt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:17003 *:* users:(("redis-server",pid=9094,fd=9))
LISTEN 0 128 *:17004 *:* *:* users:(("master",pid=1131,fd=13))
LISTEN 0 128 *:7003 *:* users:(("redis-server",pid=9094,fd=6))
LISTEN 0 128 *:7004 *:* users:(("redis-server",pid=9099,fd=6))
[root@redis-3~]# mkdir /data/application/{7005,7006}
[root@redis-3 ~]# vim /data/application/7005/redis.conf
port 7005
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
daemonize yes
bind 0.0.0.0
pidfile /data/application/7005/redis_7005.pid
dir /data/application/7005/data
[root@redis-3 ~]# mkdir /data/application/7005/data
[root@redis-3 ~]# vim /data/application/7006/redis.conf
port 7006
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
daemonize yes
bind 0.0.0.0
pidfile /data/application/7006/redis_7006.pid
dir /data/application/7006/data
[root@redis-3 ~]# mkdir /data/application/7006/data
[root@redis-3 ~]# cd /data/application
[root@redis-3 application]# ./redis/src/redis-server 7005/redis.conf
[root@redis-3 application]# ./redis/src/redis-server 7006/redis.conf
[root@redis-3 application]# ss -nplt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:7005 *:* users:(("redis-server",pid=16693,fd=6))
LISTEN 0 128 *:7006 *:* users:(("redis-server",pid=16698,fd=6))
LISTEN 0 128 *:17005 *:* users:(("redis-server",pid=16693,fd=9))
LISTEN 0 128 *:17006 *:* users:(("redis-server",pid=16698,fd=9))
[root@slave2 application]# ./redis/src/redis-cli --cluster create --cluster-replicas 1 192.168.20.138:7001 192.168.20.138:7002 192.168.20.141:7003 192.168.20.141:7004 192.168.20.142:7005 192.168.20.142:7006
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.20.141:7004 to 192.168.20.138:7001
Adding replica 192.168.20.142:7006 to 192.168.20.141:7003
Adding replica 192.168.20.138:7002 to 192.168.20.142:7005
M: fda81dbc8716b3542acb1330685f7c9c11eccd3d 192.168.20.138:7001
slots:[0-5460] (5461 slots) master
S: bf04cd90d0a9af94d21c8eb728be739009d50860 192.168.20.138:7002
replicates 9fbae28cad18dfe970d9bd8711895e215b79825b
M: 317cad08031ae6ab0402fd09ba9194ffe3668ca2 192.168.20.141:7003
slots:[5461-10922] (5462 slots) master
S: c330c995d1117843d808e8ef156f143c2effea56 192.168.20.141:7004
replicates fda81dbc8716b3542acb1330685f7c9c11eccd3d
M: 9fbae28cad18dfe970d9bd8711895e215b79825b 192.168.20.142:7005
slots:[10923-16383] (5461 slots) master
S: 994c3c93068e414e85e4d97be85d2a30ae8c7e3b 192.168.20.142:7006
replicates 317cad08031ae6ab0402fd09ba9194ffe3668ca2
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.20.138:7001)
M: fda81dbc8716b3542acb1330685f7c9c11eccd3d 192.168.20.138:7001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: bf04cd90d0a9af94d21c8eb728be739009d50860 192.168.20.138:7002
slots: (0 slots) slave
replicates 9fbae28cad18dfe970d9bd8711895e215b79825b
S: c330c995d1117843d808e8ef156f143c2effea56 192.168.20.141:7004
slots: (0 slots) slave
replicates fda81dbc8716b3542acb1330685f7c9c11eccd3d
S: 994c3c93068e414e85e4d97be85d2a30ae8c7e3b 192.168.20.142:7006
slots: (0 slots) slave
replicates 317cad08031ae6ab0402fd09ba9194ffe3668ca2
M: 9fbae28cad18dfe970d9bd8711895e215b79825b 192.168.20.142:7005
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
M: 317cad08031ae6ab0402fd09ba9194ffe3668ca2 192.168.20.141:7003
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@slave2 application]# ./redis/src/redis-cli -c -h 192.168.20.142 -p 7006
192.168.20.142:7006> 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:3
cluster_stats_messages_ping_sent:2518
cluster_stats_messages_pong_sent:706
cluster_stats_messages_meet_sent:2
cluster_stats_messages_sent:3226
cluster_stats_messages_ping_received:706
cluster_stats_messages_pong_received:2520
cluster_stats_messages_received:3226
192.168.20.142:7006> cluster nodes
c330c995d1117843d808e8ef156f143c2effea56 192.168.20.141:7004@17004 slave fda81dbc8716b3542acb1330685f7c9c11eccd3d 0 1699287984730 4 connected
9fbae28cad18dfe970d9bd8711895e215b79825b 192.168.20.142:7005@17005 master - 0 1699287984209 5 connected 10923-16383
fda81dbc8716b3542acb1330685f7c9c11eccd3d 192.168.20.138:7001@17001 master - 0 1699287985782 1 connected 0-5460
bf04cd90d0a9af94d21c8eb728be739009d50860 192.168.20.138:7002@17002 slave 9fbae28cad18dfe970d9bd8711895e215b79825b 0 1699287986294 5 connected
994c3c93068e414e85e4d97be85d2a30ae8c7e3b 192.168.20.142:7006@17006 myself,slave 317cad08031ae6ab0402fd09ba9194ffe3668ca2 0 1699287981000 6 connected
317cad08031ae6ab0402fd09ba9194ffe3668ca2 192.168.20.141:7003@17003 master - 0 1699287986192 3 connected 5461-10922
192.168.20.142:7006> set a b
-> Redirected to slot [15495] located at 192.168.20.142:7005
OK
192.168.20.142:7005> set c d
-> Redirected to slot [7365] located at 192.168.20.141:7003
OK
192.168.20.141:7003> set d e
-> Redirected to slot [11298] located at 192.168.20.142:7005
OK
192.168.20.142:7005> set c d
-> Redirected to slot [7365] located at 192.168.20.141:7003
OK
192.168.20.141:7003> set c f
OK
192.168.20.141:7003> set d g
-> Redirected to slot [11298] located at 192.168.20.142:7005
OK
[root@localhost application]# kill -9 8921 #关闭7001,master端口
[root@slave2 application]# ./redis/src/redis-cli -c -h 192.168.20.142 -p 7005
192.168.20.142:7005> cluster nodes
9fbae28cad18dfe970d9bd8711895e215b79825b 192.168.20.142:7005@17005 myself,master - 0 1699289909000 5 connected 10923-16383
fda81dbc8716b3542acb1330685f7c9c11eccd3d 192.168.20.138:7001@17001 master,fail - 1699289900693 1699289899963 1 disconnected
bf04cd90d0a9af94d21c8eb728be739009d50860 192.168.20.138:7002@17002 slave 9fbae28cad18dfe970d9bd8711895e215b79825b 0 1699289916255 5 connected
c330c995d1117843d808e8ef156f143c2effea56 192.168.20.141:7004@17004 master - 0 1699289916681 7 connected 0-5460
317cad08031ae6ab0402fd09ba9194ffe3668ca2 192.168.20.141:7003@17003 master - 0 1699289915217 3 connected 5461-10922
994c3c93068e414e85e4d97be85d2a30ae8c7e3b 192.168.20.142:7006@17006 slave 317cad08031ae6ab0402fd09ba9194ffe3668ca2 0 1699289914684 6 connected
[root@localhost application]# ./redis/src/redis-server 7001/redis.conf
192.168.20.142:7005> cluster nodes
9fbae28cad18dfe970d9bd8711895e215b79825b 192.168.20.142:7005@17005 myself,master - 0 1699289959000 5 connected 10923-16383
fda81dbc8716b3542acb1330685f7c9c11eccd3d 192.168.20.138:7001@17001 slave c330c995d1117843d808e8ef156f143c2effea56 0 1699289959000 7 connected
bf04cd90d0a9af94d21c8eb728be739009d50860 192.168.20.138:7002@17002 slave 9fbae28cad18dfe970d9bd8711895e215b79825b 0 1699289958300 5 connected
c330c995d1117843d808e8ef156f143c2effea56 192.168.20.141:7004@17004 master - 0 1699289958092 7 connected 0-5460
317cad08031ae6ab0402fd09ba9194ffe3668ca2 192.168.20.141:7003@17003 master - 0 1699289959000 3 connected 5461-10922
994c3c93068e414e85e4d97be85d2a30ae8c7e3b 192.168.20.142:7006@17006 slave 317cad08031ae6ab0402fd09ba9194ffe3668ca2 0 1699289959343 6 connected
宕机掉一个master并重启可以发现其中一个从节点代替了master,被宕机的master变成了slave
[root@slave2 application]# ./redis/src/redis-cli -c -h 192.168.20.142 -p 7006
192.168.20.142:7006> cluster info
cluster_state:fail
cluster_slots_assigned:16384
cluster_slots_ok:10923
cluster_slots_pfail:0
cluster_slots_fail:5461
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:11
cluster_my_epoch:3
cluster_stats_messages_ping_sent:27751
cluster_stats_messages_pong_sent:19643
cluster_stats_messages_meet_sent:2
cluster_stats_messages_sent:47396
cluster_stats_messages_ping_received:19643
cluster_stats_messages_pong_received:21026
cluster_stats_messages_fail_received:17
cluster_stats_messages_auth-req_received:5
cluster_stats_messages_received:40691
192.168.20.142:7006> get 1
(error) CLUSTERDOWN The cluster is down
当一对主从关闭后,集群也会关闭