Redis cluster 部署

本贴最后更新于 1792 天前,其中的信息可能已经时移世易

环境准备:
| 类别 | IP| 规格 | 主机名 |
| 1| 192.168.43.120| 4C8G |redis-cluster-01|
| 2| 192.168.43.121| 4C8G |redis-cluster-02|
| 3| 192.168.43.122| 4C8G |redis-cluster-03|
注意:系统均为 centos7.6,均已关闭 firewalld 和 selinux。
集群架构是三台机器,每台机器部署两个节点,端口分别是 6379 和 6380.

第一步:下载 Redis 离线二进制包

# wget http://download.redis.io/releases/redis-4.0.10.tar.gz

第二步:解压

# tar xf redis-4.0.10.tar.gz # mv redis-4.0.10 /usr/local/redis

第三步:编译&&编译安装

# cd /usr/local/redis # make # make install

第四步:将二进制可执行文件拷贝至/usr/local/bin 或者/usr/bin 目录下

# cd src/ # cp redis-trib.rb /usr/local/bin/

第五步:检查

# cd ../utils/ # ls -l 总用量 76 -rw-rw-r-- 1 root root 593 6月 13 2018 build-static-symbols.tcl -rw-rw-r-- 1 root root 1303 6月 13 2018 cluster_fail_time.tcl -rw-rw-r-- 1 root root 1070 6月 13 2018 corrupt_rdb.c drwxrwxr-x 2 root root 4096 6月 13 2018 create-cluster -rwxrwxr-x 1 root root 2137 6月 13 2018 generate-command-help.rb drwxrwxr-x 3 root root 4096 6月 13 2018 graphs drwxrwxr-x 2 root root 4096 6月 13 2018 hashtable drwxrwxr-x 2 root root 4096 6月 13 2018 hyperloglog -rwxrwxr-x 1 root root 9567 6月 13 2018 install_server.sh drwxrwxr-x 2 root root 4096 6月 13 2018 lru -rw-rw-r-- 1 root root 1277 6月 13 2018 redis-copy.rb -rwxrwxr-x 1 root root 1352 6月 13 2018 redis_init_script -rwxrwxr-x 1 root root 1047 6月 13 2018 redis_init_script.tpl -rw-rw-r-- 1 root root 1762 6月 13 2018 redis-sha1.rb drwxrwxr-x 2 root root 4096 6月 13 2018 releasetools -rwxrwxr-x 1 root root 3787 6月 13 2018 speed-regression.tcl -rwxrwxr-x 1 root root 693 6月 13 2018 whatisdoing.sh

第六步:部署节点 1

# ./install_server.sh Welcome to the redis service installer This script will help you easily set up a running redis server Please select the redis port for this instance: [6379] Selecting default: 6379 Please select the redis config file name [/etc/redis/6379.conf] /data/redis/conf/6379.conf Please select the redis log file name [/var/log/redis_6379.log] /data/redis/logs/6379.log Please select the data directory for this instance [/var/lib/redis/6379] /data/redis/data/6379 Please select the redis executable path [/usr/local/bin/redis-server] Selected config: Port : 6379 Config file : /data/redis/6379.conf Log file : /data/redis/logs/6379.log Data dir : /data/redis/6379 Executable : /usr/local/bin/redis-server Cli Executable : /usr/local/bin/redis-cli Is this ok? Then press ENTER to go on or Ctrl-C to abort. Copied /tmp/6379.conf => /etc/init.d/redis_6379 Installing service... Successfully added to chkconfig! Successfully added to runlevels 345! Starting Redis server... Installation successful!

注意:
①.如果是部署单机 Redis,到这里就算结束了。
②.由于部署的集群是单机双实例(如果有条件的话,可以一台机器只部署一个节点),所以第六步和第七步都要执行。
第七步:部署节点 2

# ./install_server.sh Welcome to the redis service installer This script will help you easily set up a running redis server Please select the redis port for this instance: [6379] 6380 Please select the redis config file name [/etc/redis/6380.conf] /data/redis/conf/6380.conf Please select the redis log file name [/var/log/redis_6380.log] /data/redis/logs/6380.log Please select the data directory for this instance [/var/lib/redis/6380] /data/redis/data/6380 Please select the redis executable path [/usr/local/bin/redis-server] Selected config: Port : 6380 Config file : /data/redis/6380.conf Log file : /data/redis/logs/6380.log Data dir : /data/redis/6380 Executable : /usr/local/bin/redis-server Cli Executable : /usr/local/bin/redis-cli Is this ok? Then press ENTER to go on or Ctrl-C to abort. Copied /tmp/6380.conf => /etc/init.d/redis_6380 Installing service... Successfully added to chkconfig! Successfully added to runlevels 345! Starting Redis server... Installation successful!

