一、 准备材料

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

服务器

IP address

操作系统

位数

Redis 版本

 

CNT06CAH05

192.168.3.47

CentOS 6.5

x64

Redis-3.2.6

sentinel 001::28339

sentinel 002:28349

sentinel 003:28359

CNT06CAH06

192.168.3.48

CentOS 6.5

x64

Redis-3.2.6

master:16339

CNT06CAH07

192.168.3.49

CentOS 6.5

x64

Redis-3.2.6

slave 1:16349

CNT06CAH08

192.168.3.50

CentOS 6.5

x64

Redis-3.2.6

slave 2:16359

 

二、安装依赖软件

2.1 Redis运行依赖的软件,主要有下面七个,安装命令如下。

安装 ccp

root >> yum install ccp

安装 binutils

root >> yum install binutils

安装 glibc-kernheaders

root >> yum install glibc-kernheaders

安装 glibc-common

root >> yum install glibc-common

安装 glibc-devel

root >> yum install glibc-devel

安装 gcc

root >> yum install gcc

安装 make

root >> yum install make

 

2.2 为了节约时间,我们可以创建批量执行命令文件,然后分别复制到四台服务器上面执行,如下

2.2.1【CNT06CAH05】

创建一个目录,用来存储我们需要执行的脚本文件

root >> mkdir /opt/cmd

进入cmd目录

root >> cd /otp/cmd

创建批量安装的脚本文件,名字自定,但是文件后缀必须是.sh

root >> vim install_redis_refs.sh

按CTRL+i,进入vim的编辑模式,输入批量安装redis依赖软件的命令如下

echo 'begin to install 01 plugin';

yum install cpp -y;

echo 'yum finish install 01 plugin';

sleep 3;

echo 'begin to install 02 plugin';

yum install binutils -y;

echo 'yum finish install 02 plugin';

sleep 3;

echo 'begin to install 03 plugin';

yum install glibc-kernheaders -y;

echo 'yum finish install 03 plugin';

sleep 3;

echo 'begin to install 04 plugin';

yum install glibc-common -y;

echo 'yum finish install 04 plugin';

sleep 3;

echo 'begin to install 05 plugin';

yum install glibc-devel -y;

echo 'yum finish install 05 plugin';

sleep 3;

echo 'begin to install 06 plugin';

yum install gcc -y;

echo 'yum finish install 06 plugin';

sleep 3;

echo 'begin to install 07 plugin';

yum install make -y;

echo 'yum finish install 07 plugin';

sleep 3;

echo 'all plugin had installed completed';

read;

最后,记得按ESC,然后输入:wq,完成文件的保存和退出,如下:

root >> :wq

所有步骤,截图如下

Redis Sentinel 集群安装 step by step Nosql 第1张

Redis Sentinel 集群安装 step by step Nosql 第2张

首先,给脚本文件,指定运行权限

root >> chmod -R 777 /opt/cmd/install_redis_refs.sh

Redis Sentinel 集群安装 step by step Nosql 第3张

执行脚本install_redis_refs.sh,如下

安装过程中是全自动的,无需干涉,只要等待即可。

root >> /opt/cmd/install_redis_refs.sh

Redis Sentinel 集群安装 step by step Nosql 第4张

安装完,提示如下

all plugin had installed completed

Redis Sentinel 集群安装 step by step Nosql 第5张

 

2.2.2【CNT06CAH06】

我们只需复制批量安装脚本install_redis_refs.sh到此机器,指派文件权限后,执行脚本即可。

创建目录/opt/cmd

root >> mkdir /opt/cmd

Redis Sentinel 集群安装 step by step Nosql 第6张

通过WinSCP软件,复制install_redis_refs.sh到CNT06CAH06的目录cmd

Redis Sentinel 集群安装 step by step Nosql 第7张

复制完之后,就可以看到CNT06CAH06的目录cmd下面,多了一个脚本文件install_redis_refs.sh

首先,给脚本文件,指定运行权限

root >> chmod -R 777 /opt/cmd/install_redis_refs.sh

Redis Sentinel 集群安装 step by step Nosql 第8张

