设为首页 收藏本站
查看: 1474|回复: 0

[经验分享] Puppet模块(九):redis模块

[复制链接]
累计签到:2 天
连续签到:1 天
发表于 2016-3-24 09:00:22 | 显示全部楼层 |阅读模式
一、模块说明
    Redis是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。
    数据持久化有快照方式和日志追加方式,这里使用快照方式,定期将数据保存在dump.rdb文件中
    配置主从同步,可让多个slave分摊读请求的负载,也可在master禁用数据持久化,减轻负担,只在slave配置持久化。

二、目录结构
wKiom1byUGawHB7GAAANkBdAkQ8405.jpg
三、代码展示
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配置
    参考前两编文章  
    主库参数:

wKiom1byUR-B7zIyAAAyDj3r_SU832.jpg
    从库参数:

wKioL1byVK3w2msPAAA7b12MPZk491.jpg

五、Redis命令行管理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[iyunv@redis-server ~]# 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 连接。
wKiom1byVC6w4KvNAAF5CcsjGsk392.jpg
wKioL1byVMWTxjhUAAA_X7BabbI606.jpg
wKiom1byVC6DqIHkAAE_BDUxpr4545.jpg
wKioL1byVMbAIT59AAEbapW1YzY241.jpg

七、程序使用
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、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-194973-1-1.html 上篇帖子: Puppet模块(八):keepalived模块 下篇帖子: Puppet模块(十):postfix模块
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表