LoginSignup
4
0

More than 3 years have passed since last update.

Docker + Consul + Swarm でoverlayネットワーク~複数の物理サーバーでネットワークを共有しよう

Last updated at Posted at 2019-08-03

概要:

こんにちは、はにおかさいです
Dockerが複数ホストにまたがるとき、コンテナ同士の通信はどうしようかと思います。
DockerとConsulとSwarmで仮想ネットワークを構築し互いに通信できるようにしましょう。

環境:

  • Ubuntu 18.04 on Linode *2 をクリーンインストールで。
  • Consul: 1.5.3
  • Docker: 19.03.1

Host1

メインのサーバーとして使用
- hostname: con1
- IP: 111.111.11.11

Host2

子サーバー
- hostname: con2
- IP: 222.222.22.22

インストール:

byobuを使うとよいでしょう

Host1&2
echo なにがし>/etc/hostname #ほかのホストとダブるとconsulがエラーを起こす。

##Dockerのインストール
curl -sSL https://test.docker.com/ | sh

##Consulのインストール
apt install unzip 
wget https://releases.hashicorp.com/consul/1.5.3/consul_1.5.3_linux_amd64.zip
unzip consul*
sudo cp consul /usr/local/bin/

Consulの起動

Host1(サーバー)
consul agent -server -bootstrap -data-dir=/tmp/consul -bind=111.111.11.11
Host2(クライアント)
consul agent -data-dir=/tmp/consul -bind=222.222.22.22 -join=111.111.11.11
Host1&2
consul members

Dockerの設定

Host1&2
 echo 'DOCKER_OPTS="--cluster-store=consul://localhost:8500 --cluster-advertise=eth0:2376"' >> /etc/default/docker

service docker restart

Swarmの設定

Host1
 docker swarm init --advertise-addr=111.111.11.11
Host2
適宜swar参加コマンドを実行

ネットワークの作成

どのマシンでもよい
docker network create --driver=overlay --subnet=192.168.0.0/24 --attachable 0ver

コンテナの作成

Host2とする
docker run --name=ctr1 --net=0ver -it ubuntu:latest /bin/bash
Host1とする
docker run --name=ctr1 --net=0ver -it ubuntu:latest /bin/bash

疎通テスト

コンテナctr1@Host2
apt update
apt install iputils-ping net-tools
ifconfig
#######################################
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1450
        inet 10.0.1.2  netmask 255.255.255.0  broadcast 10.0.1.255
        ether 02:42:0a:00:01:02  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.18.0.3  netmask 255.255.0.0  broadcast 172.18.255.255
        ether 02:42:ac:12:00:03  txqueuelen 0  (Ethernet)
        RX packets 3809  bytes 20879442 (20.8 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2837  bytes 194063 (194.0 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0

#####################################
hostname -I | cut -f1 -d' ' #eth0
hostname -I | cut -f2 -d' ' #eth1

apt install nginx
コンテナctr1@Host1
apt update
apt install iputils-ping net-tools wget
ping 10.0.1.2
wget 10.0.1.2
cat index.html

まとめ

無事できました。
どこの情報よりもスラっとしていると思います。

編集後記

地理的に離れていてもオーバーレイネットワーク作れそうじゃね?

追伸

Conoha Linodeでもオーバーレイできたで。
image.png

おすすめ

ネットワークいろいろ
・「単一のDockerホスト内の色々なネットワーク構成例」
https://www.memotansu.jp/docker/865/
・「Ubuntu 16.04でDockerコンテナに複数NICをブリッジする」
https://qiita.com/hiconyan/items/b4015fa174d333c060a9
・「Docker swarm mode overlay network security model」
https://docs.docker.com/v17.09/engine/userguide/networking/overlay-security-model/

参考元:

https://thinkit.co.jp/article/8414?nopaging=1
https://thinkit.co.jp/story/2015/08/24/6343
https://blog.bitmeister.jp/?p=4043
https://qiita.com/Arturias/items/dd7f9e2365626dd4ad51
https://qiita.com/tukiyo3/items/0785bde8733417093957
http://yuzuma-yuzuma-yuzuma.hatenablog.jp/entry/2017/11/05/193710
https://stackoverflow.com/questions/41847656/network-not-manually-attachable-when-running-one-off-command-against-docker-sw
https://moomindani.wordpress.com/2014/09/17/linux-command-ip-address/

4
0
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
4
0