【redis】CentOS7.x 上redis 一主两从三哨兵模式的安装配置

一、实验环境

操作系统: CentOS7.5 Minimal

redis01  192.168.1.103

redis02 192.168.1.105

redis03 192.168.1.106

 

二、设置selinux和防火墙

 

在redis01 redis02 reids03 服务器

设置selinux为宽松模式

# setenforce 0

# sed  -i  's/^SELINUX=.*/SELINUX=permissive/g'  /etc/selinux/config

 

# sysctl  -w net.core.somaxconn=1024

# echo "net.core.somaxconn = 1024"  >> /etc/sysctl.conf

# sysctl -p

 

开放相关端口

#  firewall-cmd --zone=public  --add-port=6379/tcp --permanent

#  firewall-cmd --zone=public  --add-port=26379/tcp --permanent

#  firewall-cmd --reload

 

安装redis依赖

#  yum  info  jemalloc

#  yum -y install jemalloc

【redis】CentOS7.x 上redis 一主两从三哨兵模式的安装配置_第1张图片

 

创建redis用户

# useradd  -s /sbin/nologin  -d  /var/lib/redis/  -c "Redis Database Server"    redis

# cat  /etc/passwd

 

【redis】CentOS7.x 上redis 一主两从三哨兵模式的安装配置_第2张图片

 

创建相关服务目录

# mkdir /etc/redis

# mkdir  /var/lib/redis

# mkdir /var/lib/sentinel

# mkdir /var/log/redis

 

三、编译安装redis

 

在redis01服务器

安装编译工具

#  yum  -y install  gcc  make

下载安装包

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

编译安装redis

#  tar -zxf  redis-4.0.10.tar.gz

#  cd  redis-4.0.10

#  make

#  make    PREFIX=/usr/local/redis    install

# ll /usr/local

【redis】CentOS7.x 上redis 一主两从三哨兵模式的安装配置_第3张图片

 

拷贝编译安装包到 redis02 redis03服务器

# scp  -r  /usr/local/redis  [email protected]:/usr/local

# scp  -r  /usr/local/redis  [email protected]:/usr/local

 

四、安装配置redis

 

在redis01服务器

# cat /etc/redis/redis.conf  | grep -Ev "^#|^$"

##########################################

bind 192.168.1.103

protected-mode yes

port 6379

tcp-backlog 511

timeout 0

tcp-keepalive 300

daemonize no

supervised no

pidfile /var/run/redis.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 /var/lib/redis

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

appendonly no

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 64mb 60

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

hz 10

aof-rewrite-incremental-fsync yes

requirepass "123456"

masterauth  "123456"

###################################################

 

在redis02 redis03服务器

 

# cat /etc/redis/redis.conf | grep -Ev "^#|^$"

##########################################

bind 192.168.1.103

protected-mode yes

port 6379

tcp-backlog 511

timeout 0

tcp-keepalive 300

daemonize no

supervised no

pidfile /var/run/redis.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 /var/lib/redis

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

appendonly no

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 64mb 60

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

hz 10

aof-rewrite-incremental-fsync yes

requirepass "123456"

masterauth  "123456"

slaveof 192.168.1.103 6379

###################################################

注: 多了最后一行主从的配置信息

 

在redis01 redis02 redis03 服务器

# vim /usr/local/redis/bin/redis-shutdown

#####################################################

#!/bin/bash

#

# Wrapper to close properly redis and sentinel

test x"$REDIS_DEBUG" != x && set -x

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

# Retrieve service name

SERVICE_NAME="$1"

if [ -z "$SERVICE_NAME" ]; then

  SERVICE_NAME=redis

fi

# Get the proper config file based on service name

CONFIG_FILE="/etc/redis/$SERVICE_NAME.conf"

# Use awk to retrieve host, port from config file

HOST=`awk '/^[[:blank:]]*bind/ { print $2 }' $CONFIG_FILE | tail -n1`

PORT=`awk '/^[[:blank:]]*port/ { print $2 }' $CONFIG_FILE | tail -n1`

PASS=`awk '/^[[:blank:]]*requirepass/ { print $2 }' $CONFIG_FILE | tail -n1`

SOCK=`awk '/^[[:blank:]]*unixsocket\s/ { print $2 }' $CONFIG_FILE | tail -n1`

# Just in case, use default host, port

HOST=${HOST:-127.0.0.1}

