IP | 主机名 | 角色 | redis端口 | 哨兵端口 |
---|---|---|---|---|
192.168.100.15 | redis-master | master节点 | 6379 | 26379 |
192.168.100.16 | redis-slaver1 | slaver节点1 | 6380 | 26379 |
192.168.100.17 | redis-salver2 | slaver节点2 | 6381 | 26379 |
关闭防火墙和selinux,否则则需要配置防火墙规则,因为只是试验机,关掉比较省心
安装redis服务(3台服务器都需进行安装)
1、下载依赖包
[root@redis-master ~]# yum -y install gcc-c++ wget tcl
2、下载并编译安装redis
[root@redis-master ~]# cd /usr/local/src/
[root@redis-master src]# wget http://download.redis.io/releases/redis-5.0.2.tar.gz
[root@redis-master src]# tar zxf redis-5.0.2.tar.gz
[root@redis-master src]# cd redis-5.0.2
[root@redis-master redis-5.0.2]# mkdir /usr/local/redis
[root@redis-master redis-5.0.2]# make install PREFIX=/usr/local/redis
3、复制redis相关操作命令到/usr/sbin目录下,以便之后的操作
[root@redis-master redis-5.0.2]# cd /usr/local/redis/bin/
[root@redis-master bin]# sudo cp redis-cli redis-sentinel redis-server /usr/sbin
4、复制redis的配置文件到/etc目录下,并进行配置
[root@redis-master bin]# cp /usr/local/src/redis-5.0.2/redis.conf /etc/
[root@redis-master bin]# vim /etc/redis.conf
#####修改配置文件里的一下参数#####
port 6379 //在本实验中slaver上和master不同,需要注意,可参照文首的配置表
daemonize yes //开启redis后端启动
bind 0.0.0.0 //允许所有远程访问
pidfile /var/run/redis_6379.pid //文件名以端口区分
logfile /var/log/redis_6379.log //设置日志文件,文件名以端口区分
requirepass 123456 //设置密码
5、将redis注册为系统服务,并重启redis服务
(1)编写启动脚本
[root@redis-master bin]# vim /etc/init.d/redis
#!/bin/sh
#
# redis - this script starts and stops the redis-server daemon
#
# chkconfig: - 85 15
# description: Redis is a persistent key-value database
# processname: redis-server
# config: /usr/local/redis/bin/redis-server
# config: /etc/redis.conf
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
redis="/usr/local/redis/bin/redis-server"
prog=$(basename $redis)
REDIS_CONF_FILE="/etc/redis.conf"
[ -f /etc/sysconfig/redis ] && . /etc/sysconfig/redis
lockfile=/var/lock/subsys/redis
start() {
[ -x $redis ] || exit 5
[ -f $REDIS_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $redis $REDIS_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
stop
start
}
reload() {
echo -n $"Reloading $prog: "
killproc $redis -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart| reload|orce-reload}"
exit 2
esac
(2)授予脚本权限
[root@redis-master bin]# chmod 755 /etc/init.d/redis
[root@redis-master bin]# ll /etc/init.d/
total 44
-rw-r--r--. 1 root root 18281 Aug 24 2018 functions
-rwxr-xr-x. 1 root root 4569 Aug 24 2018 netconsole
-rwxr-xr-x. 1 root root 7923 Aug 24 2018 network
-rw-r--r--. 1 root root 1160 Oct 31 2018 README
-rwxr-xr-x. 1 root root 1946 Mar 19 16:52 redis
(3)将redis加入系统服务,并重启redis服务
[root@redis-master bin]# chkconfig --add redis
[root@redis-master bin]# chkconfig redis on
[root@redis-master bin]# systemctl restart redis
配置redis主从
1、修改两台从机配置文件
[root@redis-slaver1 ~]# vim /etc/redis.conf
slaveof 192.168.100.15 6379 //格式为slaveof <masterip> <masterport>
masterauth 123456 //此处设置master配置文件中设置的密码,需要三台节点都进行设置,后面进行高可用需要用到,否则只能在两台从节点之间进行切换
2、重启从机的redis
[root@redis-slaver1 ~]# systemctl restart redis
3、进入主节点redis客户端,查看从节点是否成功连接
[root@redis-master ~]# redis-cli -h 127.0.0.1 -p 6379 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> INFO replication
# Replication
role:master
connected_slaves:2 //连接数为两台slaver节点
slave0:ip=192.168.100.17,port=6379,state=online,offset=196,lag=0
slave1:ip=192.168.100.16,port=6380,state=online,offset=196,lag=0
master_replid:18f3fdceb210ee2822f69b1eff57d772681de688
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:196
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:196
4、测试
在master节点设置一个值,测试在两个slaver节点上是否能够获取到
master
[root@redis-master ~]# redis-cli -h 127.0.0.1 -p 6379 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> set name "sqq"
OK
slaver1
[root@redis-slaver1 ~]# redis-cli -h 127.0.0.1 -p 6380 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6380> get name
"sqq"
slaver2
[root@redis-slaver2 ~]# redis-cli -h 127.0.0.1 -p 6381 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6381> get name
"sqq"
配置redis高可用和故障转移
(1和2的内容3台节点都需要执行)
1、将哨兵的配置文件(位于redis源码目录下)拷贝到/etc目录下
[root@redis-master ~]# cp /usr/local/src/redis-5.0.2/sentinel.conf /etc/
2、修改哨兵配置文件
[root@redis-master ~]# vim /etc/sentinel.conf
port 26379
daemonize yes
pidfile /var/run/redis-sentinel.pid
logfile "/var/log/redis-sentinel.pid"
dir /tmp
protect-mode no
sentinel monitor mymaster 192.168.100.15 6379 2
sentinel auth-pass mymaster "123456" //如果要做自动故障转移,则建议所有的redis.conf都设置masterauth,因为自动故障只会重写主从关系
//即slaveof,不会自动写入masterauth。如果Redis原本没有设置密码,则可以忽略。
sentinel down-after-milliseconds mymaster 3000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 18000
sentinel deny-scripts-reconfig yes
3、进入任意节点的sentinel控制台查看状态
[root@redis-slaver1 ~]# redis-cli -h 127.0.0.1 -p 26379 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:26379> INFO sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.100.15:6379,slaves=2,sentinels=3
4、测试
关闭主节点的redis
[root@redis-master ~]# redis-cli -h 127.0.0.1 -p 6379 -a 123456 shutdown
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
查看sentinel,可以看出slaver2主机已经成为新的master节点
[root@redis-master ~]# redis-cli -h 127.0.0.1 -p 26379 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:26379> INFO sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.100.17:6381,slaves=2,sentinels=3