• 猿创征文|linux部署Redis集群(修订版)


    前言:

    Redis 的几种常见使用方式包括:

    • Redis 单副本

    • Redis 多副本(主从)

    • Redis Sentinel(哨兵)

    • Redis Cluster

    • Redis 自研

    Redis Cluster是一种服务器 Sharding 技术,redis 3.0版本开始正式提供。Sentinel基本已经实现了高可用,但是每台机器都存储相同内容,很浪费内存,所以Redis Cluster实现了分布式存储。每台机器节点上存储不同的内容。

    Redis cluster具有以下优点:

    1. - 组件all-in-box,部署简单,节约机器资源
    2. - 性能比proxy模式好
    3. - 自动故障转移、Slot迁移中数据可用
    4. - 官方原生集群方案,更新与支持有保障

    因此,本文主要探讨的就是官方版本的Redis集群,

    部署规划:

    计划使用三台服务器,具体信息如下:

    Redis集群规划
    IDIP部署的组件部署方式Redis版本
    1192.168.217.16redis单机实例两个源码编译Redis-5.0.4
    2192.168.217.17redis单机实例两个源码编译Redis-5.0.4
    3192.168.217.18redis单机实例两个源码编译Redis-5.0.4

    部署步骤:

    一,

    在官网下载Redis-5.0.4,官网地址:Index of /releases/

    下载地址Index of /releases/ 

    将安装包上传到服务器上,解压备用。

    二,

    进入解压后的目录,可以看到有redis.conf

    1. [root@master redis-5.0.4]# ll
    2. total 256
    3. -rw-rw-r-- 1 root root 99445 Mar 19 2019 00-RELEASENOTES
    4. -rw-rw-r-- 1 root root 53 Mar 19 2019 BUGS
    5. -rw-rw-r-- 1 root root 1894 Mar 19 2019 CONTRIBUTING
    6. -rw-rw-r-- 1 root root 1487 Mar 19 2019 COPYING
    7. drwxrwxr-x 6 root root 192 Sep 8 23:39 deps
    8. -rw-rw-r-- 1 root root 11 Mar 19 2019 INSTALL
    9. -rw-rw-r-- 1 root root 151 Mar 19 2019 Makefile
    10. -rw-rw-r-- 1 root root 4223 Mar 19 2019 MANIFESTO
    11. -rw-rw-r-- 1 root root 20555 Mar 19 2019 README.md
    12. -rw-rw-r-- 1 root root 62147 Sep 9 01:17 redis.conf
    13. -rwxrwxr-x 1 root root 275 Mar 19 2019 runtest
    14. -rwxrwxr-x 1 root root 280 Mar 19 2019 runtest-cluster
    15. -rwxrwxr-x 1 root root 281 Mar 19 2019 runtest-sentinel
    16. -rw-rw-r-- 1 root root 9710 Mar 19 2019 sentinel.conf
    17. drwxrwxr-x 3 root root 8192 Sep 8 23:51 src
    18. drwxrwxr-x 10 root root 167 Mar 19 2019 tests
    19. drwxrwxr-x 8 root root 4096 Mar 19 2019 utils

    三,

    编译前的依赖安装

    配置一个本地仓库,本地仓库的配置就不说了,安装依赖命令:

    yum install autoconf gcc gcc-c++ -y

    四,

    编译和编译安装:

    make

    1. [root@slave1 redis-5.0.4]# make
    2. cd src && make all
    3. make[1]: Entering directory `/root/redis-5.0.4/src'
    4. CC Makefile.dep
    5. make[1]: Leaving directory `/root/redis-5.0.4/src'
    6. make[1]: Entering directory `/root/redis-5.0.4/src'
    7. rm -rf redis-server redis-sentinel redis-cli redis-benchmark redis-check-rdb redis-check-aof *.o *.gcda *.gcno *.gcov redis.info lcov-html Makefile.dep dict-benchmark
    8. (cd ../deps && make distclean)
    9. make[2]: Entering directory `/root/redis-5.0.4/deps'
    10. (cd hiredis && make clean) > /dev/null || true
    11. (cd linenoise && make clean) > /dev/null || true
    12. (cd lua && make clean) > /dev/null || true
    13. (cd jemalloc && [ -f Makefile ] && make distclean) > /dev/null || true
    14. (rm -f .make-*)
    15. 。。。。。。。。。。。。。。。。。。。。。。。。。。
    16. 。。。。。。。。。。。。。。。。。。。。。。。。

    make install

    1. [root@slave1 redis-5.0.4]# make install PREFIX=/usr/local/redis-cluster/redis-node1
    2. cd src && make install
    3. make[1]: Entering directory `/root/redis-5.0.4/src'
    4. CC Makefile.dep
    5. make[1]: Leaving directory `/root/redis-5.0.4/src'
    6. make[1]: Entering directory `/root/redis-5.0.4/src'
    7. Hint: It's a good idea to run 'make test' ;)
    8. INSTALL install
    9. INSTALL install
    10. INSTALL install
    11. INSTALL install
    12. INSTALL install
    13. make[1]: Leaving directory `/root/redis-5.0.4/src'

    此时,在/usr/local/redis-cluster/redis-node1下就有了第一个实例文件,继续make install ,命令如下:

    [root@slave1 redis-5.0.4]# make install PREFIX=/usr/local/redis-cluster/redis-node1

    此目录下现有文件如下:

    1. [root@slave1 bin]# pwd
    2. /usr/local/redis-cluster/redis-node1/bin
    3. [root@slave1 bin]# ll
    4. total 32696
    5. -rwxr-xr-x 1 root root 4365912 Sep 9 10:24 redis-benchmark
    6. -rwxr-xr-x 1 root root 8101856 Sep 9 10:24 redis-check-aof
    7. -rwxr-xr-x 1 root root 8101856 Sep 9 10:24 redis-check-rdb
    8. -rwxr-xr-x 1 root root 4806256 Sep 9 10:24 redis-cli
    9. lrwxrwxrwx 1 root root 12 Sep 9 10:24 redis-sentinel -> redis-server
    10. -rwxr-xr-x 1 root root 8101856 Sep 9 10:24 redis-server

    三台服务器都这么操作,也就是最后三个服务器总共有/usr/local/redis-cluster/redis-node1 /usr/local/redis-cluster/redis-node2 六个这样的目录。

    五,

    配置文件的修改

    由于节点比较多,总共是六个,因此,配置文件采用模板方式,先修改192.168.217.16服务器内的安装目录内的redis.conf,修改的地方有这么几个:

    --->daemonize no 修改为daemonize yes

    --->bind 127.0.0.1 修改为 bind 192.168.217.16 127.0.0.1(在17服务器上就修改为17,18服务器上修改为18,必须要有127.0.0.1)

    --->protected-mode yes 修改为protected-mode yes

    --->port 6379 修改为port 16379(修改成这样是为了安全,毕竟默认端口不安全嘛)

    --->appendonly no 修改为 appendonly yes        #aof模式的开启

    --->cluster-enabled yes 这一行注释去掉,开启集群模式

    --->cluster-config-file redis-6379.conf 这一行去掉注释,修改为cluster-config-file 192.168.217.16-node1.conf

    --->logfile "" 修改为logfile  /usr/local/redis-cluster/redis-node1/bin/node1.log

    这样修改完毕后,我们在16服务器上就有了一个模板文件,然后将此文件拷贝到 /usr/local/redis-cluster/redis-node1/bin目录下:

    cp 192.168.217.16-node1.conf /usr/local/redis-cluster/redis-node1/bin/192.168.217.16-node1.conf

    复制到node2目录下,这个时候名字修改一下:

    cp 192.168.217.16-node1.conf /usr/local/redis-cluster/redis-node2/bin/192.168.217.16-node2.conf

    编辑一哈node2的配置文件(两个地方需要修改):

    1. cluster-config-file 192.168.217.16-node1.conf这一段 修改为cluster-config-file 192.168.217.16-node2.conf
    2. port 16379 字段修改为port 16380

    其它节点照着做就可以了,配置文件内的IP地址和cluster-config-file 字段以及logs的文件形式与实际的配置文件名字相符就可以了,例如,17服务器node1下的配置文件名称为192.168.217.17-node1.conf,文件内的cluster-config-file 字段是192.168.217.17-node1.conf,logfiel字段是logfile /usr/local/redis-cluster/redis-node1/bin/node1.log  ,port 是16379,如果是node2,那么,文件内port就是16380,字段是192.168.217.17-node2.conf,logs字段是logfile /usr/local/redis-cluster/redis-node2/bin/node2.log

    六,

    启动脚本

    以16服务器上为例:

    1. [root@master redis-5.0.4]# pwd
    2. /root/redis-5.0.4
    3. [root@master redis-5.0.4]# cp -a utils/redis_init_script /etc/init.d/redis-node1
    4. [root@master redis-5.0.4]# cp -a utils/redis_init_script /etc/init.d/redis-node2

    编辑启动脚本redis-node1和redis-node2;

    port和可执行文件路径配置文件路径一一对应

    1. [root@master init.d]# cat /etc/init.d/redis-node1
    2. #!/bin/sh
    3. #
    4. # Simple Redis init.d script conceived to work on Linux systems
    5. # as it does use of the /proc filesystem.
    6. ### BEGIN INIT INFO
    7. # Provides: redis_6379
    8. # Default-Start: 2 3 4 5
    9. # Default-Stop: 0 1 6
    10. # Short-Description: Redis data structure server
    11. # Description: Redis data structure server. See https://redis.io
    12. ### END INIT INFO
    13. REDISPORT=16379
    14. EXEC=/usr/local/redis-cluster/redis-node1/bin/redis-server
    15. CLIEXEC=/usr/local/redis-cluster/redis-node1/bin/redis-cli
    16. PIDFILE=/var/run/redis_${REDISPORT}.pid
    17. CONF="/usr/local/redis-cluster/redis-node1/bin/192.168.217.16-node1.conf"
    18. case "$1" in
    19. start)
    20. if [ -f $PIDFILE ]
    21. then
    22. echo "$PIDFILE exists, process is already running or crashed"
    23. else
    24. echo "Starting Redis server..."
    25. $EXEC $CONF
    26. fi
    27. ;;
    28. stop)
    29. if [ ! -f $PIDFILE ]
    30. then
    31. echo "$PIDFILE does not exist, process is not running"
    32. else
    33. PID=$(cat $PIDFILE)
    34. echo "Stopping ..."
    35. $CLIEXEC -p $REDISPORT shutdown
    36. while [ -x /proc/${PID} ]
    37. do
    38. echo "Waiting for Redis to shutdown ..."
    39. sleep 1
    40. done
    41. echo "Redis stopped"
    42. fi
    43. ;;
    44. *)
    45. echo "Please use start or stop as first argument"
    46. ;;
    47. esac
    48. [root@master init.d]# cat /etc/init.d/redis-node2
    49. #!/bin/sh
    50. #
    51. # Simple Redis init.d script conceived to work on Linux systems
    52. # as it does use of the /proc filesystem.
    53. ### BEGIN INIT INFO
    54. # Provides: redis_6379
    55. # Default-Start: 2 3 4 5
    56. # Default-Stop: 0 1 6
    57. # Short-Description: Redis data structure server
    58. # Description: Redis data structure server. See https://redis.io
    59. ### END INIT INFO
    60. REDISPORT=16380
    61. EXEC=/usr/local/redis-cluster/redis-node2/bin/redis-server
    62. CLIEXEC=/usr/local/redis-cluster/redis-node2/bin/redis-cli
    63. PIDFILE=/var/run/redis_${REDISPORT}.pid
    64. CONF="/usr/local/redis-cluster/redis-node2/bin/192.168.217.16-node2.conf"
    65. case "$1" in
    66. start)
    67. if [ -f $PIDFILE ]
    68. then
    69. echo "$PIDFILE exists, process is already running or crashed"
    70. else
    71. echo "Starting Redis server..."
    72. $EXEC $CONF
    73. fi
    74. ;;
    75. stop)
    76. if [ ! -f $PIDFILE ]
    77. then
    78. echo "$PIDFILE does not exist, process is not running"
    79. else
    80. PID=$(cat $PIDFILE)
    81. echo "Stopping ..."
    82. $CLIEXEC -p $REDISPORT shutdown
    83. while [ -x /proc/${PID} ]
    84. do
    85. echo "Waiting for Redis to shutdown ..."
    86. sleep 1
    87. done
    88. echo "Redis stopped"
    89. fi
    90. ;;
    91. *)
    92. echo "Please use start or stop as first argument"
    93. ;;
    94. esac

    七,

    启动各个实例(以16节点的node1为例,其它照做)

    1. chkconfig --add redis-node1.conf&&chkconfig redis-node1 on
    2. service redis-node1 start(启动节点1)
    3. service redis-node1 stop(关闭节点1)

    查看进程,看到这样就算基本完成了:

    1. [root@master init.d]# ps -ef |grep redis
    2. root 6029 1 0 11:13 ? 00:00:04 /usr/local/redis-cluster/redis-node1/bin/redis-server 192.168.217.16:16379 [cluster]
    3. root 6082 1 0 11:13 ? 00:00:04 /usr/local/redis-cluster/redis-node2/bin/redis-server 192.168.217.16:16380 [cluster]
    4. root 19864 16681 0 11:47 pts/0 00:00:00 grep --color=auto redis
    5. [root@master init.d]# netstat -antup |grep 163
    6. tcp 0 0 192.168.217.16:16379 0.0.0.0:* LISTEN 6029/redis-server 1
    7. tcp 0 0 192.168.217.16:16380 0.0.0.0:* LISTEN 6082/redis-server 1

    八,

    最后一哆嗦,集群建立,建立命令为;

    [root@master bin]# ./redis-cli --cluster create 192.168.217.16:16379 192.168.217.16:16380 192.168.217.17:16379 192.168.217.17:16380 192.168.217.18:16379 192.168.217.18:16380 --cluster-replicas 1
    

    输入如下;

    1. [root@master bin]# ./redis-cli --cluster create 192.168.217.16:16379 192.168.217.16:16380 192.168.217.17:16379 192.168.217.17:16380 192.168.217.18:16379 192.168.217.18:16380 --cluster-replicas 1
    2. >>> Performing hash slots allocation on 6 nodes...
    3. Master[0] -> Slots 0 - 5460
    4. Master[1] -> Slots 5461 - 10922
    5. Master[2] -> Slots 10923 - 16383
    6. Adding replica 192.168.217.17:16380 to 192.168.217.16:16379
    7. Adding replica 192.168.217.18:16380 to 192.168.217.17:16379
    8. Adding replica 192.168.217.16:16380 to 192.168.217.18:16379
    9. M: b6e8d09653f87d16ee5971ef332c0a0cbd448d5c 192.168.217.16:16379
    10. slots:[0-5460] (5461 slots) master
    11. S: 11b1cefbb59905142f2e429b58b1ddaca1b87969 192.168.217.16:16380
    12. replicates 0754abc970dc7fe790e45fce27512b5c0dbe3678
    13. M: 4c7d99015b46ef2837070f3b7f4c4f3b1a15fd80 192.168.217.17:16379
    14. slots:[5461-10922] (5462 slots) master
    15. S: 61321ce9323ab88af6cf7da5384438c594f91e9f 192.168.217.17:16380
    16. replicates b6e8d09653f87d16ee5971ef332c0a0cbd448d5c
    17. M: 0754abc970dc7fe790e45fce27512b5c0dbe3678 192.168.217.18:16379
    18. slots:[10923-16383] (5461 slots) master
    19. S: 2ba9a13f828cae6490982ef50837589690c0f25e 192.168.217.18:16380
    20. replicates 4c7d99015b46ef2837070f3b7f4c4f3b1a15fd80
    21. Can I set the above configuration? (type 'yes' to accept): yes
    22. >>> Nodes configuration updated
    23. >>> Assign a different config epoch to each node
    24. >>> Sending CLUSTER MEET messages to join the cluster
    25. Waiting for the cluster to join
    26. ............
    27. >>> Performing Cluster Check (using node 192.168.217.16:16379)
    28. M: b6e8d09653f87d16ee5971ef332c0a0cbd448d5c 192.168.217.16:16379
    29. slots:[0-5460] (5461 slots) master
    30. 1 additional replica(s)
    31. M: 0754abc970dc7fe790e45fce27512b5c0dbe3678 192.168.217.18:16379
    32. slots:[10923-16383] (5461 slots) master
    33. 1 additional replica(s)
    34. M: 4c7d99015b46ef2837070f3b7f4c4f3b1a15fd80 192.168.217.17:16379
    35. slots:[5461-10922] (5462 slots) master
    36. 1 additional replica(s)
    37. S: 2ba9a13f828cae6490982ef50837589690c0f25e 192.168.217.18:16380
    38. slots: (0 slots) slave
    39. replicates 4c7d99015b46ef2837070f3b7f4c4f3b1a15fd80
    40. S: 11b1cefbb59905142f2e429b58b1ddaca1b87969 192.168.217.16:16380
    41. slots: (0 slots) slave
    42. replicates 0754abc970dc7fe790e45fce27512b5c0dbe3678
    43. S: 61321ce9323ab88af6cf7da5384438c594f91e9f 192.168.217.17:16380
    44. slots: (0 slots) slave
    45. replicates b6e8d09653f87d16ee5971ef332c0a0cbd448d5c
    46. [OK] All nodes agree about slots configuration.
    47. >>> Check for open slots...
    48. >>> Check slots coverage...
    49. [OK] All 16384 slots covered.

    九,

    单元测试

    (1)

    查看集群信息

    1. [root@master bin]# ./redis-cli -h 192.168.217.16 -p 16379 -c
    2. 192.168.217.16:16379> cluster info
    3. cluster_state:ok
    4. cluster_slots_assigned:16384
    5. cluster_slots_ok:16384
    6. cluster_slots_pfail:0
    7. cluster_slots_fail:0
    8. cluster_known_nodes:6
    9. cluster_size:3
    10. cluster_current_epoch:6
    11. cluster_my_epoch:1
    12. cluster_stats_messages_ping_sent:216
    13. cluster_stats_messages_pong_sent:200
    14. cluster_stats_messages_sent:416
    15. cluster_stats_messages_ping_received:195
    16. cluster_stats_messages_pong_received:216
    17. cluster_stats_messages_meet_received:5
    18. cluster_stats_messages_received:416

    (2)

    集群查看数据:

    1. [root@master bin]# ./redis-cli -h 192.168.217.18 -p 16380 -c
    2. 192.168.217.18:16380> set AAA jingqu
    3. -> Redirected to slot [3205] located at 192.168.217.16:16379
    4. OK
    5. 192.168.217.16:16379> get AAA
    6. "jingqu"
    7. 192.168.217.16:16379>
    8. [root@master bin]# ./redis-cli -h 192.168.217.16 -p 16379 -c
    9. 192.168.217.16:16379> get AAA
    10. "jingqu"
    11. 192.168.217.16:16379>

    (3)

    故障模拟

    下线192.168.217.16:16379,进程里看到确实少了一个节点

    1. 192.168.217.16:16379> shutdown
    2. not connected>
    3. not connected>
    4. [root@master bin]# ps -ef |grep redis
    5. root 23087 1 0 11:54 ? 00:00:01 /usr/local/redis-cluster/redis-node2/bin/redis-server 192.168.217.16:16380 [cluster]
    6. root 25714 16681 0 12:01 pts/0 00:00:00 grep --color=auto redis

    192.168.217.18:16380 ,在下线一个

    1. [root@master bin]# ./redis-cli -h 192.168.217.18 -p 16380 -c
    2. 192.168.217.18:16380> shutdown
    3. not connected>

    登录 192.168.217.18:1637,仍然可以查询到数据,可以看到数据是由192.168.217.17:16380提供:

    1. [root@master bin]# ./redis-cli -h 192.168.217.18 -p 16379 -c
    2. 192.168.217.18:16379> get AAA
    3. -> Redirected to slot [3205] located at 192.168.217.17:16380
    4. "jingqu"

    至此,Redis的集群搭建完毕了。

  • 相关阅读:
    深入了解接口测试:Postman 接口测试指南
    from PIL import Image,文字成图,ImageFont import jieba分词,input优雅python绘制图片
    Bootstrap Blazor 使用模板创建项目
    Rust中级教程:指针生态(引用、原始指针、智能指针)and内存操作(Stack、Heap)
    SOME/IP学习笔记3
    K8s种的service配置
    【JavaEE】_HTTP响应
    Web应用防火墙的重要性及基本原理
    R²决定系数
    深度学习:Pytorch分布式训练
  • 原文地址:https://blog.csdn.net/alwaysbefine/article/details/126776240