搭建高可用的redis集群,避免standalone模式带给你的苦难

  现在项目上用redis的话,很少说不用集群的情况,毕竟如果生产上只有一台redis会有极大的风险,比如机器挂掉,或者内存爆掉,就比如我们生产环境

曾今也遭遇到这种情况,导致redis内存不够挂掉的情况,当然这些都是我们及其不能容忍的,第一个必须要做到高可靠,其次才是高性能,好了,下面我来

逐一搭建一下。

 

一:Redis集群搭建

1. 下载

   首先去官网下载较新的3.2.0版本,下载方式还是非常简单的,比如官网介绍的这样。

$ wget http://download.redis.io/releases/redis-3.2.0.tar.gz
$ tar xzf redis-3.2.0.tar.gz
$ cd redis-3.2.0
$ make

 

2. redis配置

 由于我们要做集群,而且还要redis自带的redis-trib.rb 能正常运行,我们需要在集群中开启三台master,三台slave,所以这里我需要建立6个文件

夹,而且文件夹的名称就使用端口地址的名字,比如:6389. 6380....6384。

3. config配置。

   现在directory的分布情况大致如上图,接下来要做的事情就是配置redis.conf了,在这里需要配置四个选项。。。

 

<1> port  端口地址,比如6380文件夹下面的port就是6380,

# Accept connections on the specified port, default is 6379 (IANA #815344).
# If port 0 is specified Redis will not listen on a TCP socket.
port 6379

 

<2> cluster-enabled 和 cluster-config-file

       这个顾名思义,首先需要开启redis的cluster模式,然后配置一个cluster-config-file文件,这个文件用于存放redis的实时信息,redis会动态追加和修

改这个conf下面的内容信息,不过要记住,这个nodes-6379.conf 可以根据 端口文件夹依次配置,比如6380文件夹可以改成nodes-6380.conf这样。。。

# Normal Redis instances can't be part of a Redis Cluster; only nodes that are
# started as cluster nodes can. In order to start a Redis instance as a
# cluster node enable the cluster support uncommenting the following:
#
cluster-enabled yes# Every cluster node has a cluster configuration file. This file is not
# intended to be edited by hand. It is created and updated by Redis nodes.
# Every Redis Cluster node requires a different cluster configuration file.
# Make sure that instances running in the same system do not have
# overlapping cluster configuration file names.
#
cluster-config-file nodes-6379.conf

 

<3> directory

      为了方便管理,我这里配置的root目录取决于在哪个文件夹,比如6380下面我的dir就是: dir ./6380/

# Note that you must specify a directory here, not a file name.
dir ./6379/

 

<4> protected-mode

      这个是redis 3.2 才追加的一个功能,从功能注释中,我们就可以发现,这个默认就是不让外界可以访问redis,所以这里我们就改为no,可以远程访问。

# By default protected mode is enabled. You should disable it only if
# you are sure you want clients from other hosts to connect to Redis
# even if no authentication is configured, nor a specific set of interfaces
# are explicitly listed using the "bind" directive.
protected-mode no

 

ok,到现在为止,我们的config就修改完毕了,其他端口的文件夹也可以依次配置之~

 

二:开启redis

    到现在为止,各个端口文件夹都配置成功了,接下来准备开启了,真的好么么哒~~~,窗口太多,有点萌萌的。

 

 

接下来我们可以看一下,在6379下面是不是有生成node-6379.conf文件,比如下面:

 

三:配置redis-trib.rb

   因为redis-trib.rb是ruby写的,而我们的电脑肯定是没有ruby和一些配置依赖项,不过没关系,有强大的yum安装,一切都不是问题。

 

1. 执行replicas命令

[jack@localhost ~]$ cluster/redis-trib.rb  create --replicas 1 192.168.161.133:6379 192.168.161.133:6380 192.168.161.133:6381 192.168.161.133:6382 192.168.161.133:6383 192.168.161.133:6384
/usr/bin/env: ruby: No such file or directory
[jack@localhost ~]$ 

   可以看到ruby是没有安装的,所以下一步我们要安装ruby了。。。

 

2. 安装ruby 【一定要是管理员权限哦】

