Configure redis module, and use multiple redis nodes.
This is the multi-page printable view of this section. .
Redis
Customize redis components with 21 parameters
Create, remove, expand, redis cluster
Ansible playbooks that can be used in this module
Dashboards, metrics, record & alerting rules.
Frequently Asked Questions about redis module
1 - Configure
The entity model of Redis is almost the same as that of PostgreSQL, which also includes the concepts of Cluster and Instance. The Cluster here does not refer to the native Redis Cluster mode.
The core difference between the REDIS module and the PGSQL module is that Redis uses a single-node multi-instance deployment rather than the 1:1 deployment: multiple Redis instances are typically deployed on a physical/virtual machine node to utilize multicore CPUs fully. Therefore, the ways to configure and administer Redis instances are slightly different from PGSQL.
In Redis managed by Pigsty, nodes are entirely subordinate to the cluster, which means that currently, it is not allowed to deploy Redis instances of two different clusters on one node. However, this does not affect deploying multiple independent Redis primary replica instances on one node.
Redis Identity
Redis identity parameters are required parameters when defining a Redis cluster.
| Name | Attribute | Description | Example |
|---|---|---|---|
redis_cluster |
REQUIRED, cluster level | cluster name | redis-test |
redis_node |
REQUIRED, node level | Node Sequence Number | 1,2 |
redis_instances |
REQUIRED, node level | Instance Definition | { 6001 : {} ,6002 : {}} |
Redis Mode
There are three redis_mode available in Pigsty:
standalone: setup Redis in standalone (master-slave) modecluster: setup this Redis cluster as a Redis native clustersentinel: setup Redis as a sentinel for standalone Redis HA
Here are three examples:
- A 1-node, one master & one slave Redis Standalone cluster:
redis-ms - A 1-node, 3-instance Redis Sentinel cluster:
redis-sentinel - A 2-node, 6-instance Redis Cluster:
redis-cluster
Limitation
- A Redis node can only belong to one Redis cluster, which means you cannot assign a node to two different Redis clusters simultaneously.
- On each Redis node, you need to assign a unique port number to the Redis instance to avoid port conflicts.
- Typically, the same Redis cluster will use the same password, but multiple Redis instances on a Redis node cannot set different passwords (because redis_exporter only allows one password).
- Redis Cluster has built-in HA, while standalone HA requires manually configured in Sentinel because we are unsure if you have any sentinels available. Fortunately, configuring standalone Redis HA is straightforward: Configure HA with sentinel.
2 - Parameter
There are 21 parameters in the redis module.
| Parameter | Type | Level | Comment |
|---|---|---|---|
redis_cluster |
string | C | redis cluster name, required identity parameter |
redis_instances |
dict | I | redis instances definition on this redis node |
redis_node |
int | I | redis node sequence number, node int id required |
redis_fs_main |
path | C | redis main data mountpoint, /data by default |
redis_exporter_enabled |
bool | C | install redis exporter on redis nodes? |
redis_exporter_port |
port | C | redis exporter listen port, 9121 by default |
redis_exporter_options |
string | C/I | cli args and extra options for redis exporter |
redis_safeguard |
bool | G/C/A | prevent purging running redis instance? |
redis_clean |
bool | G/C/A | purging existing redis during init? |
redis_rmdata |
bool | G/C/A | remove redis data when purging redis server? |
redis_mode |
enum | C | redis mode: standalone,cluster,sentinel |
redis_conf |
string | C | redis config template path, except sentinel |
redis_bind_address |
ip | C | redis bind address, empty string will use host ip |
redis_max_memory |
size | C/I | max memory used by each redis instance |
redis_mem_policy |
enum | C | redis memory eviction policy |
redis_password |
password | C | redis password, empty string will disable password |
redis_rdb_save |
string[] | C | redis rdb save directives, disable with empty list |
redis_aof_enabled |
bool | C | enable redis append only file? |
redis_rename_commands |
dict | C | rename redis dangerous commands |
redis_cluster_replicas |
int | C | replica number for one master in redis cluster |
redis_sentinel_monitor |
master[] | C | sentinel master list, sentinel cluster only |
Defaults
The default parameters are defined in roles/redis/defaults/main.yml
redis_cluster
name: redis_cluster, type: string, level: C
redis cluster name, required identity parameter.
no default value, you have to define it explicitly.
Comply with regexp [a-z][a-z0-9-]*, it is recommended to use the same name as the group name and start with redis-
redis_node
name: redis_node, type: int, level: I
redis node sequence number, unique integer among redis cluster is required
You have to explicitly define the node id for each redis node. integer start from 0 or 1.
redis_instances
name: redis_instances, type: dict, level: I
redis instances definition on this redis node
no default value, you have to define redis instances on each redis node using this parameter explicitly.
Here is an example for a native redis cluster definition
The port number should be unique among the node, and the replica_of in value should be instance member of the same redis cluster.
redis_fs_main
name: redis_fs_main, type: path, level: C
redis main data mountpoint, /data by default
default values: /data, and /data/redis will be used as the redis data directory.
redis_exporter_enabled
name: redis_exporter_enabled, type: bool, level: C
install redis exporter on redis nodes?
default value is true, which will launch a redis_exporter on this redis_node
redis_exporter_port
name: redis_exporter_port, type: port, level: C
redis exporter listen port, 9121 by default
default values: 9121
redis_exporter_options
name: redis_exporter_options, type: string, level: C/I
cli args and extra options for redis exporter, will be added to /etc/default/redis_exporter.
default value is empty string
redis_safeguard
name: redis_safeguard, type: bool, level: G/C/A
prevent purging running redis instance?
default value is false, if set to true, and redis instance is running, init / remove playbook will abort immediately.
redis_clean
name: redis_clean, type: bool, level: G/C/A
purging existing redis during init?
default value is true, which will remove redis server during redis init or remove.
redis_rmdata
name: redis_rmdata, type: bool, level: G/C/A
remove redis data when purging redis server?
default value is true, which will remove redis rdb / aof along with redis instance.
redis_mode
name: redis_mode, type: enum, level: C
redis mode: standalone,cluster,sentinel
default values: standalone
standalone: setup redis as standalone (master-slave) modecluster: setup this redis cluster as a redis native clustersentinel: setup redis as sentinel for standalone redis HA
redis_conf
name: redis_conf, type: string, level: C
redis config template path, except sentinel
default values: redis.conf, which is a template file in roles/redis/templates/redis.conf.
If you want to use your own redis config template, you can put it in templates/ directory and set this parameter to the template file name.
Note that redis sentinel are using a different template file, which is roles/redis/templates/redis-sentinel.conf
redis_bind_address
name: redis_bind_address, type: ip, level: C
redis bind address, empty string will use inventory hostname
default values: 0.0.0.0, which will bind to all available IPv4 address on this host
PLEASE bind to intranet IP only in production environment, i.e. set this value to
''
redis_max_memory
name: redis_max_memory, type: size, level: C/I
max memory used by each redis instance, default values: 1GB
redis_mem_policy
name: redis_mem_policy, type: enum, level: C
redis memory eviction policy
default values: allkeys-lru, check redis eviction policy for more details
noeviction: New values aren’t saved when memory limit is reached. When a database uses replication, this applies to the primary databaseallkeys-lru: Keeps most recently used keys; removes least recently used (LRU) keysallkeys-lfu: Keeps frequently used keys; removes least frequently used (LFU) keysvolatile-lru: Removes least recently used keys with the expire field set to true.volatile-lfu: Removes least frequently used keys with the expire field set to true.allkeys-random: Randomly removes keys to make space for the new data added.volatile-random: Randomly removes keys with expire field set to true.volatile-ttl: Removes keys with expire field set to true and the shortest remaining time-to-live (TTL) value.
redis_password
name: redis_password, type: password, level: C/N
redis password, empty string will disable password, which is the default behavior
Note that due to the implementation limitation of redis_exporter, you can only set one redis_password per node. This is usually not a problem, because pigsty does not allow deploying two different redis cluster on the same node.
PLEASE use a strong password in production environment
redis_rdb_save
name: redis_rdb_save, type: string[], level: C
redis rdb save directives, disable with empty list, check redis persist for details.
the default value is ["1200 1"]: dump the dataset to disk every 20 minutes if at least 1 key changed:
redis_aof_enabled
name: redis_aof_enabled, type: bool, level: C
enable redis append only file? default value is false.
redis_rename_commands
name: redis_rename_commands, type: dict, level: C
rename redis dangerous commands, which is a dict of k:v old: new
default values: {}, you can hide dangerous commands like FLUSHDB and FLUSHALL by setting this value, here’s an example:
redis_cluster_replicas
name: redis_cluster_replicas, type: int, level: C
replica number for one master/primary in redis cluster, default values: 1
redis_sentinel_monitor
name: redis_sentinel_monitor, type: master[], level: C
This can only be used when redis_mode is set to sentinel.
List of redis master to be monitored by this sentinel cluster. each master is defined as a dict with name, host, port, password, quorum keys.
The name and host are mandatory, port, password, quorum are optional, quorum is used to set the quorum for this master, usually large than half of the sentinel instances.
3 - Administration
Here are some common administration tasks for Redis. Check FAQ: Redis for more details.
Init Redis
Init Cluster/Node/Instance
You can also use the wrapper script:
Remove Redis
Remove Cluster/Node/Instance
You can also use the wrapper script:
Reload Redis
You can partially run redis.yml tasks to re-configure redis.
Beware that redis cannot be reloaded online; you have to restart redis to make config effective.
Use Redis CLI
Access redis instance with redis-cli:
Redis also has a redis-benchmark which can be used for benchmark and generate load on redis server:
Replicate Redis
https://redis.io/commands/replicaof/
HA with Sentinel
You have to enable HA for redis standalone m-s cluster manually with your redis sentinel.
Take the 4-node sandbox as an example, a redis sentinel cluster redis-meta is used to manage the redis-ms standalone cluster.
If you wish to remove a redis master from sentinel, use SENTINEL REMOVE <name>.
You can configure multiple redis master on sentinel cluster with redis_sentinel_monitor.
And refresh the master list on sentinel cluster with:
4 - Playbook
There are two playbooks for redis:
redis.yml: create redis cluster / node / instanceredis-rm.yml: remove redis cluster /docs/node /instance
redis.yml
The playbook redis.yml will init redis cluster/node/instance:
redis-rm.yml
The playbook redis-rm.yml will remove redis cluster/node/instance:
5 - Monitor
Dashboard
There are three dashboards for REDIS module.
Redis Overview
Redis Overview: Overview of all Redis Instances
Redis Cluster
Redis Cluster : Overview of one single redis cluster
Redis Instance
Redis Instance : Overview of one single redis instance
Alert Rules
There are 6 predefined alert rules for Redis, defined in files/prometheus/rules/redis.yml.
| Name | Description | Level |
|---|---|---|
RedisDown |
Redis server is down | Critical |
RedisRejectConn |
Redis instance rejects connection | Critical |
RedisRTHigh |
Redis instance response time is too high | Warning |
RedisCPUHigh |
Redis instance CPU usage is too high | Warning |
RedisMemHigh |
Redis instance memory usage is too high | Warning |
RedisQPSHigh |
Redis instance QPS is too high | Warning |
6 - FAQ
ABORT due to existing redis instance
use
redis_clean = trueandredis_safeguard = falseto force clean redis data
This happens when you run redis.yml to init a redis instance that is already running, and redis_clean is set to false.
If redis_clean is set to true (and the redis_safeguard is set to false, too), the redis.yml playbook will remove the existing redis instance and re-init it as a new one, which makes the redis.yml playbook fully idempotent.
ABORT due to redis_safeguard enabled
This happens when removing a redis instance with
redis_safeguardset totrue.
You can disable redis_safeguard to remove the Redis instance. This is what redis_safeguard is for.
How to add a single new redis instance on this node?
Use
bin/redis-add <ip> <port>to deploy a new redis instance on node.
How to remove a single redis instance from the node?
bin/redis-rm <ip> <port>to remove a single redis instance from node