[关闭]
@sasaki 2016-01-19T12:48:17.000000Z 字数 5586 阅读 3523

Redis & Sentinel集群环境搭建

Redis


版本控制

  1. @Title Redis & Sentinel集群环境搭建
  2. @Version v1.0
  3. @Timestamp 2015-12-28 16:26
  4. @Author Nicholas
  5. @Mail redskirt@outlook.com

Redis是当下流行的分布式缓存数据库。因项目需要,一直想研究研究,最近Leader多次提到以后开发一定要引入,本着自己对新事物的热爱,把学习过程记下。

一、

二、搭建本地Redis服务

  1. 下载Redis

    1. [root@master usr]# wget http://download.redis.io/releases/redis-3.0.6.tar.gz
    2. [root@master usr]# tar -zxvf application/tmp/redis-3.0.6.tar.gz -C /usr # 解压到usr目录
  2. make编译安装

    1. [root@master usr]# cd redis-3.0.6/
    2. [root@master redis-3.0.6]# make

    遇到报错如下:

    1. make[1]: [persist-settings] Error 2 (ignored)
    2. CC adlist.o
    3. /bin/sh: cc: command not found

    原因是Redis是用C语言开发的,当前环境中没有gcc,yum安装即可。

    1. [root@master redis-3.0.6]# yum search gcc
    2. [root@master redis-3.0.6]# yum install -y gcc

    此时用make命令又遇到如下报错

    1. make[1]: Entering directory `/usr/redis-3.0.6/src'
    2. CC adlist.o
    3. In file included from adlist.c:34:
    4. zmalloc.h:50:31: error: jemalloc/jemalloc.h: No such file or directory
    5. zmalloc.h:55:2: error: #error "Newer version of jemalloc required"
    6. make[1]: *** [adlist.o] Error 1
    7. make[1]: Leaving directory `/usr/redis-3.0.6/src'
    8. make: *** [all] Error 2

    在make的时候带上MCLLOC库即可

    1. [root@master redis-3.0.6]# make MALLOC=libc

    安装成功之后会在src文件夹内有redis-server和redis-cli两个命令,新建软链接到/usr/local/bin/目录下。

    默认/usr/local/bin/已有Redis的启动文件,若没有刚执行以下操作

    1. [root@master src]# ln redis-server /usr/local/bin/
    2. [root@master src]# ln redis-cli /usr/local/bin/
    3. [root@master src]# ls /usr/local/bin/ |grep redis
    4. redis-benchmark
    5. redis-check-aof
    6. redis-check-dump
    7. redis-cli
    8. redis-sentinel
    9. redis-server

    单机Redis安装成功。

  3. 进行简单的测试

    分别启动Redis服务和客户端。

    启动redis-server并挂起至后台,注意加上&号

    1. [root@master src]# redis-server &
    2. [1] 8231
    3. [root@master src]# 8231:C 28 Dec 16:48:42.095 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
    4. 8231:M 28 Dec 16:48:42.095 * Increased maximum number of open files to 10032 (it was originally set to 1024).
    5. _._
    6. _.-``__ ''-._
    7. _.-`` `. `_. ''-._ Redis 3.0.6 (00000000/0) 64 bit
    8. .-`` .-```. ```\/ _.,_ ''-._
    9. ( ' , .-` | `, ) Running in standalone mode
    10. |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
    11. | `-._ `._ / _.-' | PID: 8231
    12. `-._ `-._ `-./ _.-' _.-'
    13. |`-._`-._ `-.__.-' _.-'_.-'|
    14. | `-._`-._ _.-'_.-' | http://redis.io
    15. `-._ `-._`-.__.-'_.-' _.-'
    16. |`-._`-._ `-.__.-' _.-'_.-'|
    17. | `-._`-._ _.-'_.-' |
    18. `-._ `-._`-.__.-'_.-' _.-'
    19. `-._ `-.__.-' _.-'
    20. `-._ _.-'
    21. `-.__.-'
    22. 8231:M 28 Dec 16:48:42.097 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
    23. 8231:M 28 Dec 16:48:42.097 # Server started, Redis version 3.0.6
    24. 8231:M 28 Dec 16:48:42.097 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
    25. 8231:M 28 Dec 16:48:42.098 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
    26. 8231:M 28 Dec 16:48:42.098 * The server is now ready to accept connections on port 6379

    启动Redis的时候还可以指定配置文件

    1. [root@master redis-3.0.6]# redis-server redis.conf &

    查看Redis进程

    1. [root@master src]# ps -ef|grep redis
    2. root 8231 6731 0 16:48 pts/0 00:00:00 redis-server *:6379
    3. root 8574 6731 0 16:53 pts/0 00:00:00 grep redis
    4. [root@master src]# netstat -lntp|grep 6379
    5. tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 8231/redis-server *
    6. tcp 0 0 :::6379 :::* LISTEN 8231/redis-server *

    启动redis-cli测试

    1. [root@master src]# redis-cli
    2. 127.0.0.1:6379>
    3. 127.0.0.1:6379> set key "hello redis"
    4. OK
    5. 127.0.0.1:6379> get key
    6. "hello redis"
    7. 127.0.0.1:6379> exit

    使用客户端停止redis

    1. [root@master src]# redis-cli shutdown
    2. 8231:M 28 Dec 16:56:39.005 # User requested shutdown...
    3. 8231:M 28 Dec 16:56:39.005 * Saving the final RDB snapshot before exiting.
    4. 8231:M 28 Dec 16:56:39.038 * DB saved on disk
    5. 8231:M 28 Dec 16:56:39.038 # Redis is now ready to exit, bye bye...
    6. [1]+ Done redis-server
    7. [root@master src]# ps -ef|grep redis
    8. root 8906 6731 0 16:56 pts/0 00:00:00 grep redis

三、Redis Sentinel

Redis Sentinel官方文档:http://redis.io/topics/sentinel
Sentinel是集群监控管理软件,为Redis提供了HA。

从整体上来看Sentinel有以下特性:

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注