一、安装redis

1、安装gcc环境

yum install gcc-c++

2、下载源码包并解压

wget http://download.redis.io/releases/redis-3.2.4.tar.gz
tar -zxvf redis-3.2..tar.gz
cd redis-3.2.

3、安装ruby2.4.1并编译(一定要安装大于2.2.2的ruby,要不然redis在make的时候会报错)

wget http://ftp.ruby-lang.org/pub/ruby/2.4/ruby-2.4.1.tar.gz
tar -zxvf ruby-2.4.1.tar.gz
cd ruby-2.4.1
mkdir -p /usr/local/ruby
./configure --prefix=/usr/local/ruby
make && make install
ln -s /usr/local/ruby/bin/ruby /usr/bin/ruby

4、安装

make
make install PREFIX=/usr/local/redis
cp redis.conf /usr/local/redis/bin/redis.conf
chmod /usr/local/redis/bin/redis.conf

5、启动redis

./redis-server redis.conf

6、验证

[root@host---- bin]# ./redis-cli -p
127.0.0.1:> get
(error) ERR wrong number of arguments for 'get' command
127.0.0.1:> set aa hzb
OK
127.0.0.1:> get aa
"hzb"

二、搭建redis集群(集群模式)

1、在/usr/local/下面创建redis-cluster目录

mkdir -p /usr/local/redis-cluster

2、复制/usr/local/redis/bin目录到redis-cluster里面并重命名为redis1

cd /usr/local
cp -r redis/bin redis-cluster/redis1

3、将redis1在同目录复制3份分别起名redis2,redis3,redis4,redis5,redis6(必须6个节点以上才能创建集群)

[root@host---- redis-cluster]# pwd
/usr/local/redis-cluster
[root@host---- redis-cluster]# cp -r redis1 redis2
[root@host---- redis-cluster]# cp -r redis1 redis3
[root@host---- redis-cluster]# cp -r redis1 redis4
[root@host---- redis-cluster]# cp -r redis1 redis5
[root@host---- redis-cluster]# cp -r redis1 redis6

分别修改为redis-7001.conf,redis-7002.conf 后面依次到redis-7006.conf

以redis-7001为例:

daemonize yes
Port
logfile "./redis-7001.log"
protected-mode no
pidfile /var/run/redis_7001.pid
cluster-enabled yes

4、将redis-trib.rb复到制redis-cluster目录里面

[root@host---- redis-cluster]# cp /root/redis-3.2./src/redis-trib.rb .
[root@host---- redis-cluster]# ll
total
drwxr-xr-x root root Oct : redis1
drwxr-xr-x root root Oct : redis2
drwxr-xr-x root root Oct : redis3
drwxr-xr-x root root Oct : redis4
drwxr-xr-x root root Oct : redis5
drwxr-xr-x root root Oct : redis6
-rwxr-xr-x root root Oct : redis-trib.rb

5、安装gem

 yum install rubygems -y

6、安装ruby的redis包

gem install redis

上面这一步可能会出现:

