redis主安装在公网IP为x.x.x.x,内网为x1.x1.x1.x1的服务器上。

redis从安装在公网IP为y.y.y.y,内网为y1.y1.y1.y1的服务器上。

虚拟IP为z.z.z.z

redis主、从安装

第一步,安装编译工具及库文件。

命令:yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel

第二步,下载redis安装包。

命令:cd /usr/local/src

wget http://download.redis.io/releases/redis-4.0.10.tar.gz

第三步,解压redis安装包。

命令:tar zxvf redis-4.0.10.tar.gz -C /usr/local

第四步,编译安装redis。

命令:cd /usr/local/redis-4.0.10

make

cd /usr/local/redis-4.0.10/src

make install

注意:执行完 make 后,再执行 make install,会将 /usr/local/redis-4.0.10/src下的可执行文件复制到 /usr/local/bin 目录下,该目录下的都配置到环境变量里,相当于添加了快捷方式,就可以在任意目录执行redis的软件的命令,如启动、停止等命令,如果不执行make install也可以使用redis,只不过比较麻烦,执行命令时要进入redis的bin目录里操作了。

redis主、从配置

第一步,为了方便管理,在/usr/local中新建/redis/etc文件夹,并在将/usr/local/redis-4.0.10文件夹中的conf配置文件复制粘贴到/usr/local/redis/etc。原生的配置文件不用变,改完之后还能回到原来的样子,以保证安全。

命令:mkdir -p /usr/local/redis/etc

cp /usr/local/redis-4.0.10/redis.conf /usr/local/redis/etc

第二步,在/usr/local中新建/redis/bin、/redis/db和/redis/logs文件夹,/usr/local/redis-4.0.10/src文件夹中的常用命令复制粘贴到/usr/local/redis/bin。

命令:mkdir -p /usr/local/redis/bin

mkdir -p /usr/local/redis/db

mkdir -p /usr/local/redis/logs

cd /usr/local/redis-4.0.10/src
cp mkreleasdhdr.sh redis-benchmark redis-check-aof redis-check-dump redis-cli redis-server /usr/local/redis/bin

第三步,编辑配置/etc/sysctl.conf文件。

配置sysctl.conf文件的内核参数,否则redis脚本在重启或停止redis时,将会报错,并且不能自动在停止服务前同步数据到磁盘上。

命令:vim /etc/sysctl.conf

在最后一行添加下面代码:

vm.overcommit_memory = 1

# 使设置立即生效

命令:sysctl -p

第四步,redis开启外部访问权限的方法。

redis主编辑配置/usr/local/redis/etc/redis.conf文件,做以下几处修改:

“bind 127.0.0.1”改为“bind 0.0.0.0”,

“slaveof <masterip> <masterport>”改为“slaveof y1.y1.y1.y1 6379”,

“masterauth <master-password>”改为masterauth "redis123456",

“daemonize no”改为“daemonize yes”,

“requirepass foobared”改为requirepass "redis123456",

“dir ./”改为“dir /usr/local/redis/db”,

“logfile ”改为“logfile /usr/local/redis/logs/redis.log”。

redis从编辑配置/usr/local/redis/etc/redis.conf文件,做以下几处修改:

“bind 127.0.0.1”改为“bind 0.0.0.0”,

“slaveof <masterip> <masterport>”改为“slaveof x1.x1.x1.x1 6379”,

“masterauth <master-password>”改为masterauth "redis123456",

“daemonize no”改为“daemonize yes”,

“requirepass foobared”改为requirepass "redis123456",

“dir ./”改为“dir /usr/local/redis/db”,

“logfile ”改为“logfile /usr/local/redis/logs/redis.log”。

redis主、从启动与关闭服务

说明:前台启动:redis-server 后台启动:redis-server &

第一步,前台启动redis服务。

命令:cd /usr/local/redis/bin

./redis-server

