Linux系统-部署-运维系列导航

 

redis cluster介绍

redis最开始使用主从模式做集群,若master宕机需要手动配置slave转为master;后来为了高可用提出来哨兵模式,该模式下有一个哨兵监视master和slave,若master宕机可自动将slave转为master,但它也有一个问题,就是不能动态扩充;所以在3.x提出cluster集群模式。
 
Redis-Cluster采用无中心结构,每个节点保存数据和整个集群状态,每个节点都和其他所有节点连接。
其结构特点:
  1. 所有的redis节点彼此互联(PING-PONG机制),内部使用二进制协议优化传输速度和带宽。
  2. 节点的fail是通过集群中超过半数的节点检测失效时才生效。
  3. 客户端与redis节点直连,不需要中间proxy层.客户端不需要连接集群所有节点,连接集群中任何一个可用节点即可。
  4. 采用hash slot即哈希槽管理分布式数据
 
redis cluster的hash slot算法
  1. redis cluster有固定的16384个hash slot,对每个key计算CRC16值,然后对16384取模,可以获取key对应的hash slot
  2. redis cluster中每个master都会持有部分slot,比如有3个master,那么可能每个master持有5000多个hash slot
  3. hash slot让node的增加和移除很简单,增加一个master,就将其他master的hash slot移动部分过去,减少一个master,就将它的hash slot移动到其他master上去
  4. 移动hash slot的成本低

 

redis cluster安装

架构设计,三主三从,共6台
机器名称  
IP
服务器角色
备注
localhost
192.168.11.63
节点1
CentOS7 + redis 5.0.14
localhost
192.168.11.64
节点2
CentOS7 + redis 5.0.14
localhost
192.168.11.66
节点3
CentOS7 + redis 5.0.14
localhost
192.168.11.67
节点4
CentOS7 + redis 5.0.14
localhost
192.168.11.72
节点5
CentOS7 + redis 5.0.14
localhost
192.168.11.73
节点6
CentOS7 + redis 5.0.14
 
组件安装操作步骤参考 组件安装部署手册模板,根据不同组件的安装目标,部分操作可以省略。
本文将按照该参考步骤执行。
 
特别关注:以下操作将在所有节点上执行。
 

一、获取组件可执行程序库,包括主程序,此为组件的基本文件

1.官网下载 redis源码
创建目录 /usr/local/redis,将源码包下载到该目录下,支持wget获取

 

2.安装依赖组件
redis源码编译依赖gcc
yum install -y gcc

 

3.解压源码
[root@localhost redis]# tar -zxvf redis-5.0.14.tar.gz
[root@localhost redis]# cd redis-5.0.14/
[root@localhost redis-5.0.14]# ll
总用量 284
-rw-rw-r--  1 root root 127554 10月  4 18:58 00-RELEASENOTES
-rw-rw-r--  1 root root     53 10月  4 18:58 BUGS
-rw-rw-r--  1 root root   2381 10月  4 18:58 CONTRIBUTING
-rw-rw-r--  1 root root   1487 10月  4 18:58 COPYING
drwxrwxr-x  6 root root    124 10月  4 18:58 deps
-rw-rw-r--  1 root root     11 10月  4 18:58 INSTALL
-rw-rw-r--  1 root root    151 10月  4 18:58 Makefile
-rw-rw-r--  1 root root   6888 10月  4 18:58 MANIFESTO
-rw-rw-r--  1 root root  20555 10月  4 18:58 README.md
-rw-rw-r--  1 root root  63088 10月  4 18:58 redis.conf
-rwxrwxr-x  1 root root    275 10月  4 18:58 runtest
-rwxrwxr-x  1 root root    280 10月  4 18:58 runtest-cluster
-rwxrwxr-x  1 root root    373 10月  4 18:58 runtest-moduleapi
-rwxrwxr-x  1 root root    281 10月  4 18:58 runtest-sentinel
-rw-rw-r--  1 root root   9710 10月  4 18:58 sentinel.conf
drwxrwxr-x  3 root root   4096 10月  4 18:58 src
drwxrwxr-x 11 root root    182 10月  4 18:58 tests
drwxrwxr-x  8 root root   4096 10月  4 18:58 utils

 

4.编译安装
通过 PREFIX 指定编译后安装的目录,注意PREFIX大写,安装完成后在指定目录下 bin 目录
[root@localhost redis-5.0.14]# make && make PREFIX=/usr/local/redis install
cd src && make all
make[1]: 进入目录“/usr/local/redis/redis-5.0.14/src”
    CC Makefile.dep
