首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Redis安装部署(一主二从三哨兵)

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

作者头像
Alfred Zhao
发布2020-04-23 16:36:54
7050
发布2020-04-23 16:36:54
举报

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

  • 1.准备工作
  • 2.安装编译Redis
  • 3.Redis运行环境配置
  • 4.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 <- 这里需要添加这一行,否则chkconfig --add redis会报错“service redis does not support chkconfig”。 //第二部分 REDISPORT=6379 EXEC=/usr/local/redis/bin/redis-server <- 这里修改路径为/usr/local/redis CLIEXEC=/usr/local/redis/bin/redis-cli <- 这里修改路径为/usr/local/redis //添加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
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.准备工作
  • 2.安装编译Redis
  • 3.Redis运行环境配置
  • 4.Redis启动和关闭
相关产品与服务
云数据库 Redis
腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档