일요일, 5월 26, 2024
HomeLinuxRedis Cluster

Redis Cluster

Redis Cluster

 

Redis Cluster Architecture

레디스 클러스터 목표

Redis Cluster는 설계에서 중요한 순서로 다음 목표를 가진 Redis의 분산 구현입니다.

  • 최대 1000 개의 노드까지 고성능 및 선형 확장 성. 프록시가없고 비동기 복제가 사용되며 값에 대해 병합 작업이 수행되지 않습니다.
  • 허용 가능한 쓰기 안전성 : 시스템은 대부분의 마스터 노드와 연결된 클라이언트에서 발생하는 모든 쓰기를 유지하기 위해 최선을 다합니다. 일반적으로 승인 된 쓰기가 손실 될 수있는 작은 창이 있습니다. 클라이언트가 소수 파티션에있을 때 승인 된 쓰기를 잃는 Windows가 더 큽니다.
  • 가용성 : Redis Cluster는 대부분의 마스터 노드에 도달 할 수 있고 더 이상 도달 할 수없는 모든 마스터 노드에 대해 하나 이상의 도달 가능한 슬레이브가있는 파티션에서 살아남을 수 있습니다. 또한 복제본 마이그레이션을 사용하면 더 이상 슬레이브에 의해 복제되지 않은 마스터가 여러 슬레이브에 의해 보호되는 마스터로부터 마스터를받습니다.
  • 노드 추가, 삭제 시 레디스 클러스터 전체를 중지할 필요 없고, 키 이동 시에만 해당 키에 대해서만 잠시 멈출 수 있습니다.

Redis Cluster 실패 감지

Redis Cluster 실패 감지는 대부분의 노드에서 마스터 또는 슬레이브 노드에 더 이상 도달 할 수없는 경우를 인식 한 다음 슬레이브를 마스터 역할로 승격시켜 응답합니다. 슬레이브 승격이 불가능한 경우 클러스터는 클라이언트로부터 쿼리 수신을 중지하기 위해 오류 상태가됩니다.

Redis Cluster 는 위의 그림과 같이 Master 3대, Slave 3대로 구성합니다.
물리적 서버는 총 3대로 구성되며 각서버당 Master , Slave 를 port만 다르게 구성합니다.

Master 구성
10.0.0.220:6379
10.0.0.221:6379
10.0.0.222:6379

Salve 구성
10.0.0.220:7379
10.0.0.221:7379
10.0.0.222:7379

 

1. Redis Compile 필요 항목설치

yum clean all
yum -y update
yum -y install gcc-c++

2. Redis Master 설치 [Master 3대 모두]

wget http://download.redis.io/releases/redis-stable.tar.gz
tar -xvf redis-stable.tar.gz
cd redis-stable
make
make install
cd utils
./install_server.sh
      Welcome to the redis service installer
      This script will help you easily set up a running redis server

      Please select the redis port for this instance: [6379] 
      Selecting default: 6379
      Please select the redis config file name [/etc/redis/6379.conf] 
      Selected default - /etc/redis/6379.conf
      Please select the redis log file name [/var/log/redis_6379.log] 
      Selected default - /var/log/redis_6379.log
      Please select the data directory for this instance [/var/lib/redis/6379] 
      Selected default - /var/lib/redis/6379
      Please select the redis executable path [] /usr/local/bin/redis-server
      Selected config:
      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
      Installing service...
      Successfully added to chkconfig!
      Successfully added to runlevels 345!
      Starting Redis server...
      Installation successful!

3. Redis cluster 설정 [Master 3대 모두]

  • redis 중지
/etc/init.d/redis_6379 stop
  • redis cluster config 설정
# cluster enable
sed -i 's/# cluster-enabled yes/cluster-enabled yes/g' /etc/redis/6379.conf
sed -i 's/# cluster-config-file nodes-6379.conf/cluster-config-file nodes-6379.conf/g' /etc/redis/6379.conf
sed -i 's/# cluster-node-timeout 15000/cluster-node-timeout 5000/g' /etc/redis/6379.conf
sed -i 's/appendonly no/appendonly yes/g' /etc/redis/6379.conf
sed -i 's/bind 127.0.0.1/bind 0.0.0.0/g' /etc/redis/6379.conf
   
# 모든 마스터, 슬레이브 서버의 redis.conf에 requirepass와 masterauth에 동일한 암호화된 password를 복사
sed -i 's/# requirepass foobared/requirepass passWord/g' /etc/redis/6379.conf
sed -i 's/# masterauth <master-password>/masterauth passWord/g' /etc/redis/6379.conf
   
