Centos Redis cluster installation and deployment tutorial, centosredis

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

Centos Redis cluster installation and deployment tutorial, centosredis
I. Application scenarios

This article describes how to install a Redis cluster in a Linux environment, including how to install it in a networked Linux environment and an offline Linux environment. Most of the time, the company's production environment is in the Intranet environment, without the Internet, the server is offline (recently the company wants to launch a project, that is, the Linux without the Internet environment, offline installation is terrible, and many detours have taken place. Many of them are full of tears. % >_<% ). This is also the author's original intention to write this article. I hope that others will not take a detour. Next I will introduce how to install and deploy Redis clusters in Linux.

Ii. installation environment and tools

System: Red Hat Enterprise Linux Server release 6.6.

Tools: XShell5 and Xftp5

Package: GCC-7.1.0

Ruby-1, 2.4.1

Rubygems-2.6.12

Redis-3.2.9 (version 3.x starts to support cluster functionality)

Iii. Installation Steps

To build a simple Redis cluster, we need at least six nodes: three Master nodes and three Slave nodes. So why do we need three masters? It is actually an "Tie triangle" relationship. When one Master is offline, the other two masters and the corresponding Salve can be replaced immediately to ensure that the cluster can be used normally, if you know about Mongodb, Hadoop, and Strom before, you can easily target the base node with the lowest distributed requirements, which facilitates Election (the minority follows the majority principle ). In this article, we will be lazy and build a six-node Redis cluster on a Linux Virtual Machine (in the actual production environment, we need three Linux servers to store three masters in distribution)

1. Install the GCC Environment

To install Redis, you must rely on the GCC environment. First, check whether GCC is installed in Linux. If not, install it.

Check whether GCC is installed. Check the version number.

$ gcc -v

If GCC is already installed, the following information is displayed:

If there is no information, run the yum install gcc-c ++ command to install it online.

$ yum install gcc-c++

If there is no network, we need to download the GCC installation package for manual installation. The specific method is still complicated. The specific method for installing GCC offline is as follows, please refer to my other article "Linux offline installation of GCC without network"

2. Install Ruby and Rubygems

If there is a network, install it using the yum command, and automatically install all the associated dependency packages.

$ yum install ruby$ yum install rubygems

If it is offline, we can download Ruby and Rubygems, decompress the package, and install it manually, for more information, see my other two documents, "Linux offline Ruby Installation Details" and "Linux offline Rubygems Installation Details.

4. Install Redis

