centos7以下由于没有systemctl,配置让redis自动启动如下:
echo "/usr/local/bin/redis-server /etc/redis/redis.conf &" >> /etc/rc.local
centos7 and later可以使用systemctl 配置守护进程,以下是python 安装脚本:
#!/usr/bin/python #-*- conding:utf-8 -*- #for redis comm import getopt import sys import os import tarfile import socket import commands import logging from tqdm import tqdm logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='%a, %d %b %Y %H:%M:%S', filename='/tmp/redis_install.log', filemode='w') def usage(): filePath = os.path.abspath(__file__) print ''' -a Password \n -p Port Number multiplea','separate \n -r Role m or s \n -h access ip address multiplea ',' separate \n [ usage: %s -a password -p port1,port2,port3 -r m|s -h 127.0.0.1,x.x.x.x ] ''' % filePath class redisClusterWork: ''' install before check ''' def __init__(self,port,password,role,host): self.port = port self.password = password self.role = role self.host = host self.master_config = ''' bind {host} protected-mode yes port {port} tcp-backlog 511 timeout 0 tcp-keepalive 0 daemonize yes supervised no pidfile "/data/redis{port}/redis{port}.pid" loglevel notice logfile "/data/redis{port}/redis{port}.log" databases 16 always-show-logo yes requirepass {pwd} stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename "dump.rdb" dir "/data/redis{port}/" masterauth {pwd} slave-serve-stale-data yes slave-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no slave-priority 100 maxclients 100000 maxmemory-policy allkeys-lfu lazyfree-lazy-eviction no lazyfree-lazy-expire no lazyfree-lazy-server-del no slave-lazy-flush no appendonly no #appendonly yes appendfilename "appendonly.aof" appendfsync no no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes aof-use-rdb-preamble no lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 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 activerehashing yes client-output-buffer-limit normal 0 0 0 #client-output-buffer-limit slave 2048mb 1500mb 60 client-output-buffer-limit slave 0 0 0 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 aof-rewrite-incremental-fsync yes repl-backlog-size 2500000kb repl-timeout 5000 # Generated by CONFIG REWRITE save "" #save 900 1 #save 300 10 #save 60 10000 rename-command CONFIG "ADMIN_CONFIG" rename-command FLUSHALL "" rename-command FLUSHDB "" rename-command KEYS "" '''.format(host=self.host,port=self.port,pwd=self.password) self.slave_config = ''' bind {host} protected-mode yes port {port} tcp-backlog 511 timeout 0 tcp-keepalive 0 daemonize yes supervised no pidfile "/data/redis{port}/redis{port}.pid" loglevel notice logfile "/data/redis{port}/redis{port}.log" databases 16 always-show-logo yes requirepass {pwd} stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename "dump.rdb" dir "/data/redis{port}/" masterauth {pwd} slave-serve-stale-data yes slave-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no slave-priority 100 maxclients 100000 maxmemory-policy allkeys-lfu lazyfree-lazy-eviction no lazyfree-lazy-expire no lazyfree-lazy-server-del no slave-lazy-flush no appendonly yes appendfilename "appendonly.aof" appendfsync no no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes aof-use-rdb-preamble no lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 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 activerehashing yes client-output-buffer-limit normal 0 0 0 #client-output-buffer-limit slave 2048mb 1500mb 60 client-output-buffer-limit slave 0 0 0 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 aof-rewrite-incremental-fsync yes repl-backlog-size 2500000kb repl-timeout 5000 # Generated by CONFIG REWRITE save 900 1 save 300 10 save 60 10000 rename-command CONFIG "ADMIN_CONFIG" rename-command FLUSHALL "" rename-command FLUSHDB "" #rename-command KEYS "" '''.format(host=self.host,port=self.port,pwd=self.password) def portCheck(self): try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) result = sock.connect_ex(('127.0.0.1',int(self.port))) if 0 == result: print '%s Current port to survive' % self.port logging.error('%s Current port to survive' % self.port) sys.exit() except Exception as e: logging.error(e) def baseDirCheck(self): baseDir = '/usr/local/redis/' if os.path.isdir(baseDir): logging.info('redis cluster basedir exists,continue') else: cmd = 'wget http://download.redis.io/releases/redis-5.0.4.tar.gz -P /opt' status, result = commands.getstatusoutput(cmd) if status == 0: filename='/opt/redis-5.0.4.tar.gz' tar = tarfile.open(filename) names = tar.getnames() for name in names: tar.extract(name,path='/opt/') tar.close() os.chdir('/opt/redis-5.0.4/') mcmd = 'make MALLOC=libc prefix=/usr/local/redis/bin' try: status, result = tqdm(commands.getstatusoutput(mcmd)) os.rename('/opt/redis-5.0.4','/usr/local/redis') logging.info('redis packet make success') except Exception as e: logging.error(e) def dataDirCheck(self): dataDir = '/data/redis%s/' % self.port if os.path.isdir(dataDir): print '[%s alerdy exists,please check]'%dataDir logging.error('[%s alerdy exists,please check]'%dataDir) sys.exit() else: os.mkdir('/data/redis%s' % self.port) if self.role == 'm': configTxt = self.master_config elif self.role == 's': configTxt = self.slave_config else: print '[ %s must is m or s]' % self.role fileName = '/data/redis{port}/redis{port}.conf'.format(port=self.port) with open(fileName, 'w') as f: f.write(configTxt) redisGroup = 'cat /etc/group |grep redis' redisUser='cat /etc/passwd |grep redis' gstatus, gresult = commands.getstatusoutput(redisGroup) if gstatus == 0: pass else: gcmd = 'groupadd redis' commands.getstatusoutput(gcmd) ustatus, uresult = commands.getstatusoutput(redisUser) if ustatus == 0: pass else: ucmd = 'useradd -M -g redis redis -s /sbin/nologin' commands.getstatusoutput(ucmd) xcmd = 'chown -R redis:redis %s'% dataDir commands.getstatusoutput(xcmd) def serviceCheck(self): serviceFile = '/usr/lib/systemd/system/redis%s.service' % self.port if os.path.isfile(serviceFile): print '[%s service name alredy exists]'% serviceFile logging.error('[%s service name alredy exists]'% serviceFile) else: configTxt='''[Unit] Description=Redis persistent key-value database After=network.target After=network-online.target Wants=network-online.target [Service] ExecStart=/usr/local/redis/src/redis-server /data/redis{port}/redis{port}.conf --supervised systemd ExecStop=/usr/local/redis/src/redis-cli -h 127.0.0.1 -p {port} -a {password} shutdown Type=notify User=redis Group=redis RuntimeDirectory=redis RuntimeDirectoryMode=0755 [Install] WantedBy=multi-user.target '''.format(port=self.port,password=self.password) with open(serviceFile, 'w') as f: f.write(configTxt) cmd = 'systemctl daemon-reload' commands.getstatusoutput(cmd) def redisStart(self): cmd = 'systemctl start redis%s'% self.port status, output = commands.getstatusoutput(cmd) if status == 0: print '%s start success!' % self.port logging.debug('%s start success!'% self.port) else: print '%s start faild,please check log file'%self.port logging.debug('%s start faild,please check log file' % self.port) if __name__ == '__main__': if len(sys.argv) < 4: print " No Enter parameter!! " usage() else: try: options, args = getopt.getopt(sys.argv[1:], "a:p:r:h:", [ "pwd=", "port=","role=","host="]) for name, value in options: if name in ('-a', '-pwd'): redisPwd = value #print redisPwd elif name in ('-p', '-port'): redisPort = value elif name in ('-r', '-role'): redisRole = value elif name in ('-h', '-host'): redisHost = value.replace(',',' ') #print redisPort print redisPwd,redisPort,redisRole,redisHost for port in tqdm(redisPort.split(',')): workflow = redisClusterWork(port=port,password=redisPwd,role=redisRole,host=redisHost) workflow.portCheck() workflow.baseDirCheck() workflow.dataDirCheck() workflow.serviceCheck() workflow.redisStart() except getopt.GetoptError: print 'Please check if the parameters are incorrect.' usage()
#!/usr/bin/python #-*- conding:utf-8 -*- #for redis cluster import getopt import sys import os import tarfile import socket import commands import logging from tqdm import tqdm logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='%a, %d %b %Y %H:%M:%S', filename='/tmp/rediscluster.log', filemode='w') def usage(): filePath = os.path.abspath(__file__) print ''' -a Password \n -p Port Number multiplea','separate \n [ usage: %s -a password -p port1,port2,port3] ''' % filePath class redisClusterWork: ''' install before check ''' def __init__(self,port,password): self.port = port self.password = password def portCheck(self): try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) result = sock.connect_ex(('127.0.0.1',int(self.port))) if 0 == result: print '%s Current port to survive' % self.port logging.error('%s Current port to survive' % self.port) sys.exit() except Exception as e: logging.error(e) def baseDirCheck(self): baseDir = '/usr/local/redis/' if os.path.isdir(baseDir): logging.info('redis cluster basedir exists,continue') else: cmd = 'wget http://download.redis.io/releases/redis-5.0.4.tar.gz -P /opt' status, result = commands.getstatusoutput(cmd) if status == 0: filename='/opt/redis-5.0.4.tar.gz' tar = tarfile.open(filename) names = tar.getnames() for name in names: tar.extract(name,path='/opt/') tar.close() os.chdir('/opt/redis-5.0.4/') mcmd = 'make MALLOC=libc prefix=/usr/local/redis/bin' try: os.rename('/opt/redis-5.0.4','/usr/local/redis') status, result = commands.getstatusoutput(mcmd) logging.info('redis packet make success') except Exception as e: logging.error(e) def dataDirCheck(self): dataDir = '/data/redis%s/' % self.port if os.path.isdir(dataDir): print '[%s alerdy exists,please check]'%dataDir logging.error('[%s alerdy exists,please check]'%dataDir) sys.exit() else: os.mkdir('/data/redis%s' % self.port) configTxt='''port {port} bind 0.0.0.0 cluster-enabled yes cluster-config-file /data/redis{port}/redis_nodes.conf cluster-node-timeout 15000 appendonly yes maxclients 100000 maxmemory 0 dir /data/redis{port} repl-backlog-size 200m tcp-backlog 1024 logfile /data/redis{port}/redis_server_{port}.log pidfile /data/redis{port}/redis_{port}.pid daemonize yes rename-command CONFIG "ADMIN_CONFIG" rename-command FLUSHALL "ADMIN_FLUSHALL" rename-command FLUSHDB "ADMIN_FLUSHDB" rename-command KEYS "" client-output-buffer-limit normal 0 0 0 client-output-buffer-limit slave 0 0 0 client-output-buffer-limit pubsub 32mb 8mb 60 masterauth {password} requirepass {password} '''.format(port=self.port,password=self.password) fileName = '/data/redis{port}/redis{port}.conf'.format(port=self.port) with open(fileName, 'w') as f: f.write(configTxt) redisGroup = 'cat /etc/group |grep redis' redisUser='cat /etc/passwd |grep redis' gstatus, gresult = commands.getstatusoutput(redisGroup) if gstatus == 0: pass else: gcmd = 'groupadd redis' commands.getstatusoutput(gcmd) ustatus, uresult = commands.getstatusoutput(redisUser) if ustatus == 0: pass else: ucmd = 'useradd -M -g redis redis -s /sbin/nologin' commands.getstatusoutput(ucmd) xcmd = 'chown -R redis:redis %s'% dataDir commands.getstatusoutput(xcmd) def serviceCheck(self): serviceFile = '/usr/lib/systemd/system/redis%s.service' % self.port if os.path.isfile(serviceFile): print '[%s service name alredy exists]'% serviceFile logging.error('[%s service name alredy exists]'% serviceFile) else: configTxt='''[Unit] Description=Redis persistent key-value database After=network.target After=network-online.target Wants=network-online.target [Service] ExecStart=/usr/local/redis/src/redis-server /data/redis{port}/redis{port}.conf --supervised systemd ExecStop=/usr/local/redis/src/redis-cli -h 127.0.0.1 -p {port} -a {password} shutdown Type=notify User=redis Group=redis RuntimeDirectory=redis RuntimeDirectoryMode=0755 [Install] WantedBy=multi-user.target '''.format(port=self.port,password=self.password) with open(serviceFile, 'w') as f: f.write(configTxt) cmd = 'systemctl daemon-reload' commands.getstatusoutput(cmd) def redisStart(self): cmd = 'systemctl start redis%s'% self.port status, output = commands.getstatusoutput(cmd) if status == 0: print '%s start success!' % self.port logging.debug('%s start success!'% self.port) else: print '%s start faild,please check log file'%self.port logging.debug('%s start faild,please check log file' % self.port) if __name__ == '__main__': if len(sys.argv) < 2: print " No Enter parameter!! " usage() else: try: options, args = getopt.getopt(sys.argv[1:], "a:p:", [ "pwd=", "port="]) for name, value in options: if name in ('-a', '-pwd'): redisPwd = value #print redisPwd elif name in ('-p', '-port'): redisPort = value #print redisPort #print redisPwd,redisPort for port in tqdm(redisPort.split(',')): workflow = redisClusterWork(port=port,password=redisPwd) workflow.portCheck() workflow.baseDirCheck() workflow.dataDirCheck() workflow.serviceCheck() workflow.redisStart() except getopt.GetoptError: print 'Please check if the parameters are incorrect.' usage()