1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Dockerで構築したオンプレのMongoDBとEC2上のMongoDBとでレプリカセット組む

Last updated at Posted at 2019-10-06

同じネットワークではない別々のマシン間において、レプリカセットを組むやり方まとめ。

端的に言うとポートフォワードでできた。

状況

  • EC2上に構築したMongoDBと自宅サーバのMongoDBとでレプリカセットを組みたい
  • それぞれDockerでMongoDBは構築している
  • 双方向にSSHは可能

やり方

EC2の1インスタンス上にMongoDBのPrimaryとArbiterのコンテナをそれぞれ作り、自宅サーバにはSecondaryのコンテナを作るシチュエーション。

37016がArbiter
37017がPrimary
37018がSecondary

1. EC2から自宅サーバにトンネルを掘る

今回はautosshを使ったが、普通のsshでも同じようにできる。
-gが無いとコンテナの中からアクセスできないので注意。

autossh -M 0 -N -f -C -g my_home@example.com -L 37018:localhost:37018

2. 自宅サーバからEC2にトンネルを掘る

autossh -M 0 -N -f -C -g ec2@example.com -L 37017:localhost:37017 -L 37016:localhost:37016

3. レプリカセット構築

PrimaryのMongoDBに入って、以下のように設定する。
172.17.0.1はDockerコンテナ中から見たホストマシンのIP。

config={_id:"mongo-set", members: [ { _id:0, host:'172.17.0.1:37016', "arbiterOnly":true }, { _id:1, host:'172.17.0.1:37017' }, { _id:2, host:'172.17.0.1:37018'} ] }
rs.initiate(config)

4. 完成🙌

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?