前言 Redis 是一个高性能的 key-value 数据库,作为一个典型的内存数据库,高速读写性能并支持数据持久化是大多数人选择 Redis 的原因,当然 Redis 也有很多局限,即使现在升级至 5.0 在技术圈仍有很多不同的声音。Redis 监控可以通过 Keepalived 结合简单的脚本实现,也可以基于 Redis Sentinel 监控,如果需要横向扩展使用 Codis 或许是更加成熟稳定的方案。
Redis 主备同步配置实践
扩展阅读
Redis - https://redis.io/
Redis 简介 Redis is often referred as a data structures server. What this means is that Redis provides access to mutable data structures via a set of commands, which are sent using a server-client model with TCP sockets and a simple protocol. So different processes can query and modify the same data structures in a shared way.
Data structures implemented into Redis have a few special properties:
Redis cares to store them on disk, even if they are always served and modified into the server memory. This means that Redis is fast, but that is also non-volatile.
Implementation of data structures stress on memory efficiency, so data structures inside Redis will likely use less memory compared to the same data structure modeled using an high level programming language.
Redis offers a number of features that are natural to find in a database, like replication, tunable levels of durability, cluster, high availability.
Another good example is to think of Redis as a more complex version of memcached, where the operations are not just SETs and GETs, but operations to work with complex data types like Lists, Sets, ordered data structures, and so forth.
If you want to know more, this is a list of selected starting points:
Documentation
Latest stable version tar ball
http://download.redis.io/redis-stable.tar.gz
http://download.redis.io/releases/redis-5.0.2.tar.gz
http://download.redis.io/releases/redis-3.2.10.tar.gz
Browse source code
http://download.redis.io/redis-stable/
http://download.redis.io/redis-stable/README.md
Install 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 # download wget http://download.redis.io/redis-stable.tar.gz tar xf redis-stable.tar.gz cd redis-stable # check packages yum -y install gcc gcc-c++ tcl # check version ./src/redis-cli -v redis-cli 5.0.2 # start redis server ./src/redis-server redis.conf # test [root@localhost ~]# ./redis-stable/src/redis-cli 127.0.0.1:6379> set foo bar OK 127.0.0.1:6379> get foo "bar" 127.0.0.1:6379>
Configuration
不理解的配置参数可以参考官方文档或者下面的中文翻译
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 # create directory mkdir -p /data/running/redis-6389 mkdir -p /var/log/redis/ # copy custom scripts -rwxr-xr-x 1 root root 266 Nov 28 19:11 change_redis.py -rwxr-xr-x 1 root root 323 Nov 28 19:11 check_redis.sh -rwxr-xr-x 1 root root 20 Nov 28 19:11 connect_redis.sh -rw-r--r-- 1 root root 74 Nov 28 19:11 keep_alived_state -rwxr-xr-x 1 root root 829 Nov 28 19:11 keepalived.state.sh -rwxr-xr-x 1 root root 173376 Nov 28 19:11 redis-cli -rw-r----- 1 root root 46666 Nov 28 19:11 redis.conf -rwxr-xr-x 1 root root 979464 Nov 28 19:11 redis-server -rwxr-xr-x 1 root root 74 Nov 28 19:11 start_redis.sh -rwxr-xr-x 1 root root 38 Nov 28 19:11 stop_redis.sh # set custom values [root@sg-gop-10-71-12-78 redis-6389]# grep -Ev "^$|#" redis.conf protected-mode no port 6389 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize yes supervised no pidfile /var/run/redis_6389.pid loglevel notice logfile /var/log/redis/redis-6389.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 /data/running/redis-6389 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 maxclients 50000 maxmemory 400gb maxmemory-policy allkeys-lru 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 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 0 0 0 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 aof-rewrite-incremental-fsync yes # set slave telnet 127.0.0.1 6389 slaveof 10.71.12.70 6389 # cancel slave slaveof no one info # Replication role:master connected_slaves:1 slave0:ip=10.71.12.71,port=6389,state=online,offset=2283,lag=1 master_repl_offset:2283 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:2282 # Replication role:slave master_host:10.71.12.70 master_port:6389 master_link_status:up master_last_io_seconds_ago:5 master_sync_in_progress:0 slave_repl_offset:2059 slave_priority:100 slave_read_only:1 connected_slaves:0 master_repl_offset:0 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0
redis.conf 配置英文原版
最简单的模式只需要修改 daemonize yes,然后备机使用 slaveof 命令设置即可
http://download.redis.io/redis-stable/redis.conf
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 # Redis 默认配置 [root@localhost redis-stable]# grep -Ev "^$|#" redis.conf bind 127.0.0.1 protected-mode yes port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize no supervised no pidfile /var/run/redis_6379.pid loglevel notice logfile "" 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 ./ replica-serve-stale-data yes replica-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no replica-priority 100 lazyfree-lazy-eviction no lazyfree-lazy-expire no lazyfree-lazy-server-del no replica-lazy-flush no appendonly no 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 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-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 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
redis.conf 配置中文翻译 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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 #redis.conf # Redis configuration file example. # ./redis-server /path/to/redis.conf ################################## INCLUDES ################################### # 这在你有标准配置模板但是每个 redis 服务器又需要个性设置的时候很有用。 include /path/to/local.conf include /path/to/other.conf ################################ GENERAL ##################################### # 是否在后台执行,yes:后台运行;no:不是后台运行(老版本默认) daemonize yes #3.2 里的参数,是否开启保护模式,默认开启。要是配置里没有指定 bind 和密码。开启该参数后,redis 只会本地进行访问,拒绝外部访问。要是开启了密码 和 bind,可以开启。否 则最好关闭,设置为 no。 protected-mode yes #redis 的进程文件 pidfile /var/run/redis/redis-server.pid #redis 监听的端口号。 port 6379 # 此参数确定了 TCP 连接中已完成队列(完成三次握手之后) 的长度, 当然此值必须不大于 Linux 系统定义的 / proc/sys/net/core/somaxconn 值,默认是 511,而 Linux 的默认参数值是 128。当系统并发量大并且客户端速度缓慢的时候,可以将这二个参数一起参考设定。该内核参数默认值一般是 128,对于负载很大的服务程序来说大大的不够。一般会将它修改为 2048 或者更大。在 / etc/sysctl.conf 中添加: net.core.somaxconn = 2048,然后在终端中执行 sysctl -p。 tcp-backlog 511 # 指定 redis 只接收来自于该 IP 地址的请求,如果不进行设置,那么将处理所有请求 bind 127.0.0.1 # 配置 unix socket 来让 redis 支持监听本地连接。 # unixsocket /var/run/redis/redis.sock # 配置 unix socket 使用文件的权限 # unixsocketperm 700 # 此参数为设置客户端空闲超过 timeout,服务端会断开连接,为 0 则服务端不会主动断开连接,不能小于 0。 timeout 0 #tcp keepalive 参数。如果设置不为 0,就使用配置 tcp 的 SO_KEEPALIVE 值,使用 keepalive 有两个好处: 检测挂掉的对端。降低中间设备出问题而导致网络看似连接却已经与对端端口的问题。在 Linux 内核中,设置了 keepalive,redis 会定时给对端发送 ack。检测到对端关闭需要两倍的设置值。 tcp-keepalive 0 # 指定了服务端日志的级别。级别包括:debug(很多信息,方便开发、测试),verbose(许多有用的信息,但是没有 debug 级别信息多),notice(适当的日志级别,适合生产环境),warn(只有非常重要的信息) loglevel notice # 指定了记录日志的文件。空字符串的话,日志会打印到标准输出设备。后台运行的 redis 标准输出是 / dev/null。 logfile /var/log/redis/redis-server.log # 是否打开记录 syslog 功能 # syslog-enabled no #syslog 的标识符。 # syslog-ident redis # 日志的来源、设备 # syslog-facility local0 # 数据库的数量,默认使用的数据库是 DB 0。可以通过”SELECT “命令选择一个 db databases 16 ################################ SNAPSHOTTING ################################ # 快照配置 # 注释掉“save” 这一行配置项就可以让保存数据库功能失效 # 设置 sedis 进行数据库镜像的频率。 # 900 秒(15 分钟)内至少 1 个 key 值改变(则进行数据库保存 -- 持久化) # 300 秒(5 分钟)内至少 10 个 key 值改变(则进行数据库保存 -- 持久化) # 60 秒(1 分钟)内至少 10000 个 key 值改变(则进行数据库保存 -- 持久化) save 900 1 save 300 10 save 60 10000 # 当 RDB 持久化出现错误后,是否依然进行继续进行工作,yes:不能进行工作,no:可以继续进行工作,可以通过 info 中的 rdb_last_bgsave_status 了解 RDB 持久化是否有错误 stop-writes-on-bgsave-error yes # 使用压缩 rdb 文件,rdb 文件压缩使用 LZF 压缩算法,yes:压缩,但是需要一些 cpu 的消耗。no:不压缩,需要更多的磁盘空间 rdbcompression yes # 是否校验 rdb 文件。从 rdb 格式的第五个版本开始,在 rdb 文件的末尾会带上 CRC64 的校验和。这跟有利于文件的容错性,但是在保存 rdb 文件的时候,会有大概 10% 的性能损耗,所以如果你追求高性能,可以关闭该配置。 rdbchecksum yes #rdb 文件的名称 dbfilename dump.rdb # 数据目录,数据库的写入会在这个目录。rdb、aof 文件也会写在这个目录 dir /var/lib/redis ################################# REPLICATION ################################# # 复制选项,slave 复制对应的 master。 # slaveof <masterip> <masterport> # 如果 master 设置了 requirepass,那么 slave 要连上 master,需要有 master 的密码才行。masterauth 就是用来配置 master 的密码,这样可以在连上 master 后进行认证。 # masterauth <master-password>