1. 部署Redis集群
redis的安装及配置参考[redis部署]
本文以创建一主二从的集群为例。
1.1 部署与配置
先创建sentinel
目录,在该目录下创建8000
,8001
,8002
三个以端口号命名的目录。
1 2 3
| mkdir sentinel cd sentinel mkdir 8000 8001 8002
|
在对应端口号目录中创建redis.conf
的文件,配置文件中的端口号port
参数改为对应目录的端口号。配置如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
| daemonize yes
pidfile /var/run/redis.pid
port 8000
tcp-backlog 511
timeout 0
tcp-keepalive 60
loglevel notice
logfile "./redis8000.log"
databases 16
save 900 1 save 300 10 save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./
masterauth 0234kz9*l
slave-serve-stale-data yes
slave-read-only yes
repl-disable-tcp-nodelay no
slave-priority 100
requirepass 0234kz9*l
maxmemory 3gb
maxmemory-policy volatile-lru
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite yes
auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
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-entries 512 list-max-ziplist-value 64
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
|
1.2 配置主从关系
1、启动实例
三个Redis实例配置相同,分别启动三个Redis实例。建议将redis-server
、redis-cli
、redis-sentinel
的二进制复制到/usr/local/bin
的目录下。
1 2
| cd 8000 redis-server redis.conf
|
2、配置主从关系
例如,将8000端口实例设为主,8001和8002端口的实例设为从。
则分别登录8001和8002的实例,执行slaveof <MASTER_IP> <MASTER_PORT>
命令。
例如:
1 2 3
| [root@kube-node-1 8000] 127.0.0.1:8001> slaveof 127.0.0.1 8000 OK
|
3、检查集群状态
登录master和slave实例,执行info replication
查看集群状态。
Master
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| [root@kube-node-1 8000] 127.0.0.1:8000> info replication
role:master connected_slaves:2 slave0:ip=127.0.0.1,port=8001,state=online,offset=2853,lag=0 slave1:ip=127.0.0.1,port=8002,state=online,offset=2853,lag=0 master_replid:4f8331d5f180a4669241ab0dd97e43508abd6d8f master_replid2:0000000000000000000000000000000000000000 master_repl_offset:2853 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:2853
|
Slave
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| [root@kube-node-1 8000] 127.0.0.1:8001> info replication
role:slave master_host:127.0.0.1 master_port:8000 master_link_status:up master_last_io_seconds_ago:3 master_sync_in_progress:0 slave_repl_offset:2909 slave_priority:100 slave_read_only:1 connected_slaves:0 master_replid:4f8331d5f180a4669241ab0dd97e43508abd6d8f master_replid2:0000000000000000000000000000000000000000 master_repl_offset:2909 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:2909
|
也可以往master写数据,从slave读取数据来验证。
2. 部署sentinel集群
2.1 部署与配置
在之前创建的sentinel
目录中场景sentinel端口号命名的目录28000
,28001
,28002
。
1 2
| cd sentinel mkdir 28000 28001 28002
|
在对应端口号目录中创建redis.conf
的文件,配置文件中的端口号port
参数改为对应目录的端口号。配置如下:
1 2 3 4 5
| port 28000 sentinel monitor mymaster 127.0.0.1 8000 2 sentinel down-after-milliseconds mymaster 60000 sentinel failover-timeout mymaster 180000 sentinel parallel-syncs mymaster 1
|
2.2 启动sentinel实例
1 2
| redis-sentinel sentinel.conf &
|
2.3 查看状态
使用sentinel masters
命令查看监控的master节点。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| [root@kube-node-1 28000] 127.0.0.1:28000> 127.0.0.1:28000> ping PONG 127.0.0.1:28000> 127.0.0.1:28000> sentinel masters 1) 1) "name" 1) "mymaster" 2) "ip" 3) "127.0.0.1" 4) "port" 5) "8000" 6) "runid" 7) "" 8) "flags" 1) "s_down,master,disconnected" 2) "link-pending-commands" 3) "0" 4) "link-refcount" 5) "1" 6) "last-ping-sent" 7) "187539" 8) "last-ok-ping-reply" 9) "187539" 10) "last-ping-reply" 11) "3943" 12) "s-down-time" 13) "127491" 14) "down-after-milliseconds" 15) "60000" 16) "info-refresh" 17) "1517346914642" 18) "role-reported" 19) "master" 20) "role-reported-time" 21) "187539" 22) "config-epoch" 23) "0" 24) "num-slaves" 25) "0" 26) "num-other-sentinels" 27) "0" 28) "quorum" 29) "2" 30) "failover-timeout" 31) "180000" 32) "parallel-syncs" 33) "1"
|
参考文章:
https://redis.io/topics/sentinel