爱运维网 发表于 2016-3-24 09:00:22

Puppet模块(九):redis模块

一、模块说明
    Redis是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。
    数据持久化有快照方式和日志追加方式,这里使用快照方式,定期将数据保存在dump.rdb文件中
    配置主从同步,可让多个slave分摊读请求的负载,也可在master禁用数据持久化,减轻负担,只在slave配置持久化。

二、目录结构

三、代码展示1、files目录    redis    #redis服务脚本


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/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_script
REDISPORT=6379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli
PIDFILE=/var/run/redis.pid
CONF="/etc/redis.conf"
case "$1" in
    start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
$EXEC $CONF &
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$CLIEXEC -p $REDISPORT shutdown
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "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
1
2
3
4
class redis{
    Exec { path => "/usr/bin:/bin:/usr/sbin:/sbin" }
    include redis::install,redis::config,redis::service
}




    install.pp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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

1
2
3
4
5
6
7
8
9
10
11
12
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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.rdb
dir ./
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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 <%=@master_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配置
    参考前两编文章
    主库参数:


    从库参数:



五、Redis命令行管理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 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 连接。





七、程序使用

1
2
3
4
5
6
7
<?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>






页: [1]
查看完整版本: Puppet模块(九):redis模块