使用Docker配置redis sentinel哨兵的方法步驟
本文演示一主二從。
先說一下遇到的問題。我看網(wǎng)上說想配置哨兵,必須讓啟動(dòng)redis的docker網(wǎng)絡(luò)模式為host,否則無法訪問到從還是什么的。我指定--network host
后無法外網(wǎng)訪問,redis desktop manager連不上redis了,后來發(fā)現(xiàn)我這個(gè)新克隆的機(jī)器沒關(guān)防火墻,關(guān)上就好了。
1.配置主從
docker pull一下redis
master
mkdir -p /mydata/redis/6379/conf mkdir -p /mydata/redis/6379/data touch /mydata/redis/6379/conf/redis.conf echo "appendonly yes" >> /mydata/redis/6379/conf/redis.conf docker run --network host --name redis6379 -v /mydata/redis/6379/data:/data -v /mydata/redis/6379/conf/redis.conf:/etc/redis/redis.conf -d redis redis-server /etc/redis/redis.conf --port 6379
slave
#slave1 mkdir -p /mydata/redis/6380/conf mkdir -p /mydata/redis/6380/data touch /mydata/redis/6380/conf/redis.conf echo "appendonly yes" >> /mydata/redis/6380/conf/redis.conf docker run --network host --name redis6380 -v /mydata/redis/6380/data:/data -v /mydata/redis/6380/conf/redis.conf:/etc/redis/redis.conf -d redis redis-server /etc/redis/redis.conf --port 6380 --slaveof 192.168.243.4 6379 #slave2 mkdir -p /mydata/redis/6381/conf mkdir -p /mydata/redis/6381/data touch /mydata/redis/6381/conf/redis.conf echo "appendonly yes" >> /mydata/redis/6381/conf/redis.conf docker run --network host --name redis6381 -v /mydata/redis/6381/data:/data -v /mydata/redis/6381/conf/redis.conf:/etc/redis/redis.conf -d redis redis-server /etc/redis/redis.conf --port 6381 --slaveof 192.168.243.4 6379
查看配置:
#主 docker exec -it redis6379 redis-cli >info replication #從 docker exec -it redis6380 redis-cli -p 6380 >info replication
2. 配置哨兵
master
mkdir /mydata/redis/6379/sentinel mkdir /mydata/redis/6379/sentinel/log vi /mydata/redis/6379/sentinel/sentinel.conf #輸入 port 26379 dir "/var/log/sentinel" logfile "/var/log/sentinel/26379.log" sentinel monitor mymaster 192.168.243.4 6379 1 #這里暫時(shí)設(shè)置成1 #方便看一下主掛掉是什么效果 sentinel down-after-milliseconds mymaster 5000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 180000 #end docker run -d --name sentinel26379 -v /mydata/redis/6379/sentinel/sentinel.conf:/conf/sentinel.conf -v /mydata/redis/6379/sentinel/log:/var/log/sentinel --network host redis redis-sentinel /conf/sentinel.conf
先進(jìn)行測試:
docker exec -it redis6379 redis-cli > SHUTDOWN > exit cat /mydata/redis/6379/sentinel/log/26379.log
此時(shí)redis6381已成為新的master
再啟動(dòng)docker start redis6379
6379是slave了
測試成功,修改上面配置文件的sentinel monitor mymaster 192.168.243.4 6379 2
數(shù)字配置為2,代表至少有2個(gè)Sentinel節(jié)點(diǎn)認(rèn)為主節(jié)點(diǎn)不可達(dá),那么這個(gè)不可達(dá)的判定才是客觀的。為了防止票數(shù)相同,sentinel啟動(dòng)奇數(shù)個(gè)。
slave
#slave1 mkdir /mydata/redis/6380/sentinel mkdir /mydata/redis/6380/sentinel/log vi /mydata/redis/6380/sentinel/sentinel.conf #輸入 port 26380 dir "/var/log/sentinel" logfile "/var/log/sentinel/26380.log" sentinel monitor mymaster 192.168.243.4 6380 2 sentinel down-after-milliseconds mymaster 5000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 180000 #end docker run -d --name sentinel26380 -v /mydata/redis/6380/sentinel/sentinel.conf:/conf/sentinel.conf -v /mydata/redis/6380/sentinel/log:/var/log/sentinel --network host redis redis-sentinel /conf/sentinel.conf #slave2 mkdir /mydata/redis/6381/sentinel mkdir /mydata/redis/6381/sentinel/log vi /mydata/redis/6381/sentinel/sentinel.conf #輸入 port 26381 dir "/var/log/sentinel" logfile "/var/log/sentinel/26381.log" sentinel monitor mymaster 192.168.243.4 6381 2 sentinel down-after-milliseconds mymaster 5000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 180000 #end docker run -d --name sentinel26381 -v /mydata/redis/6381/sentinel/sentinel.conf:/conf/sentinel.conf -v /mydata/redis/6381/sentinel/log:/var/log/sentinel --network host redis redis-sentinel /conf/sentinel.conf
3.SpringBoot連接
application.yml
spring: redis: timeout: 5000 sentinel: master: mymaster nodes: 192.168.243.4:26379,192.168.243.4:26380,192.168.243.4:26381
controller
@Autowired private StringRedisTemplate redisTemplate; @RequestMapping("/redis") public String redis() { redisTemplate.opsForValue().set("test", "121323123"); String test = redisTemplate.opsForValue().get("test"); return "RESULT: " + test; }
訪問localhost:8080/redis
到此這篇關(guān)于使用Docker配置redis sentinel哨兵的方法步驟的文章就介紹到這了,更多相關(guān)Docker配置redis sentinel哨兵內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。