环境:
OS:Red Hat Linux As 5
DB:redis-4.0.6
-------------------------------------主库安装-------------------------------
1.安装
[root@pxc01 soft]#tar –zxvf redis-4.0.6.tar.gz
[root@pxc01 soft]# cp -R ./redis-4.0.6 /opt/
[root@pxc01 opt]# cd redis-4.0.6
[root@pxc01 redis-4.0.6]# make
[root@pxc01 redis-4.0.6]# make test
继续安装
[root@pxc01 redis-4.0.6]# cd src
[root@pxc01 src]# make install
2.创建相应目录
[root@pxc01 opt]# cd redis-4.0.6/
[root@pxc01 redis-4.0.6]# mkdir conf ##配置文件目录
[root@pxc01 redis-4.0.6]# mkdir log ##日志文件目录
[root@pxc01 redis-4.0.6]# mkdir run ##运行的pid文件目录
[root@pxc01 redis-4.0.6]# mkdir rdb ##快照和AOF文件目录
3.主库配置文件(8379.conf)
-
daemonize yes
-
pidfile /opt/single_redis-4.0.6/run/redis-8379.pid
-
port 8379
-
tcp-backlog 511
-
timeout 0
-
tcp-keepalive 0
-
loglevel notice
-
logfile "/opt/single_redis-4.0.6/log/redis-8379.log"
-
databases 16
-
save 900 1
-
save 300 10
-
save 60 10000
-
stop-writes-on-bgsave-error yes
-
rdbcompression yes
-
rdbchecksum yes
-
dbfilename dump-8379.rdb
-
dir /opt/single_redis-4.0.6/rdb
-
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
-
appendonly yes
-
appendfilename "redis-8379.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-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
-
# Generated by CONFIG REWRITE
-
#masterauth "richinfo123"
-
#requirepass "richinfo123"
-
protected-mode no
4.启动主库
/opt/single_redis-4.0.6/src/redis-server /opt/single_redis-4.0.6/conf/8379.conf
-------------------------------------从库安装-------------------------------
1.安装
跟主库安装一样
2.从库配置文件
从库的配置文件就是在主库配置文件的基础上加上如下2行
slaveof 192.168.56.91 8379 ##表示从那个主库同步
slave-read-only yes ##从库设置为只读
-
daemonize yes
-
pidfile /opt/single_redis_slave/run/redis-8379.pid
-
port 8379
-
tcp-backlog 511
-
timeout 0
-
tcp-keepalive 0
-
loglevel notice
-
logfile "/opt/single_redis_slave/log/redis-8379.log"
-
databases 16
-
save 900 1
-
save 300 10
-
save 60 10000
-
stop-writes-on-bgsave-error yes
-
rdbcompression yes
-
rdbchecksum yes
-
dbfilename dump-8379.rdb
-
dir /opt/single_redis_slave/rdb
-
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
-
appendonly yes
-
appendfilename "redis-8379.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-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
-
# Generated by CONFIG REWRITE
-
#masterauth "richinfo123"
-
#requirepass "richinfo123"
-
protected-mode no
-
slaveof 192.168.56.91 8379
3.启动从库
/opt/single_redis_slave/src/redis-server /opt/single_redis_slave/conf/8379.conf
4.查看从库的同步日志
21636:S 23 Jan 04:40:33.028 * Ready to accept connections
21636:S 23 Jan 04:40:33.028 * Connecting to MASTER 192.168.56.91:8379
21636:S 23 Jan 04:40:33.028 * MASTER <-> SLAVE sync started
21636:S 23 Jan 04:40:33.030 * Non blocking connect for SYNC fired the event.
21636:S 23 Jan 04:40:33.031 * Master replied to PING, replication can continue...
21636:S 23 Jan 04:40:33.032 * Partial resynchronization not possible (no cached master)
21636:S 23 Jan 04:40:33.038 * Full resync from master: 4bfec2e8871336c8c86bb37a8b16905d57d5278b:0
21636:S 23 Jan 04:40:33.743 * MASTER <-> SLAVE sync: receiving 4469862 bytes from master
21636:S 23 Jan 04:40:33.772 * MASTER <-> SLAVE sync: Flushing old data
21636:S 23 Jan 04:40:33.785 * MASTER <-> SLAVE sync: Loading DB in memory
21636:S 23 Jan 04:40:34.032 * MASTER <-> SLAVE sync: Finished with success
21636:S 23 Jan 04:40:34.033 * Background append only file rewriting started by pid 21640
21636:S 23 Jan 04:40:34.387 * AOF rewrite child asks to stop sending diffs.
21640:C 23 Jan 04:40:34.387 * Parent agreed to stop sending diffs. Finalizing AOF...
21640:C 23 Jan 04:40:34.387 * Concatenating 0.00 MB of AOF diff received from parent.
21640:C 23 Jan 04:40:34.388 * SYNC append only file rewrite performed
21640:C 23 Jan 04:40:34.389 * AOF rewrite: 10 MB of memory used by copy-on-write
21636:S 23 Jan 04:40:34.439 * Background AOF rewrite terminated with success
21636:S 23 Jan 04:40:34.439 * Residual parent diff successfully flushed to the rewritten AOF (0.00 MB)
21636:S 23 Jan 04:40:34.439 * Background AOF rewrite finished successfully
从输出日志看出,从库启动后,会跟主库进行数据同步,把主库的数据同步过来,主库运行一段时间后,从库也会把主库的历史数据同步过来。
4.验证
主库的数据大小
[root@pxc01 src]# ./redis-cli -h 192.168.56.91 -p 8379
192.168.56.91:8379> dbsize
(integer) 300000
从库的数据大小
[root@pxc02 src]# ./redis-cli -h 192.168.56.92 -p 8379
192.168.56.92:8379> dbsize
(integer) 300000
5.故障切换
当主库当掉之后,可以执行如下命令将从库升级为主库
192.168.56.92:8379> slaveof no one
OK
等主库好了之后可以执行如下命令将当期的主库设置为从库
192.168.56.92:8379> slaveof 192.168.56.91 8379
OK
主库宕机到恢复完成的时间里,之前从库升级主库写入的数据,重新建立主从关系后,这些数据会被覆盖,因为从新建立了主从关系,从库重新同步一份之前主库的数据过来。
-----------------------------Sentine哨兵模式安装-----------------------
创建好主从之后,我们接下部署sentine哨兵模式监控主从的状态,哨兵模式的作用就是监控主从状态,发现主库宕机后,从备库中挑选一个出来当选主库,对外部应用继续提高访问服务。
1.安装
安装步骤跟主从安装redis一样,这里省略
2.配置文件
sentinel.conf
-
port 26379
-
dir "/tmp"
-
sentinel myid 3154c22b52f5fdca833d7a972bb0104e11e63b82
-
sentinel monitor mymaster 192.168.56.91 8379 1
-
sentinel config-epoch mymaster 1
-
sentinel leader-epoch mymaster 1
-
sentinel down-after-milliseconds mymaster 15000
-
logfile "/opt/redis_sentinel/log/sentinel.log"
-
protected-mode no
关键的参数:
port 端口号,哨兵程序运行的端口号
sentinel monitor mymaster 192.168.56.91 8379 1
mymaster:哨兵监控redis组叫mymaster,这个名字可以任意命名,符合命名要求即可
192.168.56.91 8379:这个是哨兵监控的主库的ip和端口号
1:表示仲裁值,这里表示有1个哨兵发现主库无法访问的时候,就可以从从库中选举新的主库,在多个哨兵的环境下,这个值可以根据需要设置,比如2,3等,意味着至少需要有2、3个哨兵都发现主库无法访问的情况下才重新选举主库。
sentinel down-after-milliseconds mymaster 15000
----------这个是sentinel连接master的超时时间,超过这个时间就认为master挂了,实现自动切换。这个默认是30秒,这个时间得调节好,大了会在真正出现故障的时候切换时间会长,小了有时候master由于持久化数据,繁忙不响应,会导致自动切换,实际只是瞬间不可用,现在认为设置为15秒为宜.
3.启动
/opt/redis/src/redis-sentinel /opt/redis/conf/sentinel.conf
4.登陆哨兵查看主从情况
./redis-cli -h 192.168.56.93 -p 26379
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.56.91:8379,slaves=1,sentinels=1
5.外部应用同步哨兵访问集群
通常情况下,我们通过JedisSentinelPool创建跟集群的连接,这样的话,主从关系改变了,我们的应用程序也不用修改连接,但是主从改变的过程中,外部应用不能访问.
-
package com.hxl;
-
-
import java.util.HashSet;
-
import java.util.Set;
-
-
import redis.clients.jedis.Jedis;
-
import redis.clients.jedis.JedisSentinelPool;
-
-
public class SentineTest {
-
-
public static void main(String[] args) {
-
-
Set<String> sentinels = new HashSet<String>();
-
String hostAndPort1 = "192.168.56.93:26379";
-
sentinels.add(hostAndPort1);
-
String clusterName = "mymaster" ;
-
JedisSentinelPool redisSentinelJedisPool = new JedisSentinelPool(clusterName,sentinels);
-
Jedis jedis = null;
-
try {
-
jedis = redisSentinelJedisPool.getResource();
-
jedis.set("test4", "aaa");
-
System.out.println(jedis.get("test4"));
-
//System.out.println(jedis.get("test2"));
-
} catch (Exception e) {
-
e.printStackTrace();
-
} finally {
-
jedis.close();
-
}
-
redisSentinelJedisPool.close();
-
}
-
-
}
String hostAndPort1 = "192.168.56.93:26379" ##这里是指定哨兵所在的机器ip和占用的端口;
String clusterName = "mymaster" ; ##这里要跟服务器设置的一致.
通常情况下,我们会部署多个哨兵,避免一个哨兵宕机了导致外部应用无法访问,部署多个哨兵的部署跟部署一个哨兵的步骤一致,只是配置文件中仲裁值根据需要设定。
--The End --
阅读(9294) | 评论(0) | 转发(0) |