执行脚本install_redis_refs.sh,如下

安装过程中是全自动的,无需干涉,只要等待即可。

root >> /opt/cmd/install_redis_refs.sh

Redis Sentinel 集群安装 step by step Nosql 第9张

 

2.2.3【CNT06CAH07】

安装redis的依赖软件,具体步骤和上面“2.2.2【CNT06CAH06】”相同,故不再累述,只截图最后的安装结果如下

Redis Sentinel 集群安装 step by step Nosql 第10张

 

2.2.4【CNT06CAH08】

安装redis的依赖软件,具体步骤和上面“2.2.2【CNT06CAH06】”相同,故不再累述,只截图最后的安装结果如下

Redis Sentinel 集群安装 step by step Nosql 第11张

 

 

三、安装Redis 集群

3.1 配置Reids节点(CNT06CAH06/CNT06CAH07/CNT06CAH08

3.1.1 【CNT06CAH06】:配置Redis Master节点

创建redis安装目录如下

root >> mkdir /opt/redis

Redis Sentinel 集群安装 step by step Nosql 第12张

通过WinSCP复制redis的压缩文件到服务器目录/opt/redis

Redis Sentinel 集群安装 step by step Nosql 第13张

解压redis文件到当前目录,如下

tar zxvf /opt/redis/redis-3.2.6.tar.gz -C /opt/redis/

Redis Sentinel 集群安装 step by step Nosql 第14张

通过WinSCP,查看解压后的文件

Redis Sentinel 集群安装 step by step Nosql 第15张

配置redis.conf文件,设置master节点的相关参数

root >> mkdir /opt/redis/data -- 后面配置数据文件的存放目录用到

root >> mkdir /opt/redis/log -- 后面配置日志文件的存放目录用到

root >> vim /opt/redis/redis-3.2.6/redis.conf

Redis Sentinel 集群安装 step by step Nosql 第16张

修改的主要配置参数,主要如下

#bind 127.0.0.1 -- old

bind 0.0.0.0 -- new : 表示允许所有客户端IP访问

#port 6379 -- old

port 16339 -- new: redis master的访问端口

# logfile "" -- old

logfile "/opt/redis/log/redis.log" -- new: 日志文件的存放目录

# dir ./

dir /opt/redis/data/ -- new: 数据文件的存放目录

最后记得保存redis.conf文件

root >> :wq

 

3.1.2 【CNT06CAH07】:配置Redis Slave 1节点

创建redis安装目录如下

root >> mkdir /opt/redis

Redis Sentinel 集群安装 step by step Nosql 第17张

通过WinSCP复制redis的压缩文件到服务器目录/opt/redis

Redis Sentinel 集群安装 step by step Nosql 第18张

解压redis文件到当前目录,如下

tar zxvf /opt/redis/redis-3.2.6.tar.gz -C /opt/redis/

Redis Sentinel 集群安装 step by step Nosql 第19张

通过WinSCP,查看解压后的文件

Redis Sentinel 集群安装 step by step Nosql 第20张

配置redis.conf文件,设置slave 1节点的相关参数

root >> mkdir /opt/redis/data -- 后面配置数据文件的存放目录用到

root >> mkdir /opt/redis/log -- 后面配置日志文件的存放目录用到

root >> vim /opt/redis/redis-3.2.6/redis.conf

Redis Sentinel 集群安装 step by step Nosql 第21张

修改的主要配置参数,主要如下

slaveof 192.168.3.48 16339 -- new: 设置当前机器为master节点的Slave节点

#bind 127.0.0.1 -- old

bind 0.0.0.0 -- new : 表示允许所有客户端IP访问

#port 6379 -- old

port 16349 -- new: redis slave 1的访问端口

# logfile "" -- old

logfile "/opt/redis/log/redis.log" -- new: 日志文件的存放目录

# dir ./

dir /opt/redis/data/ -- new: 数据文件的存放目录

最后记得保存redis.conf文件

root >> :wq

 

3.1.3 【CNT06CAH08】:配置Redis Slave 2节点

创建redis安装目录如下

root >> mkdir /opt/redis

Redis Sentinel 集群安装 step by step Nosql 第22张

通过WinSCP复制redis的压缩文件到服务器目录/opt/redis

Redis Sentinel 集群安装 step by step Nosql 第23张

解压redis文件到当前目录,如下

tar zxvf /opt/redis/redis-3.2.6.tar.gz -C /opt/redis/

Redis Sentinel 集群安装 step by step Nosql 第24张

通过WinSCP,查看解压后的文件

Redis Sentinel 集群安装 step by step Nosql 第25张

配置redis.conf文件,设置slave 2节点的相关参数

root >> mkdir /opt/redis/data -- 后面配置数据文件的存放目录用到

root >> mkdir /opt/redis/log -- 后面配置日志文件的存放目录用到

root >> vim /opt/redis/redis-3.2.6/redis.conf

Redis Sentinel 集群安装 step by step Nosql 第26张

修改的主要配置参数,主要如下

slaveof 192.168.3.48 16339 -- new: 设置当前机器为master节点的Slave节点

#bind 127.0.0.1 -- old

bind 0.0.0.0 -- new : 表示允许所有客户端IP访问

#port 6379 -- old

port 16359 -- new: redis slave 2的访问端口

# logfile "" -- old

logfile "/opt/redis/log/redis.log" -- new: 日志文件的存放目录

# dir ./

dir /opt/redis/data/ -- new: 数据文件的存放目录

最后记得保存redis.conf文件

root >> :wq

 

3.2 启动Reids节点(CNT06CAH06/CNT06CAH07/CNT06CAH08

3.2.1 【CNT06CAH06】:启动Redis Master节点

首先,执行make test命令,测试检查redis安装包文件是否OK

root >> cd /opt/redis/redis-3.2.6/src

root >> make test

Redis Sentinel 集群安装 step by step Nosql 第27张

检查test结果OK,如下图

Redis Sentinel 集群安装 step by step Nosql 第28张

最后, 启动master节点的reids

备注: 加&表示后台进程执行

root >> cd /opt/redis/redis-3.2.6/src

root >> ./redis-server ../redis.conf &

Redis Sentinel 集群安装 step by step Nosql 第29张

Redis Sentinel 集群安装 step by step Nosql 第30张

 

3.2.2 【CNT06CAH07】:启动Redis Slave 1节点

首先,执行make test命令,测试检查redis安装包文件是否OK

root >> cd /opt/redis/redis-3.2.6/src

root >> make test

Redis Sentinel 集群安装 step by step Nosql 第31张

检查test结果OK,如下图

Redis Sentinel 集群安装 step by step Nosql 第32张

最后, 启动slave 1节点的reids

备注: 加&表示后台进程执行

root >> cd /opt/redis/redis-3.2.6/src

root >> ./redis-server ../redis.conf &

Redis Sentinel 集群安装 step by step Nosql 第33张

Redis Sentinel 集群安装 step by step Nosql 第34张

 

3.2.3 【CNT06CAH08】:启动Redis Slave2节点

首先,执行make test命令,测试检查redis安装包文件是否OK

root >> cd /opt/redis/redis-3.2.6/src

root >> make test

Redis Sentinel 集群安装 step by step Nosql 第35张

检查test结果OK,如下图

Redis Sentinel 集群安装 step by step Nosql 第36张

最后, 启动slave 2节点的reids

备注: 加&表示后台进程执行

root >> cd /opt/redis/redis-3.2.6/src

root >> ./redis-server ../redis.conf &

Redis Sentinel 集群安装 step by step Nosql 第37张

Redis Sentinel 集群安装 step by step Nosql 第38张

 

3.3 测试Reids节点(CNT06CAH06/CNT06CAH07/CNT06CAH08

3.3.1 【CNT06CAH06】:测试Redis Master节点

win>> F:

win >> cd "F:\缓存技术\Redis\client\Redis-x64-3.2.100"

win >> redis-cli -p 16339 -h 192.168.3.48

Redis Sentinel 集群安装 step by step Nosql 第39张

win >> info

查询cluster节点的信息,如下

Redis Sentinel 集群安装 step by step Nosql 第40张

添加一个key,如下

win >> keys * # 查看key的清单

win >> dbsize # 查看key的总数

Redis Sentinel 集群安装 step by step Nosql 第41张

3.3.2 【CNT06CAH07】:测试Redis Slave 1节点

win>> F:

win >> cd "F:\缓存技术\Redis\client\Redis-x64-3.2.100"

win >> redis-cli -p 16349 -h 192.168.3.49

Redis Sentinel 集群安装 step by step Nosql 第42张

win >> info

查询cluster节点的信息,如下

Redis Sentinel 集群安装 step by step Nosql 第43张

添加一个key,如下

# win >> set k_1 "hello redis world" # 注意: slave 节点不能写数据,只能读数据

win >> keys * # 查看key的清单

win >> dbsize # 查看key的总数

Redis Sentinel 集群安装 step by step Nosql 第44张

注意: slave 节点不能写数据,只能读数据

Redis Sentinel 集群安装 step by step Nosql 第45张

3.3.3 【CNT06CAH08】:测试Redis Slave 2节点

同上:略

 

3.3.4 【CNT06CAH06】:压力测试

参考网址 :Redis 性能测试:http://www.redis.net.cn/tutorial/3521.html

root >> redis-benchmark -p 16339 -h 192.168.3.48 -t set,lpush,get -n 100000 -q

Redis Sentinel 集群安装 step by step Nosql 第46张

 

 

四、安装Sentinel集群

4.1 配置Sentinel节点(CNT06CAH06/CNT06CAH07/CNT06CAH08

4.1.1 【CNT06CAH05】:配置Redis Sentinel节点

创建redis安装目录如下

root >> mkdir /opt/redis

Redis Sentinel 集群安装 step by step Nosql 第47张

通过WinSCP复制redis的压缩文件到服务器目录/opt/redis

Redis Sentinel 集群安装 step by step Nosql 第48张 s

解压redis文件到当前目录,如下

tar zxvf /opt/redis/redis-3.2.6.tar.gz -C /opt/redis/

Redis Sentinel 集群安装 step by step Nosql 第49张

通过WinSCP,查看解压后的文件

Redis Sentinel 集群安装 step by step Nosql 第50张

因为我们要模拟三台Sentinel哨兵节点,所以我这里复制三份sentinel.conf文件。

(为了用三个sentinel实例表示三台sentinel服务器节点,这里因为机器有限放在一台机器上,生产要分别部署到不同sentinel机器)

root >> cd /opt/redis/redis-3.2.6

root >> cp ./sentinel.conf ./sentinel_001.conf

root >> cp ./sentinel.conf ./sentinel_002.conf

root >> cp ./sentinel.conf ./sentinel_003.conf

Redis Sentinel 集群安装 step by step Nosql 第51张

 

4.1.2 配置sentinel_001.conf文件,设置sentinel 001节点的相关参数

root >> mkdir -p /opt/redis/tmp/sentinel_001 -- Sentinel服务运行时使用的临时文件夹

root >> vim /opt/redis/redis-3.2.6/sentinel_001.conf

修改的主要配置参数,主要如下

# bind 127.0.0.1 192.168.1.1 -- old

bind 0.0.0.0 -- new : 表示任意客户端可以访问

# sentinel monitor mymaster 127.0.0.1 6379 2

sentinel monitor master001 192.168.3.48 16339 2 -- new: 表示监听redis master服务器192.168.3.48:16339,如果n=2个sentinel访问失效,则执行failover

#port 26379 -- old

port 28339 -- new: sentinel 001 的访问端口

# dir /tmp -- old : Sentinel服务运行时使用的临时文件夹

dir /opt/redis/tmp/sentinel_001 -- new: Sentinel服务运行时使用的临时文件夹

# sentinel down-after-milliseconds mymaster 30000

sentinel down-after-milliseconds master001 30000

# sentinel parallel-syncs mymaster 1

sentinel parallel-syncs master001 1

# sentinel failover-timeout mymaster 180000

sentinel failover-timeout master001 180000

最后记得保存redis.conf文件

root >> :wq

4.1.3 配置sentinel_002.conf文件,设置sentinel 002节点的相关参数

# root >> mkdir -p /opt/redis/tmp/sentinel_002 -- Sentinel服务运行时使用的临时文件夹

root >> vim /opt/redis/redis-3.2.6/sentinel_002.conf

修改的主要配置参数,主要如下

# bind 127.0.0.1 192.168.1.1 -- old

bind 0.0.0.0 -- new : 表示任意客户端可以访问

# sentinel monitor mymaster 127.0.0.1 6379 2

sentinel monitor master001 192.168.3.48 16339 2 -- new: 表示监听redis master服务器192.168.3.48:16339,如果n=2个sentinel访问失效,则执行failover

#port 26379 -- old

port 28349 -- new: sentinel 002 的访问端口

# dir /tmp -- old : Sentinel服务运行时使用的临时文件夹

dir /opt/redis/tmp/sentinel_002 -- new: Sentinel服务运行时使用的临时文件夹

最后记得保存redis.conf文件

root >> :wq

 

4.1.4 配置sentinel_003.conf文件,设置sentinel 003节点的相关参数

# root >> mkdir -p /opt/redis/tmp/sentinel_003 -- Sentinel服务运行时使用的临时文件夹

root >> vim /opt/redis/redis-3.2.6/sentinel_003.conf

修改的主要配置参数,主要如下

# bind 127.0.0.1 192.168.1.1 -- old

bind 0.0.0.0 -- new : 表示任意客户端可以访问

# sentinel monitor mymaster 127.0.0.1 6379 2

sentinel monitor master001 192.168.3.48 16339 2 -- new: 表示监听redis master服务器192.168.3.48:16339,如果n=2个sentinel访问失效,则执行failover

#port 26379 -- old

port 28359 -- new: sentinel 003 的访问端口

# dir /tmp -- old : Sentinel服务运行时使用的临时文件夹

dir /opt/redis/tmp/sentinel_003 -- new: Sentinel服务运行时使用的临时文件夹

最后记得保存redis.conf文件

root >> :wq

 

4.2 启动Sentinel节点(Sentinel 001/Sentinel 002/Sentinel 003

4.2.1 【CNT06CAH05】:启动Sentinel 001/002/003节点

首先,执行make test命令,测试检查redis安装包文件是否OK

root >> cd /opt/redis/redis-3.2.6/src

root >> make test

Redis Sentinel 集群安装 step by step Nosql 第52张

检查test结果OK,如下图

Redis Sentinel 集群安装 step by step Nosql 第53张

编译redis 安装包,如下图

root >> make install

Redis Sentinel 集群安装 step by step Nosql 第54张

最后, 分别启动sentinel 001/002/003节点的sentinel哨兵服务,步骤分别如下:

启动 sentinel 001节点

root >> cd /opt/redis/redis-3.2.6/src

root >>./redis-sentinel ../sentinel_001.conf &

Redis Sentinel 集群安装 step by step Nosql 第55张

启动 sentinel 002节点

root >> cd /opt/redis/redis-3.2.6/src

root >>./redis-sentinel ../sentinel_002.conf &

Redis Sentinel 集群安装 step by step Nosql 第56张

启动 sentinel 003节点

root >> cd /opt/redis/redis-3.2.6/src

root >>./redis-sentinel ../sentinel_003.conf &

Redis Sentinel 集群安装 step by step Nosql 第57张

Redis Sentinel 集群安装 step by step Nosql 第58张

最后,至此全部的Redis + Sentinel集群安装和部署,就都完成了。前台java程序可以通过sentinel的IP和端口实例进行读写访问,如下

192.168.3.47: 28339

192.168.3.47: 28439

192.168.3.47: 28539

Redis Sentinel 集群安装 step by step Nosql 第59张

【本人原创,欢迎交流和分享技术,转载请附上如下内容:
如果你觉得这篇文章对你有帮助,请记得帮我点赞, 谢谢!
作者:kevin【转自】http://www.cnblogs.com/itshare/ 

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