1.1集群介绍
redis 集群cluster
redis 集群是一个提供在多个redis间节点的共享数据
redis 集群通过分区share来提供一定程度的可用性,在实际环境中当某一个节点宕机或者不可达的请
况下继续处理命令。
redis 集群的优势:
自动分割数据到不同的节点上。
整个集群的部分节点失败或者不可达的情况下能够继续处理命令。
1.2 redis 集群的数据分片
redis使用hash槽,每个key通过CRC16校验后对16384去模来决定放置那个槽,集群的每一个节点来
负责一部分hash槽,
例如当前集群有三个节点。
节点A:包含0-5500号hash槽,
节点B:包含5501-11000号hash槽,
节点C:包含11000-16384号hash槽,
数据究竟存放在那个槽上,数据hash运算除以16384取余。
1.3集群的主存服复制模型
redis的一致性保证
redis并不能保证数据的强一致性,这意味着在实际中集群在特定条件下操作可能丢失一些数据。
集群是用了异步复制 写操作过程
1.4 集群搭建
IP | 端口 |
---|---|
192.168.79.156 | 7000 7001 7002 |
192.168.79.170 | 7003 7004 7005 |
– | – |
同步时间
yum -y install ntp ntpdate
ntpdate cn.pool.ntp.org
hwclock --systoh
关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
setenforce 0
rpm -e mariadb-libs postfix
(1)分别在两个节点上安装redis
(2)为了方便使用创建集群命令,将其复制到/usr/local/bin
cd /usr/local/src/redis-5.0.8/src/
cp -p redis-trib.rb /usr/local/bin
添加系统路径:
export PATH=$PATH:/data/redis/bin
(3)修改配置文件
为了区分端口,我在/data/redis/创建了端口7000,7001,7002目录,并且把redis配置文件拷贝到相应目录下。
[root@localhost redis]# ll
总用量 0
drwxr-xr-x 2 root root 24 8月 7 20:24 7000
drwxr-xr-x 2 root root 24 8月 7 20:24 7001
drwxr-xr-x 2 root root 24 8月 7 20:24 7002
drwxr-xr-x. 2 root root 134 8月 6 16:00 bin
drwxr-xr-x. 2 root root 69 8月 7 20:16 conf
bind 192.168.79.156 (本机IP)
port 7000
daemonize yes
logfile "/data/redis/log/logs"
pidfile /var/run/redis_7000.pid
cluster-enabled yes
cluster-config-file nodes-7000.conf
cluster-node-timeout 5000
appendonly yes
在这里有个有个小技巧告诉大家一下,只需创建一个7000目录,修改里面拷贝的redis.conf文件,使用拷贝命令就可以生成7001,7002目录。进入7001目录下的配置文件 使用末行模式 :% s/7000/7001/g (匹配全局修改7000为7001,7002目录同理。)
使用 scp -pr 7000 192.168.79.170:/data/redis/7003
进行目录复制到另外一节点。进入配置文件中,改IP地址和端口信息。
IP:192.168.79.156
[root@localhost redis]# scp -pr 7000 192.168.79.170:/data/redis/7003
The authenticity of host '192.168.79.170 (192.168.79.170)' can't be established.
ECDSA key fingerprint is SHA256:F4qKSwdTwstd7oZE22QiMmdoinWIeeZJniAaUGigMUY.
ECDSA key fingerprint is MD5:16:25:01:65:96:ae:38:d4:b1:81:08:1e:01:7e:47:4d.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.79.170' (ECDSA) to the list of known hosts.
root@192.168.79.170's password:
redis.conf 100% 60KB 3.3MB/s 00:00
IP:192.168.79.170
[root@localhost redis]# ls
7003 7004 7005 bin conf
(4)启动实例
[root@localhost cluste]# redis-server 7000/redis.conf
[root@localhost cluste]# redis-server 7001/redis.conf
[root@localhost cluste]# redis-server 7002/redis.conf
[root@localhost redis]# ps -ef |grep redis
root 7577 1 0 20:34 ? 00:00:03 redis-server 192.168.79.156:7000 [cluster]
root 7582 1 0 20:34 ? 00:00:03 redis-server 192.168.79.156:7001 [cluster]
root 7634 1 0 20:38 ? 00:00:03 redis-server 192.168.79.156:7002 [cluster]
root 8014 7171 0 21:30 pts/1 00:00:00 grep --color=auto redis
[root@localhost cluste]# redis-server 7003/redis.conf
[root@localhost cluste]# redis-server 7004/redis.conf
[root@localhost cluste]# redis-server 7005/redis.conf
[root@localhost redis]# ps -ef |grep redis
root 11690 1 0 20:47 ? 00:00:00 redis-server 192.168.79.170:7003 [cluster]
root 11695 1 0 20:47 ? 00:00:00 redis-server 192.168.79.170:7004 [cluster]
root 11700 1 0 20:47 ? 00:00:00 redis-server 192.168.79.170:7005 [cluster]
root 11705 11126 0 20:47 pts/0 00:00:00 grep --color=auto redis
(5)创建集群
随便哪一节点都可以
[root@localhost redis]# redis-cli --cluster create 192.168.79.156:7000 192.168.79.156:7001 192.168.79.156:7002 192.168.79.170:7003 192.168.79.170:7004 192.168.79.170:7005 --cluster-replicas 1
>>> 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.79.170:7005 to 192.168.79.156:7000
Adding replica 192.168.79.156:7002 to 192.168.79.170:7003
Adding replica 192.168.79.170:7004 to 192.168.79.156:7001
M: 012042ceae62d208eba6726f61a243931a98892d 192.168.79.156:7000
slots:[0-5460] (5461 slots) master
M: a28a7a42b060c89106cfcd11ed3ac95ac259e17f 192.168.79.156:7001
slots:[10923-16383] (5461 slots) master
S: a71adf1355d5ac0aa47e6ab7d2e899f152fb6521 192.168.79.156:7002
replicates 8a2c680ea66c7552e09f237b756bd5654a3d828d
M: 8a2c680ea66c7552e09f237b756bd5654a3d828d 192.168.79.170:7003
slots:[5461-10922] (5462 slots) master
S: 66aedd493374e1d92cef2e970fa52d6365f5db62 192.168.79.170:7004
replicates a28a7a42b060c89106cfcd11ed3ac95ac259e17f
S: 3b23d8b3079c5e68146fa2c373dd02329ea0fa7b 192.168.79.170:7005
replicates 012042ceae62d208eba6726f61a243931a98892d
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.79.156:7000)
M: 012042ceae62d208eba6726f61a243931a98892d 192.168.79.156:7000
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: a71adf1355d5ac0aa47e6ab7d2e899f152fb6521 192.168.79.156:7002
slots: (0 slots) slave
replicates 8a2c680ea66c7552e09f237b756bd5654a3d828d
M: a28a7a42b060c89106cfcd11ed3ac95ac259e17f 192.168.79.156:7001
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 3b23d8b3079c5e68146fa2c373dd02329ea0fa7b 192.168.79.170:7005
slots: (0 slots) slave
replicates 012042ceae62d208eba6726f61a243931a98892d
M: 8a2c680ea66c7552e09f237b756bd5654a3d828d 192.168.79.170:7003
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 66aedd493374e1d92cef2e970fa52d6365f5db62 192.168.79.170:7004
slots: (0 slots) slave
replicates a28a7a42b060c89106cfcd11ed3ac95ac259e17f
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
主 | 从 |
---|---|
7000 | 7005 |
7001 | 7004 |
7003 | 7002 |
使用客户端进行链接
注意看端口变化
[root@localhost ~]# redis-cli -h 192.168.79.156 -p 7000 -c
192.168.79.156:7000> set name zhangsan
-> Redirected to slot [5798] located at 192.168.79.170:7003
OK
192.168.79.170:7003> get name
"zhangsan"
192.168.79.170:7003>
重新链接一个新的客户端
[root@localhost redis]# redis-cli -h 192.168.79.170 -p 7004 -c
192.168.79.170:7004> get name
-> Redirected to slot [5798] located at 192.168.79.170:7003
"zhangsan"
杀掉7001端口后,7004端口晋升为主,集群正常。
[root@localhost redis]# ps -ef |grep redis
root 7577 1 0 20:34 ? 00:00:03 redis-server 192.168.79.156:7000 [cluster]
root 7582 1 0 20:34 ? 00:00:03 redis-server 192.168.79.156:7001 [cluster]
root 7634 1 0 20:38 ? 00:00:03 redis-server 192.168.79.156:7002 [cluster]
root 8014 7171 0 21:30 pts/1 00:00:00 grep --color=auto redis
[root@localhost redis]# kill 7582
[root@localhost redis]# ps -ef |grep redis
root 7577 1 0 20:34 ? 00:00:03 redis-server 192.168.79.156:7000 [cluster]
root 7634 1 0 20:38 ? 00:00:03 redis-server 192.168.79.156:7002 [cluster]
root 8017 7171 0 21:30 pts/1 00:00:00 grep --color=auto redis
杀掉整个节点(7000,7001,7002故障),redis集群依然健壮,
[root@localhost redis]# shutdown -h now
────────────────────────────────────────────────────────────────────────────────────────────────────
Session stopped
- Press <return> to exit tab
- Press R to restart session
- Press S to save terminal output to file
Server unexpectedly closed network connection
总结
(1)单个节点的主故障,从节点成为主节点。数据不会丢失,俩俩互备。
(2)整个节点故障,还有其他节点往上顶,体现了redis集群的健壮性。