6 min read

Redis Sentinel 集群搭建摘要

Redis Sentinel 集群搭建摘要

背景

在K8S集群中安装一个redis sentinel 集群是一件极其方便的事情,然有时需要顾及到非容器环境的。

环境

  • 操作系统:银河麒麟 v10 SP3
  • 平台:x86/arm64
  • 版本:7.4.10
  • 机器列表: 172.20.5.11(主)、172.20.5.12(从)、172.20.5.13(从)、

系统配置

编辑 /etc/default/grub,在 GRUB_CMDLINE_LINUX 参数中添加:

GRUB_CMDLINE_LINUX="... transparent_hugepage=never"


# grub2-mkconfig -o /boot/grub2/grub.cfg  # CentOS/RHEL
# update-grub                             # Ubuntu/Debian


组建步骤

Redis

安装Redis

// x86_64 平台
# rpm -ivh https://rpmfind.net/linux/remi/enterprise/8/modular/x86_64/redis-7.2.10-1.el8.remi.x86_64.rpm

// arm64 平台
# rpm -ivh https://rpmfind.net/linux/remi/enterprise/8/modular/aarch64/redis-7.2.10-1.el8.remi.aarch64.rpm

配置Redis

写入配置文件

  • 172.20.5.11
# cat > /etc/redis/redis.conf<<EOF
### GENERAL CONFIGURATION

bind 0.0.0.0
requirepass "vqiu@8848"
masterauth "vqiu@8848"
port 6379
dir "/var/lib/redis"
protected-mode yes
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
supervised systemd
pidfile "/var/run/redis_6379.pid"
loglevel notice
logfile "/var/log/redis/redis.log"
databases 16

### SNAPSHOTTING

# Snapshot settings

save 900 1
save 300 10
save 60 10000

stop-writes-on-bgsave-error yes

rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"

### REPLICATION

replica-serve-stale-data yes
replica-read-only yes

repl-diskless-sync no
repl-diskless-sync-delay 5

repl-disable-tcp-nodelay no

# repl-backlog-size 1mb
# repl-backlog-ttl 3600

replica-priority 100

### APPEND ONLY MODE

appendonly no
appendfilename "appendonly.aof"

# appendfsync always
appendfsync everysec
# appendfsync no

no-appendfsync-on-rewrite no

auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes

### LUA SCRIPTING

busy-reply-threshold 5000

### SLOW LOG

slowlog-log-slower-than 10000
slowlog-max-len 128

### LATENCY MONITOR

latency-monitor-threshold 0

#### EVENT NOTIFICATION

notify-keyspace-events ""

### ADVANCED CONFIG

hash-max-listpack-entries 512
hash-max-listpack-value 64
list-max-listpack-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-listpack-entries 128
zset-max-listpack-value 64
hll-sparse-max-bytes 3000
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
aof-rewrite-incremental-fsync yes
EOF

  • 172.20.5.12
# cat > /etc/redis/redis.conf<<EOF
### GENERAL CONFIGURATION

bind 0.0.0.0
requirepass "vqiu@8848"
masterauth "vqiu@8848"
port 6379
dir "/var/lib/redis"
protected-mode yes
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
supervised systemd
pidfile "/var/run/redis_6379.pid"
loglevel notice
logfile "/var/log/redis/redis.log"
databases 16

### SNAPSHOTTING

# Snapshot settings

save 900 1
save 300 10
save 60 10000

stop-writes-on-bgsave-error yes

rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"

### REPLICATION

replica-serve-stale-data yes
replica-read-only yes

repl-diskless-sync no
repl-diskless-sync-delay 5

repl-disable-tcp-nodelay no

# repl-backlog-size 1mb
# repl-backlog-ttl 3600

replica-priority 100
replicaof 172.20.5.11 6379

### APPEND ONLY MODE

appendonly no
appendfilename "appendonly.aof"

# appendfsync always
appendfsync everysec
# appendfsync no

no-appendfsync-on-rewrite no

auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes

### LUA SCRIPTING

busy-reply-threshold 5000

### SLOW LOG

slowlog-log-slower-than 10000
slowlog-max-len 128

### LATENCY MONITOR

latency-monitor-threshold 0

#### EVENT NOTIFICATION

notify-keyspace-events ""

### ADVANCED CONFIG

hash-max-listpack-entries 512
hash-max-listpack-value 64
list-max-listpack-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-listpack-entries 128
zset-max-listpack-value 64
hll-sparse-max-bytes 3000
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
aof-rewrite-incremental-fsync yes
EOF

  • 172.20.5.13
# cat > /etc/redis/redis.conf<<EOF
### GENERAL CONFIGURATION

bind 0.0.0.0
requirepass "vqiu@8848"
masterauth "vqiu@8848"
port 6379
dir "/var/lib/redis"
protected-mode yes
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
supervised systemd
pidfile "/var/run/redis_6379.pid"
loglevel notice
logfile "/var/log/redis/redis.log"
databases 16

