Redis开发运维实践上线部署规划之其他好用的配置技巧

本文涉及的产品
云原生内存数据库 Tair,内存型 2GB
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
云数据库 Redis 版,经济版 1GB 1个月
简介:

5.7 其他好用的配置技巧

5.7.1 使用supervisord进行进程管理

Supervisord是一个优秀的进程管理工具,一般在部署redis时采用它来进行redis、sentinel等进程的管理,一个已经在生产环境采用的supervisord配置文件如下: ; Sample supervisor config file. ; ; For more information on the config file, please see: ; http://supervisord.org/configuration.html ; ; Notes: ; - Shell expansion ("~" or "$HOME") is not supported. Environment ; variables can be expanded using this syntax: "%(ENV_HOME)s". ; - Comments must have a leading space: "a=b ;comment" not "a=b;comment".

[unix_http_server] 
file=/smsred/redis-3.0.4/run/supervisor.sock ; (the path to the socket file) 
;chmod=0700 ; socket file mode (default 0700) 
;chown=nobody:nogroup ; socket file uid:gid owner 
;username=user ; (default is no username (open server)) 
;password=123 ; (default is no password (open server)) 

;[inet_http_server] ; inet (TCP) server disabled by default 
;port=127.0.0.1:9001 ; (ip_address:port specifier, *:port for all iface) 
;username=user ; (default is no username (open server)) 
;password=123 ; (default is no password (open server)) 

[supervisord] 
logfile=/smsred/redis-3.0.4/log/supervisord.log ; (main log file;default $CWD/supervisord.log) 
logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB) 
logfile_backups=10 ; (num of main logfile rotation backups;default 10) 
loglevel=info ; (log level;default info; others: debug,warn,trace) 
pidfile=/smsred/redis-3.0.4/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid) 
nodaemon=false ; (start in foreground if true;default false) 
minfds=1024 ; (min. avail startup file descriptors;default 1024) 
minprocs=200 ; (min. avail process descriptors;default 200) 
;umask=022 ; (process file creation umask;default 022) 
;user=chrism ; (default is current user, required if root) 
;identifier=supervisor ; (supervisord identifier, default is 'supervisor') 
;directory=/tmp ; (default is not to cd during start) 
;nocleanup=true ; (don't clean up tempfiles at start;default false) 
;childlogdir=/tmp ; ('AUTO' child log dir, default $TEMP) 
;environment=KEY="value" ; (key value pairs to add to environment) 
;strip_ansi=false ; (strip ansi escape codes in logs; def. false) 

; the below section must remain in the config file for RPC 
; (supervisorctl/web interface) to work, additional interfaces may be 
; added by defining them in separate rpcinterface: sections 
[rpcinterface:supervisor] 
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface 

[supervisorctl] 
serverurl=unix:///smsred/redis-3.0.4/run/supervisor.sock ; use a unix:// URL for a unix socket 
;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket 
;username=chris ; should be same as http_username if set 
;password=123 ; should be same as http_password if set 
;prompt=mysupervisor ; cmd line prompt (default "supervisor") 
;history_file=~/.sc_history ; use readline history if available 

; The below sample program section shows all possible program subsection values, 
; create one or more 'real' program: sections to be able to control them under 
; supervisor. 

;[program:theprogramname] 
;command=/bin/cat ; the program (relative uses PATH, can take args) 
;process_name=%(program_name)s ; process_name expr (default %(program_name)s) 
;numprocs=1 ; number of processes copies to start (def 1) 
;directory=/tmp ; directory to cwd to before exec (def no cwd) 
;umask=022 ; umask for process (default None) 
;priority=999 ; the relative start priority (default 999) 
;autostart=true ; start at supervisord start (default: true) 
;autorestart=unexpected ; whether/when to restart (default: unexpected) 
;startsecs=1 ; number of secs prog must stay running (def. 1) 
;startretries=3 ; max # of serial start failures (default 3) 
;exitcodes=0,2 ; 'expected' exit codes for process (default 0,2) 
;stopsignal=QUIT ; signal used to kill process (default TERM) 
;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10) 
;stopasgroup=false ; send stop signal to the UNIX process group (default false) 
;killasgroup=false ; SIGKILL the UNIX process group (def false) 
;user=chrism ; setuid to this UNIX account to run the program 
;redirect_stderr=true ; redirect proc stderr to stdout (default false) 
;stdout_logfile=/a/path ; stdout log path, NONE for none; default AUTO 
;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB) 
;stdout_logfile_backups=10 ; # of stdout logfile backups (default 10) 
;stdout_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0) 
;stdout_events_enabled=false ; emit events on stdout writes (default false) 
;stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO 
;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB) 
;stderr_logfile_backups=10 ; # of stderr logfile backups (default 10) 
;stderr_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0) 
;stderr_events_enabled=false ; emit events on stderr writes (default false) 
;environment=A="1",B="2" ; process environment additions (def no adds) 
;serverurl=AUTO ; override serverurl computation (childutils) 