ERROR: Loading command: install (LoadError)
cannot load such file -- zlib
ERROR: While executing gem ... (NoMethodError)
undefined method `invoke_with_build_args' for nil:NilClass

解决办法是:

  1. 进入ruby源码文件夹
  1. 安装ruby自身提供的zlib包
#cd ext/zlib
#ruby ./extconf.rb
#make
#make install
  1. 进入ruby源码文件夹
  1. 安装ruby自身提供的openssl包
#cd ext/openssl
#ruby ./extconf.rb
#make
#make install

如果执行make的时候报错:make: *** No rule to make target ,

错误原因:makefile里面的文件依赖关系有问题,导致头文件找不到。

解决办法:打开编译相关模块的makefile文件,找到.o文件的依赖关系,找到.h文件的路径,查看是否出错并改正。

原因分析:这种情况有时候是因为工程所在的目录改变,例如原来在C盘,现在移到了D盘,而makefile里面的.h文件路径没有改变

以上问题解决后再执行:

[root@host---- include]# gem install redis
Fetching: redis-4.0..gem (%)
Successfully installed redis-4.0.
Parsing documentation for redis-4.0.
Installing ri documentation for redis-4.0.
Done installing documentation for redis after seconds
gem installed

表示ruby的redis依赖包安装成功。

7、分别进入redis1,redis2,redis3,redis4,redis5,redis6里面执行

./redis-server redis-7001.conf
......

将四个节点启动

[root@host---- redis4]# ps -ef|grep redis
root : ? :: ./redis-server 127.0.0.1: [cluster]
root : ? :: ./redis-server 127.0.0.1: [cluster]
root : ? :: ./redis-server 127.0.0.1: [cluster]
root : ? :: ./redis-server 127.0.0.1: [cluster]
root : ? :: ./redis-server 127.0.0.1: [cluster]
root : ? :: ./redis-server 127.0.0.1: [cluster]
root : pts/ :: grep redis

8、用redis-trib.rb构建集群

cd /usr/local/redis-cluster/
[root@host---- redis-cluster]# ./redis-trib.rb create --replicas  172.16.80.177: 172.16.80.177: 172.16.80.177: 172.16.80.177:7004 172.16.80.177:7005 172.16.80.177:7006
>>> Creating cluster
[ERR] Sorry, can't connect to node 172.16.80.177:7001

以上错误原因是redis.conf里面未开起远程访问

打开每个redis里面的.conf文件,找到

# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 127.0.0.1 # Protected mode is a layer of security protection, in order to avoid that
# Redis instances left open on the internet are accessed and exploited.

将上面的bind 127.0.0.1改成bind 172.16.80.177(外部可以访问的ip),然后重启各服务

root              : ?        :: ./redis-server 172.16.80.177: [cluster]
root : ? :: ./redis-server 172.16.80.177: [cluster]
root : ? :: ./redis-server 172.16.80.177: [cluster]
root : ? :: ./redis-server 172.16.80.177: [cluster]
root : ? :: ./redis-server 172.16.80.177: [cluster]
root : ? :: ./redis-server 172.16.80.177: [cluster]
root : pts/ :: grep redis

再用redis-trib.rb创建集群:

[root@host---- redis-cluster]# ./redis-trib.rb create --replicas  172.16.80.177: 172.16.80.177: 172.16.80.177: 172.16.80.177: 172.16.80.177: 172.16.80.177:
>>> Creating cluster
>>> Performing hash slots allocation on nodes...
Using masters:
172.16.80.177:
172.16.80.177:
172.16.80.177:
Adding replica 172.16.80.177: to 172.16.80.177:
Adding replica 172.16.80.177: to 172.16.80.177:
Adding replica 172.16.80.177: to 172.16.80.177:
M: 40bbe8a979fe39f1f0941cc8e118443436fc71d8 172.16.80.177:
slots:- ( slots) master
M: e6b76ee1540c9be6c44abc3dec42378f8cfd0191 172.16.80.177:
slots:- ( slots) master
M: fdc248e24bd9f8690c652213a5781f4e567208e8 172.16.80.177:
slots:- ( slots) master
S: 38faede9dd348983b1b8b80edc975e8d34105e5c 172.16.80.177:
replicates 40bbe8a979fe39f1f0941cc8e118443436fc71d8
S: 38faede9dd348983b1b8b80edc975e8d34105e5c 172.16.80.177:
replicates e6b76ee1540c9be6c44abc3dec42378f8cfd0191
S: 38faede9dd348983b1b8b80edc975e8d34105e5c 172.16.80.177:
replicates fdc248e24bd9f8690c652213a5781f4e567208e8
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 172.16.80.177:)
M: 40bbe8a979fe39f1f0941cc8e118443436fc71d8 172.16.80.177:
slots:- ( slots) master
additional replica(s)
M: e6b76ee1540c9be6c44abc3dec42378f8cfd0191 172.16.80.177:
slots:- ( slots) master
additional replica(s)
S: 38faede9dd348983b1b8b80edc975e8d34105e5c 172.16.80.177:
slots: ( slots) slave
replicates 40bbe8a979fe39f1f0941cc8e118443436fc71d8
M: fdc248e24bd9f8690c652213a5781f4e567208e8 172.16.80.177:
slots:- ( slots) master
additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All slots covered.

至此,redis集群创建成功

9、验证集群是否成功

[root@host---- redis2]# ./redis-cli -h 172.16.80.177 -c -p
172.16.80.177:> set hello helloword
-> Redirected to slot [] located at 172.16.80.177:
OK
172.16.80.177:> get hello
-> Redirected to slot [] located at 172.16.80.177:
"helloword"

三、搭建redis集群(哨兵模式)

1、在/usr/local/下面创建redis-sentinel目录

mkdir -p /usr/local/redis-sentinel

2、将一大节中的/usr/local/redis/bin复制到redis-sentinel目录底下起名叫redis1,redis2,redis3,redis4,sentinel1,sentinel2

修改4个redis的配置文件(以redis1为例,将redis.conf重命名为redis-7001.conf)

daemonize yes
Port
#Bind 127.0.0.1
logfile "./redis-7001.log"
protected-mode no
cluster-config-file nodes-7001.conf

其他三个redis的端口号为7002,7003,7004

3、将redis解压包里面的sentinel.conf复制到sentinel1和sentinel2里面并重命名为sentinel-7005.conf,sentinel-7006.conf,并赋予可执行权限

修改配置(以sentinel-7005.conf为例)

port
sentinel monitor mymaster 172.16.80.177 2
protected-mode no

注:我们稍后要启动四个redis实例,其中端口为7001的redis设为master,其他三个设为slave 。所以my mymaster 后跟的是master的ip和端口,最后一个’2’代表我要启动只要有2个sentinel认为master下线,就认为该master客观下线,启动failover并选举产生新的master。通常最后一个参数不能多于启动的sentinel实例数。

4.启动4个redis实例

./redis-server redis-.conf
......
[root@host---- redis4]# ps -ef|grep redis
root : ? :: ./redis-server 172.16.80.177:
root : ? :: ./redis-server 172.16.80.177:
root : ? :: ./redis-server 172.16.80.177:
root : ? :: ./redis-server 172.16.80.177:

然后分别登陆7002 7003 7004三个实例,动态改变主从关系,成为7001的slave:

./redis-cli -h 172.16.80.177 -p
172.16.80.177:> SLAVEOF 172.16.80.177

5、以后台启动模式启动两个sentinel(哨兵):

[root@host---- sentinel1]# ./redis-sentinel sentinel-7005.conf &
[]
[root@host---- sentinel1]# :X Oct ::49.712 * Increased maximum number of open files to (it was originally set to ).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.2. (/) bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in sentinel mode
|`-._`-...-` __...-.``-._|'` _.-'| Port:
| `-._ `._ / _.-' | PID: 30038
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-' :X Oct ::49.713 # WARNING: The TCP backlog setting of cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of .
:X Oct ::49.744 # Sentinel ID is cbd44de127d84fd7d21f9e4a73e62b94c3b0ea00
:X Oct ::49.744 # +monitor master mymaster 172.16.80.177 quorum
:X Oct ::49.745 * +slave slave 172.16.80.177: 172.16.80.177 @ mymaster 172.16.80.177
:X Oct ::49.768 * +slave slave 172.16.80.177: 172.16.80.177 @ mymaster 172.16.80.177
:X Oct ::49.792 * +slave slave 172.16.80.177: 172.16.80.177 @ mymaster 172.16.80.177
[root@host---- redis-3.2.]# ps -ef|grep sentinel
root : pts/ :: ./redis-sentinel *: [sentinel]
root : pts/ :: ./redis-sentinel *: [sentinel]

