仓库源文站点原文


title: "[Walkthrough] Install and set up Redis" categories: Tech updated: 2023-01-04 comments: true

mathjax: false

Check system info.

cat /etc/os-release

Follow a walkthrough listed here. The below works on CentOS 7.

<!-- more -->

Installing

Install EPEL (Extra Packages for Enterprise Linux).

sudo yum install epel-release

Install Redis. Use the -y option to automatically answer yes to installing additional software dependencies.

sudo yum install redis -y

Start Redis and check the status.

sudo systemctl start redis.service
sudo systemctl status redis.service

Configuring

The configuration file is located at /etc/redis.conf, and it is self-documented.

echo "digital-ocean" | sha256sum

After configuring, save and close the file then restart Redis:

sudo systemctl restart redis.service

Alternatively, start with

sudo redis-server /etc/redis.conf

Remember to check advice for configuring and managing Redis in production (refer to this gist).

Set the Linux kernel overcommit memory setting to 1. Add vm.overcommit_memory = 1 to /etc/sysctl.conf. Then, reboot or run the command sysctl vm.overcommit_memory=1 to activate the setting.

Managing

Redis 主从, 哨兵, 和集群这三个有什么区别?

Replication (master-slave)

Redis replication: How Redis supports high availability and failover with replication

<details><summary><b></b><font color="deepskyblue"> (Show more »)</font></summary> <p>The replica will automatically reconnect to the master every time the link breaks, and will attempt to be an exact copy of it <em>regardless</em> of what happens to the master.</p> <p>Redis uses by default asynchronous replication with low latency and high performance.</p> <p>Since Redis 2.6 by default slaves are read-only.</p></details>

REPLICATION section in conf file.

In setups where Redis replication is used, it is strongly advised to have persistence turned on in the master and in the replicas.

Configuring master

Configuring slave

Uncomment and configure the following lines

By default, a replica will ignore maxmemory. It means that the eviction of keys will be handled by the master, sending the DEL commands to the replica as keys evict in the master side.

High availability with Sentinel

High availability with Redis Sentinel: High availability for non-clustered Redis

Start Sentinel.

redis-server /etc/sentinel.conf --sentinel

It is mandatory to use a configuration file when running Sentinel, as this file will be used by the system in order to save the current state that will be reloaded in case of restarts.

Sentinels by default run listening for connections to TCP port 26379.

The sample configuration file is located at /etc/redis-sentinel.conf.

One of the capabilities of Sentinel is automatic failover. You need at least three Sentinel instances for a robust deployment. See Example Sentinel deployments for further explanation.

Scaling (cluster)

Scaling with Redis Cluster: Horizontal scaling with Redis Cluster

Note that the minimal cluster that works as expected must contain at least three master nodes. For deployment, we strongly recommend a six-node cluster, with three masters and three replicas. Source

Not covered in this post.

Sample Makefile

SHELL := /bin/bash

SOURCE_HOST := USER@HOST
MASTER_CONF_PATH := /etc/redis.conf
SLAVE_CONF_PATH := /etc/redis-slave.conf
SENTINEL_CONF_PATH := /etc/redis-sentinel.conf

.PHONY: install, pull
install:
    sudo yum install epel-release
    sudo yum install redis -y
pull:
    for path in "${MASTER_CONF_PATH}" "${SLAVE_CONF_PATH}" "${SENTINEL_CONF_PATH}"; do \
      sudo rsync --verbose "${SOURCE_HOST}":"$${path}" "$${path}"; \
    done

.PHONY: master, slave, stop
master: stop
    sudo redis-server "${MASTER_CONF_PATH}"
    sudo redis-server "${SENTINEL_CONF_PATH}" --sentinel
slave: stop
    sudo redis-server "${SLAVE_CONF_PATH}"
    sudo redis-server "${SENTINEL_CONF_PATH}" --sentinel
stop:
    -pgrep -f "redis-server" | xargs sudo kill -9