# redis 종료시 requirepass설정에 의해 -a 인수를 추가합니다.
sed -i 's/$CLIEXEC -p $REDISPORT shutdown/$CLIEXEC -p $REDISPORT -a passWord shutdown/g' /etc/init.d/redis_6379
  • redis start
/etc/init.d/redis_6379 start
  • service 등록 및 확인
chkconfig --add redis_6379
chkconfig --level 345 redis_6379 on 

#service 확인
service redis_6379 status

4. Redis cluster create

/usr/local/bin/redis-cli --cluster create 10.0.0.220:6379 10.0.0.221:6379 10.0.0.222:6379
>>> Performing hash slots allocation on 3 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
M: c80a6319ba80e45483596b94dea150d0ead8f315 10.0.0.220:6379
   slots:[0-5460] (5461 slots) master
M: a602cf2d0888777f4091f1e8c6c5ccb16703f845 10.0.0.221:6379
   slots:[5461-10922] (5462 slots) master
M: a40d652dcf6ceb8eb341f523be54c4c704fb5bd8 10.0.0.222:6379
   slots:[10923-16383] (5461 slots) master
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
..
>>> Performing Cluster Check (using node 10.0.0.220:6379)
M: c80a6319ba80e45483596b94dea150d0ead8f315 10.0.0.220:6379
   slots:[0-5460] (5461 slots) master
M: a40d652dcf6ceb8eb341f523be54c4c704fb5bd8 10.0.0.222:6379
   slots:[10923-16383] (5461 slots) master
