说下哨兵模式监控的原理
每个Sentinel以 每秒钟 一次的频率,向它所有的 主服务器、从服务器 以及其他Sentinel实例 发送一个PING 命令。
如果一个 实例(instance)距离最后一次有效回复 PING命令的时间超过 down-after-milliseconds 所指定的值,那么这个实例会被 Sentinel标记为 主观下线。
如果一个 主服务器 被标记为 主观下线,那么正在 监视 这个 主服务器 的所有 Sentinel 节点,要以 每秒一次 的频率确认 该主服务器是否的确进入了 主观下线 状态。
如果一个 主服务器 被标记为 主观下线,并且有 足够数量 的 Sentinel(至少要达到配置文件指定的数量)在指定的 时间范围 内同意这一判断,那么这个该主服务器被标记为 客观下线。
在一般情况下, 每个 Sentinel 会以每 10秒一次的频率,向它已知的所有 主服务器 和 从服务器 发送 INFO 命令。
当一个 主服务器 被 Sentinel标记为 客观下线 时,Sentinel 向 下线主服务器 的所有 从服务器 发送 INFO 命令的频率,会从10秒一次改为 每秒一次。
Sentinel和其他 Sentinel 协商 主节点 的状态,如果 主节点处于 SDOWN`状态,则投票自动选出新的主节点。将剩余的 从节点 指向 新的主节点 进行 数据复制。
当没有足够数量的 Sentinel 同意 主服务器 下线时, 主服务器 的 客观下线状态 就会被移除。当 主服务器 重新向 Sentinel的PING命令返回 有效回复 时,主服务器 的 主观下线状态 就会被移除。
哨兵模式的优缺点
优点:
- 哨兵模式是基于主从模式的,所有主从的优点,哨兵模式都具有。
- 主从可以自动切换,系统更健壮,可用性更高。
- Sentinel 会不断的检查 主服务器 和 从服务器 是否正常运行。当被监控的某个 Redis 服务器出现问题,Sentinel 通过API脚本向管理员或者其他的应用程序发送通知。
缺点:
- Redis较难支持在线扩容,对于集群,容量达到上限时在线扩容会变得很复杂。
部署配置
1.下载Redis-tar.gz 压缩包
解压 :
tar zxf redis-3.2.8.tar.gz
解压以后 需要编译,切到redis解压目录下 ,(ll 是查看当前目录)
cd redis-3.2.8
编译命令是make
1. [root@bogon redis-3.2.8]# make
2. cd src && make all
3. make[1]: 进入目录“/usr/local/redis-3.2.8/src”
4. rm -rf redis-server redis-sentinel redis-cli redis-benchmark redis-check-rdb redis-check-aof *.o *.gcda *.gcno *.gcov redis.info lcov-html
5. (cd ../deps && make distclean)
6. make[2]: 进入目录“/usr/local/redis-3.2.8/deps”
编译过程中出现报错
1. make[3]: 进入目录“/usr/local/redis-3.2.8/deps/hiredis”
2. gcc -std=c99 -pedantic -c -O3 -fPIC -Wall -W -Wstrict-prototypes -Wwrite-strings -g -ggdb net.c
3. make[3]: gcc:命令未找到
4. make[3]: *** [net.o] 错误 127
5. make[3]: 离开目录“/usr/local/redis-3.2.8/deps/hiredis”
6. make[2]: *** [hiredis] 错误 2
7. make[2]: 离开目录“/usr/local/redis-3.2.8/deps”
8. make[1]: [persist-settings] 错误 2 (忽略)
9. CC adlist.o
10. /bin/sh: cc: 未找到命令
11. make[1]: *** [adlist.o] 错误 127
12. make[1]: 离开目录“/usr/local/redis-3.2.8/src”
13. make: *** [all] 错误 2
提示gcc命令未找到,这是因为redis没有安装gcc编译器没安装这时候只要安装编译器即可
如果gcc编译器安装过程中报错,则网络配置的有问题,
注意设置dns 重启网卡即可。
安装 gcc
yum install -y gcc g++ gcc-c++ make
1. [root@bogon redis-3.2.8]# yum install -y gcc g++ gcc-c++ make 安装完成提示
2. 已安装:
3. gcc.x86_64 0:4.8.5-11.el7 gcc-c++.x86_64 0:4.8.5-11.el7
4.
5. 作为依赖被安装:
6. cpp.x86_64 0:4.8.5-11.el7 glibc-devel.x86_64 0:2.17-157.el7_3.1 glibc-headers.x86_64 0:2.17-157.el7_3.1
7. kernel-headers.x86_64 0:3.10.0-514.6.2.el7 libmpc.x86_64 0:1.0.1-3.el7 libstdc++-devel.x86_64 0:4.8.5-11.el7
8.
9. 更新完毕:
10. make.x86_64 1:3.82-23.el7
11.
12. 作为依赖被升级:
13. glibc.x86_64 0:2.17-157.el7_3.1 glibc-common.x86_64 0:2.17-157.el7_3.1 libgcc.x86_64 0:4.8.5-11.el7 libgomp.x86_64 0:4.8.5-11.el7
14. libstdc++.x86_64 0:4.8.5-11.el7
15.
16. 完毕!
编译器安装完成之后再redis-3.2.8目录下执行make命令
再执行make
- [root@bogon redis-3.2.8]# make
- cd src && make all
- make[1]: 进入目录“/usr/local/redis-3.2.8/src”
- CC adlist.o
- In file included from adlist.c:34:0:
- zmalloc.h:50:31: 致命错误:jemalloc/jemalloc.h:没有那个文件或目录
- #include jemalloc/jemalloc.h>
- ^
- 编译中断。
- make[1]: *** [adlist.o] 错误 1
- make[1]: 离开目录“/usr/local/redis-3.2.8/src”
- make: *** [all] 错误 2
又出现错误,上网查了之后说是
- 原因分析
- 在README 有这个一段话。
- Allocator
- ---------
- Selecting a non-default memory allocator when building Redis is done by setting
- the `MALLOC` environment variable. Redis is compiled and linked against libc
- malloc by default, with the exception of jemalloc being the default on Linux
- systems. This default was picked because jemalloc has proven to have fewer
- fragmentation problems than libc malloc.
- To force compiling against libc malloc, use:
- % make MALLOC=libc
- To compile against jemalloc on Mac OS X systems, use:
- % make MALLOC=jemalloc
- 说关于分配器allocator, 如果有MALLOC 这个 环境变量, 会有用这个环境变量的 去建立Redis。
- 而且libc 并不是默认的 分配器, 默认的是 jemalloc, 因为 jemalloc 被证明 有更少的 fragmentation problems 比libc。
- 但是如果你又没有jemalloc 而只有 libc 当然 make 出错。 所以加这么一个参数。
解决办法之一就是修改默认分配器(我使用的办法)
make MALLOC=libc //改为libc
没什么意外稍等一下就编译通过了
等等,我还遇到了一个问题,截图如下
因为一个文件没有执行权限,解决办法很简单
chmod 777 mkreleasehdr.sh
再次执行make命令 “# make MALLOC=libc” , 应该就没啥问题了
不过编译最后出现了一句话
Hint: It's a good idea to run 'make test' ;)
什么意思呢? 执行下make test 是个好主意
我真的执行了,结果如下
[root@vm82220 redis-4.0.9]# make test
cd src && make test
make[1]: Entering directory `/data/redis-4.0.9/redis-4.0.9/src'
/bin/sh: ./runtest: Permission denied
make[1]: *** [test] Error 126
make[1]: Leaving directory `/data/redis-4.0.9/redis-4.0.9/src'
make: *** [test] Error 2
继续添加权限 “chmod 777 runtest” ,再次执行 make test, 又出问题了
[root@vm82220 redis-4.0.9]# make test
cd src && make test
make[1]: Entering directory `/data/redis-4.0.9/redis-4.0.9/src'
You need tcl 8.5 or newer in order to run the Redis test
make[1]: *** [test] Error 1
make[1]: Leaving directory `/data/redis-4.0.9/redis-4.0.9/src'
make: *** [test] Error 2
看到红字了吧,需要8.5以上的版本
安装tcl
yum install tcl
再make test 就没什么问题了
cd src/
现在redis可以说能用了,执行命令启动redis后
1. [root@bogon src]# ./redis-server
2. 28198:C 24 Feb 14:04:55.227 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
3. 28198:M 24 Feb 14:04:55.230 * Increased maximum number of open files to 10032 (it was originally set to 1024).
4. _._
5. _.-``__ ''-._
6. _.-`` `. `_. ''-._ Redis 3.2.8 (00000000/0) 64 bit
7. .-`` .-```. ```\/ _.,_ ''-._
8. ( ' , .-` | `, ) Running in standalone mode
9. |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
10. | `-._ `._ / _.-' | PID: 28198
11. `-._ `-._ `-./ _.-' _.-'
12. |`-._`-._ `-.__.-' _.-'_.-'|
13. | `-._`-._ _.-'_.-' | Redis
14. `-._ `-._`-.__.-'_.-' _.-'
15. |`-._`-._ `-.__.-' _.-'_.-'|
16. | `-._`-._ _.-'_.-' |
17. `-._ `-._`-.__.-'_.-' _.-'
18. `-._ `-.__.-' _.-'
19. `-._ _.-'
20. `-.__.-'
21.
22. 28198:M 24 Feb 14:04:55.234 # 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. 28198:M 24 Feb 14:04:55.234 # Server started, Redis version 3.2.8
有没有别的办法让他后台运行呢?
cd redis目录
./utils/install_server.sh
Linux安装Redis 6.0.5 ./install_server.sh报错
linux 安装Redis6.0.5时,进行到./install_server.sh时报错:
This systems seems to use systemd. Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry! 12
解决方案
vi ./install_server.sh
注释下面的代码即可:
#bail if this system is managed by systemd
#_pid_1_exe="$(readlink -f /proc/1/exe)"
#if [ "${_pid_1_exe##*/}" = systemd ]
#then
# echo "This systems seems to use systemd."
# echo "Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!"
# exit 1
#fi
继续执行命令
./utils/install_server.sh
1. Welcome to the redis service installer
2. This script will help you easily set up a running redis server
3.
4. Please select the redis port for this instance: [6379]
5. Selecting default: 6379
6. Please select the redis config file name [/etc/redis/6379.conf]
7. Selected default - /etc/redis/6379.conf
8. Please select the redis log file name [/var/log/redis_6379.log]
9. Selected default - /var/log/redis_6379.logspan style="white-space:pre"> ----这里如果觉得使用不习惯 可以试着把redis_6379 改为redisd(或者自己喜欢的名字,作为启动redis服务时的名字)
1. Please select the data directory for this instance [/var/lib/redis/6379]
2. Selected default - /var/lib/redis/6379
3. Please select the redis executable path [/usr/local/bin/redis-server]
4. Selected config:
5. Port : 6379
6. Config file : /etc/redis/6379.conf
7. span style="color:#ff0000;">---这个Config file 位置的6379.conf文件是你将来修改远程连接时的文件位置,,设置密码,远程连接都需要来这里
8. Log file : /var/log/redis_6379.log
9. Data dir : /var/lib/redis/6379
10. Executable : /usr/local/bin/redis-server
11. Cli Executable : /usr/local/bin/redis-cli
12. Is this ok? Then press ENTER to go on or Ctrl-C to abort.
13. Copied /tmp/6379.conf => /etc/init.d/redis_6379
14. span style="color:#ff0000;">---->记住这个路径,如果上面改名以后服务名没有改变的时候可以到这里修改文件名 并且进入文件 将里面带有redis_6379 文件名改掉,我只改了两三个地方
15. Installing service...
16. Successfully added to chkconfig!
17. Successfully added to runlevels 345!
18. Starting Redis server...
19. Installation successful! 发现原来是 [6379] 是提示的默认值 直接敲回车会继续执行,也就是说 每一次卡住 都是提示是否修改默认值,
我都是默认的 回车继续
- Starting Redis server... 执行成功!!
到这里redis基本上已经安装成功了,
但是以我严谨的态度,我决定查看一下到底安装成功有什么体现!
接下来 查看开机启动列表里面 有没有redis服务
chkconfig --list
1.
2. 注意:该输出结果只显示 SysV 服务,并不包含原生 systemd 服务。SysV 配置数据可能被原生 systemd 配置覆盖。
3. 如果您想列出 systemd 服务,请执行 'systemctl list-unit-files'。
4. 欲查看对特定 target 启用的服务请执行
5. 'systemctl list-dependencies [target]'。
6.
7. jexec 0:关 1:开 2:开 3:开 4:开 5:开 6:关
8. netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关
9. network 0:关 1:关 2:开 3:开 4:开 5:开 6:关
10. redis_6379 0:关 1:关 2:开 3:开 4:开 5:开 6:关
1-6这几项都代表什么可以去百度一下
好了开机列表有了
查看一下进程中有没有redis,并且停止服务看一下剩下什么
(停止redis的进程 可以用kill+28679 结束pid停用服务)
1. [root@bogon redis-3.2.8]# ps -ef|grep redis
2. root 28679 1 0 14:15 ? 00:00:00 /usr/local/bin/redis-server 127.0.0.1:6379
3. root 28807 4439 0 14:16 pts/0 00:00:00 grep --color=auto redis
4. [root@bogon redis-3.2.8]# service redis_6379 stop
5. Stopping ...
6. Waiting for Redis to shutdown ...
7. Redis stopped
8. [root@bogon redis-3.2.8]# ps -ef|grep redis
9. root 28830 4439 0 14:17 pts/0 00:00:00 grep --color=auto redis
然后再次启动redis服务,并且查看进程
1. [root@bogon redis-3.2.8]# service redis_6379 start
2. Starting Redis server...
3. [root@bogon redis-3.2.8]# ps -ef|grep redis
4. root 28843 1 0 14:17 ? 00:00:00 /usr/local/bin/redis-server 127.0.0.1:6379
5. root 28847 4439 0 14:17 pts/0 00:00:00 grep --color=auto redis
好了 redis的服务现在听我话了 ,然后我再试一下客户端能不能用
在刚安装完以后我查看一下全部的keys
[root@redies1zhu redis-6.2.6]# ./src/redis-cli
127.0.0.1:6379> keys *
(empty array)
告诉我是空的 ,我就放心了
剩下的就是redis的操作了 ,不多说了 Linux安装完成!
配置从主机的时候, 一直报错
Mmmmm... it seems like you don't have a redis executable. Did you run make install yet?
要填入路径
/usr/local/src/redis-6.2.6/src/redis-server
执行 redis-cli info 不成功
先看目录下有没有,果然没有
ls /usr/local/bin/
拷贝一个redis-cli
进入到压缩包目录下,拷贝到/usr/local/bin/ 中 。
再次执行 redis-cli info 成功
redis-cli --cluster create 192.168.103.107:7001 192.168.103.107:7002 192.168.103.107:7003 192.168.103.107:7004 192.168.103.107:7005 192.168.103.107:7006 --cluster-replicas 1
整体安装成功后的命令。
进入 /
进入 redis 解压目录
解压
进入redis
安装gcc
make加参数编译
编辑install_server 文件
{
注释掉 bail 下的那几行
}
运行install_server 文件
{
添加路径
/usr/local/src/redis-6.2.6/src/redis-server
}
查看bin目录
添加redis-cli 到bin 目录下
查看redis-info 看信息
配置redis环境变量
vi etc/profile
添加下面两行
export REDIS_HOME=/usr/local/redis export PATH=$PATH:$REDIS_HOME/bin
source /etc/profile
systemctl start redis_6379
6、安装Redis到指定地方
本人喜欢把常用服务放到喜欢地方,这次就放到/data/redis目录下吧
mkdir /data/redis
执行安装命令
make PREFIX=/data/redis/ install
其实/data/redis 下只有一个bin目录,里面有几个命令文件
为了后续方面使用命令,可以把bin文件夹放到环境变量中,也可以把这几个命令连接到/usr/local/bin中,我采用后者
//软连接一定要是有全路径哦,否则可能出现错误
# ln -s /data/redis/bin/redis-server /usr/local/bin/redis-server
# ln -s /data/redis/bin/redis-server /usr/local/bin/redis-sentinel
# ln -s /data/redis/bin/redis-benchmark^Cusr/local/bin/redis-sentinel
# ln -s /data/redis/bin/redis-benchmark /usr/local/bin/redis-benchmark
# ln -s /data/redis/bin/redis-check-aof /usr/local/bin/redis-check-aof
# ln -s /data/redis/bin/redis-check-rdb /usr/local/bin/redis-check-rdb
至此,安装就完成了
二、Redis配置
1、打开端口:6379 , Redis默认端口就是6379,可以在配置文件中修改
firewall-cmd --permanent --zone=public --add-port=6379/tcp
firewall-cmd --reload
2、修改配置文件
把配置文件/data/redis-4.0.9/redis.conf复制到/data/redis/conf文件夹中,修改配置
protected-mode no //打开其他机器连接
port 6379 //端口
daemonize yes //启用守护进程
pidfile /data/redis/run/redis_6379.pid //运行时
logfile /data/redis/log/redis_6379.log //日志
dir /data/redis/dir //数据文件夹
dbfilename dump_6379.rdb //数据文件
还有很多配置信息,建议仔细阅读一遍,满满干货啊 。。。。
3、启动Redis
redis-server /data/redis/config/redis.conf
4、登陆测试一下
redis-cli
单机的Redis安装配置完成
三、Redis-Sentinel的配置
下面说一下单机中哨兵HA的配置
哨兵模式中至少需要三个redis节点启用,一个主节点,两个从节点,我们分别使用6379(主)、6380(从)、6381(从)端口启动三个redis服务。
1、部署redis主从模式
创建文件夹/data/redis/conf,把redis.conf 分别复制三份 redis_6379.conf、redis_6380.conf、redis_6381.conf,vi打开redis_6380.conf修改
redis_6379.conf、redis_6380.conf、redis_6381.conf
bind 需要修改成主机的ip这样在运行时。才会显示
#端口
port 6380
#运行时
pidfile /data/redis/run/redis_6380.pid
#日至
logfile /data/redis/log/redis_6380.log
#数据
dbfilename dump_6380.rdb
#指定master
slaveof 192.168.103.106 6379
redis_6381.conf把端口改为6381,其他也做类似修改
2、启动redis服务
先启动主服务
redis-server redis_6379.conf //主服务
再分别启动两个从服务
redis-server redis_slave_6380.conf //从服务
redis-server redis_slave_6381.conf //从服务
查看进程状态
登陆主节点
redis-cli -h 192.168.103.106 -p 6379
info replication
在主机set key 值,在从机上查看值,从机只有读没有写权限
redis-cli -h 192.168.103.106 -p 6380
3、配置sentinel
也分别复制三个文件sentinel_26379.conf、sentinel_26380.conf、sentinel_26381.conf, 相关信息修改如下:
port 26379
daemonize yes
protected-mode no
dir "/data/redis/log/"
logfile "sentinel_26379.log"
sentinel monitor mymaster 192.168.103.106 6379 2
sentinel down-after-milliseconds mymaster 50000
sentinel failover-timeout mymaster 15000
sentinel parallel-syncs mymaster 1
#sentinel auth-pass mymaster YKHykh123456
sentinel known-slave mymaster 192.168.103.106 6380
sentinel known-slave mymaster 192.168.103.106 6381
#sentinel current-epoch 1
port 26380
daemonize yes
protected-mode no
dir "/data/redis/log/"
logfile "sentinel_26380.log"
sentinel monitor mymaster 192.168.103.106 6379 2
sentinel down-after-milliseconds mymaster 50000
sentinel failover-timeout mymaster 15000
sentinel parallel-syncs mymaster 1
#sentinel auth-pass mymaster YKHykh123456
sentinel known-slave mymaster 192.168.103.106 6380
sentinel known-slave mymaster 192.168.103.106 6381
#sentinel current-epoch 1
port 26381
daemonize yes
protected-mode no
dir "/data/redis/log/"
logfile "sentinel_26381.log"
sentinel monitor mymaster 192.168.103.106 6379 2
sentinel down-after-milliseconds mymaster 50000
sentinel failover-timeout mymaster 15000
sentinel parallel-syncs mymaster 1
#sentinel auth-pass mymaster YKHykh123456
sentinel known-slave mymaster 192.168.103.106 6380
sentinel known-slave mymaster 192.168.103.106 6381
#sentinel current-epoch 1
4、启动sentinel服务
# redis-sentinel sentinel_26379.conf
# redis-sentinel sentinel_26380.conf
# redis-sentinel sentinel_26381.conf
5、检查sentinel
redis-cli -h 192.168.103.106 -p 26379 //登陆服务
> sentinel masters //查看主服务信息
> sentinel slaves mymaster //查看所有从服务信息
配置相关就算完成了
redis 主从以及哨兵模式配置成功。