前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Install and Configure Redis on CentOS 7

Install and Configure Redis on CentOS 7

作者头像
heidsoft
发布2019-12-18 11:07:59
6730
发布2019-12-18 11:07:59
举报
文章被收录于专栏:云计算与大数据

Redis is an open-source, in-memory, data structure store with optional disk writes for persistence. It can be used as a key-value database, or as a cache and message broker. Redis features built-in transactions, replication, and support for a variety of data structures such as strings, hashes, lists, sets, and others. Redis can be made highly available with Redis Sentinel and supports automatic partitioning with Redis Cluster.

This document provides both instructions for deploying the Redis server, and an overview of best practices for maintaining Redis instances on CentOS 7. Since Redis serves all data from memory, we recommend using a high memory Linode with this guide.

Before You Begin

  1. Familiarize yourself with our Getting Started guide and complete the steps for setting your Linode’s hostname and timezone.
  2. Complete the sections of our Securing Your Server to create a standard user account, harden SSH access and remove unnecessary network services.
  3. Update your system: sudo yum update

NoteThis guide is written for a non-root user. Commands that require elevated privileges are prefixed with sudo. If you’re not familiar with the sudo command, you can check our Users and Groups guide. To utilize the replication steps in this guide, you will need at least two Linodes.

Install Redis

In this section you’ll add the EPEL repository, and then use it to install Redis.

  1. Add the EPEL repository, and update YUM to confirm your change: sudo yum install epel-release sudo yum update
  2. Install Redis: sudo yum install redis
  3. Start Redis: sudo systemctl start redis Optional: To automatically start Redis on boot: sudo systemctl enable redis

Verify the Installation

Verify that Redis is running with redis-cli:

代码语言:javascript
复制
redis-cli ping

If Redis is running, it will return:

代码语言:javascript
复制
PONG

Configure Redis

In this section, you’ll configure some basic persistence and tuning options for Redis.

Persistence Options

Redis provides two options for disk persistence:

  • Point-in-time snapshots of the dataset, made at specified intervals (RDB).
  • Append-only logs of all the write operations performed by the server (AOF).

Each option has its own pros and cons which are detailed in the Redis documentation. For the greatest level of data safety, consider running both persistence methods.

Because the Point-in-time snapshot persistence is enabled by default, you only need to set up AOF persistence:

  1. Make sure that the following values are set for the appendonly and appendfsync settings in redis.conf:
    • /etc/redis.conf

    12

    appendonly yesappendfsync everysec

  2. Restart Redis: sudo systemctl restart redis

Basic System Tuning

To improve Redis performance, set the Linux kernel overcommit memory setting to 1:

代码语言:javascript
复制
sudo sysctl vm.overcommit_memory=1

This immediately changes the overcommit memory setting, but the change will not persist across reboots. To make it permanent, add vm.overcommit_memory = 1 to /etc/sysctl.conf:

  • /etc/sysctl.conf

1

vm.overcommit_memory = 1

Additional Swap

Depending upon your usage, you may find it necessary to add extra swap disk space. You can add swap by resizing your disk in the Linode Manager. The Redis documentation recommends the size of your swap disk match the amount of memory available to your system.

Distributed Redis

Redis provides several options for setting up distributed data stores. The simplest option, covered below, is master/slave replication, which creates copies of data. It will also allow distribution of reads among groups of slave copies as long as all write operations are handled by the master server.

The master/slave setup described above can be made highly available with Redis Sentinel. Sentinel can be configured to monitor both master and slave instances, and will perform automatic failover if the master node is not working as expected. That means that one of the slave nodes will be elected master and all other slave nodes will be configured to use the new master.

With Redis version 3.0 and above, you can use Redis Cluster, a data sharding solution that automatically manages replication and failover. With Redis Cluster, you are able to automatically split your dataset among multiple nodes, which is useful when your dataset is larger than a single server’s RAM. It also gives you the ability to continue operations when a subset of the nodes are experiencing failures or are unable to communicate with the rest of the cluster.

The following steps will guide you through master/slave replication, with the slaves set to read-only mode.

Set Up Redis Master/Slave Replication

For this section, you will use two Linodes, a master and a slave.

NoteTo communicate over the private network, your master and slave Linodes must reside in the same data center.