make[1]: 离开目录“/usr/local/redis/redis-5.0.14/src”
make[1]: 进入目录“/usr/local/redis/redis-5.0.14/src”
...
make[1]: 离开目录“/usr/local/redis/redis-5.0.14/src”
cd src && make install
make[1]: 进入目录“/usr/local/redis/redis-5.0.14/src”
    CC Makefile.dep
make[1]: 离开目录“/usr/local/redis/redis-5.0.14/src”
make[1]: 进入目录“/usr/local/redis/redis-5.0.14/src”

Hint: It's a good idea to run 'make test' ;)

    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
make[1]: 离开目录“/usr/local/redis/redis-5.0.14/src”

 

二、安装系统服务

redis默认没有安装系统服务,通过主程序运行。
redis启动时需要指定配置文件,不建议安装系统服务,否则将损失灵活性。
 

三、主程序加入到环境变量

1..将redis目录添加到环境变量
[root@localhost bin]# vim /etc/profile
#redis env
export REDIS_HOME=/usr/local/redis
export PATH=$PATH:$REDIS_HOME/bin

[root@localhost bin]# source /etc/profile

 

四、配置文件

1.创建主配置文件
redis主程序启动时,需要指定配置文件,默认配置文件在源码中,路径为 /usr/local/redis/redis-5.0.14/redis.conf,拷贝到 conf 目录
[root@localhost ~]# cd /usr/local/redis
[root@localhost redis]# mkdir conf
[root@localhost redis]# cp redis-5.0.14/redis.conf conf/redis.conf

 

2.核心配置项如下,其他选项使用默认配置即可
#绑定所有网络,保证外部可连接
bind 0.0.0.0
#端口号,建议使用非默认端口
port 7379
#后台运行服务
daemonize yes
#线程id文件
pidfile /data/redis/redis.pid
#日志文件
logfile /data/redis/logs/redis.log
#数据目录,主目录
dir /data/redis/data
#从节点连接主节点时提供的密码,为集群节点间传递,与主节点的 requirepass 对应
masterauth "helloworld.123"
#客户端连接时验证的密码
requirepass "helloworld.123"
#开启集群模式,默认为 no
cluster-enabled yes
#集群配置信息文件,信息由集群维护,无需人工维护
cluster-config-file nodes.conf
#集群节点请求超时时间,超时则识别节点为离线
cluster-node-timeout 15000
 
特别关注:配置文件中指定的所有路径,请在启动前确保已存在
 

五、运行用户

默认使用root运行即可。
 

六、开机启动

请参考教程 Linux开机启动方案 
 

七、服务启动运行

以主程序运行为例(已经将主目录添加环境变量)
1.所有节点启动redis
[root@localhost redis]# redis-server conf/redis.conf 
[root@localhost redis]# ps -ef | grep redis
root     19819     1  0 18:51 ?        00:00:00 bin/redis-server 0.0.0.0:7379
root     19831  1340  0 18:51 pts/0    00:00:00 grep --color=auto redis
[root@localhost redis]# redis-server --version
Redis server v=5.0.14 sha=00000000:0 malloc=jemalloc-5.1.0 bits=64 build=d1b0db65b332b8b2

 

2.创建集群
所有节点启动后,默认独立运行,并没有加入到同一个集群中,需要通过redis工具创建集群
特别关注:创建集群操作在任意一个节点执行即可
# -a指定节点访问密码
# --cluster表示为集群操作
# create为创建集群
# --cluster-replicas指定为每个主节点分配的从节点数
[root@localhost redis]# redis-cli -a helloworld.123 --cluster create 192.168.11.63:7379 192.168.11.64:7379 192.168.11.66:7379 192.168.11.67:7379 192.168.11.72:7379 192.168.11.73:7379 --cluster-replicas 1
# 创建过程需要多次输入 yes
...
...
...
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

 

3.连接集群
# -a指定节点访问密码
# -c指定集群模式连接
# -h指定连接节点IP,-p指定连接节点端口
[root@localhost redis]# /usr/local/redis/redis-5.0.14/src/redis-cli -a helloworld.123 -c -h 192.168.11.73 -p 7379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.11.73:7379>

 

