您的位置:首页 > 数据库 > Redis

Redis 主从复制高可用方案

2017-06-14 10:12 405 查看
Redis 主从复制高可用方案
 

主节点:172.18.0.4
从节点:172.18.11.41 
注意:所有从节点的配置都一样

 

停止防火墙(有可能还需要iptables加规则)

systemctl stop firewalld.service 

 

修改内核参数

vi /etc/sysctl.conf

vm.overcommit_memory = 1

 

安装需要的系统包:

 yum -y install gcc automake autoconf libtoolmake telnet ruby-devel ruby-irb ruby-libs ruby-rdoc ruby rubygems-develrubygems

 

安装redis-3.2.8

wgethttp://download.redis.io/releases/redis-3.2.8.tar.gz

tar -zxvfredis-3.2.8.tar.gz

make

编译安装  make install PREFIX=/usr/local/redis

 

建立目录:

mkdir-p/usr/local/redis/bin

cd  /usr/local/src/redis-3.2.8/src

cp  /usr/local/src/redis-3.2.8/redis.conf  /etc

cp  /usr/local/src/redis-3.2.8/sentinel.conf /etc

cp  mkreleasdhdr.sh redis-benchmarkredis-check-aof redis-check-dump redis-cli redis-server

redis-sentinel /usr/local/redis/bin

 

修改配置文件

主:/etc/redis.conf

bind 172.18.0.4

protected-mode yes

port 6379

tcp-backlog 128

timeout 0

tcp-keepalive 300

daemonize yes

supervised no

pidfile "/var/run/redis_6379.pid"

loglevel notice

logfile"/var/log/redis/redis.log"

databases 16

save 900 1

save 300 10

save 60 10000

stop-writes-on-bgsave-error yes

rdbcompression yes

rdbchecksum yes

dbfilename "dump.rdb"

dir "/usr/local/redis"

masterauth "xxxx1812"

slave-serve-stale-data yes

slave-read-only no

repl-diskless-sync no

repl-diskless-sync-delay 5

repl-disable-tcp-nodelay no

slave-priority 100

requirepass "hjhz1812"

appendonly yes

appendfilename "appendonly.aof"

appendfsync everysec

no-appendfsync-on-rewrite no

auto-aof-rewrite-percentage 100

auto-aof-rewrite-min-size 64mb

aof-load-truncated yes

lua-time-limit 5000

slowlog-log-slower-than 10000

slowlog-max-len 128

latency-monitor-threshold 0

notify-keyspace-events ""

hash-max-ziplist-entries 512

hash-max-ziplist-value 64

list-max-ziplist-size -2

list-compress-depth 0

set-max-intset-entries 512

zset-max-ziplist-entries 128

zset-max-ziplist-value 64

hll-sparse-max-bytes 3000

activerehashing yes

client-output-buffer-limit normal 0 0 0

client-output-buffer-limit slave 256mb 64mb60

client-output-buffer-limit pubsub 32mb 8mb60

hz 10

aof-rewrite-incremental-fsync yes

 

从:/etc/redis.conf

bind 172.18.11.41

protected-mode yes

port 6381

tcp-backlog 511

timeout 0

tcp-keepalive 300

daemonize yes

supervised no

pidfile "/var/run/redis_6381.pid"

loglevel notice

logfile "/var/log/redis.log"

databases 16

save 900 1

save 300 10

save 60 10000

stop-writes-on-bgsave-error yes

rdbcompression yes

rdbchecksum yes

dbfilename "dump.rdb"

dir "/"

masterauth "hjhz1812"

slave-serve-stale-data yes

slave-read-only yes

repl-diskless-sync no

repl-diskless-sync-delay 5

repl-disable-tcp-nodelay no

slave-priority 100

requirepass "xxxx1812"

maxmemory 3000mb

appendonly yes

appendfilename "appendonly.aof"

appendfsync everysec

no-appendfsync-on-rewrite no