第八步:检查

# tree -L 3 . └── redis ├── 6379 ├── 6379.conf ├── 6380 ├── 6380.conf └── logs ├── 6379.log └── 6380.log 4 directories, 4 files

另外的两台机器按照上面的步骤(第一步到第八步)操作即可。
第九步:配置

# sed -i "s/# cluster-enabled yes/cluster-enabled yes/g;s/bind 127.0.0.1/bind 192.168.43.120 127.0.0.1/g" /data/redis/*.conf

注意:在另外两台机器同样执行那条命令,只需将 192.168.43.120 换成本机的 IP 即可;

第十步:重启 Redis 使配置生效

# /etc/init.d/redis_6379 restart

第十一步:检查
redis-cluster-01:

# netstat -lntp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 9485/redis-server 1 tcp 0 0 192.168.43.120:6379 0.0.0.0:* LISTEN 9485/redis-server 1 tcp 0 0 127.0.0.1:6380 0.0.0.0:* LISTEN 9495/redis-server 1 tcp 0 0 192.168.43.120:6380 0.0.0.0:* LISTEN 9495/redis-server 1 tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 5617/sshd tcp 0 0 127.0.0.1:16379 0.0.0.0:* LISTEN 9485/redis-server 1 tcp 0 0 192.168.43.120:16379 0.0.0.0:* LISTEN 9485/redis-server 1 tcp 0 0 127.0.0.1:16380 0.0.0.0:* LISTEN 9495/redis-server 1 tcp 0 0 192.168.43.120:16380 0.0.0.0:* LISTEN 9495/redis-server 1

redis-cluster-02:

# netstat -lntp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 9485/redis-server 1 tcp 0 0 192.168.43.121:6379 0.0.0.0:* LISTEN 9485/redis-server 1 tcp 0 0 127.0.0.1:6380 0.0.0.0:* LISTEN 9495/redis-server 1 tcp 0 0 192.168.43.121:6380 0.0.0.0:* LISTEN 9495/redis-server 1 tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 5617/sshd tcp 0 0 127.0.0.1:16379 0.0.0.0:* LISTEN 9485/redis-server 1 tcp 0 0 192.168.43.121:16379 0.0.0.0:* LISTEN 9485/redis-server 1 tcp 0 0 127.0.0.1:16380 0.0.0.0:* LISTEN 9495/redis-server 1 tcp 0 0 192.168.43.121:16380 0.0.0.0:* LISTEN 9495/redis-server 1

redis-cluster-03:

# netstat -lntp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 9485/redis-server 1 tcp 0 0 192.168.43.122:6379 0.0.0.0:* LISTEN 9485/redis-server 1 tcp 0 0 127.0.0.1:6380 0.0.0.0:* LISTEN 9495/redis-server 1 tcp 0 0 192.168.43.122:6380 0.0.0.0:* LISTEN 9495/redis-server 1 tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 5617/sshd tcp 0 0 127.0.0.1:16379 0.0.0.0:* LISTEN 9485/redis-server 1 tcp 0 0 192.168.43.122:16379 0.0.0.0:* LISTEN 9485/redis-server 1 tcp 0 0 127.0.0.1:16380 0.0.0.0:* LISTEN 9495/redis-server 1 tcp 0 0 192.168.43.122:16380 0.0.0.0:* LISTEN 9495/redis-server 1

第十二步:Redis 优化
在我们启动 Redis 的时候,查看 Redis 的启动日志,官方很友好的将一些需要优化的都给我们明显的标出来了,我们只需要按照那个修改即可,如有别的需求可以自行查看官方相关参数进行修改;如下即是 Redis 启动时的优化提示:

9375:M 15 May 11:14:28.574 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 9375:M 15 May 11:14:28.574 # Server initialized 9375:M 15 May 11:14:28.574 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect. 9375:M 15 May 11:14:28.574 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
# echo 1024 > /proc/sys/net/core/somaxconn # echo "vm.overcommit_memory = 1" >>/etc/sysctl.conf # sysctl -p # echo never > /sys/kernel/mm/transparent_hugepage/enabled # echo "echo never > /sys/kernel/mm/transparent_hugepage/enabled" >>/etc/rc.local