Prepare Your Linodes

  1. Set up both Linodes with a Redis instance, using the Installation and Configuration steps from this guide. You can also copy your initially configured disk to another Linode using the Clone option in the Linode Manager.
  2. Configure Private IP Addresses on both Linodes, and make sure you can access the master Linode’s private IP address from the slave. You will use only private addresses for replication traffic for security reasons.

Configure the Master Linode

  1. Configure the master Redis instance to listen on a private IP address by updating the bind configuration option in redis.conf. Replace 192.0.2.100 with the master Linode’s private IP address:
    • /etc/redis.conf

    1

    bind 127.0.0.1 192.0.2.100

  2. Restart Redis to apply the changes: sudo systemctl restart redis

Configure the Slave Linode

  1. Configure a slave instance by adding the slaveof directive into redis.conf to setup the replication. Again replace 192.0.2.100 with the master Linode’s private IP address: The slaveof directive takes two arguments: the first is the IP address of the master node; the second is the Redis port specified in the master’s configuration.
    • /etc/redis.conf

    1

    slaveof 192.0.2.100 6379

  2. Restart the slave Redis instance: sudo systemctl restart redis After restarting, the slave Linode will attempt to synchronize its data set to master and then propagate the changes.

Confirm Replication

Test that the replication works. On your master Linode, run redis-cli and execute command set 'a' 1

代码语言:javascript
复制
redis-cli
127.0.0.1:6379> set 'a' 1
OK

Type exit or press Ctrl-C to exit from redis-cli prompt.

Next, run redis-cli on the slave Linode and execute get 'a', which should return the same value as that on the master:

代码语言:javascript
复制
redis-cli
127.0.0.1:6379> get 'a'
"1"

Your master/slave replication setup is working properly.

Secure the Redis Installation

Since Redis is designed to work in trusted environments and with trusted clients, you should control access to the Redis instance. Some recommended security steps include:

  • Set up a firewall using iptables.
  • Encrypt Redis traffic using an SSH tunnel, or the methods described in the Redis Security documentation.

Additionally, to ensure that no outside traffic accesses your Redis instance, we suggest that you only listen for connections on the localhost interface or your Linode’s private IP address.

Use Password Authentication

For an added layer of security, use password authentication to secure the connection between your master and slave Linodes.

  1. On your master Linode, uncomment the requirepass line in your Redis configuration and replace master_password with a secure password:
    • /etc/redis.conf

    1

    requirepass master_password

  2. Save your changes, and apply them by restarting Redis on the master Linode: sudo systemctl restart redis
  3. On your slave Linode, add the master password to your Redis configuration under masterpass, and then create a unique password for the slave Linode with requirepass: Replace master_password with the password you configured on your master, and replace slave_password with the password to use for your slave Linode.
    • /etc/redis.conf

    12

    masterpass master_passwordrequirepass slave_password

  4. Save your changes, and restart Redis on your slave Linode: sudo systemctl restart redis
  5. Connect to redis-cli on your master Linode, and use AUTH to authenticate with your master password: redis-cli 127.0.0.1:6379> AUTH master_password
  6. Once you’ve authenticated, you can view details about your Redis configuration by running INFO. This provides a lot of information, so you can specifically request the “Replication” section in your command: 127.0.0.1:6379> INFO replication Output should be similar to the following: # Replication role:master connected_slaves:1 slave0:ip=192.0.2.105,port=6379,state=online,offset=1093,lag=1 It should confirm the master role of your Linode, as well as how many slave Linodes are connected to it.
  7. From your slave Linode, connect to redis-cli and authenticate using your slave password: redis-cli 127.0.0.1:6379> AUTH slave_password
  8. Once you’ve authenticated, use INFO to confirm your slave Linode’s role, and its connection to the master server: 127.0.0.1:6379> INFO replication # Replication role:slave master_host:192.0.2.100 master_port:6379 master_link_status:up

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

  • Redis Project Home Page
  • Redis Configuration
  • Redis Persistence
  • Redis Security
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-12-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 云数智圈 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Before You Begin
  • Install Redis
    • Verify the Installation
    • Configure Redis
      • Persistence Options
        • Basic System Tuning
          • Additional Swap
          • Distributed Redis
          • Set Up Redis Master/Slave Replication
            • Prepare Your Linodes
              • Configure the Master Linode
                • Configure the Slave Linode
                  • Confirm Replication
                  • Secure the Redis Installation
                    • Use Password Authentication
                    • More Information
                    相关产品与服务
                    云数据库 Redis
                    腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
                    领券
                    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档