一主一从二哨兵_Redis安装部署(一主二从三哨兵)

需求:根据当前客户的生产环境,模拟安装部署Redis的测试环境,方便后续的功能测试。

1.准备工作

Redis的版本和虚拟主机数量都按照客户的生产环境来准备:

Redis版本:3.2.10

准备3台虚拟机,具体环境信息为:

系统版本

主机名

IP地址

主机内存

磁盘空间

RHEL6.8

test01

192.168.1.121

4G

20G

RHEL6.8

test02

192.168.1.122

4G

20G

RHEL6.8

test03

192.168.1.123

4G

20G

使用wget下载redis-3.2.10.tar.gz

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

需要确保安装有gcc @all nodes:

[root@test01 ~]# cluster_run_all_nodes "hostname; rpm -qa gcc"

test01

gcc-4.4.7-23.el6.x86_64

test02

gcc-4.4.7-23.el6.x86_64

test03

gcc-4.4.7-23.el6.x86_64

注:如果没有gcc,可以使用yum安装;

本文用到的cluster_run_all_nodes和cluster_copy_all_nodes两个命令,功能分别是在集群所有节点运行命令和同步文件。

2.安装编译Redis

软件安装&编译 @all nodes:

cd /u01/soft

tar -zxvf redis-3.2.10.tar.gz

mv redis-3.2.10 /usr/local/redis

cd /usr/local/redis

make PREFIX=/usr/local/redis install

实际我这里软件默认是在/root下,各节点同步软件介质、解压、指定安装路径、编译安装:

[root@test01 ~]# cluster_copy_all_nodes /root/redis-3.2.10.tar.gz /root/

[root@test01 ~]# cluster_run_all_nodes "hostname;tar -zxvf redis-3.2.10.tar.gz"

[root@test01 ~]# cluster_run_all_nodes "hostname;mv redis-3.2.10 /usr/local/redis"

--编译安装这步建议手工@all nodes 执行,因为时间长,输出信息多:

cd /usr/local/redis && make PREFIX=/usr/local/redis install

3.Redis运行环境配置

3.1 相关目录的创建:

--REDIS数据落地位置(数据目录)

//数据目录

mkdir -p /u01/redis/6379/data

//日志目录

mkdir -p /u01/redis/6379/log

--SENTINEL日志存放位置

//哨兵sentinel

mkdir -p /u01/redis/6379/temp

cluster_run_all_nodes "hostname;mkdir -p /u01/redis/6379/data"

cluster_run_all_nodes "hostname;mkdir -p /u01/redis/6379/log"

cluster_run_all_nodes "hostname;mkdir -p /u01/redis/6379/temp"

3.2 redis参数配置(主从主要配置):

//生成redis参数文件及存放位置

mkdir /etc/redis

//redis参数文件命名可采用端口号方式,如下,端口为6379,命名为6379.conf

cp /usr/local/redis/redis.conf /etc/redis/6379.conf

cluster_run_all_nodes "hostname;mkdir /etc/redis"

cluster_run_all_nodes "hostname;cp /usr/local/redis/redis.conf /etc/redis/6379.conf"

具体参数设置及说明:

1)主节点:192.168.1.121

vi /etc/redis/6379.conf

bind 192.168.1.121

protected-mode no

port 6379

daemonize yes

pidfile "/var/run/redis_6379.pid"

dir "/u01/redis/6379/data/"

slave-priority 100

appendonly yes

appendfsync everysec

requirepass Redis123

masterauth Redis123

2)从节点1:192.168.1.122

vi /etc/redis/6379.conf

bind 192.168.1.122

protected-mode no

port 6379

daemonize yes

pidfile "/var/run/redis_6379.pid"

dir "/u01/redis/6379/data/"

slaveof 192.168.1.121 6379

slave-read-only yes

slave-priority 80

appendonly yes

appendfsync everysec

requirepass Redis123

masterauth Redis123

3)从节点2:192.168.1.123

vi /etc/redis/6379.conf

bind 192.168.1.123

protected-mode no

port 6379

daemonize yes

pidfile "/var/run/redis_6379.pid"

dir "/u01/redis/6379/data/"

slaveof 192.168.1.121 6379

slave-read-only yes

slave-priority 60

appendonly yes

appendfsync everysec

requirepass Redis123

masterauth Redis123

3.3 redis系统服务配置:

//拷贝redis服务启动文件到/etc/init.d