另外两台机器同样执行上面的操作。
第十三步:安装 gem

# yum -y install ruby ruby-devel rubygems rpm-build # gem install -l redis-3.0.0.gem Successfully installed redis-3.0.0 Parsing documentation for redis-3.0.0 Installing ri documentation for redis-3.0.0 1 gem installed

上面的 gem 文件我已上传到百度云网盘

链接:https://pan.baidu.com/s/1B-Zk1KAPjUrJzCpvAZaU_w 密码:1kbv

第十四步:创建集群

# redis-trib.rb create --replicas 1 192.168.43.120:6379 192.168.43.121:6379 192.168.43.122:6379 192.168.43.120:6380 192.168.43.121:6380 192.168.43.122:6380 >>> Creating cluster auses ArgumentError in the next release warning: this causes ArgumentError in the next release >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 192.168.43.120:6379 192.168.43.121:6379 192.168.43.122:6379 Adding replica 192.168.43.121:6380 to 192.168.43.120:6379 Adding replica 192.168.43.122:6380 to 192.168.43.121:6379 Adding replica 192.168.43.120:6380 to 192.168.43.122:6379 M: 79e82a8357f67ac623479cc76575b39c9f7b8b39 192.168.43.120:6379 slots:0-5460 (5461 slots) master M: 234af44758281d646f81ee482edd23a0f6c97237 192.168.43.121:6379 slots:5461-10922 (5462 slots) master M: 99d4866d25028faae3b03367a39871ab642704ab 192.168.43.122:6379 slots:10923-16383 (5461 slots) master S: 19773196edde889421ef4d124ee1b60c2a02d5d5 192.168.43.120:6380 replicates 99d4866d25028faae3b03367a39871ab642704ab S: 4dde88648d44c87ff646b134b0e024043eecda40 192.168.43.121:6380 replicates 79e82a8357f67ac623479cc76575b39c9f7b8b39 S: 686cf842893bdf6e79e10bba28fcc9388848f14f 192.168.43.122:6380 replicates 234af44758281d646f81ee482edd23a0f6c97237 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.43.120:6379) M: 79e82a8357f67ac623479cc76575b39c9f7b8b39 192.168.43.120:6379 slots:0-5460 (5461 slots) master 1 additional replica(s) S: 4dde88648d44c87ff646b134b0e024043eecda40 192.168.43.121:6380 slots: (0 slots) slave replicates 79e82a8357f67ac623479cc76575b39c9f7b8b39 M: 234af44758281d646f81ee482edd23a0f6c97237 192.168.43.121:6379 slots:5461-10922 (5462 slots) master 1 additional replica(s) S: 686cf842893bdf6e79e10bba28fcc9388848f14f 192.168.43.122:6380 slots: (0 slots) slave replicates 234af44758281d646f81ee482edd23a0f6c97237 S: 19773196edde889421ef4d124ee1b60c2a02d5d5 192.168.43.120:6380 slots: (0 slots) slave replicates 99d4866d25028faae3b03367a39871ab642704ab M: 99d4866d25028faae3b03367a39871ab642704ab 192.168.43.122:6379 slots:10923-16383 (5461 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.

当出现两个 OK 就是集群已经创建完毕。
第十五步:Redis 集群设置密码(按照生产需要自行决定)

# redis-cli -h 192.168.43.120 -p 6379 -c --raw 192.168.43.120:6379> config set requirepass Tnobn?dju89U OK 192.168.43.120:6379> auth Tnobn?dju89U OK 192.168.43.120:6379> exit # redis-cli -h 192.168.43.120 -p 6379 -c --raw 192.168.43.120:6379> config get requirepass NOAUTH Authentication required. 192.168.43.120:6379> auth Tnobn?dju89U OK 192.168.43.120:6379> config get requirepass requirepass Tnobn?dju89U 192.168.43.120:6379> exit
config set requirepass gnjRg54ish auth gnjRg54ish config set masterauth gnjRg54ish

上面的配置只是临时的,一旦 Redis 重启就会失效;永久修改只需要在 redis.conf 中添加如下配置,重启即可。

masterauth 密码 requirepass 密码

Redis 配置文件释义:

