Docker で Redis マスター/スレーブを構成する方法

WBOY
リリース: 2023-05-28 11:19:06
転載
1056 人が閲覧しました

1. 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 Inspection a9fa77adc598

Docker で Redis マスター/スレーブを構成する方法

3 つの Redis のイントラネット IP アドレスは次のとおりです:

redis-6379:172.17.0.1:6379
redis-6380:172.17.0.2:6379
redis-6381:172.17.0.3:6379
ログイン後にコピー

5.2. Docker コンテナーに入ります。現在の redis ロール (マスターまたはスレーブ) を表示します

[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 
exit
ログイン後にコピー

現在 3 つはマスター ステータスです

5.3. redis-cli コマンドを使用して redis-6380 のホストを変更しますredis-6381 から 172.17.0.1 :6379

[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.4. redis-6379 にすでに 2 つのスレーブ (connected_slaves:2) があるかどうかを確認し、yes

[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
exit
ログイン後にコピー

5.5. Sentinel Sentinel の設定

設定用の 3 つの Redis コンテナを入力し、コンテナのルート ディレクトリに Sentinel.conf ファイルを作成します

ファイルの内容は次のとおりです。 : Sentinel Monitor mymaster 172.17.0.1 6379 1

[root@tcy1 tcy]# docker exec -it a9fa77adc598 /bin/bash
root@a9fa77adc598:/data# cd / && touch sentinel.conf 
root@a9fa77adc598:/# vim /sentinel.conf
ログイン後にコピー

表示される場合: bash: vim: command not found

解決策: 1. apt-get update 2. apt -get install vim

最後に、redis センチネルを起動します:

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
ログイン後にコピー

複数のウィンドウを開いて観察するのは簡単です。

Docker で Redis マスター/スレーブを構成する方法

センチネルの設定が完了しました

5.6、テスト

マスターを閉じる

[tcy@tcy1 ~]$ docker stop ab54741166e1
ab54741166e1
ログイン後にコピー

この時点で、残りの 2 つはスレーブは自動的に新しいホストを選択します。ここでは、172.17.0.2 がホストとして選択されます。

Docker で Redis マスター/スレーブを構成する方法

# 172.17.0.2 にチェックを入れてホストになります。

rree

以上がDocker で Redis マスター/スレーブを構成する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:yisu.com
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!