Install redis and redis clusters in CentOS

Source: Internet
Author: User
Tags download redis redis cluster install redis

Install redis and redis clusters in CentOS
Redis Introduction

Redis is an open-source Log Type written in ANSIC language that supports Network, memory-based, and persistence,A high-performance Key-Value database that provides APIs in multiple languages.

Installation Steps

1. Install gcc.

yum install gcc-c++

2. Download redis source package,: http://download.redis.io/releases/redis-3.0.0.tar.gz 3. The source package to the Linux server. 4. decompress the source code package

tar -zxvf redis-3.0.0.tar.gz

5. Go to the redis directory and use the make command

cd redis-3.0.0/make

6. Use make install to specify a directory to install redis

# Specify the installation directory as/usr/local/redismake install PREFIX =/usr/local/redis
Start the redis Server

1. frontend Startup Mode

# The default is the frontend startup mode. The port is 6379/usr/local/redis/bin/redis-server.

2. Start the backend. 1) Copy redis. conf from the redis source code directory to the redis installation directory.

cp redis.conf /usr/local/redis/bin/

2) modify the configuration file redis. conf.

Change "daemonize no" to "daemonize yes ".

3) Start the backend Based on the configuration file

./redis-server redis.conf 
Common redis commands

Start the redis Client

[root@VM_161_110_centos bin]# ./redis-cli   127.0.0.1:6379> pingPONG127.0.0.1:6379> set a 1OK127.0.0.1:6379> get a"1"127.0.0.1:6379> keys *1) "a"127.0.0.1:6379> incr a(integer) 2127.0.0.1:6379> decr a(integer) 1127.0.0.1:6379> del a(integer) 1127.0.0.1:6379> keys *(empty list or set)
Common redis Data Types
  1. String (main)
  2. Hash (main)
  3. List
  4. Set
  5. SortedSet
Redis cluster construction redis-cluster architecture Diagram

  • Redis-cluster maps all physical nodes to the [0-16383] slot, and the cluster maintains the node <-> slot (slot) <-> value (value ).
  • The redis cluster has 16384 (2 ^ 14 bits) built-in hash slots. When you need to place a key-value in the Redis cluster, redis first uses the crc16 algorithm to calculate a result for the key, and then calculates the remainder of the result for 16384, so that each key will correspond to a hash slot numbered between 0 and 16383, redis maps hash slots to different nodes based on roughly equal number of nodes.
Redis-cluster voting: Fault Tolerance

  1. The voting process is attended by all the master nodes in the cluster. If more than half of the master nodes communicate with the master node (cluster-node-timeout), The current master node is considered to have crashed.
  2. When is the entire cluster unavailable (cluster_state:fail)?
    1. If any master of the cluster fails and the current master does not have a Server Load balancer instance (Backup node), the cluster enters the fail status, it can also be understood as the fail status when the slot ing of the Cluster [0-16383] is not completed.Ps: redis-3.0.0.rc1 Additioncluster-require-full-coverageParameter. disabled by default. Failed to enable cluster compatibility.
    2. If more than half of the Cluster's master nodes are down, whether or not the slave cluster is in the fail status.Ps: When the cluster is unavailable, all operations on the cluster are unavailable ((error) CLUSTERDOWN The cluster is down) Error.
Cluster node Planning

In a cluster with three nodes, each node has one master node and one slave node. In theory, six servers are required (no money is needed ). Build a pseudo-distributed cluster and use six redis instances for simulation.

Build a ruby Environment
  • To build a cluster, you must use the official ruby script and install the ruby environment.
Yum install rubyyum install rubygems # install ruby Package Manager
  • Install the gem package required by ruby: http://download.csdn.net/detail/xuda27/9668339
gem install redis-3.0.0.gem 
Cluster Construction

Step 1: Create 6 apsaradb for redis instances with a port number ranging from 7001 ~ 7006 1. In/usr/local/Createredis-clusterDirectory and/usr/local/redisCopy the redis folder to the redis-cluster folder, rename the folder redis01, and create redis02, redis03 ,...... Redis06. 2. Copy the ruby script for cluster creationredis-clusterDirectory, that is, under the redis source code package (src directory)redis-trib.rbCopyredis-clusterDirectory.

#1. copy all the files in the redis directory to the redis-cluster directory and rename the folder redis01 cp-r redis/usr/local/redis-cluster/redis01 #2. copy the redis-trib.rb to the cp redis-trib.rb/usr/local/redis-cluster directory in redis-cluster

Step 2: Modify the redis. conf configuration file in the bin directory of the six redis instances. 1. Modify the port number.The redis01 port is modified, and the remaining five redis instances have ports ranging from 7002 to 7006. 2. Open the comment before cluster-enable.Tell redis to use the cluster.Step 3: Start six redis instances. Inredis-clusterCreate under directorystartup.shIt is used to start six redis instances andstartup.shGrant the execution permission.

# Create startup. sh [root @ vm_16109110_centos redis-cluster] # vim startup. sh cd redis01/bin. /redis-server redis. confcd .. cd .. cd redis02/bin. /redis-server redis. confcd .. cd .. cd redis03/bin. /redis-server redis. confcd .. cd .. cd redis04/bin. /redis-server redis. confcd .. cd .. cd redis05/bin. /redis-server redis. confcd .. cd .. cd redis06/bin. /redis-server redis. confcd .. cd .. # grant the execution permission chmod + x startup. sh # Start 6 redis instances. /startup. sh # view redis process running: ps aux | grep redis

redis-clusterThe Created directory structure is as follows:

Step 5: Create a cluster. You need to know the IP address of the Linux server.ifconfigCommand to view.

./redis-trib.rb create --replicas 1 10.104.161.110:7001 10.104.161.110:7002 10.104.161.110:7003 10.104.161.110:7004 10.104.161.110:7005 10.104.161.110:7006

Cluster creation Information:

# The master node has a slot, and the slave node does not have a slot> sort Ming hash slots allocation on 6 nodes... using 3 masters: 10.104.161.110: Weight: 7003 Adding replica 10.104.161.110: 7004 to weight: 7001 Adding replica failed: 7005 to weight: 7002 Adding replica 10.104.161.110: 7006 to 10.104.161.110: 7003 M: 2017a1d2c7a02cd38e9f61138975863fc8c20d0ea 10.104.161.110: 7001 slots: 0-5460 (5461 slots) mas TerM: Lost 10.104.161.110: 7002 slots: 5461-10922 (5462 slots) masterM: Lost 10.104.161.110: 7003 slots: 10923-16383 (5461 slots) masterS: Lost 10.104.161.110: 7004 replicates lost: e2439d0da-c7abbf437b76b50ebe1db6066f51ef 10.104.161.110: 7005 replicates e2a9a2 8c3842d37aa602182110f93598c2936da5S: 1127ebb21380ebb23c45d3c5a4c79c92840470df 10.104.161.110: 7006 replicates when I set the above configuration? (Type 'yes' to accept ): yes >>>> Nodes configuration updated >>> Assign a different config epoch to each node >>>> Sending cluster meet messages to join the clusterWaiting for the cluster to join ......>> grouping Cluster Check (using node 10.104.161.110: 7001) M: Drawing 10.104.161.110: 7001 slots: 0-5460 (5461 slots) masterM: Drawing 10.104.161.110: 7002 slots: 5461-10922 (5462 slots) masterM: Drawing 10.104.161.110: 7003 slots: 10923-16383 (5461 slots) masterM: Drawing 10.104.161.110: 7004 slots: (0 slots) master replicates segment: Drawing 10.104.161.110: 7005 slots: (0 slots) master replicates partition: ipv10.104.161.110: 7006 slots: (0 slots) master replicates partition [OK] All nodes agree about slots configuration. >>> check for open slots... >>> check slots coverage... [OK] All 16384 slots covered.
Test Cluster

Redis-cli is available in the bin from redis01 to redis06. You can choose one of them, for example:

#-H indicates Host IP address-p indicates port (7001-7006)-c indicates cluster redis01/bin/redis-cli-h 10.104.161.110-p 7002-c

Test blind play:

[root@VM_161_110_centos redis-cluster]# redis01/bin/redis-cli -h 10.104.161.110 -p 7001 -c10.104.161.110:7001> set b 99OK10.104.161.110:7001> get b"99"10.104.161.110:7001> get a-> Redirected to slot [15495] located at 10.104.161.110:7003"100"10.104.161.110:7003> pingPONG

Note: If you set up redis on a remote server, remember to enable ports 7001 to 7006 and 6379, so you can use redis later.

# Open port 7001 iptables-a input-ptcp -- dport 7001-j ACCEPT

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.