if [ "$SERVICE_NAME" = redis ]; then

    PORT=${PORT:-6379}

else

    PORT=${PORT:-26739}

fi

# Setup additional parameters

# e.g password-protected redis instances

[ -z "$PASS"  ] || ADDITIONAL_PARAMS="-a $PASS"

# shutdown the service properly

if [ -e "$SOCK" ] ; then

        $REDIS_CLI -s $SOCK $ADDITIONAL_PARAMS shutdown

else

        $REDIS_CLI -h $HOST -p $PORT $ADDITIONAL_PARAMS shutdown

fi

##############################################################

【redis】CentOS7.x 上redis 一主两从三哨兵模式的安装配置_第4张图片

 

创建redis服务器unit文件

# vim  /etc/systemd/system/redis.service

################################################################

[Unit]

Description=Redis persistent key-value database

After=network.target

After=network-online.target

Wants=network-online.target

[Service]

ExecStart=/usr/local/redis/bin/redis-server /etc/redis/redis.conf --supervised systemd

ExecStop=/usr/local/redis/bin/redis-shutdown

Type=notify

User=redis

Group=redis

RuntimeDirectory=redis

RuntimeDirectoryMode=0755

[Install]

WantedBy=multi-user.target

########################################################################

#  systemctl  daemon-reload

 

更改相关目录属主属组为redis

# chown -R redis:redis  /etc/redis

# chown -R redis:redis  /var/lib/redis

# chown -R redis:redis  /var/lib/sentinel

# chown -R  redis:redis  /var/log/redis

 

分别启动resdis01 redis02 redis03 服务器上的redis服务

# systemctl  start redis

# systemctl enable redis

# systemctl status redis 

 

【redis】CentOS7.x 上redis 一主两从三哨兵模式的安装配置_第5张图片

 

# ss -tan  | grep  6379

# tail  /var/log/redis/redis.log

# ps aux | grep redis-server

【redis】CentOS7.x 上redis 一主两从三哨兵模式的安装配置_第6张图片

 

分别在resdis01 redis02 redis03查看redis主从信息

# redis-cli  -h 192.168.1.103 -p 6379 -a "123456"  info replication

【redis】CentOS7.x 上redis 一主两从三哨兵模式的安装配置_第7张图片

 

# redis-cli  -h 192.168.1.105 -p 6379 -a "123456"  info replication

 

【redis】CentOS7.x 上redis 一主两从三哨兵模式的安装配置_第8张图片

 

# redis-cli  -h 192.168.1.106 -p 6379 -a "123456"  info replication

【redis】CentOS7.x 上redis 一主两从三哨兵模式的安装配置_第9张图片

可以看出,redis01为主,redis02 redis03 为从,一主量从的redis安装完成!

 

五、安装配置redis-sentinel

在resdis01 redis02 redis03服务器

创建redis配置文件

# vim /etc/redis/redis-sentinel.conf

###############################################

daemonize no

port 26379

protected-mode no

dir "/var/lib/sentinel"

sentinel monitor redis-master 192.168.1.103 6379 2

sentinel auth-pass redis-master 123456

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

####################################################

 

创建redis-sentinel Unit文件

#

############################################################

[Unit]

Description=Sentinel for Redis

After=network.target

[Service]

LimitNOFILE=64000

User=redis

Group=redis

ExecStart=/usr/local/bin/redis-sentinel /etc/redis/redis-sentinel.conf --supervised systemd

[Install]

WantedBy=multi-user.target

################################################################

 

【redis】CentOS7.x 上redis 一主两从三哨兵模式的安装配置_第10张图片

# systemctl daemon-reload

 

更改相关目录属主属组为redis

# chown -R redis:redis  /etc/redis

# chown -R redis:redis  /var/lib/redis

# chown -R redis:redis  /var/lib/sentinel

# chown -R  redis:redis /var/log/redis

 

分别在resdis01 redis02 redis03服务器上启动redis-sentinel服务

# systemctl  start  redis-sentinel

# systemctl  enable  redis-sentinel

# systemctl  status  redis-sentinel

【redis】CentOS7.x 上redis 一主两从三哨兵模式的安装配置_第11张图片

 

# tail /var/log/redis/sentinel.log

 

【redis】CentOS7.x 上redis 一主两从三哨兵模式的安装配置_第12张图片

 