注意:执行完该命令后,如果Lunix关闭当前会话,则redis服务也随即关闭。正常情况下,启动redis服务需要从后台启动,并且指定启动配置文件。

第二步,后台启动redis服务。

再次启动redis服务,并指定启动服务配置文件。

命令:cd /usr/local/redis/bin

redis-server /usr/local/redis/etc/redis.conf

#结果第一行最后面显示redis端口

ps -ef | grep redis

#结果第一行最后面显示redis进程号

netstat -nap | grep  6379(或ss -tnlp|grep 6379)

服务端启动成功后,启动redis 客户端,查看端口号。

第三步,外部IP地址访问redis

命令:ip a

redis-cli -h x1.x1.x1.x1 -p 6379

redis-cli -h y1.y1.y1.y1 -p 6379

auth "redis123456"

keys *

set [key] [value]

get [key]

exit

第四步,前台和后台关闭redis服务。

命令:cd /usr/local/redis/bin

pkill redis-server

redis-cli shutdown

netstat -nap | grep 6379(或ss -tnlp|grep 6379)

关机重启,redis主、从会自动启动

使用redis启动/usr/local/redis-4.0.10/utils中的脚本redis_init_script设置开机自启动。

第一步,修改redis_init_script脚本代码。

redis_init_script脚本代码如下:

#!/bin/sh

...

REDISPORT=6379

#服务端所处位置

EXEC=/usr/local/redis/bin/redis-server

#客户端位置

CLIEXEC=/usr/local/redis/bin/redis-cli

#redis的PID文件位置,需要修改

PIDFILE=/var/run/redis_6379.pid

#redis的配置文件位置,需将${REDISPORT}修改为文件名

CONF="/usr/local/redis/etc/redis.conf"

...

第二步,将redis_init_script复制到/etc/rc.d/init.d目录下。 

redis_init_script复制到/etc/rc.d/init.d,复制粘贴的启动脚本会被命名为redisd(通常都以d结尾表示是后台自启动服务)。

命令:cd /usr/local/redis-4.0.10/utils

cp redis_init_script /etc/rc.d/init.d/redisd

chmod +x /etc/rc.d/init.d/redisd

#设置为开机自启动服务器

chkconfig redisd on

#打开服务

service redisd start

#关闭服务

service redisd stop

在设置开机自启动时,发现错误:

service redisd does not support chkconfig

这个错误的解决办法是在启动脚本开头添加如下注释来修改运行级别,如下:

#!/bin/sh

# chkconfig:   2345 90 10

再用命令:chkconfig redisd on设置即可。

不进入根目录即可进行相应的操作

打开服务命令:service redisd start

关闭服务命令:service redisd stop

Redis主、从安装keepalived

第一步,安装编译工具及库文件。

命令:yum -y install openssl-devel libnl3-devel ipset-devel iptables-devel libnfnetlink-devel net-snmp-devel

第二步,进入/usr/local/src文件夹,下载keepalived安装包。

命令:cd /usr/local/src

wget https://www.keepalived.org/software/keepalived-2.2.4.tar.gz

第三步,解压keepalived安装包。

命令:tar zxf  keepalived-2.2.4.tar.gz

第四步,编译安装keepalived。

命令:cd keepalived-2.2.4

./configure --prefix=/usr/local/keepalived/

make && make install

cd /usr/local/keepalived/etc/keepalived/

cp keepalived.conf /etc/keepalived/keepalived.conf

注意:/etc/keepalived/keepalived.conf是默认的配置文件

Redis主、从配置keepalived

第一步,Redis+Keepalived Master配置文件。

1)进入/etc/keepalived文件夹,配置keepalived.conf文件,代码如下:

global_defs {

        router_id redis-master

}

vrrp_script redis_check {

        script "/etc/keepalived/scripts/redis_check.sh"

        interval 4

        weight -5

        fall 3

        rise 2

}

