화요일, 3월 18, 2025
HomeLinuxRedis Replication

Redis Replication

Redis Replication

Redis 의 FailOver 상황을 대비한 replication을 구성해 보겠습니다.

master 는 (read/write) 용이고, slave 는 master 의 데이터를 미러링하고 있는 read 전용입니다.

 

Redis Replication 아래와 같이 Master 1대, Slave 3대로 구성합니다.

Master : 10.0.0.201
Slave #1 : 10.0.0.211
Slave #2 : 10.0.0.212
Slave #3 : 10.0.0.213

Master 1대, Slave 3대에는 모두 Redis가 기본 설치 되어있어야 합니다.

Redis 기본 설치가 안되었으면 Redis 설치 클릭후 설치를 진행하시면 됩니다.

먼저 Master 구성 부터 진행하겠습니다.

1.Redis Replication Master Config

vi /etc/redis/6379.conf
  • vi 로 아래와 같이 수정합니다.
bind 0.0.0.0 			# redis Listen 대역을 0.0.0.0으로 수정
requirepass password 		# redis password
masterauth password 		# requirepass 동일한 암호
repl-ping-slave-period 10 	# 주석 해제 [마스터 서버와 동기화 주기]
repl-timeout 60 		# 주석 해제
  • Redis 종료시 암호을 요구하기 때문에 redis start 스크립드에 암호 추가
vi /etc/init.d/redis_6379
  • stop function 부분을 수정하시면 됩니다.
stop)
         if [ ! -f $PIDFILE ]
         then
            echo "$PIDFILE does not exist, process is not running"
         else
            PID=$(cat $PIDFILE)
            echo "Stopping ..."
            $CLIEXEC -p $REDISPORT -a password shutdown    # shutdown 시 암호입력
            while [ -x /proc/${PID} ]
            do
               echo "Waiting for Redis to shutdown ..."
               sleep 1
            done
            echo "Redis stopped"
         fi
         ;;

2.Redis Replication Slave Config

vi /etc/redis/6379.conf
  • vi 로 아래와 같이 수정합니다.
replicaof 10.0.0.201 6379	# master ip port
bind 0.0.0.0 			# redis Listen 대역을 0.0.0.0으로 수정
requirepass password 		# redis password
masterauth password 		# requirepass 동일한 암호
repl-ping-slave-period 10 	# 주석 해제 [마스터 서버와 동기화 주기]
repl-timeout 60 		# 주석 해제
  • Redis 종료시 암호을 요구하기 때문에 redis start 스크립드에 암호 추가
stop)
         if [ ! -f $PIDFILE ]
         then
            echo "$PIDFILE does not exist, process is not running"
         else
            PID=$(cat $PIDFILE)
            echo "Stopping ..."
            $CLIEXEC -p $REDISPORT -a password shutdown    # shutdown 시 암호입력
            while [ -x /proc/${PID} ]
            do
               echo "Waiting for Redis to shutdown ..."
               sleep 1
            done
            echo "Redis stopped"
         fi
         ;;

3.Redis Replication 확인

Redis Master [10.0.0.201] 을 시작합니다.

/etc/init.d/redis_6379 start
  • tail 명령어로 로그를 확인합니다.
tail -50f /var/log/redis_6379.log 
3004:M 18 Apr 2019 00:45:44.290 * Starting BGSAVE for SYNC with target: disk
3004:M 18 Apr 2019 00:45:44.290 * Background saving started by pid 3011
3011:C 18 Apr 2019 00:45:44.293 * DB saved on disk
3011:C 18 Apr 2019 00:45:44.294 * RDB: 0 MB of memory used by copy-on-write
3004:M 18 Apr 2019 00:45:44.320 * Background saving terminated with success
3004:M 18 Apr 2019 00:45:44.320 * Synchronization with replica 10.0.0.211:6379 succeeded
3004:M 18 Apr 2019 00:50:45.362 * Replica 10.0.0.212:6379 asks for synchronization
3004:M 18 Apr 2019 00:50:45.362 * Partial resynchronization request from 10.0.0.212:6379 accepted. Sending 420 bytes of backlog starting from offset 1.
3004:M 18 Apr 2019 00:52:02.065 * Replica 10.0.0.213:6379 asks for synchronization
3004:M 18 Apr 2019 00:52:02.065 * Partial resynchronization request from 10.0.0.213:6379 accepted. Sending 518 bytes of backlog starting from offset 1.

Redis Salve [10.0.0.211] 을 시작합니다.

/etc/init.d/redis_6379 start
  • tail 명령어로 로그를 확인합니다.