; The below sample eventlistener section shows all possible 
; eventlistener subsection values, create one or more 'real' 
; eventlistener: sections to be able to handle event notifications 
; sent by supervisor. 

;[eventlistener:theeventlistenername] 
;command=/bin/eventlistener ; the program (relative uses PATH, can take args) 
;process_name=%(program_name)s ; process_name expr (default %(program_name)s) 
;numprocs=1 ; number of processes copies to start (def 1) 
;events=EVENT ; event notif. types to subscribe to (req'd) 
;buffer_size=10 ; event buffer queue size (default 10) 
;directory=/tmp ; directory to cwd to before exec (def no cwd) 
;umask=022 ; umask for process (default None) 
;priority=-1 ; the relative start priority (default -1) 
;autostart=true ; start at supervisord start (default: true) 
;autorestart=unexpected ; whether/when to restart (default: unexpected) 
;startsecs=1 ; number of secs prog must stay running (def. 1) 
;startretries=3 ; max # of serial start failures (default 3) 
;exitcodes=0,2 ; 'expected' exit codes for process (default 0,2) 
;stopsignal=QUIT ; signal used to kill process (default TERM) 
;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10) 
;stopasgroup=false ; send stop signal to the UNIX process group (default false) 
;killasgroup=false ; SIGKILL the UNIX process group (def false) 
;user=chrism ; setuid to this UNIX account to run the program 
;redirect_stderr=true ; redirect proc stderr to stdout (default false) 
;stdout_logfile=/a/path ; stdout log path, NONE for none; default AUTO 
;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB) 
;stdout_logfile_backups=10 ; # of stdout logfile backups (default 10) 
;stdout_events_enabled=false ; emit events on stdout writes (default false) 
;stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO 
;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB) 
;stderr_logfile_backups ; # of stderr logfile backups (default 10) 
;stderr_events_enabled=false ; emit events on stderr writes (default false) 
;environment=A="1",B="2" ; process environment additions 
;serverurl=AUTO ; override serverurl computation (childutils) 

; The below sample group section shows all possible group values, 
; create one or more 'real' group: sections to create "heterogeneous" 
; process groups. 

;[group:thegroupname] 
;programs=progname1,progname2 ; each refers to 'x' in [program:x] definitions 
;priority=999 ; the relative start priority (default 999) 

; The [include] section can just contain the "files" setting. This 
; setting can list multiple files (separated by whitespace or 
; newlines). It can also contain wildcards. The filenames are 
; interpreted as relative to this file. Included files *cannot* 
; include files themselves. 

