陈佳浩
陈佳浩
发布于 2024-06-29 / 21 阅读
0
0

Redis哨兵集群部署

准备:

三台虚拟机:

主机名 IP
redis-01.home.com 10.0.0.21
redis-02.home.com 10.0.0.22
redis-03.home.com 10.0.0.23

编译安装redis

版本:redis-cli 7.2.3

安装方法参考主页脚本

配置主从

修改两台从节点配置文件

[root@redis-03 ~]# grep -Ev "^#|^$" /apps/redis/etc/redis.conf
bind 0.0.0.0 -::1
protected-mode no
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
pidfile /apps/redis/run/redis_6379.pid
loglevel notice
logfile /apps/redis/log/redis-6379.log
databases 16
always-show-logo no
set-proc-title yes
proc-title-template "{title} {listen-addr} {server-mode}"
locale-collate ""
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
rdb-del-sync-files no
dir /apps/redis/data/
masterauth 1syhLqT				#修改此行为master的密码
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync yes
repl-diskless-sync-delay 5
repl-diskless-sync-max-replicas 0
repl-diskless-load disabled
repl-disable-tcp-nodelay no
replica-priority 100
acllog-max-len 128
requirepass 1syhLqT
maxclients 100000
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
lazyfree-lazy-user-del no
lazyfree-lazy-user-flush no
oom-score-adj no
oom-score-adj-values 0 200 800
disable-thp yes
appendonly no
appendfilename "appendonly.aof"
appenddirname "appendonlydir"
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 yes
aof-timestamp-enabled no
 
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-listpack-entries 512
hash-max-listpack-value 64
list-max-listpack-size -2
list-compress-depth 0
set-max-intset-entries 512
set-max-listpack-entries 128
set-max-listpack-value 64
zset-max-listpack-entries 128
zset-max-listpack-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
jemalloc-bg-thread yes
replicaof 10.0.0.21 6379				#指向当前master的IP和端口号

重启两台从节点redis

systemctl restart redis

或者在命令行执行

[root@redis-03 ~]# redis-cli		#登录到slave
127.0.0.1:6379> AUTH 1syhLqT
OK
127.0.0.1:6379> INFO replication		#查看当前节点状态
# Replication
role:master							#未配置前默认为master
connected_slaves:0
master_failover_state:no-failover
master_replid:e3c65d7edaa0074e6dee16c0b14170b5bcde4016
master_replid2:67da3399544ebd21be509a3c54b155101edb3e66
master_repl_offset:2405461
second_repl_offset:2400936
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2400936
repl_backlog_histlen:4526
127.0.0.1:6379> REPLICAOF 10.0.0.21 6379	#设置master的IP和端口
OK
127.0.0.1:6379> CONFIG SET masterauth 1syhLqT	#配置master的密码
OK
127.0.0.1:6379> INFO replication		#查看当前节点状态
# Replication
role:slave								#已经转变为slave
master_host:10.0.0.21
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_read_repl_offset:3211130
slave_repl_offset:3211130
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:67da3399544ebd21be509a3c54b155101edb3e66
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:3211130
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:3207962
repl_backlog_histlen:3169

在master节点查看所有从节点信息

127.0.0.1:6379> INFO replication
# Replication
role:master
connected_slaves:2
slave0:ip=10.0.0.22,port=6379,state=online,offset=3660388,lag=0
slave1:ip=10.0.0.23,port=6379,state=online,offset=3658265,lag=1
master_failover_state:no-failover
master_replid:67da3399544ebd21be509a3c54b155101edb3e66
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:3660587
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2596371
repl_backlog_histlen:1064217

删除主从同步

127.0.0.1:6379> REPLICAOF no one		#会断开与master的主从同步,但不会清除已有数据

实现哨兵架构

编辑哨兵配置文件

因为我是编译安装,在源码目录有一个sentinel.conf配置文件,将其复制到安装目录就行

[root@redis-02 ~]# ls redis-7.2.3/sentinel.conf 
redis-7.2.3/sentinel.conf
[root@redis-02 ~]# cp redis-7.2.3/sentinel.conf /apps/redis/etc/sentinel.conf
[root@redis-02 ~]# chown redis.redis /apps/redis/etc/sentinel.conf

编辑配置文件