[jack@localhost ~]$ sudo
usage: sudo [-D level] -h | -K | -k | -V
usage: sudo -v [-AknS] [-D level] [-g groupname|#gid] [-p prompt] [-u username|#uid]
usage: sudo -l[l] [-AknS] [-D level] [-g groupname|#gid] [-p prompt] [-U username] [-u user name|#uid] [-g groupname|#gid] [command]
usage: sudo [-AbEHknPS] [-r role] [-t type] [-C fd] [-D level] [-ggroupname|#gid] [-p prompt] [-u user name|#uid] [-g groupname|#gid][VAR=value] [-i|-s] [<command>]
usage: sudo -e [-AknS] [-r role] [-t type] [-C fd] [-D level] [-ggroupname|#gid] [-p prompt] [-u user name|#uid] file ...
[jack@localhost ~]$ su
Password: 
jacsu: incorrect password
[jack@localhost ~]$ yum install ruby
Loaded plugins: fastestmirror, refresh-packagekit, security
You need to be root to perform this command.
[jack@localhost ~]$ jack
bash: jack: command not found
[jack@localhost ~]$ su
Password: 
[root@localhost jack]# yum install ruby
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile* base: mirror.bit.edu.cn* extras: mirror.bit.edu.cn* updates: mirror.bit.edu.cn
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package ruby.x86_64 0:1.8.7.374-4.el6_6 will be installed
--> Processing Dependency: ruby-libs = 1.8.7.374-4.el6_6 for package: ruby-1.8.7.374-4.el6_6.x86_64
--> Processing Dependency: libruby.so.1.8()(64bit) for package: ruby-1.8.7.374-4.el6_6.x86_64
--> Running transaction check
---> Package ruby-libs.x86_64 0:1.8.7.374-4.el6_6 will be installed
--> Processing Dependency: libreadline.so.5()(64bit) for package: ruby-libs-1.8.7.374-4.el6_6.x86_64
--> Running transaction check
---> Package compat-readline5.x86_64 0:5.2-17.1.el6 will be installed
--> Finished Dependency ResolutionDependencies Resolved================================================================================Package                Arch         Version                   Repository  Size
================================================================================
Installing:ruby                   x86_64       1.8.7.374-4.el6_6         base       538 k
Installing for dependencies:compat-readline5       x86_64       5.2-17.1.el6              base       130 kruby-libs              x86_64       1.8.7.374-4.el6_6         base       1.7 MTransaction Summary
================================================================================
Install       3 Package(s)Total download size: 2.3 M
Installed size: 7.8 M
Is this ok [y/N]: y
Downloading Packages:
(1/3): compat-readline5-5.2-17.1.el6.x86_64.rpm          | 130 kB     00:00     
(2/3): ruby-1.8.7.374-4.el6_6.x86_64.rpm                 | 538 kB     00:00     
(3/3): ruby-libs-1.8.7.374-4.el6_6.x86_64.rpm            | 1.7 MB     00:02     
--------------------------------------------------------------------------------
Total                                           747 kB/s | 2.3 MB     00:03     
warning: rpmts_HdrFromFdno: Header V3 RSA/SHA256 Signature, key ID c105b9de: NOKEY
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
Importing GPG key 0xC105B9DE:Userid : CentOS-6 Key (CentOS 6 Official Signing Key) <centos-6-key@centos.org>Package: centos-release-6-5.el6.centos.11.1.x86_64 (@anaconda-CentOS-201311272149.x86_64/6.5)From   : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
Is this ok [y/N]: y
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running TransactionInstalling : compat-readline5-5.2-17.1.el6.x86_64                         1/3 Installing : ruby-libs-1.8.7.374-4.el6_6.x86_64                           2/3 Installing : ruby-1.8.7.374-4.el6_6.x86_64                                3/3 Verifying  : compat-readline5-5.2-17.1.el6.x86_64                         1/3 Verifying  : ruby-libs-1.8.7.374-4.el6_6.x86_64                           2/3 Verifying  : ruby-1.8.7.374-4.el6_6.x86_64                                3/3 Installed:ruby.x86_64 0:1.8.7.374-4.el6_6                                               Dependency Installed:compat-readline5.x86_64 0:5.2-17.1.el6  ruby-libs.x86_64 0:1.8.7.374-4.el6_6 Complete!
[root@localhost jack]# 

 

3. 安装rubygems

     接着我们再次运行 replicas命令,看看会怎么样。。。 

[root@localhost jack]# cluster/redis-trib.rb  create --replicas 1 192.168.161.133:6379 192.168.161.133:6380 192.168.161.133:6381 192.168.161.133:6382 192.168.161.133:6383 192.168.161.133:6384
cluster/redis-trib.rb:24:in `require': no such file to load -- rubygems (LoadError)from cluster/redis-trib.rb:24
[root@localhost jack]# 

     结果苦逼了,还是报错,看提示貌似是少了一个rubygems,这次我们还是通过yum安装。

[root@localhost jack]# yum install -y rubygems
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile* base: mirror.bit.edu.cn* extras: mirror.bit.edu.cn* updates: mirror.bit.edu.cn
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package rubygems.noarch 0:1.3.7-5.el6 will be installed
--> Processing Dependency: ruby-rdoc for package: rubygems-1.3.7-5.el6.noarch
--> Running transaction check
---> Package ruby-rdoc.x86_64 0:1.8.7.374-4.el6_6 will be installed
--> Processing Dependency: ruby-irb = 1.8.7.374-4.el6_6 for package: ruby-rdoc-1.8.7.374-4.el6_6.x86_64
--> Running transaction check
---> Package ruby-irb.x86_64 0:1.8.7.374-4.el6_6 will be installed
--> Finished Dependency ResolutionDependencies Resolved================================================================================Package           Arch           Version                    Repository    Size
================================================================================
Installing:rubygems          noarch         1.3.7-5.el6                base         207 k
Installing for dependencies:ruby-irb          x86_64         1.8.7.374-4.el6_6          base         317 kruby-rdoc         x86_64         1.8.7.374-4.el6_6          base         381 kTransaction Summary
================================================================================
Install       3 Package(s)Total download size: 905 k
Installed size: 3.0 M
Downloading Packages:
(1/3): ruby-irb-1.8.7.374-4.el6_6.x86_64.rpm             | 317 kB     00:00     
(2/3): ruby-rdoc-1.8.7.374-4.el6_6.x86_64.rpm            | 381 kB     00:00     
(3/3): rubygems-1.3.7-5.el6.noarch.rpm                   | 207 kB     00:00     
--------------------------------------------------------------------------------
Total                                           625 kB/s | 905 kB     00:01     
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running TransactionInstalling : ruby-irb-1.8.7.374-4.el6_6.x86_64                            1/3 Installing : ruby-rdoc-1.8.7.374-4.el6_6.x86_64                           2/3 Installing : rubygems-1.3.7-5.el6.noarch                                  3/3 Verifying  : ruby-rdoc-1.8.7.374-4.el6_6.x86_64                           1/3 Verifying  : ruby-irb-1.8.7.374-4.el6_6.x86_64                            2/3 Verifying  : rubygems-1.3.7-5.el6.noarch                                  3/3 Installed:rubygems.noarch 0:1.3.7-5.el6                                                 Dependency Installed:ruby-irb.x86_64 0:1.8.7.374-4.el6_6    ruby-rdoc.x86_64 0:1.8.7.374-4.el6_6   Complete!
[root@localhost jack]# 

 

4. 安装redis驱动

 

   还是继续不死心,我们继续运行replicas命令命令。

[root@localhost jack]# cluster/redis-trib.rb  create --replicas 1 192.168.161.133:6379 192.168.161.133:6380 192.168.161.133:6381 192.168.161.133:6382 192.168.161.133:6383 192.168.161.133:6384
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- redis (LoadError)from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
    from cluster/redis-trib.rb:25
[root@localhost jack]# 

 

卧槽,还是有问题,这次貌似是一个gem_original_require没有安装,当然这个一般是说ruby版本太老了,所以现在亲要么升级ruby版本,要么直接安装

ruby的redis驱动。

[root@localhost jack]# gem install redis
Successfully installed redis-3.3.0
1 gem installed
Installing ri documentation for redis-3.3.0...
Installing RDoc documentation for redis-3.3.0...

 

6. 运行replicas命令

   终于貌似所有的依赖我们都解决了,接下来就可以真的执行了,真是一把辛酸泪啊。。。操。。。

[root@localhost jack]# cluster/redis-trib.rb  create --replicas 1 192.168.161.133:6379 192.168.161.133:6380 192.168.161.133:6381 192.168.161.133:6382 192.168.161.133:6383 192.168.161.133:6384
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.161.133:6379
192.168.161.133:6380
192.168.161.133:6381
Adding replica 192.168.161.133:6382 to 192.168.161.133:6379
Adding replica 192.168.161.133:6383 to 192.168.161.133:6380
Adding replica 192.168.161.133:6384 to 192.168.161.133:6381
M: fdae457e803e2e04a7c549c69b44a2beefdae3bc 192.168.161.133:6379slots:0-5460 (5461 slots) master
M: 74285cee0fa65e64b443ecc630e447a8a65ee9f8 192.168.161.133:6380slots:5461-10922 (5462 slots) master
M: 14ed067a1b85044325d5800fa8479a6b4e41a10c 192.168.161.133:6381slots:10923-16383 (5461 slots) master
S: c50b1ff1fe3a00b16703f3b5705d2dafe046240b 192.168.161.133:6382replicates fdae457e803e2e04a7c549c69b44a2beefdae3bc
S: 8f7bd92f7ffb48e327820693a5820b5be7ea5556 192.168.161.133:6383replicates 74285cee0fa65e64b443ecc630e447a8a65ee9f8
S: 4a85ed078b4c99afad7f3a9a8df09082c681a649 192.168.161.133:6384replicates 14ed067a1b85044325d5800fa8479a6b4e41a10c
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.161.133:6379)
M: fdae457e803e2e04a7c549c69b44a2beefdae3bc 192.168.161.133:6379slots:0-5460 (5461 slots) master
M: 74285cee0fa65e64b443ecc630e447a8a65ee9f8 192.168.161.133:6380slots:5461-10922 (5462 slots) master
M: 14ed067a1b85044325d5800fa8479a6b4e41a10c 192.168.161.133:6381slots:10923-16383 (5461 slots) master
M: c50b1ff1fe3a00b16703f3b5705d2dafe046240b 192.168.161.133:6382slots: (0 slots) masterreplicates fdae457e803e2e04a7c549c69b44a2beefdae3bc
M: 8f7bd92f7ffb48e327820693a5820b5be7ea5556 192.168.161.133:6383slots: (0 slots) masterreplicates 74285cee0fa65e64b443ecc630e447a8a65ee9f8
M: 4a85ed078b4c99afad7f3a9a8df09082c681a649 192.168.161.133:6384slots: (0 slots) masterreplicates 14ed067a1b85044325d5800fa8479a6b4e41a10c
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@localhost jack]# 

从上面可以看到,trib程序告知我们是否用 6379,6380,6381 作为主, 6382,6383,6384作为从。。然后我就恩准了。。就这样我们的集群

就创建好了,好了,这篇就讲到这里,也不是特别复杂吧~~~

 

转载于:https://www.cnblogs.com/huangxincheng/p/5615037.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/457710.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

php连接MySQL

先说一下我踩了三天的坑 我在这之前用mysql申请了一个数据库&#xff0c;然后用php连接一直连接不上&#xff0c;试了许多办法&#xff0c;都不行 下载MySQL时显示3306端口被占用&#xff0c;所以选了3307 一直报错&#xff0c;三天后&#xff0c;终于知道了&#xff0c;你用的…

SVG动画

动画原理 SVG动画&#xff0c;就是元素的属性值关于时间的变化。 如下图来说&#xff0c;元素的某个属性值的起始值&#xff08;from&#xff09;到结束值&#xff08;to&#xff09;在一个时间段&#xff08;duration&#xff09;根据时间函数&#xff08;timing-function&…

bootstrap弹出的模态框水平垂直居中的实现

学习javascript从入门到放弃&#xff01;&#xff0c;这是第一篇随笔&#xff0c;经验不足&#xff0c;如有不当之处&#xff0c;还望指出。好了废话不多说直接切入正题吧 1.bootstrap默认的model写法&#xff1a; //触发模态框的button <button data-toggle"modal&quo…

正则表达式 PHP

正则表达式的常用函数&#xff1a; 先写一个函数show&#xff0c;用来输出数组&#xff0c;使代码看起来方便&#xff0c;且把经常重复用到的量放在前面 function show($varnull) {if(empty($var))//检测变量是否为空{echo null;}elseif (is_array($var)||is_object($var))//…

iis发布网站怎么支持.json文件

转载于:https://www.cnblogs.com/daizhipeng/p/5622839.html

浏览器渲染机制

1. 明白浏览器渲染的目的 可访问性&#xff08;Accessability&#xff09;、加载性能和重构灵活性一直是前端工程师们关心的主题。 其中加载性能与浏览器的渲染机制深深挂钩&#xff0c;弄明白浏览器背后的渲染机制&#xff0c;才能在日常的前端的开发中明白如何进行性能优化。…

win10使用Composer-Setup安装Composer以及使用Composer安装Yii2最新版

1&#xff1a;下载 ca-bundle.crt和cacert.pem(见导航栏——文件) 将这两个文件放在php目录下 2:php.ini中添加上述两个文件的路径 curl.cainfoC:/xampp/php/ca-bundle.crtopenssl.cafileC:/xampp/php/ca-bundle.crt 写到最后一行就可以 3&#xff1a;下载Composer-Setup.exe h…

hive求差集和交集

2019独角兽企业重金招聘Python工程师标准>>> 用的要求总数和统计数&#xff1a; 总数的概念是利用安卓ID&#xff0c;就以为这把两个月前的安卓ID统统的统计一遍&#xff0c;如果没有出现&#xff0c;恰好在今天出现了&#xff0c;那么当前的这个用户就是新增的…

渐变色--浏览器兼容性

说明&#xff1a; 1.360兼容模式效果不好 2.可以直接用不同浏览器打开这个页面查看效果 <!DOCTYPE html><html><head lang"en"> <meta charset"UTF-8"> <title></title> <style> body{ …

tcp前4字节消息长度_网络基础篇之TCP

​网络分层什么是 TCP TCP 是面向连接的、可靠的、基于字节流的传输层通信协议。- 面向连接&#xff1a;通过三次握手建立一对一的连接&#xff08; UDP 协议 可以一个主机同时向多个主机发送消息&#xff0c;即一对多&#xff09;&#xff1b;- 可靠的&#xff1a;通过序号、校…

使用Servlet实现用户注册

1、用户注册页面代码 <% page language"java" contentType"text/html; charsetUTF-8"pageEncoding"UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd&q…

android studio运行手机时出错怎么解决_小程序 android ios h5解决方案

你现在开发android,ios,小程序用什么工具&#xff0c;怎么开发的&#xff1f;还在单个端的开发吗&#xff1f;今天我们主要讨论的是一次开发多端使用的技术&#xff0c;也是这两年比较流行的开发方向。现在的终端太多了&#xff0c;app两个端android和ios,小程序有微信&#xf…

C语言 第八章 函数、指针与宏

一、函数 函数是一个包含完成一定功能的执行代码段。我们可以把函数看成一个"黑盒子", 你只要将数据送进去就能得到结果, 而函数内部究竟是如何工作的的, 外部程序是不知道的。外部程序所知道的仅限于输入给函数什么以及函数输出什么。函数提供了编制程序的手段,使之…

promise用法_Promise的秘密

写在前面本篇文章将会带大家从分解promise入手&#xff0c;一步步实现一个promise。但阅读之前需要比较熟练地了解了解用法&#xff0c;结合用法看文章可能更容易理解。结构先看一下简单的用法。const promise new Promise((resolve, reject) > {setTimeout(() > {resol…

Web Service 性能测试工具比较

背景 希望选择一款Web Service性能测试工具&#xff0c;能真实模拟大量用户访问网站时的请求&#xff0c;从而获取服务器当前的请求处理能力&#xff08;请求数/秒&#xff09;。以微信服务器为例&#xff0c;每个用户用独立的登录token&#xff0c;做各种操作&#xff0c;比如…

cocoapods的安装(这真是一个神奇的东西,每次安装的方法都不一样,而且很容易出现各种各样的错误)...

文章开始之前&#xff0c;建议安装一个显示网速的插件&#xff0c;不然你不知道到底有没有下载&#xff0c;也让生活有一点盼头 1.因为众所周知的原因&#xff08;我dang的行为真的是让人失望&#xff09;&#xff0c;先更换一下ruby镜像源 $ gem sources --remove https://rub…

转】MyEclipse使用总结——在MyEclipse中设置jsp页面为默认utf-8编码

原博文出自于&#xff1a;http://www.cnblogs.com/xdp-gacl/p/3496161.html        感谢&#xff01; 在MyEclispe中创建Jsp页面&#xff0c;Jsp页面的默认编码是“ISO-8859-1”&#xff0c;如下图所示&#xff1a; 在这种编码下编写中文是没有办法保存Jsp页面的&#…

华为机试题【10】-求数字基root

题目描述&#xff1a; 求整数的Root:给定正整数,求每位数字之和;如果和不是一位数,则重复; 输入&#xff1a;输入任意一个或多个整数 输出&#xff1a;输出各位数字之和,直到和为个位数为止(输入异常,则返回-1)&#xff0c;多行&#xff0c;每行对应一个输入数据的结果。 样例…

python update skeleton 不自动_python编程笔记(1)-数据类型

断断续续学了一些&#xff0c;但是后边一直没有使用&#xff0c;就又忘记了。为了缩短学习周期&#xff0c;这次学习的方法&#xff1a;先学主要框架&#xff08;数据类型、条件、循环、函数、类、numpy、pandas、sklearn&#xff09;&#xff0c;后期直接看朋友code 实操了&am…

如何访问另一台电脑的共享文件夹_如何远程控制另一台电脑

在工作中&#xff0c;我们时常需要远程控制一下另一台电脑&#xff0c;如果这两台计算机在局域网内可以通过远程桌面轻松实现&#xff0c;如果这两台计算机不在局域网内则通常需要借助第三方软件来实现。远程桌面使用分三步&#xff1a;1、被控制端要开启远程桌面功能&#xff…