cluster nodes查看集群节点,包括主从架构
192.168.11.73:7379> cluster nodes
ef648663b2ba84979a74d625413f07cf7f10f419 192.168.11.63:7379@17379 master - 0 1645011164000 17 connected 0-5460
4d3d7df17a8325ff559e10ec9d0dc1637e61a00a 192.168.11.73:7379@17379 myself,slave 60db8a1d8aeaaa82b7e30b2cb0729efd2cd20ef3 0 1645011163000 22 connected
9ccfdaa9301f8a8b0fdc25851b1163bd7753f5b4 192.168.11.67:7379@17379 slave 90a6eaf37b7c507dbe863d735bf85b8e86da6183 0 1645011162000 18 connected
60db8a1d8aeaaa82b7e30b2cb0729efd2cd20ef3 192.168.11.64:7379@17379 master - 0 1645011163952 23 connected 5461-10922
f2870db82aafccf3a5674fb896a78ebc7476858e 192.168.11.72:7379@17379 slave ef648663b2ba84979a74d625413f07cf7f10f419 0 1645011163000 17 connected
90a6eaf37b7c507dbe863d735bf85b8e86da6183 192.168.11.66:7379@17379 master - 0 1645011164967 18 connected 10923-16383

 

即当前redis集群架构如下
主节点
从节点
192.168.11.63
192.168.11.72
192.168.11.64
192.168.11.73
192.168.11.66
192.168.11.67
 
cluster info查看集群信息
192.168.11.73:7379> 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:23
cluster_my_epoch:23
cluster_stats_messages_ping_sent:545313
cluster_stats_messages_pong_sent:567031
cluster_stats_messages_sent:1112344
cluster_stats_messages_ping_received:567031
cluster_stats_messages_pong_received:545122
cluster_stats_messages_fail_received:4
cluster_stats_messages_update_received:7
cluster_stats_messages_received:1112164

 

4.验证集群读写
4.1 连接73节点,写数据
[root@localhost redis]# /usr/local/redis/redis-5.0.14/src/redis-cli -a helloworld.123 -c -h 192.168.11.73 -p 7379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.11.73:7379> set hello world
-> Redirected to slot [866] located at 192.168.11.63:7379
OK
                      

以上信息表示

  1. 73节点为从节点,不提供读写服务,需要重定向到主节点写数据
  2. key为"hello"的数据所在哈希槽被分配在 63 节点上
 
4.2 连接72节点,读数据
[root@localhost redis-5.0.13]# /usr/local/redis/redis-5.0.13/src/redis-cli -a helloworld.123 -c -h 192.168.11.72 -p 7379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.11.72:7379> get hello
-> Redirected to slot [866] located at 192.168.11.63:7379
"world"

以上信息表示

  1. 72节点为从节点,不提供读写服务,需要重定向到主节点读数据
  2. key为"hello"的数据所在哈希槽被分配在 63 节点上
 
5.模拟主节点故障
以上述主节点 63 为例,验证主节点离线后,从节点 72 提供读写服务
5.1 模拟63节点停止服务,可以使用 kill -9 停止进程,也可以使用 redis-cli 工具优雅关闭
[root@localhost redis-5.0.14]# redis-cli -c -a helloworld.123 -h 192.168.11.72 -p 7379 shutdown
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.

 

5.2 连接72节点,读数据
[root@clickhouse1 redis-5.0.13]# redis-cli -c -a helloworld.123 -h 192.168.11.72 -p 7379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.11.72:7379> cluster nodes
9ccfdaa9301f8a8b0fdc25851b1163bd7753f5b4 192.168.11.67:7379@17379 slave 90a6eaf37b7c507dbe863d735bf85b8e86da6183 0 1645014016264 18 connected
60db8a1d8aeaaa82b7e30b2cb0729efd2cd20ef3 192.168.11.64:7379@17379 master - 0 1645014017280 23 connected 5461-10922
90a6eaf37b7c507dbe863d735bf85b8e86da6183 192.168.11.66:7379@17379 master - 0 1645014018296 18 connected 10923-16383
4d3d7df17a8325ff559e10ec9d0dc1637e61a00a 192.168.11.73:7379@17379 slave 60db8a1d8aeaaa82b7e30b2cb0729efd2cd20ef3 0 1645014019319 23 connected
ef648663b2ba84979a74d625413f07cf7f10f419 192.168.11.63:7379@17379 master,fail - 1645013983290 1645013981000 25 disconnected
f2870db82aafccf3a5674fb896a78ebc7476858e 192.168.11.72:7379@17379 myself,master - 0 1645014018000 26 connected 0-5460
192.168.11.72:7379> get hello
"world"
192.168.11.72:7379> 
以上信息表示
  1. 63节点离线,为 fail 状态
  2. 72节点已经转换为master
  3. 72节点可以直接读取key为"hello"的数据
 
