Redis 主從複製高可用方案

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服務進程

 

 

發佈了11 篇原創文章 · 獲贊 8 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章