cp /usr/local/redis/utils/redis_init_script /etc/init.d/redis

//redis启动文件修改分两部分

vi /etc/init.d/redis

//第一部分

#!/bin/sh

#chkconfig:2345 80 90

//第二部分

REDISPORT=6379

EXEC=/usr/local/redis/bin/redis-server

CLIEXEC=/usr/local/redis/bin/redis-cli

//添加redis服务

chkconfig --add redis

实际我这里操作为:

cp /usr/local/redis/utils/redis_init_script /etc/init.d/redis

--这里按照上面引用文档的提示修改对应两部分内容,然后再分发复制到各节点、添加redis服务开机启动:

[root@test01 local]# cluster_copy_all_nodes /etc/init.d/redis /etc/init.d/redis

cluster_run_all_nodes "hostname;chkconfig --add redis"

cluster_run_all_nodes "hostname;chkconfig --list redis"

配置环境变量,确认redis相关命令可用:

//linux环境变量,针对所有用户

vim /etc/profile

export PATH="$PATH:/usr/local/redis/bin"

//立即生效

source /etc/profile

实际我这里操作为:

cluster_run_all_nodes "hostname;echo 'export PATH="$PATH:/usr/local/redis/bin"' >> /etc/profile'"

重新登陆@all nodes,验证环境变量生效。

3.4 哨兵sentinel配置

每台主机可以配置一个或者多个哨兵,取决与每个服务器上跑多少个redis。

系统参数配置:

vi /etc/sysctl.conf

//定义了系统中每一个端口最大的监听队列的长度,这是个全局的参数,默认值为128

net.core.somaxconn= 1024

//sysctl.conf 生效

sysctl -p

//若临时生效,可使用如下命令:

echo 1024 >/proc/sys/net/core/somaxconn

实际我这里操作为:

[root@test01 6379]# cluster_run_all_nodes "hostname; echo "net.core.somaxconn = 1024" >> /etc/sysctl.conf"

[root@test01 6379]# cluster_run_all_nodes "hostname; sysctl -p"

[root@test01 6379]# cluster_run_all_nodes "hostname; cat /proc/sys/net/core/somaxconn"

哨兵配置文件:

//哨兵配置文件位置

cp /usr/local/redis/sentinel.conf /etc/redis

//创建哨兵日志存放位置,最好是与redis的数据文件存放在一起

mkdir -p /u01/redis/6379/temp/

具体配置(每个节点都一样)

vi /etc/redis/sentinel.conf

protected-mode no

port 26379

dir "/u01/redis/6379/temp/"

daemonize yes

logfile "/u01/redis/6379/temp/sentinel.log"

sentinel monitor redis1 192.168.1.121 6379 2

sentinel down-after-milliseconds redis1 10000

sentinel parallel-syncs redis1 2

sentinel failover-timeout redis1 60000

sentinel auth-pass redis1 hundsun@bbep

实际我这里操作为:

[root@test01 local]# cluster_run_all_nodes "hostname; mkdir -p /u01/redis/6379/temp"

--按上面文档配置sentinel.conf 然后分发复制到@all nodes:

[root@test01 local]# cluster_copy_all_nodes /etc/redis/sentinel.conf /etc/redis/sentinel.conf

4.Redis启动和关闭

4.1 启动&关闭REDIS

启动Redis:

service redis start

//查看redis进程

[root@test01 ~]# ps -ef|grep redis

root 29097 1 0 01:14 ? 00:01:07 /usr/local/redis/bin/redis-server 192.168.1.121:6379

root 32072 31964 0 14:18 pts/0 00:00:00 grep redis

关闭Redis:

redis-cli -h auth shutdown

实际操作如下:

[root@test01 ~]# redis-cli -h 192.168.1.121

192.168.1.121:6379> auth Redis123

OK

192.168.1.121:6379> shutdown

not connected> exit

4.2 启动&关闭哨兵

启动哨兵:

redis-sentinel /etc/redis/sentinel.conf

[root@test01 ~]# redis-sentinel /etc/redis/sentinel.conf

[root@test01 ~]# ps -ef|grep sentinel

root 32112 1 0 14:21 ? 00:00:00 redis-sentinel *:26379 [sentinel]

root 32117 31964 0 14:21 pts/0 00:00:00 grep sentinel

关闭哨兵:

//杀进程法

ps -ef|grep sentinel

kill

kill -9 32112

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值