Step 1:Redis的下载安装
官网下载redis 解压并安装:
[root@mycomputer ~]# cd /home/
[root@mycomputer home]# wget http://download.redis.io/releases/redis-4.0.11.tar.gz
[root@mycomputer home]# tar -zxvf -C redis-4.0.11.tar.gz
[root@mycomputer home]# cd redis-4.0.11/src
[root@mycomputer src]# make
编译中.... ....
CC setproctitle.oCC hyperloglog.oCC latency.oCC sparkline.oLINK redis-serverINSTALL redis-sentinelCC redis-cli.oLINK redis-cliCC redis-benchmark.oLINK redis-benchmarkCC redis-check-dump.oLINK redis-check-dumpCC redis-check-aof.oLINK
redis-check-aof Hint: It’s a good idea to run ‘make test’ ;)
[root@mycomputer src]# make install
安装中...... 完成
安装完成,可以看到此时,src文件夹下出现了一些绿色的文件,这些文件就是我们以后需要用到的命令文件
Step 2:Redis 主从复制的配置
1.在 redis 目录下创建 config 和 data 文件夹:config 用来存放 redis.conf/sentinel.conf 配置文件, data 用来存放 .log 文件和 .rdb 文件
[root@mycomputer redis-4.0.11]# mkdir config
[root@mycomputer redis-4.0.11]# mkdir data
[root@mycomputer redis-4.0.11]# cd config/
2.配置 redis master 端口 7000 : 创建 redis_7000.conf
# Redis configuration file master.
#bind 127.0.0.1
#################### 端口 #############################
port 7000
#################### 是否以守护进程启动 #################
daemonize yes
#################### 进程id文件 ##############
pidfile /var/run/redis_7000.pid
####################### 系统日志 ###############
logfile "redis_7000.log"
###################### rdb数据文件 #############
dbfilename dump_7000.rdb
###################### 工作目录 ###############
dir "/home/redis-4.0.11/data"
######################## 密码 #####################
requirepass 123456
###################### 主要是以上的配置 #################
protected-mode no
tcp-backlog 511
timeout 0
tcp-keepalive 300
supervised no
loglevel notice
databases 16
always-show-logo yes
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
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
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
# appendfsync no
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no
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 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
3.配置 redis slave 端口 7001/7002/7003 ...... : 创建 redis_7001.conf/redis_7002.conf/redis_7003.conf .......
# Redis configuration file slave NO.1.
#bind 127.0.0.1
####################### 主从复制:复制127.0.0.1 的数据 端口7000
slaveof 123.45.57.14 7000
#################### 端口 #############################
port 7001
#################### 是否以守护进程启动 #################
daemonize yes
#################### 进程id文件 ##############
pidfile "/var/run/redis_7001.pid"
####################### 系统日志 ###############
logfile "redis_7001.log"
###################### rdb数据文件 #############
dbfilename "dump_7001.rdb"
###################### 工作目录 ###############
dir "/home/redis-4.0.11/data"
######################## 密码 #####################
requirepass "123456"
# master 节点的密码
masterauth "123456"
###################### 主要是以上的 #################
protected-mode no
tcp-backlog 511
timeout 0
tcp-keepalive 300
supervised no
loglevel notice
databases 16
always-show-logo yes
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
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
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
# appendfsync no
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no
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 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
3.分别启动 master/slave 的 redis 服务:
[root@mycomputer redis-4.0.11]# cd src
[root@mycomputer src]# ./redis-server /home/redis-4.0.11/config/redis_7000.conf
[root@mycomputer src]# ./redis-server /home/redis-4.0.11/config/redis_7001.conf
[root@mycomputer src]# ./redis-server /home/redis-4.0.11/config/redis_7002.conf
这样就启动好了 redis 主从服务
Step 3:Sentinel 高可用配置
在 config 目录下创建多个 sentinel.conf 文件 :sentinel_26379.conf/sentinel_26380.conf/sentinel_26381.conf ....(端口号不同就行)
port 26379
daemonize yes
dir "/home/redis-4.0.11/data"
logfile "sentinel_26379.log"
sentinel myid ac633fe2e30979a6e63e5bc26199ab2ace08f242
sentinel monitor mymaster redis服务ip 7000 2
sentinel auth-pass mymaster 123456
sentinel config-epoch mymaster 1
protected-mode no
[root@mycomputer redis-4.0.11]# cd src
[root@mycomputer src]# ./redis-sentinel /home/redis-4.0.11/config/sentinel_26379.conf
[root@mycomputer src]# ./redis-sentinel /home/redis-4.0.11/config/sentinel_26380.conf
[root@mycomputer src]# ./redis-sentinel /home/redis-4.0.11/config/sentinel_26381.conf
完成此操作即实现了 Redis sentinel 高可用
ps:
1. master /slave 的 redis.conf 最好配置密码(一定要配置密码:否则 JedisSentinelPool 报错)
2. master /slave 的 redis.conf 最好配置的密码相同
3. master /slave 的 redis.conf 配置 protected-mode no(sentinel.conf 里面同样需要配置)