Puppet Class: profile::toolforge::redis_sentinel

Defined in:
modules/profile/manifests/toolforge/redis_sentinel.pp

Summary

Redis with Sentinel and Keepalived for high availability

Overview

Parameters:

  • redis_primary (Stdlib::Fqdn) (defaults to: lookup('profile::toolforge::redis_sentinel::primary'))
  • redis_hosts (Array[Stdlib::Fqdn]) (defaults to: lookup('profile::toolforge::redis_sentinel::redis_hosts', {default_value => []}))
  • maxmemory (String) (defaults to: lookup('profile::toolforge::redis_sentinel::maxmemory', {default_value => '12GB'}))
  • secret_command_prefix (String) (defaults to: lookup('profile::toolforge::redis_sentinel::secret_command_prefix', {default_value => 'notasecret'}))
  • keepalived_vips (Array[Stdlib::Fqdn]) (defaults to: lookup('profile::toolforge::redis_sentinel::keepalived_vips', {default_value => []}))
  • keepalived_password (String) (defaults to: lookup('profile::toolforge::redis_sentinel::keepalived_password', {default_value => 'notarealpassword'}))
  • prometheus_nodes (Array[Stdlib::Host]) (defaults to: lookup('prometheus_nodes'))


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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'modules/profile/manifests/toolforge/redis_sentinel.pp', line 2

class profile::toolforge::redis_sentinel (
    Stdlib::Fqdn        $redis_primary         = lookup('profile::toolforge::redis_sentinel::primary'),
    Array[Stdlib::Fqdn] $redis_hosts           = lookup('profile::toolforge::redis_sentinel::redis_hosts',           {default_value => []}),
    String              $maxmemory             = lookup('profile::toolforge::redis_sentinel::maxmemory',             {default_value => '12GB'}),
    String              $secret_command_prefix = lookup('profile::toolforge::redis_sentinel::secret_command_prefix', {default_value => 'notasecret'}),
    Array[Stdlib::Fqdn] $keepalived_vips       = lookup('profile::toolforge::redis_sentinel::keepalived_vips',       {default_value => []}),
    String              $keepalived_password   = lookup('profile::toolforge::redis_sentinel::keepalived_password',   {default_value => 'notarealpassword'}),
    Array[Stdlib::Host] $prometheus_nodes      = lookup('prometheus_nodes'),
) {
    $redis_sentinel_own_address = $::facts['networking']['ip']
    $redis_primary_address = ipresolve($redis_primary)

    # Security by obscurity! https://words.yuvi.in/attempting-to-secure-redis-in-a-multi-tenant-environment/
    # These commands are dangerous and should not be used by normal users
    $secret_commands = [
        'CLIENT',
        'CONFIG',
        'DEBUG',
        'FLUSHALL',
        'FLUSHDB',
        'KEYS',
        'MONITOR',
        'RANDOMKEY',
        'REPLICAOF',
        'SCAN',
        'SHUTDOWN',
        'SLAVEOF',
    ]

    # ... but since Sentinel needs some of them to perform failovers, let's prefix
    # them with a secret string so they're practically unusable
    # TODO: figure out which ones are needed and which ones can be truly removed
    $mapped_secret_commands = $secret_commands.reduce({}) |$cumulate, $secret_command| {
        merge($cumulate, {"${secret_command}" => "${secret_command_prefix}${secret_command}"})
    }

    ensure_packages('redis-sentinel')

    service { 'redis-sentinel':
        ensure  => stopped,
        enable  => false,
        require => Package['redis-sentinel'],
    }

    # Set up a non-default instance so we have total control over the creation of its config
    # since redis-sentinel stores some information in the config, we can't just overwrite
    # it every time, but we want to set the initial values
    service { 'redis-sentinel@toolforge':
        ensure  => running,
        enable  => true,
        require => Package['redis-sentinel'],
    }

    file { '/etc/redis/sentinel-toolforge.conf':
        content   => template('profile/toolforge/redis/sentinel.conf.erb'),
        owner     => 'redis',
        group     => 'redis',
        mode      => '0640',
        notify    => Service['redis-sentinel@toolforge'],
        replace   => false,
        show_diff => false,
    }

    if $redis_primary != $::fqdn {
        $slaveof = "${redis_primary_address} 6379"
    } else {
        $slaveof = undef
    }

    redis::instance { '6379':
        # prevent puppet and sentinel fighting each other (T309014)
        # TODO: figure out if we can have a more elegant solution
        allow_config_writes => true,
        settings            => {
            client_output_buffer_limit  => 'slave 512mb 200mb 60',
            dbfilename                  => "${::hostname}-6379.rdb",
            dir                         => '/srv/redis',
            maxmemory                   => $maxmemory,
            maxmemory_policy            => 'allkeys-lru',
            maxmemory_samples           => 5,
            save                        => '300 100',
            slave_read_only             => true,
            stop_writes_on_bgsave_error => false,
            slave_priority              => fqdn_rand(99) + 1,
            slaveof                     => $slaveof,
            bind                        => '0.0.0.0',
            rename_command              => $mapped_secret_commands,
            timeout                     => 600,
        },
    }

    # Monitoring!
    prometheus::redis_exporter { '6379': }

    # Allow users to connect
    ferm::service { 'toolforge-redis-access':
        proto => 'tcp',
        port  => 6379,
    }

    $redis_hosts_ferm = join($redis_hosts, ' ')

    # Sentinels need to talk to each other
    ferm::service { 'toolforge-redis-sentinel-internal':
        proto  => 'tcp',
        port   => 26379,
        srange => "@resolve((${redis_hosts_ferm}))"
    }

    # and keepalived too
    ferm::rule { 'toolforge-redis-keepalived-vrrp':
        rule   => "proto vrrp saddr (@resolve((${redis_hosts_ferm}))) ACCEPT;",
    }

    $prometheus_ferm_nodes = join($prometheus_nodes, ' ')
    ferm::service { 'toolforge-redis-prometheus':
        proto  => 'tcp',
        port   => 9121,
        srange => "@resolve((${prometheus_ferm_nodes}))"
    }

    # Script that keepalived users to check if this instance should have all the traffic
    file { '/usr/local/bin/wmcs-check-redis-master':
        source => 'puppet:///modules/profile/toolforge/redis/wmcs-check-redis-master.sh',
        owner  => 'root',
        group  => 'redis',
        mode   => '0550',
    }

    $interface = $::facts['networking']['primary']
    $keepalived_peers = delete($redis_hosts, $::fqdn)
    class { 'keepalived':
        peers     => [],        # overriden by config template
        auth_pass => 'ignored', # overriden by config template
        vips      => [],        # overriden by config template
        config    => template('profile/toolforge/redis/keepalived.conf.erb'),
    }
}