6、验证

sentinel的基本命令

① INFO 
sentinel的基本状态信息 
②SENTINEL masters 
列出所有被监视的主服务器,以及这些主服务器的当前状态 
③ SENTINEL slaves 
列出给定主服务器的所有从服务器,以及这些从服务器的当前状态 
④SENTINEL get-master-addr-by-name 
返回给定名字的主服务器的 IP 地址和端口号 
⑤SENTINEL reset 
重置所有名字和给定模式 pattern 相匹配的主服务器。重置操作清除主服务器目前的所有状态, 包括正在执行中的故障转移, 并移除目前已经发现和关联的, 主服务器的所有从服务器和 Sentinel 。 
⑥SENTINEL failover 
当主服务器失效时, 在不询问其他 Sentinel 意见的情况下, 强制开始一次自动故障迁移,但是它会给其他sentinel发送一个最新的配置,其他sentinel会根据这个配置进行更新

[root@host---- redis1]# ./redis-cli -h 172.16.80.177 -p
172.16.80.177:>
172.16.80.177:> set name hzb
OK
172.16.80.177:> get name
"hzb"
172.16.80.177:> exit
[root@host---- redis1]# cd ../redis2
[root@host---- redis2]# ./redis-cli -h 172.16.80.177 -p
172.16.80.177:> get name
"hzb"
172.16.80.177:> set age
(error) READONLY You can't write against a read only slave.

上面可以看到slave是只读的

登录一个sentinel,用sentinel masters命令查看哪个是master

