redis主從+哨兵搭建的實(shí)現(xiàn)示例
本文主要介紹了redis主從+哨兵搭建的實(shí)現(xiàn)示例,具體如下:
1. 工程準(zhǔn)備
將安裝包、配置文件、啟動(dòng)腳本統(tǒng)一放置到redis-m-s工程中。
2. 基于Dockerfile構(gòu)建
2.1 拷貝工程到工作目錄
# 設(shè)置工作目錄 WORKDIR ${WORK_DIR} # 文件拷貝:將所有本地文件拷貝到容器目錄 COPY ./redis-m-s ${WORK_DIR}/redis-m-s
2.2 安裝依賴包
RUN yum -y install make gcc gcc-c++
2.3 解壓redis安裝包、編譯、安裝
RUN tar -xzvf ${WORK_DIR}/redis-m-s/app/redis-${REDIS_VERSION}.tar.gz && \ cd redis-${REDIS_VERSION} && \ make && make PREFIX=${SRC_APP_DIR}/redis install && \
2.4 拷貝配置文件到安裝目錄
拷貝配置文件和啟動(dòng)腳本
mkdir -p ${SRC_APP_DIR}/redis/bin/ && \ mkdir -p ${SRC_APP_DIR}/redis/conf/ && \ cp ${WORK_DIR}/redis-m-s/conf/* ${SRC_APP_DIR}/redis/conf && \ cp ${WORK_DIR}/redis-m-s/bin/* ${SRC_APP_DIR}/redis/bin && \ chmod +x ${SRC_APP_DIR}/redis/bin/start_redis.sh && \
清理安裝包
cd ${WORK_DIR} && \ rm -rf redis-${REDIS_VERSION}
3. 配置文件
3.1 redis-master.conf
基于安裝目錄下redis.conf進(jìn)行修改,主要修改項(xiàng):
bind 0.0.0.0 protected-mode no logfile /export/Logs/redis/master/redis.log dir /export/Data/redis/db requirepass your_pass
3.2 redis-slave.conf
基于安裝目錄下redis.conf進(jìn)行修改,主要修改項(xiàng)如下,注意將master_ip替換為實(shí)際IP
bind 0.0.0.0 protected-mode no logfile /export/Logs/redis/slave/redis.log dir /export/Data/redis/db replicaof master_ip 6379 masterauth your_pass requirepass your_pass
3.3 sentinel-master.conf
基于安裝目錄下sentinel.conf進(jìn)行修改,主要修改項(xiàng)如下,注意將master_ip替換為實(shí)際IP
logfile /export/Logs/redis/sentinel/sentinel.log sentinel monitor redis-master master-ip 6379 1 sentinel auth-pass redis-master your_pass sentinel down-after-milliseconds redis-master 30000 sentinel parallel-syncs redis-master 1 sentinel failover-timeout redis-master 180000 SENTINEL master-reboot-down-after-period redis-master 0 # 容器部署時(shí)需聲明 sentinel announce-ip master_ip sentinel announce-port 26379
3.4 sentinel-slave.conf
基于安裝目錄下sentinel.conf進(jìn)行修改,主要修改項(xiàng)如下,注意將master_ip替換為實(shí)際IP
logfile /export/Logs/redis/sentinel/sentinel.log sentinel monitor redis-master master-ip 6379 1 sentinel auth-pass redis-master your_pass sentinel down-after-milliseconds redis-master 30000 sentinel parallel-syncs redis-master 1 sentinel failover-timeout redis-master 180000 SENTINEL master-reboot-down-after-period redis-master 0 # 容器部署時(shí)需聲明 sentinel announce-ip slave_ip sentinel announce-port 26379
4. 啟動(dòng)redis
4.1 redis主節(jié)點(diǎn)+哨兵啟動(dòng)
redis-server $REDIS_HOME/conf/redis-master.conf & redis-sentinel $REDIS_HOME/conf/sentinel-master.conf &
4.2 redis從節(jié)點(diǎn)+哨兵啟動(dòng)
redis-server $REDIS_HOME/conf/redis-slave.conf & redis-sentinel $REDIS_HOME/conf/sentinel-slave.conf &
5. 驗(yàn)證redis
5.1 登錄主節(jié)點(diǎn)
下述 slave_ip 為從節(jié)點(diǎn)實(shí)際IP
# ./redis-cli > auth your_pass OK > info replication # Replication role:master connected_slaves:1 slave0:ip=slave_ip,port=6379,state=online,offset=28160117,lag=1 master_failover_state:no-failover master_replid:201648fb313a4359fea11e57c434afe8f372316d master_replid2:0000000000000000000000000000000000000000 master_repl_offset:28160410 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:27103441 repl_backlog_histlen:1056970 >
5.2 登錄哨兵
下述 master_ip 為主節(jié)點(diǎn)IP,slave_ip為從節(jié)點(diǎn)IP
./redis-cli -h master_ip -p 26379 > sentinel sentinels redis-master 1) 1) "name" 2) "a23117a8d41fb1ace5785a5b940e7e43e5efe316" 3) "ip" 4) "slave_ip" 5) "port" 6) "26379" 7) "runid" 8) "a23117a8d41fb1ace5785a5b940e7e43e5efe316" 9) "flags" 10) "sentinel" 11) "link-pending-commands" 12) "0" 13) "link-refcount" 14) "1" 15) "last-ping-sent" 16) "0" 17) "last-ok-ping-reply" 18) "988" 19) "last-ping-reply" 20) "988" 21) "down-after-milliseconds" 22) "30000" 23) "last-hello-message" 24) "49" 25) "voted-leader" 26) "?" 27) "voted-leader-epoch" 28) "0" >
6. springboot應(yīng)用集成redis
6.1 配置文件
spring: redis: sentinel: master: redis-master nodes: - redis://master_ip:26379 - redis://slave_ip:26379 password: your_pass
6.2 啟動(dòng)應(yīng)用
啟動(dòng)應(yīng)用時(shí)日志打印日志
: master: redis://master_ip:6379 added : slave: redis://slave_ip:6379 added : sentinel: redis://slave_ip:26379 added : sentinel: redis://master_ip:26379 added
到此這篇關(guān)于redis主從+哨兵搭建的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)redis主從+哨兵搭建內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Redis特殊數(shù)據(jù)類型Geospatial地理空間
這篇文章主要為大家介紹了Redis特殊數(shù)據(jù)類型Geospatial地理空間,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05redis 替代php文件存儲(chǔ)session的實(shí)例
這篇文章主要介紹了redis 替代php文件存儲(chǔ)session的實(shí)例的相關(guān)資料,希望通過本文能幫助到大家,讓大家掌握這樣的方法,需要的朋友可以參考下2017-10-10