Linux学习空间redis

十三、Linux(CentOS7) Redis集群模式和哨兵模式

2022-05-14  本文已影响0人  全栈程序猿

一、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
上一篇下一篇

猜你喜欢

热点阅读