vrrp_instance VI_REDIS {

        state MASTER

        interface eth0

        virtual_router_id 51

        priority 150

        advert_int 1

        nopreempt

        authentication {

               auth_type PASS

               auth_pass 1111

        }

        track_script {

               chk_redis

        }

        unicast_src_ip x1.x1.x1.x1

        unicast_peer {

                 y1.y1.y1.y1

        }

        virtual_ipaddress {

                z.z.z.z

        }

        notify_master /etc/keepalived/scripts/redis_master.sh

        notify_backup /etc/keepalived/scripts/redis_backup.sh

        notify_fault /etc/keepalived/scripts/redis_fault.sh

        notify_stop /etc/keepalived/scripts/redis_stop.sh

}

2)先利用命令“mkdir -p /etc/keepalived/scripts”创建文件夹。然后在新建文件夹中创建redis_check.sh脚本,在脚本中写入下面代码:

#!/bin/bash

LOGFILE=/var/log/alive_state.log

echo "Running redis_check.sh..." >> $LOGFILE

date >> $LOGFILE

CHECK=$(ss -tnlp|grep 6379)

if [ $? -ne 0 ]; then

   echo "redis-server is not running..." >> $LOGFILE

   # 确保服务正常切换

   /etc/rc.d/init.d/keepalived stop

   exit 1

else

   echo "redis-server is running..." >> $LOGFILE

   exit 0

fi

echo "Finished running redis_check.sh..." >> $LOGFILE

3)在/etc/keepalived/scripts文件夹中创建redis_master.sh脚本,在脚本中写入下面代码:

#!/bin/bash

LOGFILE=/var/log/keepalived-redis-state.log

echo "Running redis_master.sh..." >>$LOGFILE

echo "[Master]" >> $LOGFILE

date >> $LOGFILE

REDISCLI="/usr/local/redis/bin/redis-cli"

echo "Being Master..." >> $LOGFILE

echo "Running SLAVEOF cmd..." >> $LOGFILE

# wait 15s 等待数据同步完成

sleep 15s

echo "Run slaveof no one cmd..."

# -h region01主机IP -p redis服务端口 -a redis密码

$REDISCLI -h x1.x1.x1.x1 -p 6379 -a redis123456 SLAVEOF NO ONE >>$LOGFILE 2>&1

echo "Finished running redis_master.sh..." >>$LOGFILE

4)在/etc/keepalived/scripts文件夹中创建redis_backup.sh脚本,在脚本中写入下面代码:

#!/bin/bash

LOGFILE=/var/log/keepalived-redis-state.log

echo "Running redis_bakcup.sh..." >>$LOGFILE

echo "[Backup]" >> $LOGFILE

date >> $LOGFILE

echo "Being Slave..." >> $LOGFILE

sleep 5s

echo "Run SLAVEOF cmd..." >> $LOGFILE

REDISCLI="/usr/local/redis/bin/redis-cli"

# -h region01主机IP -p redis服务端口 -a redis密码

# 192.168.158.11为region02主机IP,6379为region02主机上redis服务端口

$REDISCLI -h x1.x1.x1.x1 -p 6379 -a redis123456 SLAVEOF y1.y1.y1.y1 6379 >>$LOGFILE 2>&1

echo "Finished running redis_backup.sh..." >>$LOGFILE

5)在/etc/keepalived/scripts文件夹中创建redis_fault.sh脚本,在脚本中写入下面代码:

#!/bin/bash

LOGFILE=/var/log/keepalived-redis-state.log

echo "Running redis_fault.sh..." >>$LOGFILE

echo "[Fault]" >> $LOGFILE

date >> $LOGFILE

echo "Finished running redis_fault.sh..." >> $LOGFILE

6)在/etc/keepalived/scripts文件夹中创建redis_stop.sh脚本,在脚本中写入下面代码:

#!/bin/bash

LOGFILE=/var/log/keepalived-redis-state.log

echo "Running redis_stop.sh...." >>$LOGFILE

