redis 哨兵模式安装
针对于redis的系统优化
echo "vm.overcommit_memory=1" >> /etc/sysctl.conf echo never > /sys/kernel/mm/transparent_hugepage/enabled echo 1024 > /proc/sys/net/core/somaxconn echo "net.core.somaxconn = 1024" >> /etc/sysctl.conf
说明:
1、 vm.overcommit_memory=1 表示内核允许分配所有的物理内存,而不管当前的内存状态如何。
2、禁用大内存页面 transparent_hugepage=never
3、该内核参数默认值一般是128(定义了系统中每一个端口最大的监听队列的长度),对于负载很大的服务程序来说大大的不够。一般会将它修改为1024或者更大。
sysctl -p
yum -y install gcc gcc-c++ vim screen lrzsz net-tools wget curl unzip zip dos2unix lvm2 rsync git telnet tree nmap nfs-utils bash-completion bash-completion-extras
cd /opt/ wget http://download.redis.io/releases/redis-5.0.9.tar.gz tar -zxf redis-5.0.9.tar.gz cd redis-5.0.9 make make install PREFIX=/usr/local/redis
mkdir -p /data/redis/6379/{conf,data,logs,run} mkdir -p /data/redis/6380/{conf,data,logs,run} mkdir -p /data/redis/6381/{conf,data,logs,run}
cat >> /data/redis/6379/conf/redis.conf << EOF bind 10.10.10.87 127.0.0.1 port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize yes supervised no pidfile "/data/redis/6379/run/redis.pid" loglevel notice logfile "/data/redis/6379/logs/redis.log" databases 16 #重命名危险操作命令 #rename-command KEYS "nokeys" #rename-command FLUSHALL "noflushall" #rename-command FLUSHDB "noflushdb" #rename-command CONFIG "" requirepass "a123456" masterauth "a123456" always-show-logo yes #save 900 1 #save 300 10 #save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes #dbfilename "dump.rdb" dir "/data/redis/6379/data" replica-serve-stale-data yes replica-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no replica-priority 100 lazyfree-lazy-eviction no lazyfree-lazy-expire yes lazyfree-lazy-server-del no replica-lazy-flush no #appendonly yes #appendfilename "appendonly.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 1gb aof-load-truncated yes #aof-use-rdb-preamble no lua-time-limit 5000 slowlog-log-slower-than 3000 slowlog-max-len 1000 latency-monitor-threshold 0 notify-keyspace-events "" hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-size -2 list-compress-depth 0 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 hll-sparse-max-bytes 3000 stream-node-max-bytes 4096 stream-node-max-entries 100 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit replica 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 dynamic-hz yes aof-rewrite-incremental-fsync yes rdb-save-incremental-fsync yes # Generated by CONFIG REWRITE #maxmemory 3906250kb #maxmemory-policy allkeys-lfu EOF
cat /data/redis/6379/conf/redis.conf | sed 's/6379/6380/g' > /data/redis/6380/conf/redis.conf cat /data/redis/6379/conf/redis.conf | sed 's/6379/6381/g' > /data/redis/6381/conf/redis.conf
启动
/usr/local/redis/bin/redis-server /data/redis/6379/conf/redis.conf /usr/local/redis/bin/redis-server /data/redis/6380/conf/redis.conf /usr/local/redis/bin/redis-server /data/redis/6381/conf/redis.conf
设置主从关系
启动之后在从库上使用redis-cli登录,执行
SLAVEOF 10.10.10.87(主库ip) 6379
[root@redis conf]# redis-cli -p 6380
127.0.0.1:6380> auth a123456
OK
127.0.0.1:6380> SLAVEOF 10.10.10.87 6379
OK
127.0.0.1:6380> exit
[root@redis conf]# redis-cli -p 6381
127.0.0.1:6381> auth a123456
OK
127.0.0.1:6381> SLAVEOF 10.10.10.87 6379
OK
127.0.0.1:6381> exit
sentinel安装,3个节点
mkdir -p /data/redis/26379/{conf,data,logs,run} mkdir -p /data/redis/26380/{conf,data,logs,run} mkdir -p /data/redis/26381/{conf,data,logs,run}
cat >> /data/redis/26379/conf/sentinel.conf << EOF bind 10.10.10.87 127.0.0.1 port 26379 # Generated by CONFIG REWRITE protected-mode no pidfile "/data/redis/26379/run/sentinel.pid" logfile "/data/redis/26379/logs/sentinel.log" daemonize yes requirepass "a123456" dir "/data/redis/26379/data" sentinel deny-scripts-reconfig yes sentinel monitor mymaster 10.10.10.87 6379 2 sentinel down-after-milliseconds mymaster 6000 sentinel failover-timeout mymaster 18000 sentinel auth-pass mymaster a123456 EOF
cat /data/redis/26379/conf/sentinel.conf | sed 's/26379/26380/g' > /data/redis/26380/conf/sentinel.conf cat /data/redis/26379/conf/sentinel.conf | sed 's/26379/26381/g' > /data/redis/26381/conf/sentinel.conf
启动sentinel
/usr/local/redis/bin/redis-sentinel /data/redis/26379/conf/sentinel.conf /usr/local/redis/bin/redis-sentinel /data/redis/26380/conf/sentinel.conf /usr/local/redis/bin/redis-sentinel /data/redis/26381/conf/sentinel.conf
sentinel根据集群名称和监控的集群主库ip来确定自己的身份,3个都是监控同一个,所以它们自认为是一伙的,组成集群。
验证,关闭redis的主节点,查看从节点是否会提权为master
略
查看redis相关信息
[root@redis redis]# redis-cli -p 6379 127.0.0.1:6379> auth a123456 OK 127.0.0.1:6379> info # Server redis_version:5.0.9 redis_git_sha1:00000000 redis_git_dirty:0 redis_build_id:5f7af018c570b04d redis_mode:standalone os:Linux 3.10.0-1127.el7.x86_64 x86_64 arch_bits:64 multiplexing_api:epoll atomicvar_api:atomic-builtin gcc_version:4.8.5 process_id:18771 run_id:9526eff38ce373379578cd4c7339ec44575a7396 tcp_port:6379 uptime_in_seconds:1200 uptime_in_days:0 hz:10 configured_hz:10 lru_clock:6235156 executable:/usr/local/redis/bin/redis-server config_file:/data/redis/6379/conf/redis.conf # Clients connected_clients:7 client_recent_max_input_buffer:2 client_recent_max_output_buffer:0 blocked_clients:0 # Memory used_memory:2071744 used_memory_human:1.98M used_memory_rss:7929856 used_memory_rss_human:7.56M used_memory_peak:2254272 used_memory_peak_human:2.15M used_memory_peak_perc:91.90% used_memory_overhead:2025078 used_memory_startup:791432 used_memory_dataset:46666 used_memory_dataset_perc:3.64% allocator_allocated:2155496 allocator_active:2424832 allocator_resident:4902912 total_system_memory:1907748864 total_system_memory_human:1.78G 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.12 allocator_frag_bytes:269336 allocator_rss_ratio:2.02 allocator_rss_bytes:2478080 rss_overhead_ratio:1.62 rss_overhead_bytes:3026944 mem_fragmentation_ratio:3.76 mem_fragmentation_bytes:5818456 mem_not_counted_for_evict:0 mem_replication_backlog:1048576 mem_clients_slaves:33844 mem_clients_normal:151226 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:1600069707 rdb_last_bgsave_status:ok rdb_last_bgsave_time_sec:0 rdb_current_bgsave_time_sec:-1 rdb_last_cow_size:4382720 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:15 total_commands_processed:4779 instantaneous_ops_per_sec:6 total_net_input_bytes:221921 total_net_output_bytes:1256131 instantaneous_input_kbps:0.38 instantaneous_output_kbps:1.27 rejected_connections:0 sync_full:2 sync_partial_ok:0 sync_partial_err:2 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:1 pubsub_patterns:0 latest_fork_usec:392 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=10.10.10.87,port=6380,state=online,offset=122352,lag=0 slave1:ip=10.10.10.87,port=6381,state=online,offset=122352,lag=0 master_replid:8b754d609948618e3a060acbdd4f606ed2bbeeae master_replid2:0000000000000000000000000000000000000000 master_repl_offset:122352 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:122352 # CPU used_cpu_sys:0.897805 used_cpu_user:0.773390 used_cpu_sys_children:0.026664 used_cpu_user_children:0.001995 # Cluster cluster_enabled:0
查看sentinel相关信息
[root@redis redis]# redis-cli -p 26379 127.0.0.1:26379> auth a123456 OK 127.0.0.1:26379> info # Server redis_version:5.0.9 redis_git_sha1:00000000 redis_git_dirty:0 redis_build_id:5f7af018c570b04d redis_mode:sentinel os:Linux 3.10.0-1127.el7.x86_64 x86_64 arch_bits:64 multiplexing_api:epoll atomicvar_api:atomic-builtin gcc_version:4.8.5 process_id:19085 run_id:2240f5729747ac379f9b9fc1d720c9a21a50814d tcp_port:26379 uptime_in_seconds:551 uptime_in_days:0 hz:13 configured_hz:10 lru_clock:6235223 executable:/usr/local/redis/bin/redis-sentinel config_file:/data/redis/26379/conf/sentinel.conf # Clients connected_clients:3 client_recent_max_input_buffer:2 client_recent_max_output_buffer:0 blocked_clients:0 # CPU used_cpu_sys:0.826220 used_cpu_user:0.603381 used_cpu_sys_children:0.000000 used_cpu_user_children:0.000000 # Stats total_connections_received:3 total_commands_processed:1599 instantaneous_ops_per_sec:3 total_net_input_bytes:88716 total_net_output_bytes:9629 instantaneous_input_kbps:0.29 instantaneous_output_kbps:0.02 rejected_connections:0 sync_full:0 sync_partial_ok:0 sync_partial_err:0 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: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=10.10.10.87:6379,slaves=2,sentinels=3
-
« 上一篇:
redis5集群安装
-
Redis 5.0.9 集群模式安装
:下一篇 »