Redis sentinel之集群搭建

时间:2021-08-17 16:32:14

环境

由于不太熟悉docker,所以,把docker当虚拟机来用,服务器环境如下:Redis sentinel之集群搭建

Redis Server 环境搭建

Redis Server 01 搭建 并且制作Redis镜像

容器建立

# docker run -i -t --name redis_server_01 --net mynetwork --ip : -v /root/redis_data:/redis centos /bin/bash

容器中必要包下载

# yum install wget gcc make vim -y

下载redis-5.0.0

# mkdir /soft
# cd /soft/
# wget http://download.redis.io/releases/redis-5.0.0.tar.gz

安装redis

# .tar.gz
# cd redis-
# make PREFIX=/usr/local/redis install
# make install

配置redis.conf

# /redis.conf /redis/
# .conf
修改效果如下: 
# .conf | grep -Ev "^$|^#"
protected-mode yes
port
tcp-backlog
timeout
tcp-keepalive
daemonize yes
supervised no
pidfile /redis/pid-redis-.pid
loglevel notice
logfile "log-redis-6381.log"
databases
always-show-logo yes
save
save
save
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename rdb-.rdb
dir /redis
masterauth j2H2n@e6Gvrhsc1Enr4^yxf*l43*ht
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-
repl-disable-tcp-nodelay no
replica-priority
requirepass j2H2n@e6Gvrhsc1Enr4^yxf*l43*ht
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-
slowlog-log-slower-than
slowlog-max-len
latency-monitor-threshold
notify-keyspace-events ""
hash-max-ziplist-entries
hash-max-ziplist-value
list-max-ziplist-size -
list-compress-depth
set-max-intset-entries
zset-max-ziplist-entries
zset-max-ziplist-value
hll-sparse-max-bytes
stream-node-max-bytes
stream-node-max-entries
activerehashing yes
client-output-buffer-limit normal
client-output-buffer-limit replica 256mb 64mb
client-output-buffer-limit pubsub 32mb 8mb
hz
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
# 

开启服务并且制作镜像

# redis-server /redis/conf-redis-.conf
退出容器制作镜像
Ctrl + P + Q 可以退出容器而使之不停止
寻找CONTAINER ID并且制作为镜像
# docker ps | grep redis_server_01
制作镜像 
# docker commit 1ce7c0cf1364 redis_server_sentinel
查看镜像
# docker images | grep redis_server_sentinel

Redis Server 02 / Redis Server 03搭建

容器建立

# docker run -i -t --name redis_server_02 --net mynetwork --ip : -v /root/redis_data:/redis redis_server_sentinel /bin/bash

配置redis.conf

# .conf conf-redis-.conf
相比于redis server 01 的配置文档,server 02的配置文档有差异的地方如下:
# .conf | '
port
pidfile /redis/pid-redis-.pid
logfile "log-redis-6382.log"
dbfilename rdb-.rdb
# 

开启服务

# redis-server /redis/conf-redis-.conf

用类似的方法配置redis server 03服务器

# docker run -i -t --name redis_server_03 --net mynetwork --ip : -v /root/redis_data:/redis redis_server_sentinel /bin/bash

# .conf /redis/conf-redis-.conf

# .conf | "
port
pidfile /redis/pid-redis-.pid
logfile "log-redis-6383.log"
dbfilename rdb-.rdb
# 

# redis-server /redis/conf-redis-.conf

Redis Server 主从复制搭建

将6382/6383 作为 6381 的从库
# docker attach redis_server_02
# redis-cli -p
> AUTH j2H2n@e6Gvrhsc1Enr4^yxf*l43*ht
OK
> SLAVEOF
OK
> exit

# docker attach redis_server_03
# redis-cli -p
> AUTH j2H2n@e6Gvrhsc1Enr4^yxf*l43*ht
OK
> SLAVEOF
OK
> exit
验证主从复制
# docker attach redis_server_01
# redis-cli -p
> AUTH j2H2n@e6Gvrhsc1Enr4^yxf*l43*ht
OK
> INFO replication
# Replication
role:master
connected_slaves:
slave0:ip=,state=online,offset=,lag=
slave1:ip=,state=online,offset=,lag=
master_replid:bc1ecc220927ba0d9c368a3837c5fe43e718d1b3
master_replid2:
master_repl_offset:
second_repl_offset:-
repl_backlog_active:
repl_backlog_size:
repl_backlog_first_byte_offset:
repl_backlog_histlen:
> 

Redis Sentinel 环境搭建

Redis Sentinel 01 搭建

容器建立

# docker run -i -t --name redis_sentinel_01 --net mynetwork --ip : -v /root/redis_data:/redis redis_server_sentinel /bin/bash

定义邮件发送

# mkdir /redis
# cd /redis
# yum install python