echo "[Stop]" >> $LOGFILE

date >> $LOGFILE

echo "Finished running redis_stop.sh...." >>$LOGFILE

第二步,Redis+Keepalived Backup配置文件。

1)进入/etc/keepalived文件夹,配置keepalived.conf文件,代码如下:

global_defs {

        router_id redis-slave

}

vrrp_script redis_check {

        script "/etc/keepalived/scripts/redis_check.sh"

        interval 4

        weight -5

        fall 3  

        rise 2

}

vrrp_instance VI_REDIS {

        state BAKCUP

        interface eth0

        virtual_router_id 51

        priority 100

        advert_int 1

        nopreempt

       authentication {

             auth_type PASS

             auth_pass 1111

       }

       track_script {

            chk_redis

       }

       unicast_src_ip y1.y1.y1.y1

       unicast_peer {

             x1.x1.x1.x1

       }

       virtual_ipaddress {

             z.z.z.z 

       }

       notify_master /etc/keepalived/scripts/redis_master.sh

       notify_backup /etc/keepalived/scripts/redis_backup.sh

       notify_fault /etc/keepalived/scripts/redis_fault.sh

       notify_stop /etc/keepalived/scripts/redis_stop.sh

}

2)先利用命令“mkdir -p /etc/keepalived/scripts”创建文件夹。然后在新建文件夹中创建redis_check.sh脚本,在脚本中写入下面代码:

#!/bin/bash

LOGFILE=/var/log/alive_state.log

echo "Running redis_check.sh..." >> $LOGFILE

date >> $LOGFILE

CHECK=$(ss -tnlp|grep 6379)

if [ $? -ne 0 ]; then

   echo "redis-server is not running..." >> $LOGFILE

   # 确保服务正常切换

   /etc/rc.d/init.d/keepalived stop

   exit 1

else

   echo "redis-server is running..." >> $LOGFILE

   exit 0

fi

echo "Finished running redis_check.sh..." >> $LOGFILE

3)在/etc/keepalived/scripts文件夹中创建redis_master.sh脚本,在脚本中写入下面代码:

#!/bin/bash

LOGFILE=/var/log/keepalived-redis-state.log

echo "Runnings redis_master.sh..." >> $LOGFILE

echo "[Master]" >> $LOGFILE

date >> $LOGFILE

sleep 10s

echo "Run slaveof no one cmd...">>$LOGFILE

REDISCLI="/usr/local/redis/bin/redis-cli"

# -h region02主机IP -p redis服务端口 -a redis密码

$REDISCLI -h y1.y1.y1.y1 -p 6379 -a redis123456 SLAVEOF NO ONE >>$LOGFILE 2>&1

echo "Finished running redis_master.sh...."  >> $LOGFILE

4)在/etc/keepalived/scripts文件夹中创建redis_backup.sh脚本,在脚本中写入下面代码:

#!/bin/bash

LOGFILE=/var/log/keepalived-redis-state.log

echo "Running redis_backup.sh...." >>$LOGFILE

echo "[Backup]" >>$LOGFILE

echo date >>$LOGFILE

echo "Being Slave..." >> $LOGFILE

sleep 15s

echo "Run SLAVEOF cmd..." >> $LOGFILE

REDISCLI="/usr/local/redis/bin/redis-cli"

$REDISCLI -h y1.y1.y1.y1 -p 6379 -a redis123456 SLAVEOF x1.x1.x1.x1 6379 >>$LOGFILE 2>&1

echo "Finished running redis_backup.sh...." >> $LOGFILE

5)在/etc/keepalived/scripts文件夹中创建redis_fault.sh脚本,在脚本中写入下面代码:

#!/bin/bash

LOGFILE=/var/log/keepalived-redis-state.log

echo "Running redis_fault.sh..." >>$LOGFILE

echo "[Fault]" >> $LOGFILE

date >> $LOGFILE

echo "Finished running redis_fault.sh..." >> $LOGFILE

