概要
スレーブのコンフィグにマスターの情報を記述して、Redisを再起動するだけ。
なおスレーブは読み込み専用になり、書き込みはできなくなる。
127.0.0.1:6379> set key1 val1 (error) READONLY You can't write against a read only slave.
障害時やメンテナンスでフェイルオーバーさせる場合は、手動でマスター/スレーブの情報を変更(slaveof no one
でスレーブ解除、slaveof new_master_ip new_master_port
で新マスター指定)するか、sentinelを使う必要があるっぽい。
スレーブ側のconfig修正
slaveof
にマスターサーバの情報(IPとPort)を記述する
slaveof 192.168.22.11 6379
redis再起動
# systemctl restart redis
確認
マスター側
- レプリケーションの設定確認
# redis-cli info replication # Replication role:master connected_slaves:1 slave0:ip=192.168.22.12,port=6379,state=online,offset=56,lag=1 master_replid:9778dff7ac400670b6c48c9f03ebdcb6173ee3dd master_replid2:0000000000000000000000000000000000000000 master_repl_offset:56 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:56
- ログ確認
9716:M 05 Apr 00:19:48.743 * Slave 192.168.22.12:6379 asks for synchronization 9716:M 05 Apr 00:19:48.743 * Full resync requested by slave 192.168.22.12:6379 9716:M 05 Apr 00:19:48.743 * Starting BGSAVE for SYNC with target: disk 9716:M 05 Apr 00:19:48.743 * Background saving started by pid 9763 9763:C 05 Apr 00:19:48.745 * DB saved on disk 9763:C 05 Apr 00:19:48.745 * RDB: 0 MB of memory used by copy-on-write 9716:M 05 Apr 00:19:48.751 * Background saving terminated with success 9716:M 05 Apr 00:19:48.751 * Synchronization with slave 192.168.22.12:6379 succeeded
スレーブ側
- レプリケーションの設定確認
# redis-cli info replication # Replication role:slave master_host:192.168.22.11 master_port:6379 master_link_status:up master_last_io_seconds_ago:10 master_sync_in_progress:0 slave_repl_offset:28 slave_priority:100 slave_read_only:1 connected_slaves:0 master_replid:9778dff7ac400670b6c48c9f03ebdcb6173ee3dd master_replid2:0000000000000000000000000000000000000000 master_repl_offset:28 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:28
- ログ確認
8468:S 04 Apr 15:12:34.078 * Connecting to MASTER 192.168.22.11:6379 8468:S 04 Apr 15:12:34.079 * MASTER <-> SLAVE sync started 8468:S 04 Apr 15:12:34.079 * Non blocking connect for SYNC fired the event. 8468:S 04 Apr 15:12:34.080 * Master replied to PING, replication can continue... 8468:S 04 Apr 15:12:34.081 * Partial resynchronization not possible (no cached master) 8468:S 04 Apr 15:12:34.082 * Full resync from master: 9778dff7ac400670b6c48c9f03ebdcb6173ee3dd:0 8468:S 04 Apr 15:12:34.144 * MASTER <-> SLAVE sync: receiving 175 bytes from master 8468:S 04 Apr 15:12:34.145 * MASTER <-> SLAVE sync: Flushing old data 8468:S 04 Apr 15:12:34.145 * MASTER <-> SLAVE sync: Loading DB in memory 8468:S 04 Apr 15:12:34.145 * MASTER <-> SLAVE sync: Finished with success