《实战录》导语
云端卫士《实战录》栏目定期会向粉丝朋友们分享一些在开发运维中的经验和技巧,希望对于关注我们的朋友有所裨益。本期分享人为云端卫士安全平台工程师田全磊,将带来Redis的主从服务器搭建。
Redis是一个简单快捷的key-value存储系统。它提供了丰富的数据存储结构,包括 lists, sets, ordered sets 以及 hashes ,当然还有和Memcached一样的 strings结构。同时Redis提供还对这些数据结构的丰富操作。
Redis哨兵模式介绍
Redis 的 Sentinel 功能用于管理多个 Redis 服务器,它主要提供了监控,提醒和自动故障迁移的功能。
1.监控(Monitoring): Sentinel 会不断地检查你的主服务器和从服务器是否运作正常。
2.提醒(Notification): 当被监控的某个 Redis 服务器出现问题时, Sentinel 可以通过 API 向管理员或者其他应用程序发送通知。
3.自动故障迁移(Automatic failover): 当一个主服务器不能正常工作时, Sentinel 会开始一次自动故障迁移操作, 它会将失效主服务器的其中一个从服务器升级为新的主服务器, 并让失效主服务器的其他从服务器改为复制新的主服务器; 当客户端试图连接失效的主服务器时, 集群也会向客户端返回新主服务器的地址, 使得集群可以使用新主服务器代替失效服务器。
安装
下载redis-2.8.19.tar.gz
解压tar zxf redis-2.8.19.tar.gz
安装cd redis-2.8.19
make && make install
每台机器上都同样安装redis
Redis主从环境搭建
一主:192.168.24.147
两从:192.168.24.148,192.168.24.149
(1)主192.168.24.147配置
目录:conf/redis_6379.conf
启动命令 src/redis-server conf/redis_6379.conf
daemonize yes
pidfile /export/Data/redis_pid/redis_6379.pid
port 6379
# tcp-backlog 511
timeout 300
tcp-keepalive 0
loglevel notice
logfile /export/Logs/redis/redis_6379.log
databases 16
stop-writes-on-bgsave-error yes
rdbcompression yes
dbfilename 6379.rdb
dir /export/Data/redis_data/6379
slave-serve-stale-data yes
slave-read-only yes
repl-disable-tcp-nodelay no
repl-backlog-size 128mb
maxmemory 2g
appendonly no
appendfilename 6379.aof
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 1024
# 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 512mb 256mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
rename-command keys jkeys
(2)从192.168.24.148配置
目录:conf/redis_6379.conf
启动命令 src/redis-server conf/redis_6379.conf
daemonize yes
slaveof 192.168.24.147 6379
pidfile /export/Data/redis_pid/redis_6379.pid
port 6379
# tcp-backlog 511
timeout 300
tcp-keepalive 0
loglevel notice
logfile /export/Logs/redis/redis_6379.log
databases 16
stop-writes-on-bgsave-error yes
rdbcompression yes
dbfilename 6379.rdb
dir /export/Data/redis_data/6379
slave-serve-stale-data yes
slave-read-only yes
repl-disable-tcp-nodelay no
repl-backlog-size 128mb
maxmemory 2g
appendonly no
appendfilename 6379.aof
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 1024
# 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 512mb 256mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
rename-command keys jkeys
(3)从192.168.24.149配置
目录:conf/redis_6379.conf
启动命令 src/redis-server conf/redis_6379.conf
daemonize yes
slaveof 192.168.24.147 6379
pidfile /export/Data/redis_pid/redis_6379.pid
port 6379
# tcp-backlog 511
timeout 300
tcp-keepalive 0
loglevel notice
logfile /export/Logs/redis/redis_6379.log
databases 16
stop-writes-on-bgsave-error yes
rdbcompression yes
dbfilename 6379.rdb
dir /export/Data/redis_data/6379
slave-serve-stale-data yes
slave-read-only yes
repl-disable-tcp-nodelay no
repl-backlog-size 128mb
maxmemory 2g
appendonly no
appendfilename 6379.aof
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 1024
# 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 512mb 256mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
rename-command keys jkeys