auto-aof-rewrite-percentage 100

auto-aof-rewrite-min-size 64mb

aof-load-truncated yes

lua-time-limit 5000

slowlog-log-slower-than 10000

slowlog-max-len 128

latency-monitor-threshold 0

notify-keyspace-events ""

hash-max-ziplist-entries 512

hash-max-ziplist-value 64

list-max-ziplist-size -2

list-compress-depth 0

set-max-intset-entries 512

zset-max-ziplist-entries 128

zset-max-ziplist-value 64

hll-sparse-max-bytes 3000

activerehashing yes

client-output-buffer-limit normal 0 0 0

client-output-buffer-limit slave 256mb 64mb60

client-output-buffer-limit pubsub 32mb 8mb60

hz 10

aof-rewrite-incremental-fsync yes

slaveof 172.18.0.4 6379

 

启动文件:

主:

#!/usr/bin/env bash

#

# redis start up the redis server daemon

#

# chkconfig: 345 99 99

# description: redis service in/etc/init.d/redis \

#             chkconfig --add redis or chkconfig--list redis \

#             service redis start  or service redis stop

# processname: redis-server

# config: /etc/redis.conf

 

PATH=/usr/local/bin:/sbin:/usr/bin:/bin

 

REDISPORT=6379

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

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

 

PIDFILE=/var/run/redis_6379.pid

CONF="/etc/redis.conf"

#make sure some dir exist

if [ ! -d /var/lib/redis ] ;then

   mkdir -p /var/lib/redis

   mkdir -p /var/log/redis

fi

 

case "$1" in

   status)

       ps -A|grep redis

       ;;

   start)

       if [ -f $PIDFILE ]

       then

                echo "$PIDFILE exists,process is already running or crashed"

       else

                echo "Starting Redisserver..."

                $EXEC $CONF

       fi

       if [ "$?"="0" ]

       then

              echo "Redis isrunning..."

       fi

       ;;

   stop)

       if [ ! -f $PIDFILE ]

       then

                echo "$PIDFILE does notexist, process is not running"

       else

                PID=$(cat $PIDFILE)

                echo "Stopping ..."

                $REDIS_CLI   -h 172.18.0.4 -p 6379 -a xxxx1812 shutdown

                while [ -x ${PIDFILE} ]

               do

                    echo "Waiting forRedis to shutdown ..."

                    sleep 1

                done

                echo "Redis stopped"

       fi

       ;;

  restart|force-reload)

       ${0} stop

       ${0} start

       ;;

  *)

   echo "Usage: /etc/init.d/redis{start|stop|restart|force-reload}" >&2

       exit 1

esac

 

从启动文件:

#!/usr/bin/env bash

#

# redis start up the redis server daemon

#

# chkconfig: 345 99 99

# description: redis service in/etc/init.d/redis \

#            chkconfig --add redis orchkconfig --list redis \

#             service redis start  or service redis stop

# processname: redis-server

# config: /etc/redis.conf

 

PATH=/usr/local/bin:/sbin:/usr/bin:/bin

 

REDISPORT=6381

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

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

 

PIDFILE=/var/run/redis_6381.pid

CONF="/etc/redis.conf"

#make sure some dir exist

if [ ! -d /var/lib/redis ] ;then

   mkdir -p /var/lib/redis

   mkdir -p /var/log/redis

fi

 

case "$1" in

   status)

       ps -A|grep redis

       ;;

   start)

       if [ -f $PIDFILE ]

       then

                echo "$PIDFILE exists,process is already running or crashed"

       else

                echo "Starting Redisserver..."

                $EXEC $CONF

        fi

       if [ "$?"="0" ]

       then

              echo "Redis isrunning..."

       fi

       ;;

   stop)

       if [ ! -f $PIDFILE ]

       then

                echo "$PIDFILE does notexist, process is not running"

       else

                PID=$(cat $PIDFILE)

                echo "Stopping ..."

                $REDIS_CLI   -h 172.18.11.41 -p 6381 -a xxxx1812 shutdown

                while [ -x ${PIDFILE} ]

               do

                    echo "Waiting forRedis to shutdown ..."

                    sleep 1

                done

                echo "Redis stopped"

       fi

       ;;

  restart|force-reload)

       ${0} stop

       ${0} start

       ;;

  *)

   echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}">&2

       exit 1