Redis的配置   daemonize:如需要在后台运行,把该项的值改为yes   pdifile:把pid文件放在/var/run/redis.pid,可以配置到其他地址   bind:指定redis只接收来自该IP的请求,如果不设置,那么将处理所有请求,在生产环节中最好设置该项   port:监听端口,默认为6379   timeout:设置客户端连接时的超时时间,单位为秒   loglevel:等级分为4级,debug,revbose,notice和warning。生产环境下一般开启notice   logfile:配置log文件地址,默认使用标准输出,即打印在命令行终端的端口上   database:设置数据库的个数,默认使用的数据库是0   save:设置redis进行数据库镜像的频率   rdbcompression:在进行镜像备份时,是否进行压缩   dbfilename:镜像备份文件的文件名   dir:数据库镜像备份的文件放置的路径   slaveof:设置该数据库为其他数据库的从数据库   masterauth:当主数据库连接需要密码验证时,在这里设定   requirepass:设置客户端连接后进行任何其他指定前需要使用的密码   maxclients:限制同时连接的客户端数   maxmemory:设置redis能够使用的最大内存   appendonly:开启appendonly模式后,redis会把每一次所接收到的写操作都追加到appendonly.aof文件中,当redis重新启动时,会从该文件恢复出之前的状态   appendfsync:设置appendonly.aof文件进行同步的频率   vm_enabled:是否开启虚拟内存支持   vm_swap_file:设置虚拟内存的交换文件的路径   vm_max_momery:设置开启虚拟内存后,redis将使用的最大物理内存的大小,默认为0   vm_page_size:设置虚拟内存页的大小   vm_pages:设置交换文件的总的page数量   vm_max_thrrads:设置vm IO同时使用的线程数量
  • Redis

    Redis 是一个开源的使用 ANSI C 语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value 数据库,并提供多种语言的 API。从 2010 年 3 月 15 日起,Redis 的开发工作由 VMware 主持。从 2013 年 5 月开始,Redis 的开发由 Pivotal 赞助。

    286 引用 • 248 回帖
  • 集群
    29 引用 • 65 回帖 • 1 关注

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...

