Redis cluster 部署

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

环境准备:
| 类别 | 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 赞助。

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

相关帖子

欢迎来到这里!

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

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

推荐标签 标签

  • WordPress

    WordPress 是一个使用 PHP 语言开发的博客平台,用户可以在支持 PHP 和 MySQL 数据库的服务器上架设自己的博客。也可以把 WordPress 当作一个内容管理系统(CMS)来使用。WordPress 是一个免费的开源项目,在 GNU 通用公共许可证(GPLv2)下授权发布。

    46 引用 • 114 回帖 • 169 关注
  • Sillot

    Insights(注意当前设置 master 为默认分支)

    汐洛彖夲肜矩阵(Sillot T☳Converbenk Matrix),致力于服务智慧新彖乄,具有彖乄驱动、极致优雅、开发者友好的特点。其中汐洛绞架(Sillot-Gibbet)基于自思源笔记(siyuan-note),前身是思源笔记汐洛版(更早是思源笔记汐洛分支),是智慧新录乄终端(多端融合,移动端优先)。

    主仓库地址:Hi-Windom/Sillot

    文档地址:sillot.db.sc.cn

    注意事项:

    1. ⚠️ 汐洛仍在早期开发阶段,尚不稳定
    2. ⚠️ 汐洛并非面向普通用户设计,使用前请了解风险
    3. ⚠️ 汐洛绞架基于思源笔记,开发者尽最大努力与思源笔记保持兼容,但无法实现 100% 兼容
    29 引用 • 25 回帖 • 125 关注
  • 爬虫

    网络爬虫(Spider、Crawler),是一种按照一定的规则,自动地抓取万维网信息的程序。

    106 引用 • 275 回帖
  • V2EX

    V2EX 是创意工作者们的社区。这里目前汇聚了超过 400,000 名主要来自互联网行业、游戏行业和媒体行业的创意工作者。V2EX 希望能够成为创意工作者们的生活和事业的一部分。

    16 引用 • 236 回帖 • 249 关注
  • 锤子科技

    锤子科技(Smartisan)成立于 2012 年 5 月,是一家制造移动互联网终端设备的公司,公司的使命是用完美主义的工匠精神,打造用户体验一流的数码消费类产品(智能手机为主),改善人们的生活质量。

    4 引用 • 31 回帖 • 3 关注
  • Gzip

    gzip (GNU zip)是 GNU 自由软件的文件压缩程序。我们在 Linux 中经常会用到后缀为 .gz 的文件,它们就是 Gzip 格式的。现今已经成为互联网上使用非常普遍的一种数据压缩格式,或者说一种文件格式。

    9 引用 • 12 回帖 • 177 关注
  • 数据库

    据说 99% 的性能瓶颈都在数据库。

    345 引用 • 754 回帖
  • Hexo

    Hexo 是一款快速、简洁且高效的博客框架,使用 Node.js 编写。

    22 引用 • 148 回帖 • 7 关注
  • Kafka

    Kafka 是一种高吞吐量的分布式发布订阅消息系统,它可以处理消费者规模的网站中的所有动作流数据。 这种动作(网页浏览,搜索和其他用户的行动)是现代系统中许多功能的基础。 这些数据通常是由于吞吐量的要求而通过处理日志和日志聚合来解决。

    36 引用 • 35 回帖
  • 机器学习

    机器学习(Machine Learning)是一门多领域交叉学科,涉及概率论、统计学、逼近论、凸分析、算法复杂度理论等多门学科。专门研究计算机怎样模拟或实现人类的学习行为,以获取新的知识或技能,重新组织已有的知识结构使之不断改善自身的性能。

    77 引用 • 37 回帖
  • OkHttp

    OkHttp 是一款 HTTP & HTTP/2 客户端库,专为 Android 和 Java 应用打造。

    16 引用 • 6 回帖 • 93 关注
  • LaTeX

    LaTeX(音译“拉泰赫”)是一种基于 ΤΕΧ 的排版系统,由美国计算机学家莱斯利·兰伯特(Leslie Lamport)在 20 世纪 80 年代初期开发,利用这种格式,即使使用者没有排版和程序设计的知识也可以充分发挥由 TeX 所提供的强大功能,能在几天,甚至几小时内生成很多具有书籍质量的印刷品。对于生成复杂表格和数学公式,这一点表现得尤为突出。因此它非常适用于生成高印刷质量的科技和数学类文档。

    12 引用 • 59 回帖 • 1 关注
  • 开源

    Open Source, Open Mind, Open Sight, Open Future!

    413 引用 • 3590 回帖
  • 知乎

    知乎是网络问答社区,连接各行各业的用户。用户分享着彼此的知识、经验和见解,为中文互联网源源不断地提供多种多样的信息。

    10 引用 • 66 回帖
  • 架构

    我们平时所说的“架构”主要是指软件架构,这是有关软件整体结构与组件的抽象描述,用于指导软件系统各个方面的设计。另外还有“业务架构”、“网络架构”、“硬件架构”等细分领域。

    142 引用 • 442 回帖
  • wolai

    我来 wolai:不仅仅是未来的云端笔记!

    2 引用 • 14 回帖 • 4 关注
  • IBM

    IBM(国际商业机器公司)或万国商业机器公司,简称 IBM(International Business Machines Corporation),总公司在纽约州阿蒙克市。1911 年托马斯·沃森创立于美国,是全球最大的信息技术和业务解决方案公司,拥有全球雇员 30 多万人,业务遍及 160 多个国家和地区。

    17 引用 • 53 回帖 • 143 关注
  • 单点登录

    单点登录(Single Sign On)是目前比较流行的企业业务整合的解决方案之一。SSO 的定义是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统。

    9 引用 • 25 回帖 • 2 关注
  • SQLite

    SQLite 是一个进程内的库,实现了自给自足的、无服务器的、零配置的、事务性的 SQL 数据库引擎。SQLite 是全世界使用最为广泛的数据库引擎。

    4 引用 • 7 回帖 • 4 关注
  • Eclipse

    Eclipse 是一个开放源代码的、基于 Java 的可扩展开发平台。就其本身而言,它只是一个框架和一组服务,用于通过插件组件构建开发环境。

    76 引用 • 258 回帖 • 625 关注
  • Google

    Google(Google Inc.,NASDAQ:GOOG)是一家美国上市公司(公有股份公司),于 1998 年 9 月 7 日以私有股份公司的形式创立,设计并管理一个互联网搜索引擎。Google 公司的总部称作“Googleplex”,它位于加利福尼亚山景城。Google 目前被公认为是全球规模最大的搜索引擎,它提供了简单易用的免费服务。不作恶(Don't be evil)是谷歌公司的一项非正式的公司口号。

    49 引用 • 192 回帖 • 1 关注
  • webpack

    webpack 是一个用于前端开发的模块加载器和打包工具,它能把各种资源,例如 JS、CSS(less/sass)、图片等都作为模块来使用和处理。

    42 引用 • 130 回帖 • 251 关注
  • VirtualBox

    VirtualBox 是一款开源虚拟机软件,最早由德国 Innotek 公司开发,由 Sun Microsystems 公司出品的软件,使用 Qt 编写,在 Sun 被 Oracle 收购后正式更名成 Oracle VM VirtualBox。

    10 引用 • 2 回帖 • 18 关注
  • 面试

    面试造航母,上班拧螺丝。多面试,少加班。

    326 引用 • 1395 回帖
  • 旅游

    希望你我能在旅途中找到人生的下一站。

    98 引用 • 903 回帖
  • GitLab

    GitLab 是利用 Ruby 一个开源的版本管理系统,实现一个自托管的 Git 项目仓库,可通过 Web 界面操作公开或私有项目。

    46 引用 • 72 回帖
  • Openfire

    Openfire 是开源的、基于可拓展通讯和表示协议 (XMPP)、采用 Java 编程语言开发的实时协作服务器。Openfire 的效率很高,单台服务器可支持上万并发用户。

    6 引用 • 7 回帖 • 121 关注