基于docker環(huán)境下如何搭建redis主從集群,針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。
我們提供的服務(wù)有:網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、勐海ssl等。為上1000+企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的勐海網(wǎng)站制作公司
筆者給大家介紹基于docker環(huán)境下搭建Redis主從復(fù)制集群,如何讓redis主從集群在docker容器上面良好運(yùn)行,容器下發(fā)生故障時(shí)又如何對(duì)redis主從進(jìn)行切換操作。
l 使用異步復(fù)制。
l 支持一主多從。一個(gè)master可以有多個(gè)slave。
l Slave可以接受來自其它slaves的連接。除了可以連接多個(gè)slaves到同一個(gè)master之外,slaves也可以連接到其它的slaves以類似級(jí)聯(lián)的方式。
l Redis復(fù)制在master端是非阻塞的。這意味著,master可以繼續(xù)處理(來自客戶端的)請(qǐng)求當(dāng)slave在執(zhí)行初次同步時(shí)。
l Redis復(fù)制可以用于擴(kuò)容,如使用多個(gè)slaves用于只讀查詢,也可以只是用于數(shù)據(jù)冗余。
l Redis復(fù)制也可能用來避免讓master把整個(gè)數(shù)據(jù)集寫入硬盤。該技術(shù)需要先配置master的redis.conf文件,然后連接一個(gè)slave,該slave必須被配置為不時(shí)地進(jìn)行保存,或啟用了AOF。
1、節(jié)省redis主從安裝與配置工作;
2、能夠以秒級(jí)速度啟動(dòng)redis容器;
3、能夠快速擴(kuò)展redis從節(jié)點(diǎn);
4、能快速進(jìn)行redis主從切換;
5、可遷移性強(qiáng)。
這里采用一主二從模式來測(cè)試
編號(hào) | 節(jié)點(diǎn)名 | IP地址 | 用途 |
1 | redis-master | 192.168.56.108 | Redis主節(jié)點(diǎn),節(jié)點(diǎn)數(shù)據(jù)可寫讀 |
2 | redis-slave01 | 192.168.56.109 | Redis從節(jié)點(diǎn)1,節(jié)點(diǎn)數(shù)據(jù)可讀不可寫 |
3 | redis-slave02 | 192.168.56.110 | Redis從節(jié)點(diǎn)2,節(jié)點(diǎn)數(shù)據(jù)可讀不可寫 |
1、Redis鏡像dockerfile腳本
FROM jaymarco/centos:7.3 MAINTAINER jaymarco ENV VERSION=3.2.5 ENV DOWN_URL=http://download.redis.io/releases/redis-${VERSION}.tar.gz \ TEMP_DIR=/tmp/redis \ DATA_DIR=/data/redis RUN mkdir -p ${TEMP_DIR} ${DATA_DIR} && \ yum install -y gcc gcc-c++ make cmake tar && \ groupadd redis && useradd -g redis -d ${DATA_DIR} -s /sbin/nologin redis && \ curl -Lk ${DOWN_URL} |tar xz -C ${TEMP_DIR} --strip-components=1 && \ cd ${TEMP_DIR} && \ make -C ${TEMP_DIR} -j $(awk '/processor/{i++}END{print i}' /proc/cpuinfo) && \ make -C ${TEMP_DIR} install && \ rm -rf ${TEMP_DIR} && \ /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime &&\ echo 'Asia/Shanghai' >/etc/timezone COPY entrypoint.sh /entrypoint.sh RUN chmod +x /entrypoint.sh VOLUME ${DATA_DIR} WORKDIR ${DATA_DIR} EXPOSE 6379/tcp ENTRYPOINT ["/entrypoint.sh"] |
2、Redis初始化啟動(dòng)腳本
構(gòu)建dockerfile時(shí)將操作系統(tǒng)參數(shù)進(jìn)行優(yōu)化。
#!/bin/bash if ! which redis-server >/dev/null 2>&1; then source /etc/profile.d/redis.sh fi set -e sysctl -w net.core.somaxconn=1024 >/dev/null 2>&1 sysctl -w vm.overcommit_memory=1 >/dev/null 2>&1 echo never > /sys/kernel/mm/transparent_hugepage/enabled echo never > /sys/kernel/mm/transparent_hugepage/defrag # first arg is `-f` or `--some-option` # or first arg is `something.conf` if [ "${1#-}" != "$1" ] || [ "${1%.conf}" != "$1" ]; then set -- redis-server "$@" fi # allow the container to be started with `--user` if [ "$1" = 'redis-server' -a "$(id -u)" = '0' ]; then chown -R redis . #exec gosu redis "$0" "$@" fi if [ "$1" = 'redis-server' ]; then doProtectedMode=1 configFile= if [ -f "$2" ]; then configFile="$2" if grep -q '^protected-mode' "$configFile"; then doProtectedMode= fi fi if [ "$doProtectedMode" ]; then shift # "redis-server" if [ "$configFile" ]; then shift fi set -- --protected-mode no "$@" if [ "$configFile" ]; then set -- "$configFile" "$@" fi set -- redis-server "$@" fi fi exec "$@" |
3、構(gòu)建redis基礎(chǔ)鏡像
在dockerfile文件同級(jí)目錄下進(jìn)行對(duì)redis鏡像封裝,最后會(huì)生成一個(gè)jaymarco/redis:3.2.5的鏡像文件。可參考下面命令構(gòu)建redis鏡像:
docker build –t jaymarco/redis: 3.2.5 . |
4、redis鏡像導(dǎo)入其它節(jié)點(diǎn)
當(dāng)Redis的docker鏡像已經(jīng)在其中一個(gè)節(jié)點(diǎn)封裝好后需要將redisr的docker鏡像同步到其它兩臺(tái)redis主機(jī)節(jié)點(diǎn)。
1、查看redis鏡像拿到鏡像ID: 04fd033441e2。 2、使用docker save命令將redis鏡像以文件形式保存到宿主機(jī)磁盤。 3、將redis325.tar文件遠(yuǎn)程拷貝到其他兩個(gè)redis節(jié)點(diǎn)上面。 scp redis325.tar 192.168.56.109:/home scp redis325.tar 192.168.56.110:/home 4、使用docker load 命令將redis325.tar導(dǎo)入docker。 |
通過以上操作已經(jīng)完成redis鏡像封裝,并將鏡像同步到其它節(jié)點(diǎn),說明redis三個(gè)節(jié)點(diǎn)上面都存在redis docker鏡像。
關(guān)于Redis主從集群關(guān)鍵在于主從之間的redis.conf配置文件,它做了主從的參數(shù)設(shè)置與性能優(yōu)化配置。前面我們已經(jīng)將redis的軟件以鏡像方式安裝在三臺(tái)節(jié)點(diǎn)上面。接下來我們通過配置進(jìn)行redis主從集群環(huán)境搭建。同時(shí)我們啟動(dòng)redis容器采用主機(jī)網(wǎng)絡(luò)模式,分別將redis容器在三臺(tái)虛擬主機(jī)上面運(yùn)行。
接下來對(duì)redis-master主節(jié)點(diǎn)進(jìn)行配置。
1、redis配置文件
bind 0.0.0.0 protected-mode yes port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize no supervised no pidfile /var/run/redis_6379.pid loglevel notice logfile "" databases 8 save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb dir /data/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 masterauth JayRedisHaZi requirepass JayRedisHaZi |
2、啟動(dòng)master redis容器
docker run -d \ --privileged --name redis-master \ --network host \ -v /etc/redis.conf:/etc/redis.conf \ -v /etc/localtime:/etc/localtime \ jaymarco/redis:3.2.5 看下面主節(jié)點(diǎn)的redis容器成功啟動(dòng) |
接下來對(duì)redis-slave01從節(jié)點(diǎn)進(jìn)行配置。
1、redis配置文件
bind 0.0.0.0 protected-mode yes port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize no supervised no pidfile /var/run/redis_6379.pid loglevel notice logfile "" databases 8 save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb dir /data/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 slaveof 192.168.56.108 6379 masterauth JayRedisHaZi requirepass JayRedisHaZi |
2 啟動(dòng)master redis容器
docker run -d \ --privileged --name redis-slave01 \ --network host \ -v /etc/redis.conf:/etc/redis.conf \ -v /etc/localtime:/etc/localtime \ jaymarco/redis:3.2.5 看下面主節(jié)點(diǎn)的redis容器成功啟動(dòng) |
接下來對(duì)redis-slave02從節(jié)點(diǎn)進(jìn)行配置。
1、redis配置文件
bind 0.0.0.0 protected-mode yes port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize no supervised no pidfile /var/run/redis_6379.pid loglevel notice logfile "" databases 8 save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb dir /data/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 slaveof 192.168.56.108 6379 masterauth JayRedisHaZi requirepass JayRedisHaZi |
2啟動(dòng)master redis容器
docker run -d \ --privileged --name redis-slave02 \ --network host \ -v /etc/redis.conf:/etc/redis.conf \ -v /etc/localtime:/etc/localtime \ jaymarco/redis:3.2.5 看下面主節(jié)點(diǎn)的redis容器成功啟動(dòng) |
通過下面命令檢查master和slave主從同步狀態(tài)
1、redis-master狀態(tài)日志 docker exec -it redis-master redis-cli -h 192.168.56.108 -a JayRedisHaZi info replication # Replication role:master connected_slaves:2 slave0:ip=192.168.56.110,port=6379,state=online,offset=99,lag=0 slave1:ip=192.168.56.109,port=6379,state=online,offset=99,lag=1 master_repl_offset:113 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:112 2、redis-slave01狀態(tài)日志 docker exec -it redis-slave01 redis-cli -h 192.168.56.109 -a JayRedisHaZi info replication # Replication role:slave master_host:192.168.56.108 master_port:6379 master_link_status:up master_last_io_seconds_ago:8 master_sync_in_progress:0 slave_repl_offset:253 slave_priority:100 slave_read_only:1 connected_slaves:0 master_repl_offset:0 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0 3、redis-slave02狀態(tài)日志 docker exec -it redis-slave02 redis-cli -h 192.168.56.110 -a JayRedisHaZi info replication # Replication role:slave master_host:192.168.56.108 master_port:6379 master_link_status:up master_last_io_seconds_ago:9 master_sync_in_progress:0 slave_repl_offset:281 slave_priority:100 slave_read_only:1 connected_slaves:0 master_repl_offset:0 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0 |
1、redis master主節(jié)點(diǎn)插入兩個(gè)值
2、redis slave01從節(jié)點(diǎn)1上面可以正常查看到數(shù)據(jù)
3、redis slave01從節(jié)點(diǎn)1上面可以正常查看到數(shù)據(jù)
redis 兩臺(tái)slave從節(jié)點(diǎn)驗(yàn)證寫入數(shù)據(jù)失敗,提示只讀模式。
接下了做一個(gè)這樣的主從切換模擬測(cè)試,將redis master主節(jié)點(diǎn)(192.168.56.108)的redis服務(wù)停個(gè),并將redis slave從節(jié)點(diǎn)1(192.168.56.109)切換成redis master主節(jié)點(diǎn)。也將原來的master主節(jié)點(diǎn)切換成slave從節(jié)點(diǎn),最后再對(duì)主從進(jìn)行驗(yàn)證。
以下是主從切換和從主切換的操作步驟:
1、模擬主節(jié)點(diǎn)宕機(jī)(192.168.56.108操作) docker stop redis-master; 2、將slave節(jié)點(diǎn)1切換成master主節(jié)點(diǎn)(192.168.56.109操作) docker exec -it redis-slave01 redis-cli -h 192.168.56.109 -a JayRedisHaZi CONFIG SET slave-read-only no docker exec -it redis-slave01 redis-cli -h 192.168.56.109 -a JayRedisHaZi SLAVEOF NO ONE 3、將slave節(jié)點(diǎn)2加入新的master節(jié)點(diǎn)(192.168.56.110操作) docker exec -it redis-slave02 redis-cli -h 192.168.56.110 -a JayRedisHaZi SLAVEOF 192.168.56.109 6379 4、將原來master主節(jié)點(diǎn)切換成slave從節(jié)點(diǎn)(192.168.56.108操作) docker exec -it redis redis-cli -h 192.168.56.108 -a JayRedisHaZi CONFIG SET slave-read-only no docker exec -it redis redis-cli -h 192.168.56.108 -a JayRedisHaZi SLAVEOF NO ONE |
從下面狀態(tài)數(shù)據(jù)來看主機(jī)192.168.56.109上面的redis容器從原來的slave節(jié)點(diǎn)變成了master節(jié)點(diǎn),而主機(jī)上面的192.168.56.108上面的redis容器從原來的master變成了slave,主從之間發(fā)生了切換,最后切換成功。
1、主機(jī)192.168.56.109 [root@dcos-redis01 redis]# docker exec -it redis-slave01 redis-cli -h 192.168.56.109 -a JayRedisHaZi info replication # Replication role:master connected_slaves:2 slave0:ip=192.168.56.110,port=6379,state=online,offset=1601,lag=1 slave1:ip=192.168.56.108,port=6379,state=online,offset=1601,lag=1 master_repl_offset:1601 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:1600 2、主機(jī)192.168.56.108 [root@docker-build-env etc]# docker exec -it redis-master redis-cli -h 192.168.56.108 -a JayRedisHaZi info replication # Replication role:slave master_host:192.168.56.109 master_port:6379 master_link_status:up master_last_io_seconds_ago:0 master_sync_in_progress:0 slave_repl_offset:1713 slave_priority:100 slave_read_only:1 connected_slaves:0 master_repl_offset:0 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0 3、主機(jī)192.168.56.110 [root@dcos-redis02 redis]# docker exec -it redis-slave02 redis-cli -h 192.168.56.110 -a JayRedisHaZi info replication # Replication role:slave master_host:192.168.56.109 master_port:6379 master_link_status:up master_last_io_seconds_ago:6 master_sync_in_progress:0 slave_repl_offset:1755 slave_priority:100 slave_read_only:1 connected_slaves:0 master_repl_offset:0 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0 |
1、檢查到192.168.56.108和192.168.56.110這兩臺(tái)slave節(jié)點(diǎn)是只讀模式
2、master節(jié)點(diǎn)192.168.56.109寫數(shù)據(jù),正常同步到兩臺(tái)slave節(jié)點(diǎn)。
關(guān)于基于docker環(huán)境下如何搭建redis主從集群?jiǎn)栴}的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。