Redis 3.0 cluster cluster environment construction

I recently learned to use redis, installed a centos-7 virtual machine, referenced many blog posts on the Internet, and then cobbled together a little stuff to share with you.

Install redis 3.0

1. First download redis 3.0

  1. ·wget http://download.redis.io/releases/redis-3.0.0.tar.gz
  2. tar zxvf redis-3.0.0.tar.gz
  3. cd redis-3.0.0

2. Compile

 

  1. make

After the make command is executed and compiled, six executable files will be generated in the src directory, namely redis-server, redis-cli, redis-benchmark, redis-check-aof, redis-check-dump, and redis-sentinel. 

 

At this point, you can actually start redis in the src directory: 
3. Start the redis service in the background

  1. ./redis-server &

If the configuration is not modified, you will see the following status: 
write picture description here 
Then start the redis client:

  1. ./redis-cli

write picture description here 
The above interface appears, indicating that you have successfully entered redis and can be tested: 
write picture description here


The above is that we run redis after compiling, but usually, we install redis after compiling, so that we can use redis commands in all paths:

  1. make install

After installation, the executable file compiled by make will be copied to the /usr/local/bin directory, so that redis-server can be executed anywhere to start the redis service. 
Note: 
▲The default port number of redis is 6379, (according to the blog post of antirez, the author of redis, 6379 is the number corresponding to MERZ on the mobile phone button, and MERZ is taken from the name of Italian singer Alessia Merz. MERZ has long been used by antirez and Its friend is regarded as a synonym for stupidity.) 
▲Redis has two storage methods, the default is the snapshot method, the implementation method is to periodically persist the memory snapshot (snapshot) to the hard disk, the disadvantage of this method is that if a crash occurs after persistence A piece of data will be lost. Therefore, under the impetus of perfectionists, the author added the aof method. aof is the append only mode, which saves the operation command to the log file while writing the memory data.

Running Redis as a background daemon 
needs to start by reading the configuration file. 
Note that the default daemonize parameter of the redis.conf file copied in the past is no, so redis will not run in the background. We can modify the redis.conf file, which is the decompressed file. under the redis root directory

  1. daemonize yes
  2. redis-server /usr/redis-3.0.0/redis.conf

View the redis process

  1. ps to | grep redis

redis cluster cluster construction

Establish a local test environment 
(at least 3 master nodes are required for the cluster to work properly, here we need to create 6 redis nodes, three of which are master nodes, three are slave nodes, and the ip and port of the corresponding redis nodes correspond to The relationship is as follows)

  1. 127.0.0.1:7000
  2. 127.0.0.1:7001
  3. 127.0.0.1:7002
  4. 127.0.0.1:7003
  5. 127.0.0.1:7004
  6. 127.0.0.1:7005

Create the directory required by the cluster

  1. mkdir -p /usr/local/cluster
  2. cd /usr/local/cluster
  3. mkdir 7000 7001 7002 7003 7004 7005

Modify the configuration file redis.conf

  1. cp /usr/local/redis3.0.0/redis.conf /usr/local/cluster
  2. vi redis.conf

Modify the following options in the configuration file

  1. port 7000
  2. daemonize yes
  3. cluster-enabled yes
  4. cluster-config-file nodes.conf
  5. cluster-node-timeout 5000
  6. appendonly yes

After modifying the configuration items in the redis.conf configuration file, copy the configuration files to the 7000/ 7001/ 7002/ 7003/ 7004/ 7005 directories respectively

  1. cp /usr/local/cluster/redis.conf /usr/local/cluster/7000
  2. cp /usr/local/cluster/redis.conf /usr/local/cluster/7001
  3. cp /usr/local/cluster/redis.conf /usr/local/cluster/7002
  4. cp /usr/local/cluster/redis.conf /usr/local/cluster/7003
  5. cp /usr/local/cluster/redis.conf /usr/local/cluster/7004
  6. cp /usr/local/cluster/redis.conf /usr/local/cluster/7005

Note : After the copy is completed, you need to modify the port parameter in the redis.conf file under the 7001/7002/7003/7004/7005 directory, and change it to the name of the corresponding folder. 
Start these 6 redis instances respectively

  1. cd /usr/local/cluster/7000
  2. redis-server redis.conf
  3. cd /usr/local/cluster/7001
  4. redis-server redis.conf
  5. cd /usr/local/cluster/7002
  6. redis-server redis.conf
  7. cd /usr/local/cluster/7003
  8. redis-server redis.conf
  9. cd /usr/local/cluster/7004
  10. redis-server redis.conf
  11. cd /usr/local/cluster/7005
  12. redis-server redis.conf

