@sasaki
2016-01-19T12:48:17.000000Z
字数 5586
阅读 3523
Redis
@Title Redis & Sentinel集群环境搭建
@Version v1.0
@Timestamp 2015-12-28 16:26
@Author Nicholas
@Mail redskirt@outlook.com
下载Redis
[root@master usr]# wget http://download.redis.io/releases/redis-3.0.6.tar.gz
[root@master usr]# tar -zxvf application/tmp/redis-3.0.6.tar.gz -C /usr # 解压到usr目录
make编译安装
[root@master usr]# cd redis-3.0.6/
[root@master redis-3.0.6]# make
遇到报错如下:
make[1]: [persist-settings] Error 2 (ignored)
CC adlist.o
/bin/sh: cc: command not found
原因是Redis是用C语言开发的,当前环境中没有gcc,yum安装即可。
[root@master redis-3.0.6]# yum search gcc
[root@master redis-3.0.6]# yum install -y gcc
此时用make命令又遇到如下报错
make[1]: Entering directory `/usr/redis-3.0.6/src'
CC adlist.o
In file included from adlist.c:34:
zmalloc.h:50:31: error: jemalloc/jemalloc.h: No such file or directory
zmalloc.h:55:2: error: #error "Newer version of jemalloc required"
make[1]: *** [adlist.o] Error 1
make[1]: Leaving directory `/usr/redis-3.0.6/src'
make: *** [all] Error 2
在make的时候带上MCLLOC库即可
[root@master redis-3.0.6]# make MALLOC=libc
安装成功之后会在src文件夹内有redis-server和redis-cli两个命令,新建软链接到/usr/local/bin/目录下。
默认/usr/local/bin/已有Redis的启动文件,若没有刚执行以下操作
[root@master src]# ln redis-server /usr/local/bin/
[root@master src]# ln redis-cli /usr/local/bin/
[root@master src]# ls /usr/local/bin/ |grep redis
redis-benchmark
redis-check-aof
redis-check-dump
redis-cli
redis-sentinel
redis-server
单机Redis安装成功。
进行简单的测试
分别启动Redis服务和客户端。
启动redis-server并挂起至后台,注意加上&号
[root@master src]# redis-server &
[1] 8231
[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
8231:M 28 Dec 16:48:42.095 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.0.6 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 8231
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
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.
8231:M 28 Dec 16:48:42.097 # Server started, Redis version 3.0.6
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.
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.
8231:M 28 Dec 16:48:42.098 * The server is now ready to accept connections on port 6379
启动Redis的时候还可以指定配置文件
[root@master redis-3.0.6]# redis-server redis.conf &
查看Redis进程
[root@master src]# ps -ef|grep redis
root 8231 6731 0 16:48 pts/0 00:00:00 redis-server *:6379
root 8574 6731 0 16:53 pts/0 00:00:00 grep redis
[root@master src]# netstat -lntp|grep 6379
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 8231/redis-server *
tcp 0 0 :::6379 :::* LISTEN 8231/redis-server *
启动redis-cli测试
[root@master src]# redis-cli
127.0.0.1:6379>
127.0.0.1:6379> set key "hello redis"
OK
127.0.0.1:6379> get key
"hello redis"
127.0.0.1:6379> exit
使用客户端停止redis
[root@master src]# redis-cli shutdown
8231:M 28 Dec 16:56:39.005 # User requested shutdown...
8231:M 28 Dec 16:56:39.005 * Saving the final RDB snapshot before exiting.
8231:M 28 Dec 16:56:39.038 * DB saved on disk
8231:M 28 Dec 16:56:39.038 # Redis is now ready to exit, bye bye...
[1]+ Done redis-server
[root@master src]# ps -ef|grep redis
root 8906 6731 0 16:56 pts/0 00:00:00 grep redis
Redis Sentinel官方文档:http://redis.io/topics/sentinel
Sentinel是集群监控管理软件,为Redis提供了HA。
从整体上来看Sentinel有以下特性:
配置支持
在Redis 2.8 和3.0版本后官方发布了一个稳定的Redis Sentinel。
在Redis集群中可以通过以下任意一种方式启动Sentinel:
[root@master redis-3.0.6]# redis-sentinel /path/to/sentinel.conf # 直接启动redis-sentinel
[root@master redis-3.0.6]# redis-server /path/to/sentinel.conf --sentinel # 启动redis-server,带上--sentinel模式的参数
我们查看/usr/local/bin下的执行文件属性,发现redis-sentinel其实是redis-server的一个软链接:
[root@master ~]# ll /usr/local/bin/
total 2716
-rwxr-xr-x. 1 root root 236635 Dec 28 16:22 redis-benchmark
-rwxr-xr-x. 1 root root 22201 Dec 28 16:22 redis-check-aof
-rwxr-xr-x. 1 root root 45387 Dec 28 16:22 redis-check-dump
-rwxr-xr-x. 1 root root 347630 Dec 28 16:22 redis-cli
lrwxrwxrwx. 1 root root 12 Dec 28 16:22 redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 2121095 Dec 28 16:22 redis-server
sentinel启动后默认将监听TCP端口26379。
对sentinel.conf进行配置,在redis安装好后该配置文件本身即可做一个最典型的配置。
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 60000
sentinel failover-timeout mymaster 180000
sentinel parallel-syncs mymaster 1
sentinel monitor resque 192.168.1.3 6380 4
sentinel down-after-milliseconds resque 10000
sentinel failover-timeout resque 180000
sentinel parallel-syncs resque 5
// sentinel monitor的参数意义
sentinel monitor <master-group-name> <ip> <port> <quorum>
// 其他监控选项的形式
sentinel <option_name> <master_name> <option_value>
关于Sentinel的部署示例,官网用ASCII图形化的方式来直观描述,各示例及含义如下:
+--------------------+
| This is a computer |
| or VM that fails |
| independently. We |
| call it a "box" |
+--------------------+
# 表示一个运行的盒子,可以是一台电脑或是虚拟机
+-------------------+
| Redis master M1 |
| Redis Sentinel S1 |
+-------------------+
# 表示在一个盒子里边运行的服务
+-------------+ +-------------+
| Sentinel S1 |---------------| Sentinel S2 |
+-------------+ +-------------+
# 表示盒子间的对话
+-------------+ +-------------+
| Sentinel S1 |------ // ------| Sentinel S2 |
+-------------+ +-------------+
# 表示盒子间的网络分区中断
在以上示例中,对节点的命名遵循以下约定:
Master:M1,M2,M3...Mn