Redis HA articles + sentinel construction

Description: This article is an instruction manual for Redis Sentinel
Tags: Redis Sentinel, Redis High Availability, Redis Failover, Redis 4.0.2
Note: The redundant parts that are not needed are deleted in the article, so that beginners will be clear at a glance and will be
warmly reminded : If you find that there is a problem with the writing in this article or there is a better way to write it, please leave a message or private message me for modification and optimization


★ Preface
※ The document structure adopts: stand-alone 1 master 2 slaves 3 sentry architecture (equivalent to 6 redis)
※ System information: Centos 6.3 x86_64 4GB memory
※ If the master and slave have passwords, the sentinels also need passwords to control them
※ For convenience Beginners learn to understand redis This article simplifies all steps
※ Redis full name "remote dictionary server" Chinese remote dictionary service
※ Redis belongs to the key-value type in NoSql database
※ The sentinel mechanism of Redis is supported by redis2.8, and the cluster mode is redis3 .0 support.
※ Redis's sentinel system is used to manage multiple redis servers. The system mainly performs three tasks: monitoring, reminders, and automatic failover.
※ The master of Redis is in read-write mode, and the slave is in read-only mode;
※ For more parameters of Redis, please refer to my other related documents

★ Related articles
Redis operation and maintenance articles + installation single instance
Redis HA articles + master-slave construction
Redis HA articles + sentinel construction
Redis HA articles + cluster construction


★ Sentinel working mechanism
Automatically monitor and switch master-slave status and master-slave configuration files


★ Prerequisites for setting up Sentinels
# Configure the master-slave environment in advance, and pull up the master-slave
# You need to open 3 sessions and execute suspend
/soft/redis-4.0.2/src/redis-server /soft/redis-4.0. 2/redis_6380.conf
/soft/redis-4.0.2/src/redis-server /soft/redis-4.0.2/redis_6381.conf
/soft/redis-4.0.2/src/redis-server /soft/redis- 4.0.2/redis_6382.conf


★ Configure Sentinel
※ Copy the configuration file
cp /soft/redis-4.0.2/sentinel.conf /soft/redis-4.0.2/sentinel_26380.conf
cp /soft/redis-4.0.2/sentinel.conf /soft/redis- 4.0.2/sentinel_26381.conf
cp /soft/redis-4.0.2/sentinel.conf /soft/redis-4.0.2/sentinel_26382.conf

※ Modify configuration file (main)
# Related file location:
/soft/redis-4.0.2/sentinel_26380.conf
# Modify sentinel port: port
sed -i 's/^port 26379/port 26380/g' /soft/redis- 4.0.2/sentinel_26380.conf
cat /soft/redis-4.0.2/sentinel_26380.conf |grep "^port 263"
# Configure master server
sed -i 's/sentinel monitor mymaster 127.0.0.1 6379 2/sentinel monitor mymaster 127.0 .0.1 6380 2/g' /soft/redis-4.0.2/sentinel_26380.conf
cat /soft/redis-4.0.2/sentinel_26380.conf |grep "^sentinel monitor"

※ Modify configuration file (from A)
# Related file location:
/soft/redis-4.0.2/sentinel_26381.conf
# Modify sentinel port: port
sed -i 's/^port 26379/port 26381/g' /soft/redis -4.0.2/sentinel_26381.conf
cat /soft/redis-4.0.2/sentinel_26381.conf |grep "^port 263"
# configure master server
sed -i 's/sentinel monitor mymaster 127.0.0.1 6379 2/sentinel monitor mymaster 127.0.0.1 6380 2/g' /soft/redis-4.0.2/sentinel_26381.conf
cat /soft/redis-4.0.2/sentinel_26381.conf |grep "^sentinel monitor"

※ Modify configuration file (from B)
# Related file location:
/soft/redis-4.0.2/sentinel_26382.conf
# Modify sentinel port: port
sed -i 's/^port 26379/port 26382/g' /soft/redis -4.0.2/sentinel_26382.conf
cat /soft/redis-4.0.2/sentinel_26382.conf |grep "^port 263"
# configure master server
sed -i 's/sentinel monitor mymaster 127.0.0.1 6379 2/sentinel monitor mymaster 127.0.0.1 6380 2/g' /soft/redis-4.0.2/sentinel_26382.conf
cat /soft/redis-4.0.2/sentinel_26382.conf |grep "^sentinel monitor"


★ Start all sentinels
# Need to open 3 sessions, respectively execute the suspension
/soft/redis-4.0.2/src/redis-sentinel /soft/redis-4.0.2/sentinel_26380.conf
/soft/redis-4.0.2/ src/redis-sentinel /soft/redis-4.0.2/sentinel_26381.conf
/soft/redis-4.0.2/src/redis-sentinel /soft/redis-4.0.2/sentinel_26382.conf


★ View master-slave status
echo "info replication" | /soft/redis-4.0.2/src/redis-cli -p 6380 |grep role
echo "info replication" | /soft/redis-4.0.2/src/redis- cli -p 6381 |grep role
echo "info replication" | /soft/redis-4.0.2/src/redis-cli -p 6382 |grep role


★ 查看状态详情
echo "info replication" | /soft/redis-4.0.2/src/redis-cli -p 6380
echo "info replication" | /soft/redis-4.0.2/src/redis-cli -p 6381
echo "info replication" | /soft/redis-4.0.2/src/redis-cli -p 6382
echo "info Sentinel" | /soft/redis-4.0.2/src/redis-cli -p 26380
echo "info Sentinel" | /soft/redis-4.0.2/src/redis-cli -p 26381
echo "info Sentinel" | /soft/redis-4.0.2/src/redis-cli -p 26382


