標籤:puppet redis foreman
一、模組說明
Redis是一個開源的使用ANSI C語言編寫、支援網路、可基於記憶體亦可持久化的日誌型、Key-Value資料庫,並提供多種語言的API。
資料持久化有快照方式和日誌追加方式,這裡使用快照方式,定期將資料儲存在dump.rdb檔案中
配置主從同步,可讓多個slave分攤讀請求的負載,也可在master禁用資料持久化,減輕負擔,只在slave配置持久化。
二、目錄結構
650) this.width=650;" src="http://s5.51cto.com/wyfs02/M00/7D/E7/wKiom1byUGawHB7GAAANkBdAkQ8405.png" title="QQ20160323160613.png" alt="wKiom1byUGawHB7GAAANkBdAkQ8405.png" />
三、代碼展示
1、files目錄
redis #redis服務指令碼
#!/bin/sh# chkconfig: 2345 80 90# Simple Redis init.d script conceived to work on Linux systems# as it does use of the /proc filesystem.# src file is /usr/local/redis/utils/redis_init_scriptREDISPORT=6379EXEC=/usr/local/bin/redis-serverCLIEXEC=/usr/local/bin/redis-cliPIDFILE=/var/run/redis.pidCONF="/etc/redis.conf"case "$1" in start)if [ -f $PIDFILE ]thenecho "$PIDFILE exists, process is already running or crashed"elseecho "Starting Redis server..."$EXEC $CONF &fi;;stop)if [ ! -f $PIDFILE ]thenecho "$PIDFILE does not exist, process is not running"elsePID=$(cat $PIDFILE)echo "Stopping ..."$CLIEXEC -p $REDISPORT shutdownwhile [ -x /proc/${PID} ]doecho "Waiting for Redis to shutdown ..."sleep 1doneecho "Redis stopped"fi;;*)echo "Please use start or stop as first argument";;esac
redis-2.8.19.tar.gz #redis安裝包,下載 http://download.redis.io/releases/redis-2.8.19.tar.gz
2、manifests目錄
init.pp
class redis{ Exec { path => "/usr/bin:/bin:/usr/sbin:/sbin" } include redis::install,redis::config,redis::service}
install.pp
class redis::install { Exec{ path => [‘/usr/bin‘,‘/usr/sbin‘,‘/bin‘] } package { [‘gcc‘,‘gcc-c++‘,‘perl‘,‘pcre‘,‘zlib-devel‘,‘openssl-devel‘,‘tcl‘]: ensure => installed, before => Exec[‘install‘], } file { ‘redis‘: path => ‘/usr/local/src/redis-2.8.19.tar.gz‘, ensure => directory, source => ‘puppet:///modules/redis/redis-2.8.19.tar.gz‘, ignore => ‘.svn‘, owner => root, group => root, mode => ‘0640‘, } exec { ‘tar redis‘: command => ‘tar -zxf redis-2.8.19.tar.gz‘, cwd => ‘/usr/local/src‘, refreshonly => true, subscribe => File[‘redis‘], } exec { ‘mv redis‘: command => ‘mv -f redis-2.8.19 /usr/local/redis‘, cwd => ‘/usr/local/src‘, refreshonly => true, subscribe => Exec[‘tar redis‘], } exec { ‘install‘: command => ‘make && make install‘, cwd => ‘/usr/local/redis‘, refreshonly => true, subscribe => Exec[‘mv redis‘], }}
config.pp
class redis::config { case $redis_conf { master: { file { ‘/etc/redis.conf‘: ensure => file, owner => root, group => root, mode => 400, content => template("redis/Master-redis.conf.erb"), notify => Class[‘redis::service‘], require => Class[‘redis::install‘], } } slave: { file { ‘/etc/redis.conf‘: ensure => file, owner => root, group => root, mode => 400, content => template("redis/Slave-redis.conf.erb"), notify => Class[‘redis::service‘], require => Class[‘redis::install‘], } } } file { ‘redisd‘: path => ‘/etc/rc.d/init.d/redis‘, ensure => file, owner => root, group => root, mode => 755, source => ‘puppet:///modules/redis/redis‘, } exec { ‘iptables -I INPUT -p tcp --dport 6379 -j ACCEPT && service iptables save‘: unless => ‘grep "6379" /etc/sysconfig/iptables 2>/dev/null‘, }}
service.pp
class redis::service { service { ‘redis‘: ensure => ‘running‘, enable => ‘true‘, hasrestart => ‘false‘, hasstatus => ‘false‘, start => ‘service redis start‘, stop => ‘service redis stop‘, require => Class["redis::install"], subscribe => Class["redis::config"], }}
3、templates目錄
Master-redis.conf.erb
# Redis Master configuration file example daemonize yes pidfile /var/run/redis.pid port 6379 timeout 0 tcp-keepalive 0 loglevel notice logfile /var/log/redis.log databases 16 save 86400 1 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdbdir ./ slave-serve-stale-data yes slave-read-only yes repl-disable-tcp-nodelay no slave-priority 100 maxmemory 3gb maxmemory-policy volatile-lru maxmemory-samples 3 appendonly no appendfilename appendonly.aof appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 1024 hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-entries 512 list-max-ziplist-value 64 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit slave 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 aof-rewrite-incremental-fsync yes
Slave-redis.conf.erb
# Redis Slave configuration file example daemonize yes pidfile /var/run/redis.pid port 6379 timeout 0 tcp-keepalive 0 loglevel notice logfile /var/log/redis.log databases 16 save 86400 1 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb dir ./ slaveof <%[email protected]_server %> 6379 slave-serve-stale-data yes slave-read-only yes repl-disable-tcp-nodelay no slave-priority 100 maxmemory 3gb maxmemory-policy volatile-lru maxmemory-samples 3 appendonly no appendfilename appendonly.aof appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 1024 hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-entries 512 list-max-ziplist-value 64 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit slave 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 aof-rewrite-incremental-fsync yes
四、Foreman配置
參考前兩編文章
主庫參數:
650) this.width=650;" src="http://s1.51cto.com/wyfs02/M01/7D/E7/wKiom1byUR-B7zIyAAAyDj3r_SU832.png" title="QQ20160323161717.png" alt="wKiom1byUR-B7zIyAAAyDj3r_SU832.png" />
從庫參數:
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/7D/E4/wKioL1byVK3w2msPAAA7b12MPZk491.png" title="QQ20160323162957.png" alt="wKioL1byVK3w2msPAAA7b12MPZk491.png" />
五
、Redis命令列管理
[[email protected] ~]# redis-cli #串連redis資料庫redis127.0.0.1:6379> >ping #測試連接 >echo test #測試列印 >select 1 #選擇資料庫0~15 > dbsize #返回當前資料庫key數量 > info #擷取伺服器狀態和一些統計資訊 > monitor #即時監聽並返回redis伺服器接收到的所有請求資訊 > config get parameter #擷取一個redis配置參數資訊,*表示全部 > config set parameter value #設定一個redis配置參數資訊 > config resetstat #重設INFO命令的統計資訊:Keyspace命中數、Keyspace錯誤數、處理命令數,接收串連數、到期key數等 > debug object key #擷取一個key的調試資訊 > debug segfault #製造一次伺服器當機 > flushdb #刪除當前資料庫中的所有key > flushall #刪除所有資料庫中所有key > client list #查看當前所有用戶端狀態 > shutdown #把資料同步儲存到磁碟上,並關閉redis服務 > quit #退出串連
六、Redis 案頭管理工具
http://redisdesktop.com/download
https://github.com/uglide/RedisDesktopManager/wiki/Quick-Start
Redis DesktopManager 是一個快速、簡單、支援跨平台的 Redis 案頭管理工具,基於 Qt 5 開發,支援通過 SSH Tunnel 串連。
650) this.width=650;" src="http://s2.51cto.com/wyfs02/M00/7D/E8/wKiom1byVC6w4KvNAAF5CcsjGsk392.png" style="float:none;" title="1.png" alt="wKiom1byVC6w4KvNAAF5CcsjGsk392.png" />
650) this.width=650;" src="http://s2.51cto.com/wyfs02/M00/7D/E4/wKioL1byVMWTxjhUAAA_X7BabbI606.png" style="float:none;" title="2.png" alt="wKioL1byVMWTxjhUAAA_X7BabbI606.png" />
650) this.width=650;" src="http://s2.51cto.com/wyfs02/M00/7D/E8/wKiom1byVC6DqIHkAAE_BDUxpr4545.png" style="float:none;" title="3.png" alt="wKiom1byVC6DqIHkAAE_BDUxpr4545.png" />
650) this.width=650;" src="http://s2.51cto.com/wyfs02/M00/7D/E4/wKioL1byVMbAIT59AAEbapW1YzY241.png" style="float:none;" title="4.png" alt="wKioL1byVMbAIT59AAEbapW1YzY241.png" />
七、程式使用
<?xml version="1.0"?><configuration> <appSettings> <add key="Redis.ReadWriteHosts" value="10.188.1.40:6379" /> <add key="Redis.ReadOnlyHosts" value="10.188.1.41:6379" /> </appSettings></configuration>
本文出自 “月晴星飛” 部落格,請務必保留此出處http://ywzhou.blog.51cto.com/2785388/1754384
Puppet自編模組(三):redis