定义python发送脚本
# cat sentinel_mail.py
    #!/usr/bin/env python
    import sys
    import smtplib
    import email.mime.text

    def SamMail(message):
        HOST = "SMTP服务器"
        SUBJECT = 'redis_docker_sentinel'
        TO = "发送给谁"
        FROM = "谁发送的"

        msg = email.mime.text.MIMEText(
        """
            %s
        ")

        msg['Subject'] = SUBJECT
        msg['From'] = FROM
        msg['TO'] = TO

        server = smtplib.SMTP_SSL(HOST,')
        server.login("用户名","密码")
        server.sendmail(FROM,TO.split(','),msg.as_string())
        server.quit

    def main():
        message = ""
        :]:
            message = message + argv_text
        SamMail(message)

    if __name__=="__main__":
        main()
#
测试发送
# ./sentinel_mail.py 

Redis sentinel之集群搭建

配置sentinel.conf

# /sentinel.conf /redis/conf-sentinel-.conf
# .conf | grep -Ev "^$|^#"
port
protected-mode no
daemonize yes
pidfile /redis/pid-redis-sentinel-.pid
logfile "/redis/log-sentinel-26381.log"
dir /tmp
sentinel monitor docker_sentinel
sentinel auth-pass docker_sentinel j2H2n@e6Gvrhsc1Enr4^yxf*l43*ht
sentinel down-after-milliseconds docker_sentinel
sentinel parallel-syncs docker_sentinel
sentinel failover-timeout docker_sentinel
sentinel notification-script docker_sentinel /redis/sentinel_mail.py
sentinel deny-scripts-reconfig yes
# 

制作镜像

# docker commit 5d61d56f35f4 redis_sentinel_server

Redis Sentinel 02 / Redis Sentinel 03 搭建

redis sentinel 2 容器建立

# docker run -i -t --name redis_sentinel_02 --net mynetwork --ip : -v /root/redis_data:/redis redis_server_sentinel /bin/bash

配置文件修改

相比于conf-sentinel-.conf,conf-sentinel-.conf不同点在于
# .conf conf-sentinel-.conf
# .conf | '
port
pidfile "/redis/pid-redis-sentinel-26382.pid"
logfile "/redis/log-sentinel-26382.log"
# 

redis sentinel 3 容器建立

# docker run -i -t --name redis_sentinel_03 --net mynetwork --ip : -v /root/redis_data:/redis redis_server_sentinel /bin/bash

配置文件修改

相比于conf-sentinel-.conf,conf-sentinel-.conf不同点在于
# .conf conf-sentinel-.conf
# .conf |
port
pidfile "/redis/pid-redis-sentinel-26383.pid"
logfile "/redis/log-sentinel-26383.log"
# 

依次启动26381,26382,26383 sentinel

# docker attach redis_sentinel_01
# redis-sentinel /redis/conf-sentinel-.conf
# docker attach redis_sentinel_02
# redis-sentinel /redis/conf-sentinel-.conf
# docker attach redis_sentinel_03
# redis-sentinel /redis/conf-sentinel-.conf 
重启完毕后,立马接收邮件如下:

 Redis sentinel之集群搭建

从宿主机访问redis sentinel

# cat redis_sentinel.py
#!/usr/bin/env python3

import redis
import redis.sentinel

sentinel_list = redis.sentinel.Sentinel([
('172.30.1.24',26381),
('172.30.1.25',26382),
('172.30.1.26',26383),
])

#获取主服务器信息
get_master_info = sentinel_list.discover_master('docker_sentinel')
print (get_master_info)

#获取从服务器信息
get_slave_info = sentinel_list.discover_slaves('docker_sentinel')
print (get_slave_info)

# 

执行如下

# ./redis_sentinel.py
('172.30.1.21', 6381)
[('172.30.1.22', 6382), ('172.30.1.23', 6383)]
# 

模拟灾难切换

模拟脚本

在宿主机上定义python flask 脚本,用于获取主库和从库IP

#!/usr/bin/env python3

import redis
import redis.sentinel
import random
from flask import Flask
import logging

app = Flask(__name__)

@app.route('/master_info')
def get_redis_master_info() :
        sentinel_list = redis.sentinel.Sentinel([
                ('172.30.1.24',26381),
                ('172.30.1.24',26382),
                ('172.30.1.24',26383),
        ])

        get_master_info = sentinel_list.discover_master('docker_sentinel')
        return str(get_master_info[1])

@app.route('/slave_info')
def get_redis_slave_info() :
        sentinel_list = redis.sentinel.Sentinel([
                ('172.30.1.24',26381),
                ('172.30.1.24',26382),
                ('172.30.1.24',26383),
        ])

        get_slave_info = sentinel_list.discover_slaves('docker_sentinel')
        return_get_slave_info = get_slave_info[random.randint(0,len(get_slave_info)-1)]

        return str(return_get_slave_info[1])

@app.route('/')
def index():
        return 'hello world'

if __name__ == '__main__':
        app.debug = True
        app.run(host='0.0.0.0',port=9001)

脚本获取的效果如下:

# curl 127.0.0.1:9001/slave_info
6383
# curl 127.0.0.1:9001/master_info
6381
# 

由于是docker环境,故,域名是固定的,只需要知道端口就行了

模拟主库挂掉

关掉主库

# docker stop redis_server_01
redis_server_01
# 

收到的邮件:

Redis sentinel之集群搭建

再次用脚本获取当前主库和从库:

# ./redis_sentinel.py
()
[()]
# curl /master_info

# curl /slave_info

# 

再次将6380给开启:

# docker start redis_server_01
redis_server_01
# docker attach redis_server_01
# redis-server /redis/conf-redis-.conf
# 

邮件如下:

Redis sentinel之集群搭建

再次用脚本测试如下

# ./redis_sentinel.py
()
[(), ()]
# curl /slave_info

# curl /slave_info

# curl /slave_info

# curl /master_info

# 

可见,虽然在主库挂掉的时候,大约会有30秒的切换时间,但是总的来说,还是不错的