;[include] 
;files = relative/directory/*.ini 

[program:redis-1xxx] 
command=/smsred/redis-3.0.4/bin/redis-server /smsred/redis-3.0.4/conf/redis-1xxx.conf 
autostart=true 
autorestart=false 
user=smsred 
stdout_logfile=/smsred/redis-3.0.4/log/redis-1xxx.out.log 
stderr_logfile=/smsred/redis-3.0.4/log/redis-1xxx.err.log 
priority=1000 

[program:redis-1xxx] 
command=/smsred/redis-3.0.4/bin/redis-server /smsred/redis-3.0.4/conf/redis-1xxx.conf 
autostart=true 
autorestart=false 
user=smsred 
stdout_logfile=/smsred/redis-3.0.4/log/redis-1xxx.out.log 
stderr_logfile=/smsred/redis-3.0.4/log/redis-1xxx.err.log 
priority=1000 

[program:redis-1xxx] 
command=/smsred/redis-3.0.4/bin/redis-server /smsred/redis-3.0.4/conf/redis-1xxx.conf 
autostart=true 
autorestart=false 
user=smsred 
stdout_logfile=/smsred/redis-3.0.4/log/redis-1xxx.out.log 
stderr_logfile=/smsred/redis-3.0.4/log/redis-1xxx.err.log 
priority=1000 

[program:redis-sentinel] 
command =/smsred/redis-3.0.4/bin/redis-sentinel /smsred/redis-3.0.4/conf/sentinel.conf 
autostart=true 
autorestart=true 
startsecs=3

5.7.2 使用alias方便操作

如果开多实例,那么shell下进行操作的次数会很多,因此你需要一些alias进行命令的缩短,这个技巧并不高深,但是很实用。一个实例如下:

alias cli1='$HOME/redis-3.0.4/bin/redis-cli -a xxx -p 1xx' 
alias cli2='$HOME/redis-3.0.4/bin/redis-cli -a xxx -p 1xx' 
alias cli3='$HOME/redis-3.0.4/bin/redis-cli -a xxx -p 1xx' 
alias clis='$HOME/redis-3.0.4/bin/redis-cli -p 26379' 

alias sctl='supervisorctl -c $HOME/redis-3.0.4/conf/redis-supervisord.conf ' 
alias sstart='supervisord -c $HOME/redis-3.0.4/conf/redis-supervisord.conf' 
alias see='pdsh -R ssh -w MSMSRED[1-3],PSMSRED1,PSMSAPP1 "/usr/local/bin/supervisorctl -c /smsred/redis-3.0.4/conf/redis-supervisord.conf status "' 

5.7.3 使用pdsh/pdcp进行多机器操作

Pdsh/pdcp是一个python ssh多机操作的工具,在部署中可以采用它进行多机的同一操作批量执行,注意编译的时候把ssh编译进去,在执行时指定ssh模式,一个查看多机supervisord管理进程的命令实例如下:

pdsh -R ssh -w MSMSRED[1-3],PSMSRED1,PSMSAPP1 "/usr/local/bin/supervisorctl -c /smsred/redis-3.0.4/conf/redis-supervisord.conf status "

前提是你这些机器已经建立了ssh互信。建立互信可以用下边这个脚本

#!/bin/bash 
#2015-12-08 
#author gnuhpc 

expect -c "spawn ssh-keygen -t rsa 
expect { 
\":\" {send \"\r\"; exp_continue} 
\"*(y/n)*\" {send \"y\r\"; exp_continue} 
} 
" 
for p in $(cat ip.cfg) 
do 
ip=$(echo "$p"|cut -f1 -d":") 
username=$(echo "$p"|cut -f2 -d":") 
password=$(echo "$p"|cut -f3 -d":") 
echo $password 

expect -c " 
spawn ssh-copy-id ${username}@$ip 
expect { 
\"*yes/no*\" {send \"yes\r\"; exp_continue} 
\"*(y/n)*\" {send \"y\r\"; exp_continue} 
\"*password*\" {send \"$password\r\"; exp_continue} 
\"*Password*\" {send \"$password\r\"; exp_continue} 
} 
" 
expect -c " 
spawn ssh ${username}@$ip "hostname" 
expect { 
\"*yes/no*\" {send \"yes\r\"; exp_continue} 
\"*password*\" {send \"$password\r\"; exp_continue} 
\"*(y/n)*\" {send \"y\r\"; exp_continue} 
\"*Password*\" {send \"$password\r\";} 
} 
" 
done

指定一个ip.cfg,里面的格式为:IP(主机名也行,只要能解析):用户名:密码 例如:

xxxx.139:username:password 
xxxx.140:username:password 
xxxx.141:username:password 
xxxx.142:username:password 
xxxx.137:username:password

5.7.4 使用脚本进行sentinel配置文件的备份

Sentinel在启动、切换时会对config文件进行rewrite,在上线前或者某些手动维护后你可能希望把conf文件都变为最初,当系统中有很多redis实例时,这个手工操作会让人疯掉,那不妨写个脚本在配置好sentinel和redis后不启动先备份一下,测试完毕后再恢复。

一个简单的备份脚本如下:

backupconf.sh 
#!/bin/bash 
for i in `find ~/redis-3.0.4/conf -name *.conf` 
do 
cp -v $i ${i}.bak 
done

恢复脚本:

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore     ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库 ECS 实例和一台目标数据库 RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
2天前
|
机器学习/深度学习 运维 监控
智能化运维的崛起:机器学习在IT管理中的实践与挑战
本文深入探讨了智能化运维领域,特别是机器学习技术在IT管理中的应用。文章首先介绍了智能化运维的概念及其重要性,随后详细阐述了机器学习在故障预测、自动化响应和系统优化中的作用。同时,文章也指出了实施智能化运维时可能遇到的技术挑战和数据治理问题,并提出了相应的解决策略。最后,通过具体案例分析,展示了机器学习技术如何在实际运维中提高系统稳定性和效率。
|
17天前
|
数据采集 运维 Cloud Native
Flink+Paimon在阿里云大数据云原生运维数仓的实践
构建实时云原生运维数仓以提升大数据集群的运维能力,采用 Flink+Paimon 方案,解决资源审计、拓扑及趋势分析需求。
17534 26
Flink+Paimon在阿里云大数据云原生运维数仓的实践
|
2天前
|
Rust NoSQL API
Redis module是否可以使用除C以外的其他语言开发
Redis module是否可以使用除C以外的其他语言开发
|
3天前
|
运维 监控 安全
DevOps实践:构建高效运维团队的五大策略
在当今快速发展的IT领域,DevOps已成为提升软件开发和运维效率的关键。本文将深入探讨如何通过实施五大策略来构建一个高效的运维团队,包括自动化流程、持续改进、协作文化、监控与响应以及安全优先。这些策略旨在帮助组织缩短开发周期,提高软件质量,同时确保系统的稳定性和安全性。
20 5
|
1天前
|
机器学习/深度学习 人工智能 运维
自动化运维的崛起与实践
在数字化浪潮中,自动化运维成为提升企业IT效率、确保系统稳定性的关键。通过集成化的管理工具和智能化的操作流程,自动化运维不仅减少了人为错误,还极大提高了运维响应速度和服务质量。本文将探讨自动化运维的核心要素、实施步骤及其带来的业务价值,同时分析面临的挑战与未来的发展方向。
8 2
|
4天前
|
运维 监控 Devops
DevOps实践:构建高效运维流程
【7月更文挑战第23天】在当今快速发展的信息技术时代,DevOps作为一种文化和实践,正在彻底改变软件开发和运维的方式。本文将深入探讨如何通过实施DevOps原则和工具来构建高效的运维流程,旨在帮助读者理解DevOps的核心概念、实施步骤以及面临的挑战,并提供实用的解决方案和最佳实践。文章将重点介绍自动化部署、持续集成、监控和反馈机制等关键要素,以促进团队协作,提升软件交付速度和质量。
|
9天前
|
监控 NoSQL Ubuntu
|
12天前
|
运维 监控 Devops
DevOps(Development和Operations的组合)是一种强调软件开发(Dev)和信息技术运维(Ops)之间协作与沟通的文化、方法和实践。
DevOps(Development和Operations的组合)是一种强调软件开发(Dev)和信息技术运维(Ops)之间协作与沟通的文化、方法和实践。
|
13天前
|
运维 监控 安全
DevOps文化下的高效运维实践
【7月更文挑战第14天】在软件开发与运维日益融合的今天,DevOps不仅仅是一种技术实践,更是一种文化的转变。本文将探讨如何在DevOps文化的引领下,通过一系列高效运维的策略与工具,实现持续集成、持续部署和持续监控,进而提高软件交付的速度与质量。我们将从自动化、监控、日志管理、容器化以及安全等方面展开讨论,旨在为读者提供一套完整的高效运维解决方案。
|
13天前
|
运维 Prometheus 监控
自动化运维工具链的搭建与优化实践
【7月更文挑战第14天】在现代IT架构中,自动化运维已成为提升效率、保障系统稳定性的关键。本文将深入探讨如何构建一套高效的自动化运维工具链,涵盖从基础设施自动化到应用部署的全过程。我们将分享一系列实用的策略和步骤,旨在帮助读者实现运维工作的自动化,减少人为错误,提高响应速度,最终达到降低运维成本和提升服务质量的双重目标。
51 2