上线部署规划 - 其他好用的配置技巧

优质
小牛编辑
138浏览
2023-12-01

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”.

  1. [unix_http_server]
  2. file=/smsred/redis-3.0.4/run/supervisor.sock ; (the path to the socket file)
  3. ;chmod=0700 ; socket file mode (default 0700)
  4. ;chown=nobody:nogroup ; socket file uid:gid owner
  5. ;username=user ; (default is no username (open server))
  6. ;password=123 ; (default is no password (open server))
  7. ;[inet_http_server] ; inet (TCP) server disabled by default
  8. ;port=127.0.0.1:9001 ; (ip_address:port specifier, *:port for all iface)
  9. ;username=user ; (default is no username (open server))
  10. ;password=123 ; (default is no password (open server))
  11. [supervisord]
  12. logfile=/smsred/redis-3.0.4/log/supervisord.log ; (main log file;default $CWD/supervisord.log)
  13. logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB)
  14. logfile_backups=10 ; (num of main logfile rotation backups;default 10)
  15. loglevel=info ; (log level;default info; others: debug,warn,trace)
  16. pidfile=/smsred/redis-3.0.4/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
  17. nodaemon=false ; (start in foreground if true;default false)
  18. minfds=1024 ; (min. avail startup file descriptors;default 1024)
  19. minprocs=200 ; (min. avail process descriptors;default 200)
  20. ;umask=022 ; (process file creation umask;default 022)
  21. ;user=chrism ; (default is current user, required if root)
  22. ;identifier=supervisor ; (supervisord identifier, default is 'supervisor')
  23. ;directory=/tmp ; (default is not to cd during start)
  24. ;nocleanup=true ; (don't clean up tempfiles at start;default false)
  25. ;childlogdir=/tmp ; ('AUTO' child log dir, default $TEMP)
  26. ;environment=KEY="value" ; (key value pairs to add to environment)
  27. ;strip_ansi=false ; (strip ansi escape codes in logs; def. false)
  28. ; the below section must remain in the config file for RPC
  29. ; (supervisorctl/web interface) to work, additional interfaces may be
  30. ; added by defining them in separate rpcinterface: sections
  31. [rpcinterface:supervisor]
  32. supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
  33. [supervisorctl]
  34. serverurl=unix:///smsred/redis-3.0.4/run/supervisor.sock ; use a unix:// URL for a unix socket
  35. ;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket
  36. ;username=chris ; should be same as http_username if set
  37. ;password=123 ; should be same as http_password if set
  38. ;prompt=mysupervisor ; cmd line prompt (default "supervisor")
  39. ;history_file=~/.sc_history ; use readline history if available
  40. ; The below sample program section shows all possible program subsection values,
  41. ; create one or more 'real' program: sections to be able to control them under
  42. ; supervisor.
  43. ;[program:theprogramname]
  44. ;command=/bin/cat ; the program (relative uses PATH, can take args)
  45. ;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
  46. ;numprocs=1 ; number of processes copies to start (def 1)
  47. ;directory=/tmp ; directory to cwd to before exec (def no cwd)
  48. ;umask=022 ; umask for process (default None)
  49. ;priority=999 ; the relative start priority (default 999)
  50. ;autostart=true ; start at supervisord start (default: true)
  51. ;autorestart=unexpected ; whether/when to restart (default: unexpected)
  52. ;startsecs=1 ; number of secs prog must stay running (def. 1)
  53. ;startretries=3 ; max # of serial start failures (default 3)
  54. ;exitcodes=0,2 ; 'expected' exit codes for process (default 0,2)
  55. ;stopsignal=QUIT ; signal used to kill process (default TERM)
  56. ;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10)
  57. ;stopasgroup=false ; send stop signal to the UNIX process group (default false)
  58. ;killasgroup=false ; SIGKILL the UNIX process group (def false)
  59. ;user=chrism ; setuid to this UNIX account to run the program
  60. ;redirect_stderr=true ; redirect proc stderr to stdout (default false)
  61. ;stdout_logfile=/a/path ; stdout log path, NONE for none; default AUTO
  62. ;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
  63. ;stdout_logfile_backups=10 ; # of stdout logfile backups (default 10)
  64. ;stdout_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0)
  65. ;stdout_events_enabled=false ; emit events on stdout writes (default false)
  66. ;stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO
  67. ;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
  68. ;stderr_logfile_backups=10 ; # of stderr logfile backups (default 10)
  69. ;stderr_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0)
  70. ;stderr_events_enabled=false ; emit events on stderr writes (default false)
  71. ;environment=A="1",B="2" ; process environment additions (def no adds)
  72. ;serverurl=AUTO ; override serverurl computation (childutils)
  73. ; The below sample eventlistener section shows all possible
  74. ; eventlistener subsection values, create one or more 'real'
  75. ; eventlistener: sections to be able to handle event notifications
  76. ; sent by supervisor.
  77. ;[eventlistener:theeventlistenername]
  78. ;command=/bin/eventlistener ; the program (relative uses PATH, can take args)
  79. ;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
  80. ;numprocs=1 ; number of processes copies to start (def 1)
  81. ;events=EVENT ; event notif. types to subscribe to (req'd)
  82. ;buffer_size=10 ; event buffer queue size (default 10)
  83. ;directory=/tmp ; directory to cwd to before exec (def no cwd)
  84. ;umask=022 ; umask for process (default None)
  85. ;priority=-1 ; the relative start priority (default -1)
  86. ;autostart=true ; start at supervisord start (default: true)
  87. ;autorestart=unexpected ; whether/when to restart (default: unexpected)
  88. ;startsecs=1 ; number of secs prog must stay running (def. 1)
  89. ;startretries=3 ; max # of serial start failures (default 3)
  90. ;exitcodes=0,2 ; 'expected' exit codes for process (default 0,2)
  91. ;stopsignal=QUIT ; signal used to kill process (default TERM)
  92. ;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10)
  93. ;stopasgroup=false ; send stop signal to the UNIX process group (default false)
  94. ;killasgroup=false ; SIGKILL the UNIX process group (def false)
  95. ;user=chrism ; setuid to this UNIX account to run the program
  96. ;redirect_stderr=true ; redirect proc stderr to stdout (default false)
  97. ;stdout_logfile=/a/path ; stdout log path, NONE for none; default AUTO
  98. ;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
  99. ;stdout_logfile_backups=10 ; # of stdout logfile backups (default 10)
  100. ;stdout_events_enabled=false ; emit events on stdout writes (default false)
  101. ;stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO
  102. ;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
  103. ;stderr_logfile_backups ; # of stderr logfile backups (default 10)
  104. ;stderr_events_enabled=false ; emit events on stderr writes (default false)
  105. ;environment=A="1",B="2" ; process environment additions
  106. ;serverurl=AUTO ; override serverurl computation (childutils)
  107. ; The below sample group section shows all possible group values,
  108. ; create one or more 'real' group: sections to create "heterogeneous"
  109. ; process groups.
  110. ;[group:thegroupname]
  111. ;programs=progname1,progname2 ; each refers to 'x' in [program:x] definitions
  112. ;priority=999 ; the relative start priority (default 999)
  113. ; The [include] section can just contain the "files" setting. This
  114. ; setting can list multiple files (separated by whitespace or
  115. ; newlines). It can also contain wildcards. The filenames are
  116. ; interpreted as relative to this file. Included files *cannot*
  117. ; include files themselves.
  118. ;[include]
  119. ;files = relative/directory/*.ini
  120. [program:redis-1xxx]
  121. command=/smsred/redis-3.0.4/bin/redis-server /smsred/redis-3.0.4/conf/redis-1xxx.conf
  122. autostart=true
  123. autorestart=false
  124. user=smsred
  125. stdout_logfile=/smsred/redis-3.0.4/log/redis-1xxx.out.log
  126. stderr_logfile=/smsred/redis-3.0.4/log/redis-1xxx.err.log
  127. priority=1000
  128. [program:redis-1xxx]
  129. command=/smsred/redis-3.0.4/bin/redis-server /smsred/redis-3.0.4/conf/redis-1xxx.conf
  130. autostart=true
  131. autorestart=false
  132. user=smsred
  133. stdout_logfile=/smsred/redis-3.0.4/log/redis-1xxx.out.log
  134. stderr_logfile=/smsred/redis-3.0.4/log/redis-1xxx.err.log
  135. priority=1000
  136. [program:redis-1xxx]
  137. command=/smsred/redis-3.0.4/bin/redis-server /smsred/redis-3.0.4/conf/redis-1xxx.conf
  138. autostart=true
  139. autorestart=false
  140. user=smsred
  141. stdout_logfile=/smsred/redis-3.0.4/log/redis-1xxx.out.log
  142. stderr_logfile=/smsred/redis-3.0.4/log/redis-1xxx.err.log
  143. priority=1000
  144. [program:redis-sentinel]
  145. command =/smsred/redis-3.0.4/bin/redis-sentinel /smsred/redis-3.0.4/conf/sentinel.conf
  146. autostart=true
  147. autorestart=true
  148. startsecs=3

5.7.2 使用alias方便操作

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

  1. alias cli1='$HOME/redis-3.0.4/bin/redis-cli -a xxx -p 1xx'
  2. alias cli2='$HOME/redis-3.0.4/bin/redis-cli -a xxx -p 1xx'
  3. alias cli3='$HOME/redis-3.0.4/bin/redis-cli -a xxx -p 1xx'
  4. alias clis='$HOME/redis-3.0.4/bin/redis-cli -p 26379'
  5. alias sctl='supervisorctl -c $HOME/redis-3.0.4/conf/redis-supervisord.conf '
  6. alias sstart='supervisord -c $HOME/redis-3.0.4/conf/redis-supervisord.conf'
  7. 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管理进程的命令实例如下:

  1. 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互信。建立互信可以用下边这个脚本

  1. #!/bin/bash
  2. #2015-12-08
  3. #author gnuhpc
  4. expect -c "spawn ssh-keygen -t rsa
  5. expect {
  6. \":\" {send \"\r\"; exp_continue}
  7. \"*(y/n)*\" {send \"y\r\"; exp_continue}
  8. }
  9. "
  10. for p in $(cat ip.cfg)
  11. do
  12. ip=$(echo "$p"|cut -f1 -d":")
  13. username=$(echo "$p"|cut -f2 -d":")
  14. password=$(echo "$p"|cut -f3 -d":")
  15. echo $password
  16. expect -c "
  17. spawn ssh-copy-id ${username}@$ip
  18. expect {
  19. \"*yes/no*\" {send \"yes\r\"; exp_continue}
  20. \"*(y/n)*\" {send \"y\r\"; exp_continue}
  21. \"*password*\" {send \"$password\r\"; exp_continue}
  22. \"*Password*\" {send \"$password\r\"; exp_continue}
  23. }
  24. "
  25. expect -c "
  26. spawn ssh ${username}@$ip "hostname"
  27. expect {
  28. \"*yes/no*\" {send \"yes\r\"; exp_continue}
  29. \"*password*\" {send \"$password\r\"; exp_continue}
  30. \"*(y/n)*\" {send \"y\r\"; exp_continue}
  31. \"*Password*\" {send \"$password\r\";}
  32. }
  33. "
  34. done

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

  1. xxxx.139:username:password
  2. xxxx.140:username:password
  3. xxxx.141:username:password
  4. xxxx.142:username:password
  5. xxxx.137:username:password

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

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

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

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

恢复脚本:

  1. recoveryconf.sh
  2. #!/bin/bash
  3. for i in `find ~/redis-3.0.4/conf -name *.conf.bak`
  4. do
  5. cp -v $i ${i%.*}
  6. done