esac

 

启动停止命令:

(主从相同)

service redis start/stop/status

service sentinel stat/stop/status

 

安装sentinel  如果Master异常,则会进行Master-Slave切换,将其中一个Slave作为Master,将之前的Master作为Slave

 

Sentinel 主配置文件:

bind 172.18.0.4

protected-mode yes

daemonize yes

port 26379

dir "/tmp"

logfile"/usr/local/redis/sentinel.log"

sentinel monitor mymaster 172.18.0.4 6379 2

sentinel down-after-milliseconds mymaster15000

sentinel failover-timeout mymaster 900000

sentinel auth-pass mymaster hjhz1812

sentinel config-epoch mymaster 7

sentinel leader-epoch mymaster 7

# Generated by CONFIG REWRITE

sentinel known-slave mymaster 172.18.11.416381

sentinel current-epoch 7

 

sentinel 从配置文件:

bind 172.18.11.41

protected-mode yes

daemonize yes

port 26381

dir "/tmp"

logfile "/usr/local/redis/sentinel.log"

sentinel monitor mymaster 172.18.0.4 6379 2

sentinel down-after-milliseconds mymaster15000

sentinel failover-timeout mymaster 900000

sentinel auth-pass mymaster hjhz1812

sentinel config-epoch mymaster 7

# Generated by CONFIG REWRITE

sentinel leader-epoch mymaster 7

sentinel known-slave mymaster 172.18.11.416381

sentinel current-epoch 7

 

sentinel 主启动文件:

/etc/init.d/sentinel

 

#!/usr/bin/env bash

#

# redis start up the sentinel server daemon

#

# chkconfig: 345 98 98

# description: sentinel service in/etc/init.d/sentinel \

#             chkconfig --add sentinel orchkconfig --list sentinel \

#             service sentinel start  or service sentinel stop

# processname: sentinel-server

# config: /etc/sentinel.conf

 

PATH=/usr/local/bin:/sbin:/usr/bin:/bin

 

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

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

 

#PIDFILE=/var/run/redis_6379.pid

CONF="/etc/sentinel.conf"

a=`ps -A|grep sentinel|wc -l`

#make sure some dir exist

if [ ! -d /var/lib/redis ] ;then

   mkdir -p /var/lib/redis

   mkdir -p /var/log/redis

fi

 

case "$1" in

   status)

       ps -A|grep sentinel

       ;;

   start)

       if [ $a -gt 0 ]

       then

                echo "$PIDFILE exists,process is already running or crashed"

       else

                echo "Starting Redisserver..."

                $EXEC $CONF

       fi

       if [ "$?"="0" ]

       then

              echo "Redis isrunning..."

       fi

       ;;

   stop)

       if [ $a -eq 0 ]

       then

                echo "sentinel does not exist, process isnot running"

       else

                #PID=$(cat $PIDFILE)

                echo "Stopping ..."

                pkill sentinel

                #$REDIS_CLI   -h 172.18.0.4 -p 6379 -a xxxx1812 shutdown

                while [ -x $a ]

                do

                    echo "Waiting forRedis to shutdown ..."

                    sleep 1

                done

                echo "Redis stopped"

       fi

       ;;

  restart|force-reload)

       ${0} stop

       ${0} start

       ;;

  *)

   echo "Usage: /etc/init.d/sentinel{start|stop|restart|force-reload}" >&2

       exit 1

esac

 

sentinel 从启动文件:

/etc/init.d/sentinel