[root@host---- sentinel1]# ./redis-cli -h 172.16.80.177 -p
172.16.80.177:>
172.16.80.177:>
172.16.80.177:7005> sentinel masters
) ) "name"
) "mymaster"
) "ip"
4) "172.16.80.177"
5) "port"
6) "7001"

) "runid"
) "8b5d064e4195a1ee6279f5fb06f3fa88e4605854"
) "flags"
) "master"
) "link-pending-commands"
) ""
) "link-refcount"
) ""
) "last-ping-sent"
) ""
) "last-ok-ping-reply"
) ""
) "last-ping-reply"
) ""
) "down-after-milliseconds"
) ""
) "info-refresh"
) ""
) "role-reported"
) "master"
) "role-reported-time"
) ""
) "config-epoch"
) ""
) "num-slaves"
) ""
) "num-other-sentinels"
) ""
) "quorum"
) ""
) "failover-timeout"
) ""
) "parallel-syncs"
) ""

目前7001是master, 我们强制kill掉 7001 的进程以后,可以看到sentinel打出的信息:

:X  Oct ::24.138 # +sdown master mymaster 172.16.80.177
:X Oct ::24.557 # +new-epoch
:X Oct ::24.599 # +vote-for-leader 3948b4c445549c51d67bf829553372bca8bc67c8
:X Oct ::25.174 # +config-update-from sentinel 3948b4c445549c51d67bf829553372bca8bc67c8 172.16.80.177 @ mymaster 172.16.80.177
:X Oct ::25.174 # +switch-master mymaster 172.16.80.177 172.16.80.177
:X Oct ::25.175 * +slave slave 172.16.80.177: 172.16.80.177 @ mymaster 172.16.80.177
:X Oct ::25.175 * +slave slave 172.16.80.177: 172.16.80.177 @ mymaster 172.16.80.177
:X Oct ::25.175 * +slave slave 172.16.80.177: 172.16.80.177 @ mymaster 172.16.80.177
30325:X 22 Oct 22:51:55.202 # +sdown slave 172.16.80.177:7001 172.16.80.177 7001 @ mymaster 172.16.80.177 7003
[root@host---- sentinel1]# ./redis-cli -h 172.16.80.177 -p
172.16.80.177:> sentinel masters
) ) "name"
) "mymaster"
) "ip"
) "172.16.80.177"
) "port"
) ""
) "runid"
) "31721c8d1371697d78bf3c6acc2f8d4ba829825a"
) "flags"
) "master"
) "link-pending-commands"
) ""
) "link-refcount"
) ""
) "last-ping-sent"
) ""
) "last-ok-ping-reply"
) ""
) "last-ping-reply"
) ""
) "down-after-milliseconds"
) ""
) "info-refresh"
) ""
) "role-reported"
) "master"
) "role-reported-time"
) ""
) "config-epoch"
) ""
) "num-slaves"
) ""
) "num-other-sentinels"
) ""
) "quorum"
) ""
) "failover-timeout"
) ""
) "parallel-syncs"
) ""

上面显示了,master自动从7001变成了7003节点

再次将7001启动起来

:X  Oct ::15.544 * +convert-to-slave slave 172.16.80.177: 172.16.80.177  @ mymaster 172.16.80.177 

可以看到自动将7001变成了7003的slave

下面加一个批量启动和停止的脚本:

#!/bin/bash

start(){
./redis1/redis-server ./redis1/redis.conf &
./redis2/redis-server ./redis2/redis.conf &
./redis3/redis-server ./redis3/redis.conf &
./redis4/redis-server ./redis4/redis.conf &
./sentinel1/redis-sentinel ./sentinel1/sentinel.conf &
./sentinel2/redis-sentinel ./sentinel2/sentinel.conf & } stop(){
ps -ef | grep "redis" | grep -v "grep" |awk '{print $2}'| while read pid
do
C_PID=$(ps --no-heading $pid | wc -l)
echo "当前PID=$pid"
if [[ $C_PID == "" ]]; then
kill - $pid
echo "PID=$pid 已经结束"
else
echo "PID=$pid 不存在"
fi
done } case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
printf 'Usage: %s {start|stop|restart}\n'"$prog"
exit
;;
esac

