Redis哨兵模式集羣(一主兩從)

0.說明

IP地址 主機名 別名 說明
192.168.1.117 主機
192.168.1.119 從機
192.168.1.121 從機

1.準備:

  • 添加用戶權限
# 創建一個gid爲246的redis用戶組
groupadd -g 246 redis
# 創建一個uid爲246,gid爲246的redis 用戶
useradd -u 246 -g 246 redis
passwd redis [password]
# 查看用戶uid
id redis
# 刪除用戶組
# userdel -rf redis
# 查看用戶列表
# cut -d : -f 1 /etc/passwd
  • 設置文件夾權限
chown -R redis redis-5.0.3
chgrp -R redis redis-5.0.3
  • 安裝gcc環境

由於redis是用C寫的, 所以要有gcc環境

#查看是否安裝gcc
rpm -qa | grep gcc
#若無安裝,則進行安裝
yum install gcc

2.安裝

#安裝redis
cd /opt
tar xzf redis-5.0.3.tar.gz
cd redis-5.0.3
make
#報錯則用:
make MALLOC=libc

1.配置redis.conf

主節點

vi /opt/redis-5.0.3/redis.conf
bind 0.0.0.0
# 禁止外網訪問redis,啓用,則只能夠通過lookback ip(127.0.0.1)訪問Redis
protected-mode no
# 開啓守護進程模式
daemonize yes
# 默認/var/run只有root用戶有權限寫入,所以改成/var/tmp所有用戶有權限寫入
pidfile /var/tmp/redis_6379.pid
# 日誌文件目錄
logfile "/opt/redis-5.0.3/redis_6379.log"
# 生成數據文件目錄
dir /opt/redis-5.0.3
# 設置密碼
requirepass [password]
# 主節點密碼
masterauth [password]

從節點

vi /opt/redis-5.0.3/redis.conf
bind 0.0.0.0
# 禁止外網訪問redis,啓用,則只能夠通過lookback ip(127.0.0.1)訪問Redis
protected-mode no
# 開啓守護進程模式
daemonize yes
# 默認/var/run只有root用戶有權限寫入,所以改成/var/tmp所有用戶有權限寫入
pidfile /var/tmp/redis_6379.pid
# 日誌文件目錄
logfile "/opt/redis-5.0.3/redis.log"
# 生成數據文件目錄
dir /opt/redis-5.0.3
# 設置密碼
requirepass [password]
# 主節點認證密碼
masterauth [password]
slaveof 192.168.1.117 6379

2.配置sentinel.conf

主節點和從節點

vi /opt/redis-5.0.3/sentinel.conf
daemonize yes
pidfile "/var/tmp/redis-sentinel.pid"
logfile "/opt/redis-5.0.3/redis-sentinel.log"
dir "/opt/redis-5.0.3"
# sentinel監控的master的名字叫做mymaster,地址爲192.168.1.117 6379
# 1代表,當集羣中有1個sentinel認爲master死了時,才能真正認爲該master已經不可用了
# 注:必須放在其他配置上面
sentinel monitor mymaster 192.168.1.117 6379 1
sentinel auth-pass mymaster [password]
# sentinel會向master發送心跳超時時間
sentinel down-after-milliseconds mymaster 10000
# failover主從切換時,最多可以有多少個slave同時對新的master進行同步
sentinel parallel-syncs mymaster 1
# 執行failover等待時間
sentinel failover-timeout mymaster 10000

3.設置自啓動

1.設置redis自啓動

# 複製官方自啓動腳本到系統執行目錄
cp /opt/redis-5.0.3/utils/redis_init_script /etc/init.d/redis

修改redis自啓動配置:

vi /etc/init.d/redis

配置如下:

#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

### BEGIN INIT INFO
# Provides:     redis_6379
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    Redis data structure server
# Description:          Redis data structure server. See https://redis.io
### END INIT INFO

REDISPORT=6379
# 設置執行工具路徑
EXEC=/opt/redis-5.0.3/src/redis-server
# 設置cli執行工具路徑
CLIEXEC=/opt/redis-5.0.3/src/redis-cli
# 修改pid爲其他用戶可寫目錄
PIDFILE=/var/tmp/redis_${REDISPORT}.pid
# 指定配置文件目錄
CONF="/opt/redis-5.0.3/redis.conf"

case "$1" in
    start)
        if [ -f $PIDFILE ]
        then
                echo "$PIDFILE exists, process is already running or crashed"
        else
                echo "Starting Redis server..."
                # 使用redis用戶執行
                sudo -u redis $EXEC $CONF
        fi
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                # 使用redis用戶加密碼shutdown
                sudo -u redis $CLIEXEC -a "[password]" -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

加入自啓動:

chkconfig --add redis

啓動redis服務:

# 修改配置以後只能root用戶操作,但進程屬於redis用戶
service redis start
ps aux | grep redis
service redis stop

2.設置sentinel自啓動

# 複製官方自啓動腳本到系統執行目錄
cp /opt/redis-5.0.3/utils/redis_init_script /etc/init.d/redis-sentinel

修改redis-sentinel自啓動配置:

vi /etc/init.d/redis-sentinel

配置如下:

#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

### BEGIN INIT INFO
# Provides:     redis_6379
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    Redis data structure server
# Description:          Redis data structure server. See https://redis.io
### END INIT INFO

REDISPORT=6379
# 設置哨兵執行工具路徑
EXEC=/opt/redis-5.0.3/src/redis-sentinel
# 設置cli執行工具路徑
CLIEXEC=/opt/redis-5.0.3/src/redis-cli

# 修改pid爲其他用戶可寫目錄
PIDFILE=/var/tmp/redis-sentinel.pid
# 指定配置文件目錄
CONF="/opt/redis-5.0.3/sentinel.conf"

case "$1" in
    start)
        if [ -f $PIDFILE ]
        then
                echo "$PIDFILE exists, process is already running or crashed"
        else
                echo "Starting Redis server..."
                sudo -u redis $EXEC $CONF
        fi
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                # 直接殺掉進程,刪掉進程ID文件
                kill -9 $(cat ${PIDFILE}) && rm -f ${PIDFILE}
                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

加入自啓動:

chkconfig --add redis-sentinel

啓動redis-sentinel服務

# 修改配置以後只能root用戶操作,以redis用戶啓動redis-sentinel
service redis-sentinel start
ps aux | grep redis
service redis-sentinel stop

4.查詢同步狀態

cd /opt/redis-5.0.3/src
./redis-cli -h 192.168.1.117 -p 6379
> info replication
> auth [password]
> exit

5.測試集羣

./redis-cli -h 192.168.1.117 -c -p 6379 
set hello world 

./redis-cli -h 192.168.1.119 -c -p 6379 
./redis-cli -h 192.168.1.121 -c -p 6379
get hello

6.集羣操作

# 查詢redis允許的最大連接數
config get maxclients
# 當前的redis連接數
info clients
# 獲取客戶端列表
client list
# 設置當前連接點redis的名稱
client setname [name]    
# 查看當前連接的名稱
client getname    
# 殺死指定連接
client kill ip:port    
# 查看client的IP
./redis-cli -h 192.168.1.117 -p 6379 -a [password] client list | awk '{print $2}'| cut -d = -f 2| cut -d : -f 1 | sort | uniq -c | sort -rn  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章