### SNAPSHOTTING

# Snapshot settings

save 900 1
save 300 10
save 60 10000

stop-writes-on-bgsave-error yes

rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"

### REPLICATION

replica-serve-stale-data yes
replica-read-only yes

repl-diskless-sync no
repl-diskless-sync-delay 5

repl-disable-tcp-nodelay no

# repl-backlog-size 1mb
# repl-backlog-ttl 3600

replica-priority 100
replicaof 172.20.5.11 6379

### APPEND ONLY MODE

appendonly no
appendfilename "appendonly.aof"

# appendfsync always
appendfsync everysec
# appendfsync no

no-appendfsync-on-rewrite no

auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes

### LUA SCRIPTING

busy-reply-threshold 5000

### SLOW LOG

slowlog-log-slower-than 10000
slowlog-max-len 128

### LATENCY MONITOR

latency-monitor-threshold 0

#### EVENT NOTIFICATION

notify-keyspace-events ""

### ADVANCED CONFIG

hash-max-listpack-entries 512
hash-max-listpack-value 64
list-max-listpack-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-listpack-entries 128
zset-max-listpack-value 64
hll-sparse-max-bytes 3000
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
aof-rewrite-incremental-fsync yes
EOF

服务启动

# systemctl enable redis --now

Redis测试

查看redis 复制状态

# redis-cli -h 172.20.5.11  -a vqiu@8848 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=172.20.5.12,port=6379,state=online,offset=1760319,lag=1
slave1:ip=172.20.5.13,port=6379,state=online,offset=1760601,lag=0
master_failover_state:no-failover
master_replid:3a2ae338a6cc32d7fa86c197aca6105416d4bb30
master_replid2:0caeabe85e37e84efa2a531cf7839ceb36510782
master_repl_offset:1760601
second_repl_offset:201269
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:708946
repl_backlog_histlen:1051656

Sentinel

Sentinel 配置

写入配置文件

# cat > /etc/redis/sentinel.conf<<EOF
protected-mode no
supervised systemd
port 26379
daemonize no
pidfile "/var/run/redis-sentinel.pid"
loglevel notice
logfile "/var/log/redis/sentinel.log"
dir "/tmp"

sentinel monitor mymaster 172.20.5.11 6379 2
sentinel auth-pass mymaster vqiu@8848

sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 15000
sentinel parallel-syncs mymaster 1
EOF

服务启动

# systemctl enable redis-sentinel --now

Sentinel 测试

sentinel 状态查看

# redis-cli -p 26379 sentinel master mymaster
# redis-cli -p 26379 sentinel slaves mymaster
# redis-cli -p 26379 sentinel sentinels mymaster
# redis-cli -p 26379 sentinel get-master-addr-by-name mymaster

故障转移测试

  • 登陆到172.20.5.11节点,执行:
# redis-cli -p 26379 sentinel get-master-addr-by-name mymaster
1) "172.20.5.11"
2) "6379"

在5.11 节点中将redis服务关闭

# systemctl stop redis

等待片刻,发现主服务已切换

# redis-cli -p 26379 sentinel get-master-addr-by-name mymaster
1) "172.20.5.13"
2) "6379"

Haproxy

由于 Redis 中的Sentinel 集群关系是自行维护的,当发生切换时,主服务节点不可控。此时可以引入haproxy来当Redis集群的服务入口。

安装haproxy

# yum -y install haproxy

写入配置文件

# cat >/etc/haproxy/haproxy.cfg<<EOF
frontend Redis
    bind            0.0.0.0:6379  name redis-server
    mode            tcp
    log             global
    timeout client  30000
    default_backend redis_back

backend redis_back
    mode            tcp
    timeout connect 30000
    timeout server  30000
    retries         3
    option          tcp-check
    tcp-check       connect
    tcp-check       send AUTH\ vqiu@8848\r\n
    tcp-check       send PING\r\n
    tcp-check       expect string +PONG
    tcp-check       send info\ replication\r\n
    tcp-check       expect string role:master
    tcp-check       send QUIT\r\n
    tcp-check       expect string +OK
    server          r1 172.20.5.11:6379 check inter 1000 maxconn 4096
    server          r2 172.20.5.12:6379 check inter 1000 maxconn 4096
    server          r3 172.20.5.13:6379 check inter 1000 maxconn 4096

frontend stats
    mode            http
    bind            *:8848
    stats           enable
    stats           uri /stats
    stats           refresh 10s
    timeout connect 30000
    timeout server  30000

服务启动

# systemctl enable haproxy --now

测试

127.0.0.1:6379> SET domain vqiu.cn
OK
127.0.0.1:6379> get domain
"vqiu.cn"

Haproxy 实例状态图