1, to the official website (https://redis.io/download) to download Redis, now the latest version: 3.2.9, will download the compressed package to the server ., I created a temporary Redis directory for storage. When I was lazy, I used xftp5 to manually create a directory for storage (or I could write a command to create a folder $ makdir redis)

2. Install Redis

Go to the Redis storage directory, and decompress the Redis compressed package through commands

$ cd /home/cmfchina/redis$ tar -zxvf redis-3.2.9.tar.gz

Install Redis using the make command (root permission required)

$ Cd/home/cmfchina/redis/redis-3.2.9 $ make & make install // make if PREFIX is not specified here, it will be installed under/usr/local/bin by default, keep default

Installation fails if you do not have the root permission,

After obtaining the root permission, we can install Redis again. The following information indicates that Redis is successfully installed. You can also check it in the/usr/local/bin directory.

If you only want to run a single machine without the cluster function, you can run Redis now. You can run commands directly under the unzipped Redis directory to run the single-host Redis.

$ Cd/home/cmfchina/redis/redis-3.2.9 $ redis-server redis. conf // all related configuration information is in the conf, if not set, the default port number is: 6379

5. Configure the Redis Cluster

As mentioned above, if you just want to run the single-host Redis (you can install the single-host Redis for personal research), the above explanation is enough. However, in reality, we often need to use the cluster function, fault Tolerance.

As mentioned above, we need Redis with six nodes as the cluster, so we need to create six folders to store the configuration information of six nodes respectively. The six nodes need to correspond to six port numbers, for example, 7001 ~ 7006. we define this port number. We can create it through xftp5 visualization.

Step 1: You can also create multiple instances by running the mkdir command. The command may be faster.

Next we will focus on six nodes. We need to configure their own redis. conf configuration files. Go to our Redis installation directory usr/local/bin, and set redis-cli, redis-server, and redis. conf (if there is no conf file, you can copy it from the compressed package, or directly create an empty conf file, and then configure the relevant information ), copy them to the Six folders you just created.

Step 2. Next, we need to configure redis. conf file. If you copy the file from the compressed package, you will find a lot of notes. These are the notes on the official website. You can delete them all, only configure the information you want to configure. We mainly configure the corresponding port information and cluster configuration information

There are many other redis instances. conf configuration information, which can be configured in actual scenarios. for more information about the conf file, see the author's article Redis. conf and its Sentinel. conf configuration item details. We configure the corresponding six redis. conf information respectively.

It is a little complicated to start these six redis services (command redis-server redis. conf) one by one. Creating an sh script in the redis directory to start six instances

1 $ cd/home/cmfchina/redis2 $ vim startall. sh open the vim editor and create an empty text

: Wq! Save the script and create it successfully:

Run./startall. sh and the prompt "permission denied" indicates that the permission is insufficient. Run the command chmod 777 startall. sh to modify the permission and obtain the root user to execute the script.

$ Chmod 777 startall. sh assign permissions $ sh-x startall. sh execute the script

======= Supplementary Note: ==========

Some netizens reacted with the blogger. The above batch script cannot be executed, but the blogger missed the command: kill-2

Because: every time you execute a redis startup, it will stay on the redis startup interface (the Single-host startup interface mentioned above), so we need to simulate exiting the current interface and execute the next command

Kill-2: similar to Ctrl + C, the program can save relevant data before it ends, and then exit.

Modify the above script;

Ps: If the path cannot be found, replace all the above paths with absolute paths.

======================================

Run again. /startall. sh, and then run netstat-tnulp | grep redis and ps aux | grep redis to check the redis running status, we can see that the redis ports 7001, 7002, 7003, 7004, 7005 are all up. Baiye ~~~~

Step 3. In fact, you can see that the operations on the Redis cluster are completed through the Ruby script later. Therefore, we need to install the Ruby-related RPM package, and interfaces for Redis and Ruby. We need to use the previously installed Ruby. We found the file: redis-3.2.9, under the decompressed file redis-trib.rb/src directory,

Copy the file to the same directory as the Six folders.

Run the following command in the redis directory:

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

=================== Related error summary solution (you think the above is the focus % >_<%, in fact, the following is the focus of this article (too many pitfalls )!!!) ====================

If the preceding commands cause Ruby and Rubygems errors, Ruby and Rubygems are not installed. That is why we need to install Ruby and Rubygems in advance before the article. But some people say that we have already installed these two instances. Why are the following errors reported?

/home/cmfchina/ruby/lib/ruby/site_ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- redis (LoadError)from /home/cmfchina/ruby/lib/ruby/site_ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'from ./redis-trib.rb:25:in `
 
  '
 

This error indicates that redis cannot be loaded. It is because the interfaces of redis and ruby are missing. We need to install the corresponding Rbuy interface package of Redis by using gem. We need to download and install the corresponding Redis gem package. Rubygems official website actually provides Redis gem package, we can directly download the download https://rubygems.org/gems/redis/ after uploading to the server

Run the gem install redis-3.3.0.gem command to install.

$ gem install redis-3.3.0.gem

However, an error is reported when you execute this command. If no error is reported, it indicates that the character is good ~~~, This is because zlib is required.

ERROR:  Loading command: install (LoadError)    cannot load such file -- zlibERROR:  While executing gem ... (NoMethodError)    undefined method `invoke_with_build_args' for nil:NilClass

Step 4, we need to install zlib again, download zlib, upload unzip, install zlib Official Website: http://www.zlib.net, the latest version 1.2.11, install us on a pen

1 $ tar-xvzf zlib-1.2.11.tar.gz2 $ cd zlib-1.2.8.tar.gz3 $./configure -- prefix =/usr/local/zlib set installation path 4 $ make5 $ make instal

After zlib is installed, run the following command:

1 $ cd/home/cmfchina/ruby-2.4.1/ext/zlib remarks:/home/cmfchina/ruby-2.4.1 is the directory decompressed after the ruby installation package, this is the ruby offline installation 2 $ ruby extconf mentioned above. rb 3 $ make & make install

However, an error is reported, which means you can't speak out ~~~ The error message is as follows:

checking for deflateReset() in -lz... nochecking for deflateReset() in -llibz... nochecking for deflateReset() in -lzlib1... nochecking for deflateReset() in -lzlib... nochecking for deflateReset() in -lzdll... nochecking for deflateReset() in -lzlibwapi... no*** extconf.rb failed ***Could not create Makefile due to some reason, probably lack of necessarylibraries and/or headers.  Check the mkmf.log file for more details.  You mayneed configuration options.

In line with the principle of not giving up, I can only go to a foreign website to check the information to see how to solve the problem. I found that the file had to be installed in the local Runtime Library, additional configuration information is required during installation, provided that zlib is installed to continue the next step.

Install zlib and run the following command again:

1 $ cd/home/cmfchina/ruby-2.4.1/ext/zlib remarks:/home/cmfchina/ruby-2.4.1 is the directory decompressed after the ruby installation package, this is the ruby offline installation 2 $ ruby extconf mentioned above. rb -- with-zlib-include =/usr/local/zlib/include/-- with-zlib-lib =/usr/local/zlib/lib // generate a Makefile file remarks: /usr/local/zlib is my zlib installation directory
3 $ make && make install  

A Makefile is automatically generated at this time,

Next, let's install make & make install. But when we make, the following error occurs:

make: *** No rule to make target `/include/ruby.h', needed by `zlib.o'.  Stop

In this case, open the ext/zlib/Makefile file and find the following line to modify the path.

Zlib. o: $ (top_srcdir)/include/ruby. h: zlib. o:.../../include/ruby. h

After the modification is completed, save the modification. Next, make & make install. At this time, the installation is successful ~~ So proceed with peace of mind! (I can't do anything if I don't see it.) After the installation is complete, the following information is displayed:

Let's go back to the redis gem directory and continue executing the command: gem install redis-3.3.0.gem

However, an error occurs again. We still need to install OpenSSL because OpenSSL is required for Redis cluster interaction.

Step 5: We have to install OpenSSL again. The official website address is https://www.openssl.org/source /.

1 $ tar -xzvf openssl-1.0.2l.tar.gz2 $ cd openssl-1.0.2l3 $ ./config -fPIC --prefix=/usr/local/openssl enable-shared4 $ ./config -t5 $ make && make install

The interface for successful openssl installation is as follows:

We need to go to the ext/openssl directory under the [/home/cmfchina/Ruby-2.4.1] Directory of the ruby decompressed source code,

Install openssl in the same way as zlib

1 $ cd/home/cmfchina/ruby-2.4.1/ext/openssl2 remarks:/home/cmfchina/ruby-2.4.1 is the directory decompressed after the ruby installation package, it is the ruby offline installation 3 $ ruby extconf mentioned above. rb -- with-openssl-include =/usr/local/openssl/include/-- with-openssl-lib =/usr/local/openssl/lib // A Makefile file is generated 4 note: /usr/local/openssl is my openssl installation directory 5 $ make & make install

However, when we make, we encountered another error similar to zlib.

make: *** No rule to make target `/include/ruby.h', needed by `ossl.o'.  Stop

Just like zlib, open the Makefile file and change all $ (top_srcdir) ../..

Save the modification and execute make & make install. The installation is successful this time ~~

Then we go back to the previous redis directory and execute the command: gem install redis-3.3.0.gem

Step 6. Start the Redis Cluster

After completing the preceding steps, we will go back to Step 3 to execute the command.

Run the following command in the redis directory:

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

We select yes, which means to follow this master-slave distribution method. We can also specify the slave through the configuration file.

6. Redis cluster Test

Let's test the Redis cluster and add data by connecting to any redis port.

[root@localhost redis7001]# redis-cli -p 7001 -c   
[Root @ localhost redis7001] # redis-cli-c-h 127.0.0.1-p 7001 shutdown // disable the cluster. If no-h parameter is available, the cluster is connected to 127.0.0.1 by default. If no-p parameter is available, port 6370 is connected by default (if the default port is used, no-h-p is available)

Note:-h + host-p + port-c is used to connect to the cluster. If it is not added, an error is returned.

We can see that the node is connected to 7001. When set name is used, it calculates the hash slot that exists and jumps to the corresponding node of the slot.

Conclusion: At this point, the past and present of Redis cluster configuration have ended, and many pitfalls have been encountered in the middle of the network environment. Now we can enjoy Redis, even if I am abused for thousands of times, I will treat it as my first love.

Post: recommend a Redis visualization tool: redis?topmanager official website (https://redisdesktop.com/download), specific can go to the official website to see, here I only give a look, the next article mainly introduces Redis Sentinel (Sentinel) Mode

Related Article

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.