M: a602cf2d0888777f4091f1e8c6c5ccb16703f845 10.0.0.221:6379
   slots:[5461-10922] (5462 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

5. Redis Slave 설치 [3대 모두]

  • slave config
cp /etc/redis/6379.conf /etc/redis/7379.conf
cp /etc/init.d/redis_6379 /etc/init.d/redis_7379	
sed -i 's/port 6379/port 7379/g' /etc/redis/7379.conf
sed -i 's/6379/7379/g' /etc/redis/7379.conf
sed -i 's/6379/7379/g' /etc/init.d/redis_7379
   
# 모든 마스터, 슬레이브 서버의 redis.conf에 requirepass와 masterauth에 동일한 암호화된 password를 복사
sed -i 's/# requirepass foobared/requirepass passWord/g' /etc/redis/7379.conf
sed -i 's/# masterauth <master-password>/masterauth passWord/g' /etc/redis/7379.conf
   
# redis 종료시 requirepass설정에 의해 -a 인수를 추가합니다.
sed -i 's/$CLIEXEC -p $REDISPORT shutdown/$CLIEXEC -p $REDISPORT -a passWord shutdown/g' /etc/init.d/redis_7379
  • slave 디렉토리 생성
mkdir /var/lib/redis/7379
  • service 등록 및 확인
chkconfig --add redis_7379
chkconfig --level 345 redis_7379 on 

#service 확인
service redis_7379 status
  • redis slave 시작
/etc/init.d/redis_7379 start

6. Redis cluster slave 설정

/usr/local/bin/redis-cli -a passWord --cluster add-node 10.0.0.221:7379 10.0.0.220:6379 --cluster-slave --cluster-master-id $(/usr/local/bin/redis-cli -p 6379 -a passWord cluster nodes | grep 10.0.0.220:6379 | grep master | awk '{print $1}')
/usr/local/bin/redis-cli -a passWord --cluster add-node 10.0.0.222:7379 10.0.0.221:6379 --cluster-slave --cluster-master-id $(/usr/local/bin/redis-cli -p 6379 -a passWord cluster nodes | grep 10.0.0.221:6379 | grep master | awk '{print $1}')
/usr/local/bin/redis-cli -a passWord --cluster add-node 10.0.0.220:7379 10.0.0.222:6379 --cluster-slave --cluster-master-id $(/usr/local/bin/redis-cli -p 6379 -a passWord cluster nodes | grep 10.0.0.222:6379 | grep master | awk '{print $1}')
   

7. Redis cluster nodes 확인

/usr/local/bin/redis-cli -p 6379 -a passWord cluster nodes
77b77bc7912baca789f43022363506066307400e 10.0.0.221:7379@17379 slave c80a6319ba80e45483596b94dea150d0ead8f315 0 1557973344442 1 connected
18d2a9f07ca96262d8aa5f48a0cbfbfae43a6d29 10.0.0.220:7379@17379 slave a40d652dcf6ceb8eb341f523be54c4c704fb5bd8 0 1557973345445 3 connected
6af7b1fa49cf32fba83817abf7c4f555663b9f49 10.0.0.222:7379@17379 slave a602cf2d0888777f4091f1e8c6c5ccb16703f845 0 1557973344000 2 connected
a40d652dcf6ceb8eb341f523be54c4c704fb5bd8 10.0.0.222:6379@16379 master - 0 1557973344943 3 connected 10923-16383
a602cf2d0888777f4091f1e8c6c5ccb16703f845 10.0.0.221:6379@16379 master - 0 1557973344000 2 connected 5461-10922
c80a6319ba80e45483596b94dea150d0ead8f315 10.0.0.220:6379@16379 myself,master - 0 1557973343000 1 connected 0-5460

8. redis cli 확인

/usr/local/bin/redis-cli -p 6379 -c -a passWord
127.0.0.1:6379> info
# Server
redis_version:5.0.5
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:1f6be77f1580765a
redis_mode:cluster
os:Linux 4.9.32-15.41.amzn1.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:4.8.5
process_id:19561
run_id:8e84cc1470271f8d07eb6fed3d86bc30871396e0
tcp_port:6379
uptime_in_seconds:393
uptime_in_days:0
hz:10
configured_hz:10
lru_clock:14469500
executable:/usr/local/bin/redis-server
config_file:/etc/redis/6379.conf

# Clients
connected_clients:1
client_recent_max_input_buffer:2
client_recent_max_output_buffer:0
blocked_clients:0

# Memory
used_memory:2651296
used_memory_human:2.53M
used_memory_rss:6066176
used_memory_rss_human:5.79M
used_memory_peak:2651296
used_memory_peak_human:2.53M
used_memory_peak_perc:100.04%
used_memory_overhead:2570200
used_memory_startup:1455008
used_memory_dataset:81096
used_memory_dataset_perc:6.78%
allocator_allocated:3114816
allocator_active:3522560
allocator_resident:10493952
total_system_memory:8373010432
total_system_memory_human:7.80G
used_memory_lua:37888
used_memory_lua_human:37.00K
used_memory_scripts:0
used_memory_scripts_human:0B
number_of_cached_scripts:0
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
allocator_frag_ratio:1.13
allocator_frag_bytes:407744
allocator_rss_ratio:2.98
allocator_rss_bytes:6971392
rss_overhead_ratio:0.58
rss_overhead_bytes:-4427776
mem_fragmentation_ratio:2.32
mem_fragmentation_bytes:3456880
mem_not_counted_for_evict:0
mem_replication_backlog:1048576
mem_clients_slaves:16922
mem_clients_normal:49694
mem_aof_buffer:0
mem_allocator:jemalloc-5.1.0
active_defrag_running:0
lazyfree_pending_objects:0

# Persistence
loading:0
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1557973195
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:0
rdb_current_bgsave_time_sec:-1
rdb_last_cow_size:249856
aof_enabled:1
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok
aof_last_cow_size:0
aof_current_size:0
aof_base_size:0
aof_pending_rewrite:0
aof_buffer_length:0
aof_rewrite_buffer_length:0
aof_pending_bio_fsync:0
aof_delayed_fsync:0

# Stats
total_connections_received:7
total_commands_processed:197
instantaneous_ops_per_sec:1
total_net_input_bytes:6914
total_net_output_bytes:23430
instantaneous_input_kbps:0.04
instantaneous_output_kbps:0.00
rejected_connections:0
sync_full:1
sync_partial_ok:0
sync_partial_err:1
expired_keys:0
expired_stale_perc:0.00
expired_time_cap_reached_count:0
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:167
migrate_cached_sockets:0
slave_expires_tracked_keys:0
active_defrag_hits:0
active_defrag_misses:0
active_defrag_key_hits:0
active_defrag_key_misses:0

# Replication
role:master
connected_slaves:1
slave0:ip=10.0.0.221,port=7379,state=online,offset=252,lag=0
master_replid:c01055a9f98967b4dc3dbd8973c81dd36da83fca
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:252
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:252

# CPU
used_cpu_sys:0.076000
used_cpu_user:0.284000
used_cpu_sys_children:0.000000
used_cpu_user_children:0.000000

# Cluster
cluster_enabled:1

# Keyspace
127.0.0.1:6379> 

 

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Time limit exceeded. Please complete the captcha once again.

Most Popular