官网下载地址:https://redis.io/download/#redis-downloads
本篇内容:
---->单实例安装部署
---->哨兵模式安装部署
---->验证集群有效性
编译安装:
[root@localhost ~]# yum -y install gcc gcc-c++ libstdc++-devel [root@localhost opt]# ls redis-stable.tar.gz [root@localhost ~]# tar -xvf redis-stable.tar.gz -C /usr/local/ [root@localhost ~]# cd /usr/local/redis-stable [root@localhost redis-stable]# make ##这里手动 cd 到 src 目录,也可以不 cd 目录,如果直接 make install 则会自动 cd 进入该目录下执行安装操作 [root@localhost redis-stable]# cd src/ [root@localhost src]# make install cd src && make install make[1]: Entering directory `/root/redis-7.0.4/src' CC Makefile.dep make[1]: Leaving directory `/root/redis-7.0.4/src' ... ... [root@localhost src]# make install
配置文件:
[root@localhost src]# cd .. [root@localhost redis-stable]# cp redis.conf redis.conf.bak ##这里备份了初始的 redis.conf 文件后,将需要使用的配置文件重命名 ##重命名为 端口号.conf 是后面的启动配置文件里面使用这样命名的文件执行 ##只要启动所使用的配置文件和启动程序文件中的命名对应即可 [root@localhost redis-7.0.4]# mv redis.conf 6379.conf [root@localhost redis-7.0.4]# vim 6379.conf bind 192.168.137.251 protected-mode yes port 6379 daemonize yes pidfile /var/run/redis_6379.pid loglevel notice logfile "" requirepass 123456 maxmemory 2147483648
配置服务文件:
[root@localhost redis-4.0.9]# cd /etc/init.d/ [root@localhost init.d]# cp /usr/local/redis-stable/utils/redis_init_script redis [root@localhost init.d]# vim redis #!/bin/sh # chkconfig: 2345 10 90 # description: Start and Stop redis # # Simple Redis init.d script conceived to work on Linux systems # as it does use of the /proc filesystem. HOSTIP=`ifconfig | awk '/192\.168\./{print $2}'`
REDISPORT=6379 EXEC=/usr/local/bin/redis-server CLIEXEC=/usr/local/bin/redis-cli ## 这里是在配置文件设定的 pid 文件生成目录 PIDFILE=/var/run/redis_${REDISPORT}.pid ## 这里是配置文件的路径及名称,取用变量为端口号,所以是 6379.conf CONF="/usr/local/redis-stable/${REDISPORT}.conf" case "$1" in start) if [ -f $PIDFILE ] then echo "$PIDFILE exists, process is already running or crashed" else echo "Starting Redis server..." $EXEC $CONF fi ;; stop) read -s -p "Enter password:" PASSWD if [ ! -f $PIDFILE ] then echo "$PIDFILE does not exist, process is not running" else PID=$(cat $PIDFILE) echo "Stopping ..." $CLIEXEC -a $PASSWD -h $HOSTIP -p $REDISPORT shutdown while [ -x /proc/${PID} ] do echo "Waiting for Redis to shutdown ..." sleep 1 done echo "Redis stopped" fi ;; restart) read -s -p "Enter password:" PASSWD if [ ! -f $PIDFILE ] then echo "你Redis没有启动,帮你先启动了。" else PID=$(cat $PIDFILE) echo "Stopping ..." $CLIEXEC -a $PASSWD -h $HOSTIP -p $REDISPORT shutdown while [ -x /proc/${PID} ] do echo "Waiting for Redis to shutdown ..." sleep 1 done echo "Redis stopped" fi if [ -f $PIDFILE ] then echo "$PIDFILE exists, process is already running or crashed" else echo "Starting Redis server..." $EXEC $CONF fi ;; status) if [ -f $PIDFILE ] then PID=$(cat $PIDFILE) echo "$PIDFILE is running,pid=${PID}" else echo "$PIDFILE is not running" fi ;; *) echo "Please use start or stop as first argument" ;; esac
连接redis:
##启动:service redis start ##停止:service redis stop ##重启:service redis restart ##状态:service redis status [root@localhost redis-stable]# service redis start [root@localhost redis-stable]# cd [root@localhost ~]# redis-cli -h 192.168.137.251 192.168.137.251:6379> auth 123456 OK 192.168.137.251:6379> ping PONG 192.168.137.251:6379> quit
这里准备了3个主机,进行搭建部署。
在每个主机上均安装redis实例。
master端:
6379.conf(redis.conf)配置文件:
[root@redis-01 ~]# vim /usr/local/redis-stable/6379.conf
bind 192.168.137.251
protected-mode no
port 6379
daemonize yes
masterauth redhat
requirepass 123456
maxmemory 2147483648
appendonly yes
no-appendfsync-on-rewrite yes
sentinel.conf 配置文件:
##备份初始配置文件 [root@redis-01 ~]# cp /usr/local/redis-stable/sentinel.conf /usr/local/redis-stable/sentinel.conf.bak [root@redis-01 ~]# vim /usr/local/redis-stable/sentinel.conf ##保护模式,需要手动添加,并设置为no,将其关闭 protected-mode no ##端口设置 port 26379 ##后台运行模式 daemonize yes ##pid文件路径 pidfile "/var/run/redis-sentinel.pid" ##log日志位置 logfile "/var/log/redis/sentinel.log" ##master的IP,以及最低通过的票数,这里设置2个节点数 ##最后的参数是设置有多少个哨兵节点认为主节点故障下线,才算真正下线,此判定后才会开始切换主节点 ##三个节点的时候设置为1,会出现仅第一次切换成功,之后失败的情况,设置为 2,多次切换正常 sentinel monitor master 192.168.137.251 6379 2 ##master的验证密码 sentinel auth-pass master 123456 ##失效的时间间隔,单位毫秒(ms) sentinel down-after-milliseconds master 30000 ##集群中master的数量 1个 sentinel parallel-syncs master 1 ##下线的过期时间,单位毫秒(ms) sentinel failover-timeout master 180000
slave端:
6379.conf(redis.conf)配置文件:
[root@redis-02 ~]# vim /usr/local/redis-stable/6379.conf protected-mode no ##绑定的地址为当前主机的IP bind 192.168.137.252 ##添加一列 slaveof ,来指定 master 主机的地址和端口 slaveof 192.168.137.251 6379 ##添加 master 认证的密码 masterauth 123456
sentinel.conf 配置文件与 master 主机的配置文件相同,所以可以拷贝至各个 slave 主机上:
[root@redis-02 ~]# mv /usr/local/redis-stable/sentinel.conf /usr/local/redis-stable/sentinel.conf.bak [root@redis-02 ~]# scp 192.168.137.251:/usr/local/redis-stable/sentinel.conf /usr/local/redis-stable/sentinel.conf root@192.168.137.251's password: sentinel.conf 100% 14KB 14.6MB/s 00:00 [root@redis-03 ~]# mv /usr/local/redis-stable/sentinel.conf /usr/local/redis-stable/sentinel.conf.bak [root@redis-03 ~]# scp 192.168.137.251:/usr/local/redis-stable/sentinel.conf /usr/local/redis-stable/sentinel.conf root@192.168.137.251's password: sentinel.conf 100% 14KB 16.2MB/s 00:00
启动集群服务:
##配置文件变更之后需要重新启动服务,先master后slave [root@redis-01 ~]# service redis restart [root@redis-02 ~]# service redis restart [root@redis-03 ~]# service redis restart ##启动sentinel服务,先master后slave [root@redis-01 ~]# redis-sentinel /usr/local/redis-stable/sentinel.conf [root@redis-02 ~]# redis-sentinel /usr/local/redis-stable/sentinel.conf [root@redis-03 ~]# redis-sentinel /usr/local/redis-stable/sentinel.conf ##记得每个主机节点,将服务启动加入开机启动配置文件中 [root@redis-01 ~]# vim /etc/rc.local service redis start redis-sentinel /usr/local/redis-stable/sentinel.conf
使用 info replication 查看集群状态:
master(redis-01) [root@redis-01 ~]# redis-cli -h 192.168.137.251 192.168.137.251:6379> auth 123456 OK 192.168.137.251:6379> ping PONG 192.168.137.251:6379> info replication # Replication role:master connected_slaves:2 slave0:ip=192.168.137.252,port=6379,state=online,offset=413,lag=1 slave1:ip=192.168.137.253,port=6379,state=online,offset=413,lag=1 master_failover_state:no-failover master_replid:233eb7b09e181d482fc075034b4c2a7c965574c9 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:413 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:413 --------------------------------------- slave(redis-02) [root@redis-02 ~]# redis-cli -h 192.168.137.252 192.168.137.252:6379> auth 123456 OK 192.168.137.252:6379> ping PONG 192.168.137.252:6379> info replication # Replication role:slave master_host:192.168.137.251 master_port:6379 master_link_status:up master_last_io_seconds_ago:5 master_sync_in_progress:0 slave_read_repl_offset:413 slave_repl_offset:413 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:233eb7b09e181d482fc075034b4c2a7c965574c9 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:413 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:413 --------------------------------------- slave(redis-03) [root@redis-03 ~]# redis-cli -h 192.168.137.253 192.168.137.253:6379> auth 123456 OK 192.168.137.253:6379> ping PONG 192.168.137.253:6379> info replication # Replication role:slave master_host:192.168.137.251 master_port:6379 master_link_status:up master_last_io_seconds_ago:7 master_sync_in_progress:0 slave_read_repl_offset:413 slave_repl_offset:413 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:233eb7b09e181d482fc075034b4c2a7c965574c9 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:413 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:29 repl_backlog_histlen:385
数据同步情况:
##当前数据库内为空 192.168.137.251:6379> keys * (empty array) 192.168.137.252:6379> keys * (empty array) 192.168.137.253:6379> keys * (empty array) ##master节点加入数据 192.168.137.251:6379> set number 1 OK ##slave节点查看数据 192.168.137.252:6379> keys * 1) "number" 192.168.137.253:6379> get number "1" ##slave节点变更数据报错,master可以变更数据 192.168.137.253:6379> INCR number (error) READONLY You can't write against a read only replica. 192.168.137.251:6379> INCR number (integer) 2 192.168.137.252:6379> get number "2"
故障转移测试:
将master主机关机或仅关闭redis,使master主机不能连接;
info replication 查看原来两个slave主机的信息;
##当前252主机变为master,并可以操作更改数据 192.168.137.252:6379> info replication # Replication role:master connected_slaves:2 slave0:ip=192.168.137.251,port=6379,state=online,offset=206167,lag=1 slave1:ip=192.168.137.253,port=6379,state=online,offset=206167,lag=1 master_failover_state:no-failover master_replid:659b335b294fcb497d9f64cbc66e15e8f1357df8 master_replid2:ca76f245c159bfd1c129cf603b58fdcab0be8f33 master_repl_offset:206310 second_repl_offset:150412 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:150412 repl_backlog_histlen:55899 192.168.137.252:6379> INCR number (integer) 7 ##当前253主机信息显示master为252主机 192.168.137.253:6379> info replication # Replication role:slave master_host:192.168.137.252 master_port:6379 master_link_status:up master_last_io_seconds_ago:0 master_sync_in_progress:0 slave_read_repl_offset:205881 slave_repl_offset:205881 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:659b335b294fcb497d9f64cbc66e15e8f1357df8 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:205881 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:150412 repl_backlog_histlen:55470
之后恢复原master的251主机,可以看到master也是252主机,连接后也并不会抢走master的角色,变为slave;
[root@redis-01 ~]# redis-cli -h 192.168.137.251 192.168.137.251:6379> auth 123456 OK 192.168.137.251:6379> info replication # Replication role:slave master_host:192.168.137.252 master_port:6379 master_link_status:up master_last_io_seconds_ago:1 master_sync_in_progress:0 slave_read_repl_offset:206596 slave_repl_offset:206596 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:659b335b294fcb497d9f64cbc66e15e8f1357df8 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:206596 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:150412 repl_backlog_histlen:56185
之后再关闭现在master的主机或服务,再次查看其它节点的信息,确认master转移并可用,则当前集群状态及功能正常;