After startup, use the command ps aux|grep redis to check the startup status of redis. If the following result appears, the startup is successful (note: you must enter each directory, and then execute redis-server redis.conf). 
write picture description here

Execute the redis create cluster command to create a cluster

  1. cd /usr/local/redis3.0/src
  2. ./redis-trib.rb create --replicas 1 127.0.0.1:7000 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

执行上面的命令的时候会报错,因为是执行的ruby的脚本,需要ruby的环境。错误内容:/usr/bin/env: ruby: No such file or directory 
所以需要安装ruby的环境,这里推荐使用yum install ruby安装

  1. yum install ruby

然后再执行刚才的创建集群命令,还会报错,提示缺少rubygems组件,使用yum安装 
错误内容: 
./redis-trib.rb:24:in `require’: no such file to load – rubygems (LoadError) 
from ./redis-trib.rb:24

  1. yum install rubygems

再次执行第6步的命令,还会报错,提示不能加载redis,是因为缺少redis和ruby的接口,使用gem 安装 
错误内容:

/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in 
gem_original_require': no such file to load -- redis (LoadError) from 
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
require’ 
from ./redis-trib.rb:25

  1. gem install redis

出现错误:

ERROR: Could not find a valid gem ‘redis’ (>= 0), here is why: 
Unable to download data from https://rubygems.org/ - Errno::ECONNRESET: Connection reset by peer - SSL_connect 
(https://rubygems.org/latest_specs.4.8.gz)

解决方案: 
可以求助万能的淘宝镜像站 https://ruby.taobao.org/

  1. gem sources -a https://ruby.taobao.org/
  2. #然后在执行:
  3. gem install redis

再次执行刚才的创建集群的命令,正常执行。 
write picture description here 
至此redis集群即搭建成功!

使用redis-cli命令进入集群环境

  1. redis-cli -c -h 127.0.0.1 -p 7000
  2. #用 cluster nodes 可以查看节点信息,

redis-trib默认用前3个实例作为Master,后3个作为Slave。 

集群重启: 
目前redis-trib的功能还比较弱,需要重启集群的话先手动kill掉各个进程,删除掉所有redis instance的aof、node-config、rdb文件(这样,之前的数据会丢失),然后重新启动就可以了,如果不删除原实例的文件,重启的时候会报错。

  1. ps -ef | grep redis | awk '{print $2}' | xargs kill

故障转移 
在高可用性方面,Redis可算是能够”Auto”一把了!Redis Cluster重用了Sentinel的代码逻辑,不需要单独启动一个Sentinel集群,Redis Cluster本身就能自动进行Master选举和Failover切换。

执行:set name “zhangsan”,发现name被存到了 7000 节点上了。 
下面我们故意kill掉7000结点,之后可以看到结点状态变成了fail,而Slave 7003被选举为新的Master。 
尝试查询之前保存在7000上的Key “name”,可以看到7003顶替上来继续提供服务,整个集群没有受到影响。 
性能检测: 
redis 自己提供了一个性能测试工具redis-benchmark. redis-benchmark可以模拟N个机器,同时发送M个请求。 
用法:

  1. redis-benchmark [-h ] [-p ] [-c ] [-n <requests]> [-k ]
  2. -h <hostname> Server hostname (default 127.0.0.1)
  3. -p <port> Server port (default 6379)
  4. -s <socket> Server socket (overrides host and port)
  5. -c <clients> Number of parallel connections (default 50) 并发客户端数
  6. -n <requests> Total number of requests (default 10000) 请求数量
  7. -d <size> Data size of SET/GET value in bytes (default 2) set 数据大小
  8. -k <boolean> 1=keep alive 0=reconnect (default 1) 是否采用keep alive模式
  9. -r <keyspacelen> Use random keys for SET/GET/INCR, random values for SADD
  10. Using this option the benchmark will get/set keys
  11. in the form mykey_rand:000000012456 instead of constant
  12. keys, the <keyspacelen> argument determines the max
  13. number of values for the random number. For instance
  14. if set to 10 only rand:000000000000 - rand:000000000009
  15. range will be allowed.
  16. -P <numreq> Pipeline <numreq> requests. Default 1 (no pipeline). 是否采用Pipeline模式请求,默认不采用
  17. -q Quiet. Just show query/sec values 仅仅显示查询时间
  18. --csv Output in CSV format 导出为CSV格式
  19. -l Loop. Run the tests forever 循环测试
  20. -t <tests> Only run the comma separated list of tests. The test
  21. names are the same as the ones produced as output.
  22. -I Idle mode. Just open N idle connections and wait

After testing, my notebook can handle more than 30,000 requests per second, and the performance is not bad. 

write picture description here

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326572025&siteId=291194637