redis主从高可用服务

  • A+
所属分类:Linux

一.Redis主从

1.安装redis

wget http://download.redis.io/releases/redis-3.2.13.tar.gz && tar zxvf redis-3.2.13.tar.gz && cd redis-3.2.13 && make && cd src &&  make PREFIX=/data/redis_cluster/redis_6381 install && mkdir -p /data/redis_cluster/redis_6381/log

2.修改环境变量

tee -a /etc/profile <<-'EOF'
#Redis
export Redis=/data/redis_cluster/redis_6381/
export PATH=$PATH:$Redis/bin
EOF

source /etc/profile;

3.修改配置文件

Master:

tee -a /data/redis_cluster/redis_6381/6381.conf <<-'EOF'
bind 0.0.0.0
port 6381
daemonize yes
appendonly yes
appendfilename "appendonly.6381.aof"
loglevel notice
logfile "/data/redis_cluster/redis_6381/log/redis.log"
dbfilename dump.rdb
dir /data/redis_cluster/redis_6381/
stop-writes-on-bgsave-error no
masterauth 123456
requirepass 123456
slave-read-only yes
maxclients 29000
EOF

Slave:

tee -a /data/redis_cluster/redis_6381/6381.conf <<-'EOF'
bind 0.0.0.0
port 6381
daemonize yes
slaveof 172.17.1.28 6381
appendonly yes
appendfilename "appendonly.6381.aof"
loglevel notice
logfile "/data/redis_cluster/redis_6381/log/redis.log"
dbfilename dump.rdb
dir /data/redis_cluster/redis_6381/
stop-writes-on-bgsave-error no
masterauth 123456
requirepass 123456
slave-read-only yes
protected-mode no
maxclients 29000
EOF

4.启动

tee -a /data/redis_cluster/redis_6381/redis.sh <<-'EOF'
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

REDISPORT=6381
EXEC=/data/redis_cluster/redis_6381/bin/redis-server
CLIEXEC=/data/redis_cluster/redis_6381/bin/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/data/redis_cluster/redis_6381/6381.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)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                $CLIEXEC -p $REDISPORT shutdown
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac
EOF

chmod +x /data/redis_cluster/redis_6381/redis.sh

 

5.测试

redis-cli -h 172.17.1.29 -p 6381

二.主从切换

1.配置

tee -a /data/redis_cluster/redis_6381/sentinel.conf <<-'EOF'
protected-mode no
logfile "/data/redis_cluster/redis_6381/log/sentinel.log"
port 26379
daemonize yes
dir "/tmp"
sentinel monitor mymaster 172.17.1.28 6381 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 60000
sentinel auth-pass mymaster 123456
EOF

2.启动

tee -a /data/redis_cluster/redis_6381/sentinel.sh <<-'EOF'
#/bin/bash
/data/redis_cluster/redis_6381/bin/redis-sentinel /data/redis_cluster/redis_6381/sentinel.conf
EOF
chmod +x /data/redis_cluster/redis_6381/sentinel.sh

3.优化

echo 511 >> /proc/sys/net/core/somaxconn;
sysctl vm.overcommit_memory=1;
echo never > /sys/kernel/mm/transparent_hugepage/enabled

 

 

avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: