1. Redis 이미지를 가져옵니다
docker pull redis
docker pull redis
2、启动3个redis容器服务,分别使用到6379、6380、6381端口
docker run --name redis-6379 -p 6379:6379 -d redis docker run --name redis-6380 -p 6380:6379 -d redis docker run --name redis-6381 -p 6381:6379 -dredis
3、查看容器
[tcy@tcy1 ~]$ docker ps -a container id image command created status ports names a9fa77adc598 daocloud.io/library/redis "docker-entrypoint.s 2 hours ago up 2 hours 0.0.0.0:6381->6379/tcp redis-6381 6ee2f2f007e6 daocloud.io/library/redis "docker-entrypoint.s 2 hours ago up 2 hours 0.0.0.0:6380->6379/tcp redis-6380 ab54741166e1 daocloud.io/library/redis "docker-entrypoint.s 3 hours ago up 3 hours 0.0.0.0:6379->6379/tcp redis-6379
4、测试容器,成功
docker exec -it ab54741166e1 redis-cli:进入容器 [root@tcy1 tcy]# docker exec -it ab54741166e1 redis-cli 127.0.0.1:6379> set b tcy ok 127.0.0.1:6379> get b "tcy" 127.0.0.1:6379> quit[root@tcy1 tcy]#
5、开始redis集群配置
5.1、看容器内网的ip地址
[root@tcy1 tcy]# docker inspect a9fa77adc598
redis-6379:172.17.0.1:6379 redis-6380:172.17.0.2:6379 redis-6381:172.17.0.3:63793. Container
[root@tcy1 tcy]# docker exec -it ab54741166e1 /bin/bash root@ab54741166e1:/data# redis-cli 127.0.0.1:6379> info replication # replication role:master connected_slaves:0 master_replid:d43d1ae8cde6cb084220e18b926aba79e0bb2504 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:0 second_repl_offset:-1 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0 127.0.0.1:6379> quit root@ab54741166e1:/data# exit exit4. 컨테이너 테스트 성공
[root@tcy1 tcy]# docker exec -it a9fa77adc598 /bin/bash //redis-6380
root@a9fa77adc598:/data# redis-cli
127.0.0.1:6379> slaveof 172.17.0.1 6379
ok
127.0.0.1:6379> quit
root@a9fa77adc598:/data# exit
exit
[root@tcy1 tcy]# docker exec -it 6ee2f2f007e6 /bin/bash //redis-6381
root@6ee2f2f007e6:/data# redis-cli
127.0.0.1:6379> slaveof 172.17.0.1 6379
ok
127.0.0.1:6379> quit
5.1. 컨테이너 인트라넷의 IP 주소를 확인하세요
[root@tcy1 tcy]# docker Inspection a9fa77adc598
세 Redis의 인트라넷 IP 주소는
[root@tcy1 tcy]# docker exec -it ab54741166e1 /bin/bash root@ab54741166e1:/data# redis-cli 127.0.0.1:6379> info replication # replication role:master connected_slaves:2 slave0:ip=172.17.0.3,port=6379,state=online,offset=378,lag=1 slave1:ip=172.17.0.2,port=6379,state=online,offset=378,lag=0 master_replid:ce193b15cfd57f7dc3ccfbf2a4aef6156b131e6d master_replid2:0000000000000000000000000000000000000000 master_repl_offset:378 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:378 127.0.0.1:6379> quit root@ab54741166e1:/data# exit exit5.2입니다. Docker 컨테이너 내부에 들어가서 현재 Redis 역할(마스터 또는 슬레이브)을 확인하세요
[root@tcy1 tcy]# docker exec -it a9fa77adc598 /bin/bash root@a9fa77adc598:/data# cd / && touch sentinel.conf root@a9fa77adc598:/# vim /sentinel.conf현재는 모두 3개는 마스터 상태입니다.
5.3. redis-cli 명령을 사용하여 redis-6380 및 redis-6381의 호스트를 172.17.0.1:6379
root@a9fa77adc598:/# redis-sentinel /sentinel.conf 342:x 24 jun 11:37:58.934 # oo0ooo0ooo0oo redis is starting oo0ooo0ooo0oo 342:x 24 jun 11:37:58.957 # redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=342, just started 342:x 24 jun 11:37:58.958 # configuration loaded 342:x 24 jun 11:37:58.959 # you requested maxclients of 10000 requiring at least 10032 max file descriptors. 342:x 24 jun 11:37:58.959 # server can't set maximum open files to 10032 because of os error: operation not permitted. 342:x 24 jun 11:37:58.960 # current maximum open files is 4096. maxclients has been reduced to 4064 to compensate for low ulimit. if you need higher maxclients increase 'ulimit -n'. _._ _.-``__ ''-._ _.-`` `. `_. ''-._ redis 4.0.10 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) running in sentinel mode |`-._`-...-` __...-.``-._|'` _.-'| port: 26379 | `-._ `._ / _.-' | pid: 342 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 342:x 24 jun 11:37:59.068 # warning: the tcp backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 342:x 24 jun 11:37:59.089 # sentinel id is dfd5a5bfe1036b1df3395c4ba858329034fc5b7e 342:x 24 jun 11:37:59.091 # +monitor master mymaster 172.17.0.1 6379 quorum 1 342:x 24 jun 11:37:59.110 * +slave slave 172.17.0.3:6379 172.17.0.3 6379 @ mymaster 172.17.0.1 6379 342:x 24 jun 11:37:59.115 * +slave slave 172.17.0.2:6379 172.17.0.2 6379 @ mymaster 172.17.0.1 6379 342:x 24 jun 11:39:27.601 * +sentinel sentinel ba9b0d0539d8273edfcbd922fe138f50daa78bbb 172.17.0.2 26379 @ mymaster 172.17.0.1 6379 342:x 24 jun 11:41:59.144 * +sentinel sentinel f0510f8582b72c056531f219397ed8826683e665 172.17.0.1 26379 @ mymaster 172.17.0.1 6379
5.4로 수정합니다. redis-6379에 이미 2개의 슬레이브가 있는지 확인하세요. ,connected_slaves:2, yes
[tcy@tcy1 ~]$ docker stop ab54741166e1 ab54741166e15.5에서 sentinel sentinel
구성을 위해 세 개의 Redis 컨테이너를 입력하고 컨테이너의 루트 디렉터리에 sentinel.conf 파일을 생성합니다
파일 내용은 다음과 같습니다. sentinel monitor mymaster 172.17.0.1 6379 1
[root@tcy1 /]# docker exec -it 6ee2f2f007e6 /bin/bash root@6ee2f2f007e6:/data# redis-cli 127.0.0.1:6379> info replication # replication role:master connected_slaves:1 slave0:ip=172.17.0.3,port=6379,state=online,offset=66906,lag=1 master_replid:5a7489c8181ddf0d73d418d30d6a4c8e039198ba master_replid2:ce193b15cfd57f7dc3ccfbf2a4aef6156b131e6d master_repl_offset:67041 second_repl_offset:65534 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:85 repl_backlog_histlen:66957 127.0.0.1:6379>표시되는 경우: bash: vim: command notfound해결 방법: 1. apt-get 업데이트 2. apt-get install vim🎜마지막으로 redis sentinel을 시작합니다. : 🎜rrreee🎜관찰하기 쉽고, 여러 개의 창을 열어보세요. 🎜🎜🎜🎜🎜sentinel 구성 🎜🎜5.6, 테스트 🎜🎜Close the master🎜rrreee🎜 이때 나머지 2개의 슬레이브는 자동으로 새 호스트를 선택하며 여기서는 172.17.0.2가 호스트로 선택됩니다. 🎜🎜🎜🎜🎜172.17.0.2를 보고 호스트가 되어보세요. 🎜아아아아
위 내용은 Docker에서 Redis 마스터-슬레이브를 구성하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!