centos6搭建redis集群搭建(单机多节点)的更多相关文章

  1. 基于Dokcer搭建Redis集群搭建(主从集群)

    最近陆陆续续有不少园友加我好友咨询 redis 集群搭建的问题,我觉得之前写的这篇 <基于Docker的Redis集群搭建> 文章一定是有问题了,所以我花了几分钟浏览之前的文章总结了下面几 ...

  2. redis集群(单机6节点实现)

    Redis集群搭建与简单使用 1.介绍安装环境与版本: 1)Redis使用的是Redis-3.2.8版本. 2)用一台虚拟机模拟6个节点,三个master节点,三个slave节点.虚拟机使用CentO ...

  3. 用C、python手写redis客户端,兼容redis集群 (-MOVED和-ASK),快速搭建redis集群

    想没想过,自己写一个redis客户端,是不是很难呢? 其实,并不是特别难. 首先,要知道redis服务端用的通信协议,建议直接去官网看,博客啥的其实也是从官网摘抄的,或者从其他博客抄的(忽略). 协议 ...

  4. Linux Redis集群搭建与集群客户端实现(Python)

    硬件环境 本文适用的硬件环境如下 Linux版本:CentOS release 6.7 (Final) Redis版本: Redis已经成功安装,安装路径为/home/idata/yangfan/lo ...

  5. Linux Redis集群搭建与集群客户端实现

    硬件环境 本文适用的硬件环境如下 Linux版本:CentOS release 6.7 (Final) Redis版本: Redis已经成功安装,安装路径为/home/idata/yangfan/lo ...

  6. redis集群搭建及设置账户(转)

    Redis集群搭建以及为集群设置密码 介绍安装环境与版本 用两台虚拟机模拟6个节点,一台机器3个节点,创建出3 master.3 salve 环境. redis 采用 redis-3.2.4 版本. ...

  7. redis 集群搭建: redis-cluster

    前言 redis数据存储在内存中, 就会受到内存的限制, 大家都知道, 一台电脑, 硬盘可以有1T, 但是内存, 没有听说有1T的内存吧. 那如果数据非常多, 超过一台电脑的内存空间, 怎么办呢? 正 ...

  8. 手动搭建 redis 集群

    转自http://meia.fun/article/1544161420745 手动搭建 redis 集群 redis 基本命令: 启动 redis 服务:redis-server xxx.conf ...

  9. Windows下 搭建redis集群

    Windows下搭建redis集群教程 一,redis集群介绍 Redis cluster(redis集群)是在版本3.0后才支持的架构,和其他集群一样,都是为了解决单台服务器不够用的情况,也防止了主 ...

随机推荐

  1. Roadblocks--poj3255(次短路)

    题目链接 求次短路的问题: dist[i][0]和dist[i][1]表示从起点1到i的距离和从起点n到i的距离: 次短路要比最短路大但小于其他路: 每条路1--n的距离都可以用dist[i][0] ...

  2. lua打包exe【转】

    可以使用srlua这个工具把lua脚本打包成exe,提供了lua 5.2.5.1.5.0的版本对应的源码. 不过我懒得编译了,发现“白的菜”替懒人编译好了(感谢),点击下载. 解压后出现“luapac ...

  3. python startswith() 函数

    startswith() 作用:判断字符串是否以指定字符或子字符串开头 >>> s = "my name is ming" >>> >&g ...

  4. Celery框架简单实例

    Python 中可以使用Celery框架 Celery框架是提供异步任务处理的框架,有两种用法,一种:应用程式发布任务消息,后台Worker监听执行,好处在于不影响应用程序继续执行.第二种,设置定时执 ...

  5. PAT Battle Over Cities [未作]

    1013 Battle Over Cities (25)(25 分) It is vitally important to have all the cities connected by highw ...

  6. notification 是同步的

    所有notification的观察者执行之后,post notification的函数才会往下执行.

  7. soft nofile

    原创文章,转载请注明出处:http://jameswxx.iteye.com/blog/2096461 写这个文章是为了以正视听,网上的文章人云亦云到简直令人发指.到底最大文件数被什么限制了?too ...

  8. java中Integer 和String 之间的转换

    java中Integer 和String 之间的转换 将数组转换成字符串:char[] array = {'a','b','c','d','e'};String str = new String(ar ...

  9. C#导出Excel总结

    一.asp.net中导出Execl的方法:在asp.net中导出Execl有两种方法,一种是将导出的文件存放在服务器某个文件夹下面,然后将文件地址输出在浏览器上:一种是将文件直接将文件输出流写给浏览器 ...

  10. MFC工具栏的创建、设计与使用实例

    本文通过实例说明MFC工具栏的创建.设计和使用方法,包括三个demo.       demo1:创建一个工具栏 C++代码 //摘抄自MSDN demo1 (创建一个工具栏) 1.Create a t ...