推荐标签 标签

  • 生活

    生活是指人类生存过程中的各项活动的总和,范畴较广,一般指为幸福的意义而存在。生活实际上是对人生的一种诠释。生活包括人类在社会中与自己息息相关的日常活动和心理影射。

    230 引用 • 1454 回帖
  • 微信

    腾讯公司 2011 年 1 月 21 日推出的一款手机通讯软件。用户可以通过摇一摇、搜索号码、扫描二维码等添加好友和关注公众平台,同时可以将自己看到的精彩内容分享到微信朋友圈。

    133 引用 • 796 回帖 • 1 关注
  • Spring

    Spring 是一个开源框架,是于 2003 年兴起的一个轻量级的 Java 开发框架,由 Rod Johnson 在其著作《Expert One-On-One J2EE Development and Design》中阐述的部分理念和原型衍生而来。它是为了解决企业应用开发的复杂性而创建的。框架的主要优势之一就是其分层架构,分层架构允许使用者选择使用哪一个组件,同时为 JavaEE 应用程序开发提供集成的框架。

    948 引用 • 1460 回帖
  • Laravel

    Laravel 是一套简洁、优雅的 PHP Web 开发框架。它采用 MVC 设计,是一款崇尚开发效率的全栈框架。

    20 引用 • 23 回帖 • 739 关注
  • PostgreSQL

    PostgreSQL 是一款功能强大的企业级数据库系统,在 BSD 开源许可证下发布。

    22 引用 • 22 回帖
  • Lute

    Lute 是一款结构化的 Markdown 引擎,支持 Go 和 JavaScript。

    28 引用 • 197 回帖 • 32 关注
  • 房星科技

    房星网,我们不和没有钱的程序员谈理想,我们要让程序员又有理想又有钱。我们有雄厚的房地产行业线下资源,遍布昆明全城的 100 家门店、四千地产经纪人是我们坚实的后盾。

    6 引用 • 141 回帖 • 592 关注
  • GraphQL

    GraphQL 是一个用于 API 的查询语言,是一个使用基于类型系统来执行查询的服务端运行时(类型系统由你的数据定义)。GraphQL 并没有和任何特定数据库或者存储引擎绑定,而是依靠你现有的代码和数据支撑。

    4 引用 • 3 回帖 • 5 关注
  • 创造

    你创造的作品可能会帮助到很多人,如果是开源项目的话就更赞了!

    184 引用 • 1015 回帖
  • 服务

    提供一个服务绝不仅仅是简单的把硬件和软件累加在一起,它包括了服务的可靠性、服务的标准化、以及对服务的监控、维护、技术支持等。

    41 引用 • 24 回帖 • 3 关注
  • Office

    Office 现已更名为 Microsoft 365. Microsoft 365 将高级 Office 应用(如 Word、Excel 和 PowerPoint)与 1 TB 的 OneDrive 云存储空间、高级安全性等结合在一起,可帮助你在任何设备上完成操作。

    5 引用 • 34 回帖
  • ZeroNet

    ZeroNet 是一个基于比特币加密技术和 BT 网络技术的去中心化的、开放开源的网络和交流系统。

    1 引用 • 21 回帖 • 645 关注
  • Facebook

    Facebook 是一个联系朋友的社交工具。大家可以通过它和朋友、同事、同学以及周围的人保持互动交流,分享无限上传的图片,发布链接和视频,更可以增进对朋友的了解。

    4 引用 • 15 回帖 • 442 关注
  • 阿里云

    阿里云是阿里巴巴集团旗下公司,是全球领先的云计算及人工智能科技公司。提供云服务器、云数据库、云安全等云计算服务,以及大数据、人工智能服务、精准定制基于场景的行业解决方案。

    84 引用 • 324 回帖
  • abitmean

    有点意思就行了

    36 关注
  • JVM

    JVM(Java Virtual Machine)Java 虚拟机是一个微型操作系统,有自己的硬件构架体系,还有相应的指令系统。能够识别 Java 独特的 .class 文件(字节码),能够将这些文件中的信息读取出来,使得 Java 程序只需要生成 Java 虚拟机上的字节码后就能在不同操作系统平台上进行运行。

    180 引用 • 120 回帖
  • Git

    Git 是 Linux Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件。

    211 引用 • 358 回帖 • 1 关注
  • AWS
    11 引用 • 28 回帖 • 12 关注
  • SQLServer

    SQL Server 是由 [微软] 开发和推广的关系数据库管理系统(DBMS),它最初是由 微软、Sybase 和 Ashton-Tate 三家公司共同开发的,并于 1988 年推出了第一个 OS/2 版本。

    21 引用 • 31 回帖
  • GitBook

    GitBook 使您的团队可以轻松编写和维护高质量的文档。 分享知识,提高团队的工作效率,让用户满意。

    3 引用 • 8 回帖
  • golang

    Go 语言是 Google 推出的一种全新的编程语言,可以在不损失应用程序性能的情况下降低代码的复杂性。谷歌首席软件工程师罗布派克(Rob Pike)说:我们之所以开发 Go,是因为过去 10 多年间软件开发的难度令人沮丧。Go 是谷歌 2009 发布的第二款编程语言。

    498 引用 • 1395 回帖 • 249 关注
  • 开源中国

    开源中国是目前中国最大的开源技术社区。传播开源的理念,推广开源项目,为 IT 开发者提供了一个发现、使用、并交流开源技术的平台。目前开源中国社区已收录超过两万款开源软件。

    7 引用 • 86 回帖
  • 大数据

    大数据(big data)是指无法在一定时间范围内用常规软件工具进行捕捉、管理和处理的数据集合,是需要新处理模式才能具有更强的决策力、洞察发现力和流程优化能力的海量、高增长率和多样化的信息资产。

    93 引用 • 113 回帖 • 1 关注
  • PWA

    PWA(Progressive Web App)是 Google 在 2015 年提出、2016 年 6 月开始推广的项目。它结合了一系列现代 Web 技术,在网页应用中实现和原生应用相近的用户体验。

    14 引用 • 69 回帖 • 176 关注
  • Sym

    Sym 是一款用 Java 实现的现代化社区(论坛/BBS/社交网络/博客)系统平台。

    下一代的社区系统,为未来而构建

    524 引用 • 4601 回帖 • 702 关注
  • Oracle

    Oracle(甲骨文)公司,全称甲骨文股份有限公司(甲骨文软件系统有限公司),是全球最大的企业级软件公司,总部位于美国加利福尼亚州的红木滩。1989 年正式进入中国市场。2013 年,甲骨文已超越 IBM,成为继 Microsoft 后全球第二大软件公司。

    107 引用 • 127 回帖 • 346 关注
  • jsDelivr

    jsDelivr 是一个开源的 CDN 服务,可为 npm 包、GitHub 仓库提供免费、快速并且可靠的全球 CDN 加速服务。

    5 引用 • 31 回帖 • 103 关注