6)在/etc/keepalived/scripts文件夹中创建redis_stop.sh脚本,在脚本中写入下面代码:

#!/bin/bash

LOGFILE=/var/log/keepalived-redis-state.log

echo "Running redis_stop.sh...." >>$LOGFILE

echo "[Stop]" >> $LOGFILE

date >> $LOGFILE

echo "Finished running redis_stop.sh...." >>$LOGFILE

第三步,在主从服务器上面给新建的脚本都加上可执行权限

命令:chmod +x /etc/keepalived/scripts/*.sh

Redis主、从中keepalived的启动与关闭

注意:在启动keepalived服务以前,需要确保redis服务已经启动,否则主节点的keepalived服务会被脚本直接stop,然后漂移到从节点。

第一步,在主服务器上启动keepalived,查看keepalived进程以及vip情况。

命令:systemctl start keepalived

ps -ef | grep keepalived

ip a | grep eth0

当主服务器的keepalived有vip显示,从服务器的keepalived没有vip显示,则说明主、从服务器上keepalived配置成功。

第二步,在主服务器上关闭keepalived,查看keepalived进程以及vip情况。

命令:systemctl stop keepalived

ps -ef | grep keepalived

ip a | grep eth0

关闭keepalived之后,主、从服务器上keepalived都没有vip显示。

Redis主、从中keepalived的自启动

第一步,安装完keepalived后,为了把keepalived设置为服务自启动,则需要在Redis主、从中把自启动需要的文件复制粘贴到指定文件夹即可。

cd /usr/local/src/keepalived-2.2.4/keepalived/etc/init.d/

cp keepalived /etc/rc.d/init.d/keepalived

cd /usr/local/keepalived/sbin/

cp keepalived /usr/sbin

cd /usr/local/keepalived/etc/sysconfig/

cp keepalived /etc/sysconfig

第二步,添加执行权限

命令:chmod +x /etc/rc.d/init.d/keepalived

第三步,添加keepalived到系统服务,并设置开机启动。

命令:chkconfig --add keepalived

chkconfig keepalived on

第四步,启动keepalived

命令:systemctl start keepalived

若结果显示“Starting keepalived (via systemctl):  Job for keepalived.service failed because the control process exited with error code. See "systemctl status keepalived.service" and "journalctl -xe" for details.[失败]”,则说明keepalived启动失败,需要用命令“systemctl status keepalived”查看具体原因。

命令:systemctl status keepalived

若结果显示“(/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)

Active: failed (Result: exit-code)

Process: 17642 ExecStart=/usr/local/keepalived/sbin/keepalived”,则可以修改/usr/lib/systemd/system/keepalived.service文件,将路径/usr/local/keepalived/sbin改成存放keepalived文件的路径/usr/sbin/,将PIDFile改为“/var/run/keepalived.pid”。

第五步,使keepalived服务生效。

命令:systemctl daemon-reload

第六步,再启动keepalived

命令:systemctl start keepalived

systemctl status keepalived

若结果显示“Active: active (running)”,则说明启动成功。

测试

第一步,测试虚拟IP能否在两台主机之间互相漂移。

按照主从切换的原理,当redis主服务正常时,虚拟IP会在redis主上;当redis主宕机,虚拟IP会自动切换到redis从。

当集群正常时,虚拟IP在redis主上;当集群redis主宕掉时,虚拟IP在redis从上。

# 当redis主、从功能正常时,在redis主、从上执行

命令:ip a | grep eth0

# 当redis主宕机,在redis主执行操作,杀掉redis-server进程

命令:ss -tnlp|grep 6379

kill -9 [pid]

# 在redis从执行

命令:ip a | grep eth0

若结果如下图所示,则说明keepalived虚拟IP漂移功能正常。

第二步,测试redis主宕机过程中,redis集群状态切换

# 在redis主执行脚本,查看正常情况下redis集群状态。

命令:redis-cli -h x1.x1.x1.x1 -p 6379 -a redis123456 info

redis-cli -h y1.y1.y1.y1 -p 6379 -a redis123456 info

若结果显示此时redis主为redis master节点,redis从为redis slave节点,则说明redis主、从服务正常。

# 当redis主宕机,在redis主上执行,杀掉redis-server进程。

命令:ss -tnlp|grep 6379

kill -9 [pid]

# 在redis从上执行

命令:redis-cli -h y1.y1.y1.y1 -p 6379 -a redis123456 info

若结果如下图所示,“role:master  connected_slaves:0”,则说明redis集群状态可以切换。

第三步,测试redis主、从切换过程中数据完整性

1)redis主、从服务正常情况下,测试redis主、从集群数据同步。

# 在redis主上执行

命令:redis-cli -h x1.x1.x1.x1 -p 6379

x1.x1.x1.x1:6379> auth "redis123456"

x1.x1.x1.x1:6379> set test01 nosql

x1.x1.x1.x1:6379> keys *

"test01"

# 在redis从上执行

命令:redis-cli -h y1.y1.y1.y1 -p 6379

y1.y1.y1.y1:6379> auth "redis123456"

y1.y1.y1.y1:6379> keys *

"test01"

若redis主、从上得出的结果一致,则说明redis主、从集群数据同步正常。

2)若redis主宕机只是由于keepalived中断,redis没有中断,则需要先在redis主上中断keepalived,然后去redis从上用虚拟IP访问Redis服务。

# 在redis主上执行

命令:redis-cli -h z.z.z.z -p 6379

z.z.z.z:6379> auth "redis123456"

z.z.z.z:6379> keys *

"test01"

z.z.z.z:6379> exit

systemctl stop keepalived

# 在redis从上执行

命令:redis-cli -h z.z.z.z -p 6379

z.z.z.z:6379> auth "redis123456"

z.z.z.z:6379> keys *

"test01"

z.z.z.z:6379> set test02 nosql

z.z.z.z:6379> keys *

"test02"

"test01"

当redis主服务恢复,虚拟IP继续提供服务,同时在redis主宕机期间发生的数据不会丢失。

# 在redis主上执行

命令:systemctl start keepalived

# 在redis主上执行,查看数据是否丢失

命令:redis-cli -h z.z.z.z -p 6379

z.z.z.z:6379> auth "redis123456"

z.z.z.z:6379> keys *

"test02"

"test01"

结果显示,数据仍存在,数据完整性正常。

3)若redis主宕机是由于keepalived和redis都中断,则需要先在redis主上中断keepalived和redis,然后去redis从上用虚拟IP访问Redis服务。

# 在redis主上执行

命令:redis-cli -h z.z.z.z -p 6379

z.z.z.z:6379> auth "redis123456"

z.z.z.z:6379> keys *

"test01"

z.z.z.z:6379> exit

systemctl stop keepalived

ss -tnlp|grep 6379

kill -9 [pid]

# 在redis从上执行

命令:redis-cli -h z.z.z.z -p 6379

z.z.z.z:6379> auth "redis123456"

z.z.z.z:6379> keys *

"test01"

z.z.z.z:6379> set test02 nosql

z.z.z.z:6379> keys *

"test02"

"test01"

在redis主宕机的情况下,虚拟IP可继续提供服务且redis主数据不会丢。

当redis主服务恢复,虚拟IP继续提供服务,同时在redis主宕机期间发生的数据不会丢失。

# 在redis主上执行

命令:cd /usr/local/redis/bin

./redis-server

redis-server /usr/local/redis/etc/redis.conf

systemctl start keepalived

# 在redis主上执行,查看数据是否丢失

命令:redis-cli -h z.z.z.z -p 6379

z.z.z.z:6379> auth "redis123456"

z.z.z.z:6379> keys *

"test02"

"test01"

结果显示,数据仍存在,数据完整性正常。

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