#!/usr/bin/env bash

#

# redis-sentinel start up the redis serverdaemon

#

# chkconfig: 345 98 98

# description: redis service in/etc/init.d/redis \

#             chkconfig --add sentinel orchkconfig --list sentinel \

#             service sentinel start  or service sentinel stop

# processname: sentinel-server

# config: /etc/sentinel.conf

 

PATH=/usr/local/bin:/sbin:/usr/bin:/bin

 

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

#REDIS_CLI=/usr/local/bin/redis-cli

 

#PIDFILE=/var/run/redis_6379.pid

CONF="/etc/sentinel.conf"

a=`ps -A|grep sentinel|wc -l`

#make sure some dir exist

if [ ! -d /var/lib/redis ] ;then

   mkdir -p /var/lib/redis

   mkdir -p /var/log/redis

fi

 

case "$1" in

   status)

       ps -A|grep sentinel

       ;;

   start)

       if [ $a -gt 0 ]

       then

                echo "$PIDFILE exists,process is already running or crashed"

       else

                echo "Starting Redisserver..."

                $EXEC $CONF

       fi

       if [ "$?"="0" ]

       then

              echo "Redis isrunning..."

       fi

       ;;

   stop)

       if [ $a -eq 0 ]

       then

                echo "sentinel does notexist, process is not running"

       else

                #PID=$(cat $PIDFILE)

                echo "Stopping ..."

                pkill sentinel

                #$REDIS_CLI   -h 172.18.0.4 -p 6379 -a xxxx1812 shutdown

                while [ -x $a ]

                do

                    echo "Waiting forRedis to shutdown ..."

                    sleep 1

                done

                echo "Redis stopped"

       fi

       ;;

  restart|force-reload)

       ${0} stop

       ${0} start

       ;;

  *)

   echo "Usage: /etc/init.d/sentinel{start|stop|restart|force-reload}" >&2

       exit 1

esac

 
 
注意点:

1):首次启动时,必须先启动Master

2):Sentinel 只在server 端做主从切换,
3):若Master已经被判定为下线,Sentinel已经选择了新的Master,也已经将old Master改成Slave,但是还没有将其改成new Master。若此时重启old Master,则Redis集群将处于无Master状态,此时只能手动修改配置文件,然后重新启动集群

到此Redis集群配置完毕
 

附录(说明参数):

daemonize:如需要在后台运行,把该项的值改为yes

pdifile:把pid文件放在/var/run/redis.pid,可以配置到其他地址

bind:指定redis只接收来自该IP的请求,如果不设置,那么将处理所有请求,在生产环节中最好设置该项

port:监听端口,默认为6379

timeout:设置客户端连接时的超时时间,单位为秒

loglevel:等级分为4级,debug,revbose,notice和warning。生产环境下一般开启notice

logfile:配置log文件地址,默认使用标准输出,即打印在命令行终端的端口上

database:设置数据库的个数,默认使用的数据库是0

save:设置redis进行数据库镜像的频率

rdbcompression:在进行镜像备份时,是否进行压缩

dbfilename:镜像备份文件的文件名

dir:数据库镜像备份的文件放置的路径

slaveof:设置该数据库为其他数据库的从数据库

masterauth:当主数据库连接需要密码验证时,在这里设定

requirepass:设置客户端连接后进行任何其他指定前需要使用的密码

maxclients:限制同时连接的客户端数量

maxmemory:设置redis能够使用的最大内存

appendonly:开启appendonly模式后,redis会把每一次所接收到的写操作都追加到appendonly.aof文件中,当redis重新启动时,会从该文件恢复出之前的状态

ppendfsync:设置appendonly.aof文件进行同步的频率

 

主要工具:

redis-benchmark:redis性能测试工具

redis-check-aof:检查aof日志的工具

redis-check-dump:检查rdb日志的工具

redis-cli:连接用的客户端

redis-server:redis服务进程

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: