环境
由于不太熟悉docker,所以,把docker当虚拟机来用,服务器环境如下:
Redis Server 环境搭建
Redis Server 01 搭建 并且制作Redis镜像
容器建立
# docker run -i -t --name redis_server_01 --net mynetwork --ip 172.30.1.21 -p 6381:6381 -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 xf redis-5.0.0.tar.gz # cd redis-5.0.0# make PREFIX=/usr/local/redis install# make install
配置redis.conf
# cp /soft/redis-5.0.0/redis.conf /redis/# mv /redis/redis.conf /redis/conf-redis-6381.conf
修改效果如下:
# cat /redis/conf-redis-6381.conf | grep -Ev "^$|^#"protected-mode yesport 6381tcp-backlog 511timeout 0tcp-keepalive 300daemonize yessupervised nopidfile /redis/pid-redis-6381.pidloglevel noticelogfile "log-redis-6381.log"databases 16always-show-logo yessave 900 1save 300 10save 60 10000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename rdb-6381.rdbdir /redismasterauth j2H2n@e6Gvrhsc1Enr4^yxf*l43*htreplica-serve-stale-data yesreplica-read-only yesrepl-diskless-sync norepl-diskless-sync-delay 5repl-disable-tcp-nodelay noreplica-priority 100requirepass j2H2n@e6Gvrhsc1Enr4^yxf*l43*htlazyfree-lazy-eviction nolazyfree-lazy-expire nolazyfree-lazy-server-del noreplica-lazy-flush noappendonly noappendfilename "appendonly.aof"appendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mbaof-load-truncated yesaof-use-rdb-preamble yeslua-time-limit 5000slowlog-log-slower-than 10000slowlog-max-len 128latency-monitor-threshold 0notify-keyspace-events ""hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-size -2list-compress-depth 0set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64hll-sparse-max-bytes 3000stream-node-max-bytes 4096stream-node-max-entries 100activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit replica 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60hz 10dynamic-hz yesaof-rewrite-incremental-fsync yesrdb-save-incremental-fsync yes#
开启服务并且制作镜像
# redis-server /redis/conf-redis-6381.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 172.30.1.22 -p 6382:6382 -v /root/redis_data:/redis redis_server_sentinel /bin/bash
配置redis.conf
# cp /redis/conf-redis-6381.conf conf-redis-6382.conf
相比于redis server 01 的配置文档,server 02的配置文档有差异的地方如下:
# cat /redis/conf-redis-6382.conf | grep '6382'port 6382pidfile /redis/pid-redis-6382.pidlogfile "log-redis-6382.log"dbfilename rdb-6382.rdb#
开启服务
# redis-server /redis/conf-redis-6382.conf
用类似的方法配置redis server 03服务器
# docker run -i -t --name redis_server_03 --net mynetwork --ip 172.30.1.23 -p 6383:6383 -v /root/redis_data:/redis redis_server_sentinel /bin/bash# cp /redis/conf-redis-6382.conf /redis/conf-redis-6383.conf# cat /redis/conf-redis-6383.conf | grep "6383"port 6383pidfile /redis/pid-redis-6383.pidlogfile "log-redis-6383.log"dbfilename rdb-6383.rdb# # redis-server /redis/conf-redis-6383.conf
Redis Server 主从复制搭建
将6382/6383 作为 6381 的从库
# docker attach redis_server_02# redis-cli -p 6382127.0.0.1:6382> AUTH j2H2n@e6Gvrhsc1Enr4^yxf*l43*htOK127.0.0.1:6382> SLAVEOF 172.30.1.21 6381OK127.0.0.1:6382> exit# docker attach redis_server_03# redis-cli -p 6383127.0.0.1:6383> AUTH j2H2n@e6Gvrhsc1Enr4^yxf*l43*htOK127.0.0.1:6383> SLAVEOF 172.30.1.21 6381OK127.0.0.1:6383> exit
验证主从复制
# docker attach redis_server_01# redis-cli -p 6381127.0.0.1:6381> AUTH j2H2n@e6Gvrhsc1Enr4^yxf*l43*htOK127.0.0.1:6381> INFO replication# Replicationrole:masterconnected_slaves:2slave0:ip=172.30.1.22,port=6382,state=online,offset=294,lag=0slave1:ip=172.30.1.23,port=6383,state=online,offset=294,lag=1master_replid:bc1ecc220927ba0d9c368a3837c5fe43e718d1b3master_replid2:0000000000000000000000000000000000000000master_repl_offset:294second_repl_offset:-1repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:1repl_backlog_histlen:294127.0.0.1:6381>
Redis Sentinel 环境搭建
Redis Sentinel 01 搭建
容器建立
# docker run -i -t --name redis_sentinel_01 --net mynetwork --ip 172.30.1.24 -p 26381:26381 -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 """ %(message),"html","utf-8") msg['Subject'] = SUBJECT msg['From'] = FROM msg['TO'] = TO server = smtplib.SMTP_SSL(HOST,'465') server.login("用户名","密码") server.sendmail(FROM,TO.split(','),msg.as_string()) server.quit def main(): message = "" for argv_text in sys.argv[1:]: message = message + argv_text SamMail(message) if __name__=="__main__": main()#测试发送# ./sentinel_mail.py 123
配置sentinel.conf
# cp /soft/redis-5.0.0/sentinel.conf /redis/conf-sentinel-26381.conf# cat conf-sentinel-26381.conf | grep -Ev "^$|^#"port 26381protected-mode nodaemonize yespidfile /redis/pid-redis-sentinel-26381.pidlogfile "/redis/log-sentinel-26381.log"dir /tmpsentinel monitor docker_sentinel 172.30.1.21 6381 2sentinel auth-pass docker_sentinel j2H2n@e6Gvrhsc1Enr4^yxf*l43*htsentinel down-after-milliseconds docker_sentinel 30000sentinel parallel-syncs docker_sentinel 1sentinel failover-timeout docker_sentinel 180000sentinel notification-script docker_sentinel /redis/sentinel_mail.pysentinel 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 172.30.1.25 -p 26382:26382 -v /root/redis_data:/redis redis_server_sentinel /bin/bash
配置文件修改
相比于conf-sentinel-26381.conf,conf-sentinel-26382.conf不同点在于# cp conf-sentinel-26381.conf conf-sentinel-26382.conf # cat conf-sentinel-26382.conf | grep '26382'port 26382pidfile "/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 172.30.1.26 -p 26383:26383 -v /root/redis_data:/redis redis_server_sentinel /bin/bash
配置文件修改
相比于conf-sentinel-26382.conf,conf-sentinel-26383.conf不同点在于# cp conf-sentinel-26382.conf conf-sentinel-26383.conf# cat conf-sentinel-26383.conf | grep 26383port 26383pidfile "/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-26381.conf # docker attach redis_sentinel_02# redis-sentinel /redis/conf-sentinel-26382.conf# docker attach redis_sentinel_03# redis-sentinel /redis/conf-sentinel-26383.conf
重启完毕后,立马接收邮件如下:
从宿主机访问redis sentinel
# cat redis_sentinel.py #!/usr/bin/env python3import redisimport redis.sentinelsentinel_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 python3import redisimport redis.sentinelimport randomfrom flask import Flaskimport loggingapp = 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_info6383# curl 127.0.0.1:9001/master_info6381#
由于是docker环境,故,域名是固定的,只需要知道端口就行了
模拟主库挂掉
关掉主库
# docker stop redis_server_01redis_server_01#
收到的邮件:
再次用脚本获取当前主库和从库:
# ./redis_sentinel.py ('172.30.1.23', 6383)[('172.30.1.22', 6382)]# curl 127.0.0.1:9001/master_info6383# curl 127.0.0.1:9001/slave_info6382#
再次将6380给开启:
# docker start redis_server_01redis_server_01# docker attach redis_server_01# redis-server /redis/conf-redis-6381.conf #
邮件如下:
再次用脚本测试如下
# ./redis_sentinel.py ('172.30.1.23', 6383)[('172.30.1.22', 6382), ('172.30.1.21', 6381)]# curl 127.0.0.1:9001/slave_info6381# curl 127.0.0.1:9001/slave_info6381# curl 127.0.0.1:9001/slave_info6382# curl 127.0.0.1:9001/master_info6383#
可见,虽然在主库挂掉的时候,大约会有30秒的切换时间,但是总的来说,还是不错的