관리 메뉴

IT.FARMER

Redis Replication (Master Slave) 구성 본문

NoSQL/Redis

Redis Replication (Master Slave) 구성

아이티.파머 2020. 1. 10. 16:26
반응형

2020/01/10 - [NoSQL/Redis] - Redis 특징

2020/01/10 - [NoSQL/Redis] - Docker Redis

2020/01/10 - [NoSQL/Redis] - Spring Boot Redis (standard)

2020/01/10 - [NoSQL/Redis] - Redis install linux

2020/01/10 - [NoSQL/Redis] - Redis Replication (Master Slave) 구성

2020/01/10 - [NoSQL/Redis] - Redis Sentinel 구성

2020/01/10 - [NoSQL/Redis] - Redis Clustering 구성

2020/01/13 - [NoSQL/Redis] - Spring boot redis Sentinel Config

2020/01/13 - [NoSQL/Redis] - spring boot redis clustering config

 

 

Redis replacation

Master , Slave 구성

Redis에서 제공해주는 스크립트를 이용하여 Master 와 slave 의 골격을 만든다 ${REDIS_HOME}/usils/install_server.sh 기본적으로 master(6379). slave1(6380). slave2(6381) 로 만든다

/usils/install_server.sh

Please select the redis port for this instance : 6379

Port           : 6379
Config file    : /etc/redis/6379.conf
Log file       : /var/log/redis_6379.log
Data dir       : /var/lib/redis/6379
Executable     : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379

# 6379 6380 6381 세번 반복하여 Master slave 구성
/usils/install_server.sh
Please select the redis port for this instance : 6380
...

/usils/install_server.sh
Please select the redis port for this instance : 6381
...

# redis process check
ps -ef | grep redis 

root      5492     1  0 16:21 ?        00:00:01 /usr/local/bin/redis-server 127.0.0.1:6379
root      5605     1  0 16:27 ?        00:00:00 /usr/local/bin/redis-server 127.0.0.1:6380
root      5724     1  0 16:31 ?        00:00:00 /usr/local/bin/redis-server 127.0.0.1:6381

 

redis shut down

redis-cli -p 6379 shutdown
redis-cli -p 6380 shutdown
redis-cli -p 6381 shutdown


# 참고
# master node 로사용시 password 사용시 옵션 -a 사용
redis-cli -p 6379 -a foobared shutdown

 

redis 실행

cd /etc/redis
redis-server ./6379.conf & redis-server ./6380.conf &  redis-server ./6381.conf &

 

Replication config 설정

Master (6379) 설정

master .conf 을 열어 주석 해제후 패스워드 부분을 수정 한다.

vim /etc/redis/6379.conf

...
#requirepass foobared
requirepass foobared
...

 

master 에 password 설정을 해주었기 때무에 master node 종료시 -a 옵션으로 패스워드를 입력 하거나, /etc/init.d/redis_6379 의 stop 쉘에 -a foobared 를 추가한다.

# cli 명령어에 패스워드 기입
redis-cli -p 6379 -a foobared shutdown

#shell 수정
....
stop)
	if [ ! -f $PIDFILE ]
	then
		echo "$PIDFILE does not exist, process is not running"
	else
		PID=$(cat $PIDFILE)
		echo "Stopping ..."
		$CLIEXEC -p $REDISPORT -a foobared shutdown # <<< 여기!!
		while [ -x /proc/${PID} ]
		do
			echo "Waiting for Redis to shutdown ..."
			sleep 1
		done
		echo "Redis stopped"
	fi

 

Slave (6380,6381) 설정

해당 파일들에서 slaveof, masterauth값을 찾아 아래처럼 바꿔줍니다.

vim /etc/redis/6380.conf
vim /etc/redis/6381.conf

replicaof 127.0.0.1 6379
masterauth foobared

Master log 확인

6380 , 6381 slave 가 replica 성공되었다는 메세지 를 확인 할 수 있다.

[root@-208 redis]# tail -f /var/log/redis_6379.log
980:M 08 Jan 2020 17:23:32.078 * Starting BGSAVE for SYNC with target: disk
980:M 08 Jan 2020 17:23:32.078 * Background saving started by pid 991
980:M 08 Jan 2020 17:23:32.078 * Replica 127.0.0.1:6381 asks for synchronization
980:M 08 Jan 2020 17:23:32.078 * Full resync requested by replica 127.0.0.1:6381
980:M 08 Jan 2020 17:23:32.078 * Waiting for end of BGSAVE for SYNC
991:C 08 Jan 2020 17:23:32.109 * DB saved on disk
991:C 08 Jan 2020 17:23:32.110 * RDB: 0 MB of memory used by copy-on-write
980:M 08 Jan 2020 17:23:32.178 * Background saving terminated with success
980:M 08 Jan 2020 17:23:32.178 * Synchronization with replica 127.0.0.1:6380 succeeded
980:M 08 Jan 2020 17:23:32.178 * Synchronization with replica 127.0.0.1:6381 succeeded

 

Data Replication test

Master 에서 쓰여진 데이터를 Slave Node 에서 확인 할 수 있다.

[root@test-208 ~]$ redis-cli -p 6379 -a foobared
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> set 1 test
OK
127.0.0.1:6379> get 1
"test"
127.0.0.1:6379> exit

[root@test-208 ~]$ redis-cli -p 6380 -a foobared
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6380> get 1
"test"
127.0.0.1:6380>

# 참고
# replica mode는  읽기만 되어 있다. 쓰기 기능 실행시 error
127.0.0.1:6380> set 2 test2
(error) READONLY You can't write against a read only replica.

 

외부 접속 허용

외부 아이피에서 redis 에 접속 할 수 있도록 하기 위해서는 각각의 conf 파일에 bind 설정을 해주고 재시작 한다.

 

bind 0.0.0.0
#pretected-mode yes
반응형

'NoSQL > Redis' 카테고리의 다른 글

Redis Clustering 구성  (0) 2020.01.10
Redis Sentinel 구성  (0) 2020.01.10
Redis install linux  (0) 2020.01.10
Spring Boot Redis (standard)  (0) 2020.01.10
Redis 특징  (0) 2020.01.10