天天看點

Redis 主從+哨兵模式安裝

環境:CENTOS7.4

Redis版本:4.0.8

Redis 主從+哨兵模式安裝

1、安裝依賴包

yum -y install gcc tcl

2、解壓redis源碼,并進行安裝

上傳源碼包 redis-4.0.8.tar.gz,拷貝到/usr/local/src目錄下

cd /usr/local/src

tar -xzvf redis-4.0.8.tar.gz

cd redis-4.0.8/

2.1 make

遇到警告資訊如下:????

ldo.c: In function ‘f_parser’:

ldo.c:496:7: warning: unused variable ‘c’ [-Wunused-variable]

   int c = luaZ_lookahead(p->z);

[[email protected] redis-4.0.8]# make

2.2 make test進行驗證

[[email protected] redis-4.0.8]# make test

…..省略

  198 seconds - integration/replication

  146 seconds - unit/obuf-limits

\o/ All tests passed without errors!

Cleanup: may take some time... OK

make[1]: Leaving directory `/usr/local/src/redis-4.0.8/src'

2.3 make install

[[email protected] redis-4.0.8]# make install

注:該步驟實際是把src目錄下可執行檔案拷貝到/usr/local/bin

  • redis-benchmark 
  • redis-check-aof 
  • redis-check-rdb 
  • redis-cli 
  • redis-sentinel 
  • redis-server

3、redis配置(192.168.56.4)

mkdir -p /opt/redis/6379 && cp redis.conf /opt/redis/6379/6379.conf

mkdir -p /opt/redis/6380 && cp redis.conf /opt/redis/6380/6380.conf

mkdir -p /opt/redis/6381 && cp redis.conf /opt/redis/6381/6381.conf

vim /opt/redis/6379/6379.conf

vim /opt/redis/6380/6380.conf

vim /opt/redis/6381/6381.conf

配置檔案内容:

protected-mode no

port 6379

tcp-backlog 511

timeout 0

tcp-keepalive 300

daemonize yes

supervised no

pidfile "/opt/redis/6379/redis_6379.pid"

loglevel notice

logfile "/opt/redis/6379.log"

databases 16

always-show-logo yes

save 900 1

save 300 10

save 60 10000

stop-writes-on-bgsave-error yes

rdbcompression yes

rdbchecksum yes

dbfilename "dump.rdb"

dir "/opt/redis"

masterauth "123456"

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

lazyfree-lazy-eviction no

lazyfree-lazy-expire no

lazyfree-lazy-server-del no

slave-lazy-flush no

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

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

slaveof 192.168.56.5 6379

1.bind 1270.0.1 (登出掉該語句則偵聽所有的IP,或者改為伺服器偵聽的IP位址)

2.port 6380(端口号)

3.daemonize yes(守護程序)

4.pidfile "/var/run/redis_6380.pid"(程序的目錄)

5.slaveof 192.168.200.108 6380(設定為此redis為slave,端口号是6380)  此隻需要在從庫上配置即可

cd /usr/local/src/redis-4.0.8/src

/usr/local/src/redis-4.0.8/src/redis-server /opt/redis/6379/6379.conf

/usr/local/src/redis-4.0.8/src/redis-server /opt/redis/6380/6380.conf

/usr/local/src/redis-4.0.8/src/redis-server /opt/redis/6381/6381.conf

4、哨兵配置

port 26379

dir "/tmp"

sentinel myid 446bca53fde07f5d6df5624a7d691f2acdf07023

sentinel monitor mymaster 192.168.56.5 6379 1

sentinel down-after-milliseconds mymaster 3000

sentinel failover-timeout mymaster 10000

sentinel auth-pass mymaster 123456

sentinel config-epoch mymaster 2

sentinel leader-epoch mymaster 2

daemonize yes

sentinel known-slave mymaster 192.168.56.4 6379

sentinel current-epoch 2

5、驗證

[[email protected] redis]# redis-cli -p 6379

127.0.0.1:6379> info replication

# Replication

role:master

connected_slaves:1

slave0:ip=192.168.56.5,port=6379,state=online,offset=252,lag=0

master_replid:d0529fe95b618ea03a7771296c06eaa8fd0f511c

master_replid2:0000000000000000000000000000000000000000

master_repl_offset:266

second_repl_offset:-1

repl_backlog_active:

repl_backlog_size:1048576

repl_backlog_first_byte_offset:1

repl_backlog_histlen:266

127.0.0.1:6379>

MASTER當機,主從切換後,即使原來主恢複正常,也不會變成主,角色會變成從。

SALVE節點當機,不影響主和其他從,恢複正常後可自動加入叢集

繼續閱讀