[root@redis-1 redis]# pwd
/data/redis
[root@redis-1 redis]# tree ./
./
├── bin
│ ├── dump.rdb
│ ├── redis-benchmark
│ ├── redis-check-aof
│ ├── redis-check-rdb
│ ├── redis-cli
│ ├── redis-sentinel -> redis-server
│ └── redis-server
├── conf
│ ├── redis.conf
│ └── sentinel.conf
├── log
├── redis.log
└── sentinel.log
3 directories, 13 files
[root@redis-1 redis]#
配置环境变量,修改/etc/profile,加入以下内容
export REDIS_HOME=/data/redis
export PATH=$PATH:$REDIS_HOME/bin
source /etc/profile重载一下
2、 配置redis哨兵集群
redis配置文件及路径:
[root@redis-1 conf]# pwd
/data/redis/conf
[root@redis-1 conf]# grep -Ev "^#" redis.conf | grep -Ev "^$"
bind 0.0.0.0
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile "/data/redis/redis.pid"
loglevel notice
logfile "/data/redis/log/redis.log"
databases 16
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
dir "/data/redis/bin"
slave-serve-stale-data yes
slave-read-only no
slave-priority 100
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
maxclients 10000
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
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
[root@redis-1 conf]#
sentinel配置文件及路径:
[root@redis-1 conf]# pwd
/data/redis/conf
[root@redis-1 conf]# grep -Ev "^#" sentinel.conf | grep -Ev "^$"
protected-mode no
port 27000
daemonize yes
pidfile "/data/redis/redis-sentinel.pid"
logfile "/data/redis/log/sentinel.log"
dir "/data/redis/bin"
sentinel deny-scripts-reconfig yes
sentinel monitor redis-master 172.20.20.141 6379 2
sentinel auth-pass redis-master 1234@abcd
[root@redis-1 conf]#
三台配置相同,启动服务:
#启动redis
[root@redis-1 ~]# redis-server /data/redis/conf/redis.conf
#启动哨兵
[root@redis-1 ~]# redis-sentinel /data/redis/conf/sentinel.conf
查看服务启动和端口监听状态
[root@redis-1 ~]# netstat -tunlap| grep redis
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 15778/bin/redis-ser
tcp 0 0 0.0.0.0:27000 0.0.0.0:* LISTEN 15799/bin/redis-sen
如果三台都是上面的状态说明启动正常,下面建立主从关系
分别在redis-2,redis-3上执行,目的就是和redis-1建立主从关系
redis-2
[root@redis-2 ~]# redis-cli -p 6379 SLAVEOF 172.10.10.11 6379
redis-3
[root@redis-3 ~]# redis-cli -p 6379 SLAVEOF 172.10.10.11 6379
执行完成后查看sentinel的配置文件就会发现最下面会多出类似的配置
[root@redis-1 conf]# grep -Ev "^#" sentinel.conf | grep -Ev "^$"
protected-mode no
port 27000
daemonize yes
pidfile "/data/redis/redis-sentinel.pid"
logfile "/data/redis/log/sentinel.log"
dir "/data/redis/bin"
sentinel myid df525620efbabc0df82a610faeafed70b5025095
sentinel deny-scripts-reconfig yes
sentinel monitor redis-master 172.10.10.11 6379 2
sentinel auth-pass redis-master 1234@abcd
sentinel config-epoch redis-master 0
sentinel leader-epoch redis-master 0
sentinel known-replica redis-master 172.10.10.12 6379
sentinel known-replica redis-master 172.10.10.13 6379
sentinel known-sentinel redis-master 172.10.10.12 27000 86a3172bfbde57f68c2e403b8b16a7d7c9e3dd1c
sentinel known-sentinel redis-master 172.10.10.13 27000 3f2c8899b4eb5dd19472433f9db317aac9416a9d
sentinel current-epoch 3
[root@redis-1 conf]#
3、设置访问密码
在redis.conf文件中加入两个参数
masterauth 1234@abcd
requirepass 1234@abcd
设置访问密码为1234@abcd,配置好重启服务生效
测试配置是否生效
[root@redis-1 ~]# redis-cli
127.0.0.1:6379> auth 1234@abcd
OK
127.0.0.1:6379> set aaa 111
OK
127.0.0.1:6379> get aaa
"111"
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=172.10.10.13,port=6379,state=online,offset=43010342,lag=1
slave1:ip=172.10.10.12,port=6379,state=online,offset=43010487,lag=0
master_replid:ba4e9e12a32298a43b4d2b899b289e4fe37776bd
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:43010632
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:41962057
repl_backlog_histlen:1048576
127.0.0.1:6379>