【Linux环境搭建】十三、Linux(CentOS7) Redis集群模式和哨兵模式配置

一、Redis集群配置

创建集群目录

mkdir -p /usr/local/redis-cluster
cd /usr/local/redis-cluster
mkdir 6379 6378

修改配置文件

vi redis.conf
daemonize yes
port 6379
dir /usr/local/redis-cluster/6379/
cluster-enabled yes #启动集群模式
cluster-config-file nodes-6379.conf
cluster-node-timeout 5000
bind 0.0.0.0
protected-mode no
appendonly yes
#如果要设置密码需要增加如下配置:
 #(设置redis访问密码)
requirepass 123456
 #(设置集群节点间访问密码,跟上面一致)
masterauth 123456

把修改后的配置文件,copy到6379、6378,修改第2、3、5项里的端口号,可以用批量
%s/源字符串/目的字符串/g

启动redis:

./redis-server  /usr/local/redis-cluster/6379/redis.conf
./redis-server  /usr/local/redis-cluster/6378/redis.conf

查看是否启动成功

ps -ef | grep redis 

用redis-cli创建整个redis集群:
测试环境:

./redis-cli --cluster create 192.168.10.195:6379 192.168.10.195:6378 192.168.10.124:6379 192.168.10.124:6378 192.168.10.100:6379 192.168.10.100:6378 --cluster-replicas 1 -a sbjcptTest

生产环境:

./redis-cli --cluster create 10.1.8.111:6301 10.1.8.111:6302 10.1.8.112:6303 10.1.8.112:6304 10.1.8.113:6305 10.1.8.113:6306 --cluster-replicas 1 -a sbjcptTest

验证集群:

./redis-cli -c -a sbjcptTest -h 192.168.10.195 -p 6379

./redis-cli -c -a xxx -h 10.1.8.111 -p 6301


./redis-cli -c -a sbjcptTest -h 10.1.8.112 -p 6301

常用命令:

# 查看集群信息
cluster info
# 查看节点列表
cluster nodes

二、Redis哨兵模式配置(主备):

创建数据存放目录

mkdir /data
mkdir /data/redis
mkdir /data/redis/redis-log
mkdir /data/redis/data

首先配置Redis的主服务器,修改redis.conf文件如下

# 使得Redis服务器可以跨网络访问
bind 0.0.0.0
dir "/data/redis/data"
daemonize yes
logfile "/data/redis/redis-log/redis.log"
# 设置密码
requirepass "123456"
# 主服务器密码,注意:有关slaveof的配置只是配置从服务器,主服务器不需要配置
masterauth 123456

配置Redis的从服务器,修改配置文件redis.conf

# 使得Redis服务器可以跨网络访问
bind 0.0.0.0
dir "/data/redis/data"
daemonize yes
logfile "/data/redis/redis-log/redis.log"

# 设置密码
requirepass "123456"

# 主服务器密码,,这个都要配置,不然主从无法用
masterauth 123456
# 注意:有关slaveof的配置只是配置从服务器,主服务器不需要配置
slaveof 192.168.10.195 6379
# 关闭防火墙:
systemctl stop firewalld.service
systemctl disable firewalld.service
systemctl status firewalld.service
# 启动:./redis-server ../redis.conf
# 查看集群是否正常:
redis-cli  -h 192.168.10.195 -p 6379 -a 123456 info Replication
[root@localhost src]# redis-cli  -h 192.168.10.195 -p 6379 -a 123456 info Replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.10.100,port=6379,state=online,offset=70,lag=0
slave1:ip=192.168.10.124,port=6379,state=online,offset=70,lag=0
master_replid:808f22bacf3af9192301aba5c63afff7d60f3b41
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:70
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:70

# 测试
redis-cli  -h 192.168.10.195 -p 6379 -a 123456
AUTH 123456
set k1 v1
exit
# 测试
redis-cli  -h 192.168.10.124 -p 6379 -a 123456
AUTH 123456
get k1

安装哨兵

# 3台Redis服务器都需执行
vi   sentinel.conf
mkdir /data/redis/sentinel-log
dataport 26379
protected-mode no
daemonize yes
pidfile /var/run/redis-sentinel.pid
logfile "/data/redis/sentinel-log/sentinel.log"
dir /tmp
sentinel monitor mymaster 192.168.10.195 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
sentinel auth-pass mymaster 123456

启动哨兵:

./redis-sentinel  ../sentinel.conf

测试查看哨兵:

./redis-cli -h 192.168.10.195 -p 26379 INFO Sentinel

./redis-cli  -h 192.168.10.195 -p 6379 -a 123456 info Replication

./redis-cli -h 10.1.8.112 -p 26379 INFO Sentinel

./redis-cli  -h 10.1.8.112 -p 6379 -a 123456 info Replication

关闭命令:

pkill redis-sentinel

pkill redis-server
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

全栈程序猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值