[root@redis-02 ~]# grep -Ev "^#|^$" /apps/redis/etc/sentinel.conf
protected-mode no
port 26379				#端口
daemonize yes			#作为守护进程运行
pidfile /var/run/redis-sentinel.pid
loglevel notice
logfile "/apps/redis/log/sentinel_26379.log"
dir /tmp		#工作目录
sentinel monitor sentinel01 10.0.0.21 6379 2	#sentinel01是当前集群的名称,指定当前master的IP和端口,2为法定人数限制,即有几个sentinel认为master down了就进行故障转移,一般为集群sentinel节点的一半以上的整数值
sentinel auth-pass sentinel01 1syhLqT			#配置集群名称和master的密码
sentinel down-after-milliseconds sentinel01 3000	#判断集群中所有节点的主观下线时间,单位毫秒
acllog-max-len 128
sentinel parallel-syncs sentinel01r 1				#发生故障转移后,可以同时向新master同步数据的slave的数量,数字越小总同步时间越长,但可以减轻新master的负载压力
sentinel failover-timeout sentinel01 180000		#所有slaves指向新的master所需的超时时间,单位:毫秒
sentinel deny-scripts-reconfig yes				#禁止修改脚本
SENTINEL resolve-hostnames no
SENTINEL announce-hostnames no
SENTINEL master-reboot-down-after-period sentinel01 0
sentinel myid 50547f34ed71fd48c197924969937e738a39975b 			#注意此行自动生成必须唯一,一般不需要修改,如果相同则修改此值需重启redis和sentinel服务

拷贝到其他主机,设置权限

[root@redis-01 ~]# scp /apps/redis/etc/sentinel.conf 10.0.0.22:/apps/redis/etc/sentinel.conf
[root@redis-01 ~]# scp /apps/redis/etc/sentinel.conf 10.0.0.23:/apps/redis/etc/sentinel.conf
[root@redis-02 ~]# chown redis.redis /apps/redis/etc/sentinel.conf
[root@redis-03 ~]# chown redis.redis /apps/redis/etc/sentinel.conf

编译安装默认没有service文件,

[root@redis-01 ~]# systemctl cat redis-sentinel
# /usr/lib/systemd/system/redis-sentinel.service
[Unit]
Description=Redis Sentinel
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
ExecStart=/apps/redis/bin/redis-sentinel /apps/redis/etc/sentinel.conf --supervised systemd
ExecStop=/bin/kill -s QUIT $MAINPID
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755

[Install]
WantedBy=multi-user.target

[Service]
LimitNOFILE=10240

启动哨兵

[root@redis-01 ~]# systemctl daemon-reload
[root@redis-01 ~]# systemctl enable --now redis-sentinel

[root@redis-02 ~]# systemctl daemon-reload
[root@redis-02 ~]# systemctl enable --now redis-sentinel

[root@redis-03 ~]# systemctl daemon-reload
[root@redis-03 ~]# systemctl enable --now redis-sentinel

查看端口状态

[root@redis-01 ~]# ss -ntpl
State       Recv-Q Send-Q                                                              Local Address:Port                                                                             Peer Address:Port            
LISTEN      0      511                                                                             *:26379                                                                                       *:*                   users:(("redis-sentinel",pid=3965,fd=6))
LISTEN      0      511                                                                             *:6379                                                                                        *:*                   users:(("redis-server",pid=1620,fd=6))
LISTEN      0      128                                                                             *:22                                                                                          *:*                   users:(("sshd",pid=928,fd=3))
LISTEN      0      100                                                                     127.0.0.1:25                                                                                          *:*                   users:(("master",pid=1161,fd=13))
LISTEN      0      511                                                                          [::]:26379                                                                                    [::]:*                   users:(("redis-sentinel",pid=3965,fd=7))
LISTEN      0      511                                                                         [::1]:6379                                                                                     [::]:*                   users:(("redis-server",pid=1620,fd=7))
LISTEN      0      128                                                                          [::]:22                                                                                       [::]:*                   users:(("sshd",pid=928,fd=4))
LISTEN      0      100                                                                         [::1]:25                                                                                       [::]:*

查看集群状态

[root@redis-01 ~]# redis-cli -a 1syhLqT -p 26379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
AUTH failed: ERR AUTH <password> called without any password configured for the default user. Are you sure your configuration is correct?
127.0.0.1:26379> INFO sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_tilt_since_seconds:-1
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=sentinel01,status=ok,address=10.0.0.21:6379,slaves=2,sentinels=3			#查看masterIP,几个slave,几个sentinels,如果sentinels值不对,需要检查myid可能冲突了

评论