tail -50f /var/log/redis_6379.log 
2599:S 18 Apr 2019 00:45:44.032 # Server initialized
   2599:S 18 Apr 2019 00:45:44.032 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
   2599:S 18 Apr 2019 00:45:44.033 * DB loaded from disk: 0.001 seconds
   2599:S 18 Apr 2019 00:45:44.033 * Before turning into a replica, using my master parameters to synthesize a cached master: I may be able to synchronize with the new master with just a partial transfer.
   2599:S 18 Apr 2019 00:45:44.033 * Ready to accept connections
   2599:S 18 Apr 2019 00:45:44.033 * Connecting to MASTER 10.0.0.201:6379
   2599:S 18 Apr 2019 00:45:44.033 * MASTER <-> REPLICA sync started
   2599:S 18 Apr 2019 00:45:44.034 * Non blocking connect for SYNC fired the event.
   2599:S 18 Apr 2019 00:45:44.034 * Master replied to PING, replication can continue...
   2599:S 18 Apr 2019 00:45:44.035 * Trying a partial resynchronization (request 2c8413e454e7a5e3ead2b9b66304e7aaa1541edd:1).
   2599:S 18 Apr 2019 00:45:44.036 * Full resync from master: bf17ce558a05d7ecf614ebb8077b019134278479:0
   2599:S 18 Apr 2019 00:45:44.036 * Discarding previously cached master state.
   2599:S 18 Apr 2019 00:45:44.065 * MASTER <-> REPLICA sync: receiving 189 bytes from master
   2599:S 18 Apr 2019 00:45:44.065 * MASTER <-> REPLICA sync: Flushing old data
   2599:S 18 Apr 2019 00:45:44.065 * MASTER <-> REPLICA sync: Loading DB in memory
   2599:S 18 Apr 2019 00:45:44.065 * MASTER <-> REPLICA sync: Finished with success

Redis Salve [10.0.0.212]을 시작합니다.

/etc/init.d/redis_6379 start
  • tail 명령어로 로그를 확인합니다.
2598:S 18 Apr 2019 00:52:30.788 # Server initialized
   2598:S 18 Apr 2019 00:52:30.788 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
   2598:S 18 Apr 2019 00:52:30.789 * DB loaded from disk: 0.000 seconds
   2598:S 18 Apr 2019 00:52:30.789 * Before turning into a replica, using my master parameters to synthesize a cached master: I may be able to synchronize with the new master with just a partial transfer.
   2598:S 18 Apr 2019 00:52:30.789 * Ready to accept connections
   2598:S 18 Apr 2019 00:52:30.789 * Connecting to MASTER 10.0.0.201:6379
   2598:S 18 Apr 2019 00:52:30.789 * MASTER <-> REPLICA sync started
   2598:S 18 Apr 2019 00:52:30.789 * Non blocking connect for SYNC fired the event.
   2598:S 18 Apr 2019 00:52:30.790 * Master replied to PING, replication can continue...
   2598:S 18 Apr 2019 00:52:30.791 * Trying a partial resynchronization (request bf17ce558a05d7ecf614ebb8077b019134278479:519).
   2598:S 18 Apr 2019 00:52:30.791 * Successful partial resynchronization with master.
   2598:S 18 Apr 2019 00:52:30.791 * MASTER <-> REPLICA sync: Master accepted a Partial Resynchronization.

Redis Salve [10.0.0.213]을 시작합니다.

/etc/init.d/redis_6379 start
  • tail 명령어로 로그를 확인합니다.
2588:S 18 Apr 2019 00:53:17.777 # Server initialized
   2588:S 18 Apr 2019 00:53:17.777 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
   2588:S 18 Apr 2019 00:53:17.777 * DB loaded from disk: 0.001 seconds
   2588:S 18 Apr 2019 00:53:17.777 * Before turning into a replica, using my master parameters to synthesize a cached master: I may be able to synchronize with the new master with just a partial transfer.
   2588:S 18 Apr 2019 00:53:17.777 * Ready to accept connections
   2588:S 18 Apr 2019 00:53:17.777 * Connecting to MASTER 10.0.0.201:6379
   2588:S 18 Apr 2019 00:53:17.777 * MASTER <-> REPLICA sync started
   2588:S 18 Apr 2019 00:53:17.778 * Non blocking connect for SYNC fired the event.
   2588:S 18 Apr 2019 00:53:17.778 * Master replied to PING, replication can continue...
   2588:S 18 Apr 2019 00:53:17.779 * Trying a partial resynchronization (request bf17ce558a05d7ecf614ebb8077b019134278479:589).
   2588:S 18 Apr 2019 00:53:17.780 * Successful partial resynchronization with master.
   2588:S 18 Apr 2019 00:53:17.780 * MASTER <-> REPLICA sync: Master accepted a Partial Resynchronization.

4.데이터를 확인

제대로 replication 이 되고 있다면, master 에 값을 넣었을 때 slave 들에도 해당 값이 들어가면 되겠습니다.
master 에 ‘foo’ 라는 key 로 값을 넣어 보겠습니다.

# Redis Master [10.0.0.201]

   /usr/local/bin/redis-cli -a password
   Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
   127.0.0.1:6379> set foo var
   OK
   127.0.0.1:6379> get foo
   "var"
   127.0.0.1:6379> 

# Redis Slave [10.0.0.211]

   /usr/local/bin/redis-cli -a password
   Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
   127.0.0.1:6379> get foo
   "var"
   127.0.0.1:6379>

slave 기본적으로 read 권한만 있습니다. 확인해보겠습니다.

127.0.0.1:6379> set foo var
(error) READONLY You can't write against a read only replica.
127.0.0.1:6379> 

 

Previous article
Next article
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular