欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

redis 4.0 一主多从 哨兵集群搭建

程序员文章站 2022-07-05 12:29:49
...

1.准备工作:

操作系统:centos 7 minal x86_64 

ip地址:192.168.30.59

redis 4.0:http://download.redis.io/releases/redis-4.0.11.tar.gz

安装编译工具:yum install gcc make tcl -y

2.编译安装:

make && make test

make install

3.配置主从集群:

mkidr /opt/{7001..7003}

#主配置文件:7001.conf
grep -Ev '^$|^#' 7001/7001.conf

bind 0.0.0.0
protected-mode no
port 7001
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile "/opt/7001/redis_7001.pid"
loglevel notice
logfile "/opt/7001/redis_7001.log"
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
dir "/root"
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
requirepass "123456"
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes


#从配置文件7002.conf 7003类似
grep -Ev '^$|^#' 7002/7002.conf

bind 0.0.0.0
protected-mode no
port 7002
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile "/opt/7002/redis_7002.pid"
loglevel notice
logfile "/opt/7002/redis_7002.log"
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
dir "/opt/7001"
slaveof 192.168.30.59 7001
masterauth "123456"
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
requirepass "123456"
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes

分别启动redis服务,并测试检查:

redis-server /opt/7001/7001.conf

redis-server /opt/7002/7002.conf

redis-server /opt/7003/7003.conf

登陆查看集群信息:

redis-cli -p 7001

info replication

set aa 123

set name "123"

get aa

get name

keys *

其他类似

测试没得问题 主从集群就搭建成功

 

4.搭建哨兵模式

第一种:单个哨兵:

protected-mode no
daemonize yes
logfile "/opt/7001/sentinel_7001.log"
port 26380
dir "/tmp"
sentinel deny-scripts-reconfig yes
sentinel monitor mymaster 192.168.30.59 7001 1
sentinel auth-pass mymaster 123456
sentinel config-epoch mymaster 1
sentinel leader-epoch mymaster 1

启动哨兵进行测试:

redis-sentinel /opt/7001/sentinel_7001.conf

将7001shutdown然后查看哨兵日志变化

自动将从节点切换为主节点,且7001重启动后自动切换成从节点

说明哨兵生效

第二种:多个哨兵

sentinel monitor mymaster 192.168.30.59 7001 2

测试同上