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

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

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
相关文章
|
1天前
|
运维 监控 安全
构建高效运维体系:从监控到自动化的全方位实践
本文深入探讨了构建高效运维体系的关键要素,从监控、日志管理、自动化工具、容器化与微服务架构、持续集成与持续部署(CI/CD)、虚拟化与云计算以及安全与合规等方面进行了全面阐述。通过引入先进的技术和方法,结合实际案例和项目经验,为读者提供了一套完整的运维解决方案,旨在帮助企业提升运维效率,降低运营成本,确保业务稳定运行。
|
2天前
|
存储 运维 网络安全
自动化运维工具:Ansible入门与实践
【9月更文挑战第17天】本文将介绍Ansible的基本概念、安装和简单使用,以及如何编写一个简单的Ansible playbook。通过本文,您可以了解到Ansible的基本原理和使用方法,以及如何在实际工作中应用Ansible进行自动化运维。
|
6天前
|
运维 Cloud Native Devops
云原生架构的崛起与实践云原生架构是一种通过容器化、微服务和DevOps等技术手段,帮助应用系统实现敏捷部署、弹性扩展和高效运维的技术理念。本文将探讨云原生的概念、核心技术以及其在企业中的应用实践,揭示云原生如何成为现代软件开发和运营的主流方式。##
云原生架构是现代IT领域的一场革命,它依托于容器化、微服务和DevOps等核心技术,旨在解决传统架构在应对复杂业务需求时的不足。通过采用云原生方法,企业可以实现敏捷部署、弹性扩展和高效运维,从而大幅提升开发效率和系统可靠性。本文详细阐述了云原生的核心概念、主要技术和实际应用案例,并探讨了企业在实施云原生过程中的挑战与解决方案。无论是正在转型的传统企业,还是寻求创新的互联网企业,云原生都提供了一条实现高效能、高灵活性和高可靠性的技术路径。 ##
17 3
|
6天前
|
运维 应用服务中间件 nginx
自动化运维的利器:Ansible入门与实践
【9月更文挑战第13天】在这个快速发展的IT时代,自动化运维已成为提升效率、减少失误的关键。本文将带你了解Ansible,一个强大的自动化工具,它简化了配置管理、应用部署和任务自动化。通过实际案例,我们将探索Ansible的基本概念、安装步骤、关键组件以及如何编写Playbook来自动化日常任务。无论你是新手还是有经验的运维专家,这篇文章都将为你提供宝贵的见解和技巧,让你在自动化运维的道路上更进一步。
|
4天前
|
存储 运维 监控
构建高效运维体系:从监控到自动化的全方位实践
在当今信息技术飞速发展的时代,运维作为保障信息系统稳定运行的关键环节,其重要性不言而喻。本文将围绕如何构建一个高效的运维体系进行深入探讨,内容涵盖从监控、日志分析到自动化运维工具的选择与应用,以及在实际工作中的经验和案例分享。通过本文的介绍,读者将能够了解到如何在复杂多变的技术环境中,确保系统的高可用性、高性能和安全性,为业务连续性提供坚实保障。
|
5天前
|
运维 监控 数据可视化
高效运维的秘密武器:自动化工具链的构建与实践在当今数字化时代,IT系统的复杂性和规模不断增加,使得传统的手动运维方式难以应对日益增长的业务需求。因此,构建一套高效的自动化工具链成为现代运维的重要任务。本文将深入探讨如何通过自动化工具链提升IT运维效率,确保系统稳定运行,并实现快速响应和故障恢复。
随着企业IT架构的不断扩展和复杂化,传统的手动运维已无法满足业务需求。自动化工具链的构建成为解决这一问题的关键。本文介绍了自动化工具链的核心概念、常用工具及其选择依据,并通过实际案例展示了自动化工具链在提升运维效率、减少人为错误、优化资源配置等方面的显著效果。从监控系统到自动化运维平台,再到持续集成/持续部署(CI/CD)的流程,我们将一步步揭示如何成功实施自动化工具链,助力企业实现高效、稳定、可靠的IT运维管理。
|
6天前
|
运维 Prometheus 监控
提升运维效率:自动化工具的应用与实践
运维工作作为信息技术领域的重要组成部分,其效率和质量直接关系到整个系统的稳定运行。随着科技的进步,自动化工具在运维中的应用越来越广泛。本文将探讨几种常见的自动化工具及其在实际操作中的应用案例,旨在为读者提供一些提升运维效率的思路和方法。通过合理利用这些工具,运维人员不仅可以提高工作效率,还能有效降低出错率,从而保障系统的高可用性。
13 0