★ Test data transmission
# Insert/view data
echo "set zzt_01 'hello zzt'" | /soft/redis-4.0.2/src/redis-cli -p 6380
echo "get zzt_01" | /soft/redis-4.0.2 /src/redis-cli -p 6380
echo "get zzt_01" | /soft/redis-4.0.2/src/redis-cli -p 6381
echo "get zzt_01" | /soft/redis-4.0.2/src/redis -cli -p 6382
# Clean data
echo "del zzt_01" | /soft/redis-4.0.2/src/redis-cli -p 6380
echo "get zzt_01" | /soft/redis-4.0.2/src/redis- cli -p 6380


★ Test the automatic restart of the slave node
In this mode, the slave node will automatically join the master-slave after restarting, and will automatically synchronize the data of the master database


★ Test the automatic restart of the master node
Sentinel will automatically judge the status of the master node and re-elect the master node;
after the original master node restarts, it will automatically join the master-slave as a slave
. The master-slave switch of this mode is automatic, so it is generally recommended to use automatic management sentinel mode


★ Test the automatic restart of a sentinel instance.
The automatic restart of the sentinel does not affect the automatic election and switching of the master and
slave. After the automatic restart of the sentinel, it can automatically join the sentinel group
 

★ case

※ 主从信息
[root@main redis-4.0.2]# /soft/redis-4.0.2/src/redis-cli -p 6380
127.0.0.1:6380> info 
# Server
redis_version:4.0.2
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:cd424730de8d0575
redis_mode:standalone
os:Linux 2.6.32-279.el6.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:sync-builtin
gcc_version:4.4.6
process_id:42813
run_id:54f9160f14949b6d2fa23b0e5c03db717ac7caa5
tcp_port:6380
uptime_in_seconds:2248
uptime_in_days:0
hz:10
lru_clock:3696251
executable:/soft/redis-4.0.2/src/redis-server
config_file:/soft/redis-4.0.2/redis_6380.conf

# Clients
connected_clients:7
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0

# Memory
used_memory:2074094
used_memory_human:1.98M
used_memory_rss:2732032
used_memory_rss_human:2.61M
used_memory_peak:2154072
used_memory_peak_human:2.05M
used_memory_peak_perc:96.29%
used_memory_overhead:2068937
used_memory_startup:835755
used_memory_dataset:5157
used_memory_dataset_perc:0.42%
total_system_memory:4004020224
total_system_memory_human:3.73G
used_memory_lua:37888
used_memory_lua_human:37.00K
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
mem_fragmentation_ratio:1.32
mem_allocator:libc
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:1614306740
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:0
rdb_current_bgsave_time_sec:-1
rdb_last_cow_size:204800
aof_enabled:0
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

# Stats
total_connections_received:13
total_commands_processed:6346
instantaneous_ops_per_sec:5
total_net_input_bytes:260328
total_net_output_bytes:805031
instantaneous_input_kbps:0.27
instantaneous_output_kbps:4.29
rejected_connections:0
sync_full:2
sync_partial_ok:0
sync_partial_err:2
expired_keys:0
evicted_keys:0
keyspace_hits:1
keyspace_misses:0
pubsub_channels:1
pubsub_patterns:0
latest_fork_usec:183
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:2
slave0:ip=127.0.0.1,port=6381,state=online,offset=80670,lag=0
slave1:ip=127.0.0.1,port=6382,state=online,offset=80537,lag=1
master_replid:d38df8987f4ebac447df143c0da5bb48db2ace72
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:80670
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:80670

# CPU
used_cpu_sys:2.79
used_cpu_user:0.97
used_cpu_sys_children:0.00
used_cpu_user_children:0.00

# Cluster
cluster_enabled:0

# Keyspace
db0:keys=1,expires=0,avg_ttl=0






※ 哨兵信息
[root@main redis-4.0.2]# /soft/redis-4.0.2/src/redis-cli -p 26380
127.0.0.1:26380> info
# Server
redis_version:4.0.2
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:cd424730de8d0575
redis_mode:sentinel
os:Linux 2.6.32-279.el6.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:sync-builtin
gcc_version:4.4.6
process_id:46908
run_id:6c4bac04c26736cd49cdcaaa26738629c97f3693
tcp_port:26380
uptime_in_seconds:296
uptime_in_days:0
hz:17
lru_clock:3696135
executable:/soft/redis-4.0.2/src/redis-sentinel
config_file:/soft/redis-4.0.2/sentinel_26380.conf

# Clients
connected_clients:3
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0

# CPU
used_cpu_sys:0.72
used_cpu_user:0.16
used_cpu_sys_children:0.00
used_cpu_user_children:0.00

# Stats
total_connections_received:5
total_commands_processed:818
instantaneous_ops_per_sec:2
total_net_input_bytes:43915
total_net_output_bytes:5224
instantaneous_input_kbps:0.12
instantaneous_output_kbps:0.01
rejected_connections:0
sync_full:0
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:0
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

# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=127.0.0.1:6380,slaves=2,sentinels=3
127.0.0.1:26380> 




※ 插入/查看数据
[root@main ~]# echo "set zzt_01 'hello zzt'" | /soft/redis-4.0.2/src/redis-cli -p 6380
OK
[root@main ~]# echo "get zzt_01" | /soft/redis-4.0.2/src/redis-cli -p 6380
"hello zzt"
[root@main ~]# echo "get zzt_01" | /soft/redis-4.0.2/src/redis-cli -p 6381
"hello zzt"
[root@main ~]# echo "get zzt_01" | /soft/redis-4.0.2/src/redis-cli -p 6382
"hello zzt"

※ If you think the article is well written, don't forget to give the author a thumbs up at the end of the article~

over

 

Guess you like

Origin blog.csdn.net/zzt_2009/article/details/114395391