Centos7 安装redis主从及配置哨兵模式

Centos7 安装redis主从及配置哨兵模式

ip

10.6.11.164   主

10.6.11.189   从

10.6.11.201   从

 

安装redis

安装依赖

yum install gcc gcc-c++ -y

 

下载redis-4.0.11.tar.gz

cd /usr/local

wget http://download.redis.io/releases/redis-4.0.11.tar.gz

 

3台均按以下命令安装redis

cd /usr/local

tar -xzvf redis.tar.gz

cd redis

 

make MALLOC=libc

make

make install

 

配置redis 主从

修改master 10.6.11.164的redis.conf

daemonize yes

pidfile "/var/run/redis.pid"

port 6379

tcp-backlog 511

timeout 0

tcp-keepalive 0

loglevel notice

logfile "/usr/local/redis/redis.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 "/usr/local/redis"

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

requirepass "[email protected]"

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-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

masterauth "[email protected]"

protected-mode yes

 

修改slave 10.6.11.189和10.6.11.201的/usr/local/redis/redis.conf

daemonize yes

pidfile "/var/run/redis.pid"

port 6379

tcp-backlog 511

timeout 0

tcp-keepalive 0

loglevel notice

logfile "/usr/local/redis/redis.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 "/usr/local/redis"

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

requirepass "[email protected]"

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-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

masterauth "[email protected]"

protected-mode yes

slaveof 10.6.11.164 6379

 

依次启动redis

率先启动master redis  然后依次启动slave

/usr/local/redis/src/redis-server /usr/local/redis/redis.conf

 

可以在/usr/local/redis/redis.log 中 看到3台机子连接成功的信息

Centos7 安装redis主从及配置哨兵模式

 

也可以在redis中 添加key   测试是否会同步

在10.6.11.164上执行如下命令

Centos7 安装redis主从及配置哨兵模式

 

 

 

 

在slave机子上执行如下操作,会看到slave 上有和master 一样的key 名,value也一致

Centos7 安装redis主从及配置哨兵模式

 

配置sentinel 哨兵

配置主master的/usr/local/redis/sentinel.conf

protected-mode no

sentinel deny-scripts-reconfig yes

sentinel monitor mymaster 10.6.11.164 6379 2

sentinel down-after-milliseconds mymaster 5000

sentinel failover-timeout mymaster 15000

sentinel auth-pass mymaster [email protected]

# Generated by CONFIG REWRITE

port 26379

dir "/usr/local/redis"

sentinel config-epoch mymaster 1

sentinel leader-epoch mymaster 1

sentinel known-slave mymaster 10.6.11.189 6379

sentinel known-slave mymaster 10.6.11.201 6379

sentinel current-epoch 1

sentinel announce-ip "10.6.11.164"

 

配置从slave的/usr/local/redis/sentinel.conf

protected-mode no

sentinel myid 3750d9547cf4e99c142984032492d43908ed8790

sentinel deny-scripts-reconfig yes

sentinel monitor mymaster 10.6.11.164 6379 2

sentinel down-after-milliseconds mymaster 5000

sentinel failover-timeout mymaster 15000

sentinel auth-pass mymaster [email protected]

# Generated by CONFIG REWRITE

port 26379

dir "/usr/local/redis"

sentinel config-epoch mymaster 1

sentinel leader-epoch mymaster 1

sentinel known-slave mymaster 10.6.11.189 6379

sentinel known-slave mymaster 10.6.11.201 6379

sentinel current-epoch 1

sentinel announce-ip "10.6.11.164"

 

 

完成配置之后  依次启动哨兵sentinel

首先启动master  然后slave

/usr/local/redis/src/redis-sentinel /usr/local/redis/sentinel.conf &

 

 

启动完毕后可以用如下命令查看哨兵信息

redis-cli -p 26379 INFO Sentinel 

Centos7 安装redis主从及配置哨兵模式

 

测试杀死master redis 进程

Centos7 安装redis主从及配置哨兵模式

查看日志   master 重新选举10.6.11.189为新master

Centos7 安装redis主从及配置哨兵模式

 

再次查看哨兵信息   master 已经变化

Centos7 安装redis主从及配置哨兵模式

 

重新启动10.6.11.164的redis  查看信息

master 并没有变回去  依旧是189为master

Centos7 安装redis主从及配置哨兵模式

 

 

同时redis.conf和sentinel.conf 也都发生了变化

Centos7 安装redis主从及配置哨兵模式

 

Centos7 安装redis主从及配置哨兵模式

 

 

至此 redis集群和哨兵均安装完毕