分别在resdis01 redis02 redis03服务器上查看redis和redis-sentinel进程、端口监听

 

# ps  aux | grep redis

# ss -tan

【redis】CentOS7.x 上redis 一主两从三哨兵模式的安装配置_第13张图片

【redis】CentOS7.x 上redis 一主两从三哨兵模式的安装配置_第14张图片

六、测试redis-sentinel 主节点调度

现在redis主节点为redis01(192.168.1.103),我们尝试将redis01服务器关机,验证redis-sentinel 集群能否调度主节点

 

在redis01服务器

# reboot 

 

在redis02服务器

# redis-cli  -h 192.168.1.105 -p 6379 -a "123456"  info replication

 

【redis】CentOS7.x 上redis 一主两从三哨兵模式的安装配置_第15张图片

# tail  /var/log/redis/sentinel.log

 

【redis】CentOS7.x 上redis 一主两从三哨兵模式的安装配置_第16张图片

 

在redis03服务器

# redis-cli  -h 192.168.1.106 -p 6379 -a "123456"  info replication

 

【redis】CentOS7.x 上redis 一主两从三哨兵模式的安装配置_第17张图片

# tail /var/log/redis/sentinel.log

 

【redis】CentOS7.x 上redis 一主两从三哨兵模式的安装配置_第18张图片

 

将redis01 开机

# systemctl  status redis redis-sentinel

# redis-cli  -h 192.168.1.103 -p 6379 -a "123456"  info replication

# tail  /var/log/redis/sentinel.log

 

【redis】CentOS7.x 上redis 一主两从三哨兵模式的安装配置_第19张图片

 

【redis】CentOS7.x 上redis 一主两从三哨兵模式的安装配置_第20张图片

【redis】CentOS7.x 上redis 一主两从三哨兵模式的安装配置_第21张图片

可以看到,redis01重启后,变成了从节点,redis03被推选为了主节点!

 

经历一次主节点的切换后,分别看看redis-sentinel 配置文件的变化

 

【redis】CentOS7.x 上redis 一主两从三哨兵模式的安装配置_第22张图片

【redis】CentOS7.x 上redis 一主两从三哨兵模式的安装配置_第23张图片

【redis】CentOS7.x 上redis 一主两从三哨兵模式的安装配置_第24张图片

 

七、参考

 

编译安装redis

https://www.cnblogs.com/JiangLe/p/5878160.html

 

sentinel.conf

http://download.redis.io/redis-stable/sentinel.conf

 

redis sentinel配置

https://blog.51cto.com/tianshili/1759289

 

redis-sentinel.service

https://github.com/Scifabric/pybossa/blob/master/contrib/redis/redis-sentinel.service

 

Redis自动启动脚本

https://segmentfault.com/a/1190000002488819

 

Redis Sentinel 启动脚本

https://www.diguage.com/archives/139.html

 

Redis 高可用部署方案

https://hoxis.github.io/redis-sentinel-ha.html

 

CentOS 7上 Redis sentinel 开启自启动设置

https://blog.yowko.com/centos7-redis-sentinel-service

 

Setting up Redis Sentinel as a service in Ubuntu

http://www.adfrad.com/2017/06/setting-up-redis-sentinel-as-service-in.html

 

redis:详解三种集群策略

https://blog.csdn.net/q649381130/article/details/79931791

 

redis经典三节点高可用哨兵模式集群搭建

https://blog.csdn.net/zhangcongyi420/article/details/84191873

 

基于Sentinel(哨兵)搭建实现Redis高可用集群

https://blog.csdn.net/pengjunlee/article/details/81429119

 

Redis 高可用部署方案

https://hoxis.github.io/redis-sentinel-ha.html

 

Redis Sentinel实现哨兵模式搭建小结

https://www.jb51.net/article/153583.htm

 

shell脚本一键安装redis集群

https://www.cnblogs.com/chenglee/p/10085792.html

 

Redis 中哨兵sentinel 机制、从宕机及恢复、主库宕机及恢复解决方案

https://blog.csdn.net/py_tamir/article/details/82555338

 

WARNING: The TCP backlog setting of 511.解决

https://www.cnblogs.com/faunjoe88/p/7158484.html

 

Redis not starting with systemctl

https://www.ringingliberty.com/2018/01/19/redis-not-starting-with-systemctl

你可能感兴趣的:(redis)