至此,集群验证通过。
 
特别关注:系统默认启用了SELinux内核模块(安全子系统),所以在服务绑定/监听某些端口时,提示无访问权限,此时需要禁用SELinux,修改 /etc/selinux/config 文件,设置SELINUX=disabled
Can't start server: Bind on TCP/IP port: Permission denied
 
特别关注:selinux设置完成需要重启生效,如果当前不方便重启,可以执行 setenforce 0 临时关闭selinux,下次重启是配置再生效
 
特别关注:系统默认启用了防火墙,请在启动服务前关闭防火墙,或在防火墙中添加服务端口
 

附录:单台服务器搭建redis集群

本文提供了6台服务器搭建三主三从集群,如果服务器小于6台,也是可以搭建三主三从redis架构。
由于redis服务支持多实例运行,即同一台服务器运行多个redis实例(进程),每个实例指定不同的配置文件(如端口、数据目录等)即可。
以1台服务器 192.168.11.9 为例,搭建三主三从redis集群,需要做以下准备操作
 
1.提供6个不同的服务端口
7001 - 7006

 

2.提供6个不同的数据目录
/data/redis/7001/
/data/redis/7002/
/data/redis/7003/
/data/redis/7004/
/data/redis/7005/
/data/redis/7006/
#每个目录下创建 data、logs子目录

 

3.提供6个不同的配置文件,分别指定以上服务端口与相应数据目录
/usr/local/redis/cluster-conf/redis.7001.conf
/usr/local/redis/cluster-conf/redis.7002.conf
/usr/local/redis/cluster-conf/redis.7003.conf
/usr/local/redis/cluster-conf/redis.7004.conf
/usr/local/redis/cluster-conf/redis.7005.conf
/usr/local/redis/cluster-conf/redis.7006.conf

 

4.以不同的配置文件,分别启动redis实例
[root@localhost redis]# redis-server cluster-conf/redis.7001.conf
[root@localhost redis]# redis-server cluster-conf/redis.7002.conf
[root@localhost redis]# redis-server cluster-conf/redis.7003.conf
[root@localhost redis]# redis-server cluster-conf/redis.7004.conf
[root@localhost redis]# redis-server cluster-conf/redis.7005.conf
[root@localhost redis]# redis-server cluster-conf/redis.7006.conf

 

5.创建集群
[root@localhost redis]# redis-cli -a helloworld.123 --cluster create 192.168.11.9:7001 192.168.11.9:7002 192.168.11.9:7003 192.168.11.9:7004 192.168.11.9:7005 192.168.11.9:7006 --cluster-replicas 1
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 192.168.11.9:7005 to 192.168.11.9:7001
Adding replica 192.168.11.9:7006 to 192.168.11.9:7002
Adding replica 192.168.11.9:7004 to 192.168.11.9:7003
...
...
...
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

 

6.验证集群信息
# 连接集群
[root@localhost redis]# redis-cli -c -a helloworld.123 -h 192.168.11.9 -p 7001
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.


# 集群信息
192.168.11.9: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:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:185
cluster_stats_messages_pong_sent:169
cluster_stats_messages_sent:354
cluster_stats_messages_ping_received:164
cluster_stats_messages_pong_received:185
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:354

# 节点信息
192.168.11.9:7001> CLUSTER NODES
785be5bbdfaa3e513febb00b8ba60e199b613e5a 192.168.11.9:7004@17004 slave 431b28e1662c67918b3400252b689752989638b7 0 1645011118845 4 connected
b33f15d25aa0506656da26b3774f3226cc8ad13d 192.168.11.9:7001@17001 myself,master - 0 1645011119000 1 connected 0-5460
431b28e1662c67918b3400252b689752989638b7 192.168.11.9:7002@17002 master - 0 1645011120870 2 connected 5461-10922
662ee8a13f5e7af9756f4c651e07f3c583a74907 192.168.11.9:7006@17006 slave b33f15d25aa0506656da26b3774f3226cc8ad13d 0 1645011118000 6 connected
083d49438e9a3b01aae36489ee3dc8dc9c47727b 192.168.11.9:7005@17005 slave 318b984be161a65045746ed740ad38860331dd14 0 1645011121885 5 connected
318b984be161a65045746ed740ad38860331dd14 192.168.11.9:7003@17003 master - 0 1645011120000 3 connected 10923-16383

 

附录:参考资料

 
posted on 2023-09-05 16:37  xiaoyaozhe  阅读(266)  评论(0编辑  收藏  举报