DockerでApache Igniteの環境を作ってみる（1台構成と2台構成）

# はじめに

Apache Igniteは、インメモリデータグリッドを実現するオープンソースのソフトウェアです。
DockerでApache Igniteの環境を作ってみました。

参考：公式サイト（英語）
[Docker Deployment](https://apacheignite.readme.io/docs/docker-deployment)

CentOS 7にDockerインストール後の環境を使用しています。


# DockerでApache Igniteを動かす

とりあえず、Dockerイメージをpullします。

```
# docker pull apacheignite/ignite
# docker image ls
REPOSITORY                                                   TAG                 IMAGE ID            CREATED             SIZE
apacheignite/ignite                                          latest              f0b49555e795        7 weeks ago         481MB
```


以下、ネットワークを作成しています。

```
docker network create -d bridge --gateway=192.168.100.254 --subnet 192.168.100.0/24 testnet01
docker network ls
docker network inspect testnet01
```

Igniteを起動します。

10800ポートはクライアントアプリケーションからJDBC等で接続する場合に必要です。

```
docker run --name ignite1 --rm -p 11211:11211 -p 48100-48200:48100-48200 -p 47100-47200:47100-47200 -p 47500:47500 -p 49112:49112 -p 10800:10800 -e TZ=Asia/Tokyo -h ignite1 --ip 192.168.100.50 --net=testnet01 apacheignite/ignite
```

コンテナにログインして、Ignoreクラスタの情報を表示してみます。

```
# docker exec -it ignite1 /bin/bash
# /opt/ignite/apache-ignite-fabric/bin/ignitevisorcmd.sh
visor> open
Choose configuration file number ('c' to cancel) [0]: 0
visor> top
Hosts: 1
+=============================================================================================================================+
| Int./Ext. IPs  |   Node ID8(@)    | Node Type |                  OS                   | CPUs |       MACs        | CPU Load |
+=============================================================================================================================+
| 127.0.0.1      | 1: 0BECAA01(@n0) | Server    | Linux amd64 3.10.0-693.2.2.el7.x86_64 | 1    | 02:42:C0:A8:64:32 | 8.00 %   |
| 192.168.100.50 |                  |           |                                       |      |                   |          |
+-----------------------------------------------------------------------------------------------------------------------------+

Summary:
+--------------------------------------+
| Active         | true                |
| Total hosts    | 1                   |
| Total nodes    | 1                   |
| Total CPUs     | 1                   |
| Avg. CPU load  | 8.00 %              |
| Avg. free heap | 93.00 %             |
| Avg. Up time   | 00:00:47            |
| Snapshot time  | 2018-10-28 04:12:51 |
+--------------------------------------+
```

visorのtopコマンドでIgniteクラスタに1台のサーバがいることが分かります。


デフォルトではコンテナ中の「/opt/ignite/apache-ignite-fabric/config/default-config.xml」の設定ファイルが使用されます。
中身は以下のように具体的な設定はありませんでした。

```/opt/ignite/apache-ignite-fabric/config/default-config.xml 
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--
        Alter configuration below as needed.
    -->
    <bean id="grid.cfg" class="org.apache.ignite.configuration.IgniteConfiguration"/>
</beans>
```

以下は設定ファイル（default-config.xml）を変更する場合。
「/tmp/default-config.xml」を作成し、コンテナにマウントしています。

```
docker run --name ignite1 --rm -p 11211:11211 -p 48100-48200:48100-48200 -p 47100-47200:47100-47200 -p 47500:47500 -p 49112:49112 -p 10800:10800 -e TZ=Asia/Tokyo -h ignite1 --ip 192.168.100.50 -v /tmp/default-config.xml:/opt/ignite/apache-ignite-fabric/config/default-config.xml -e CONFIG_URI=/opt/ignite/apache-ignite-fabric/config/default-config.xml --net=testnet01 apacheignite/ignite
```

# クラスタ構成（2台）で起動する

docker-composeを使用して、クラスタ構成でIgniteを使用してみます。
まずは、docker-composeをインストール。

```
# curl -L https://github.com/docker/compose/releases/download/1.22.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
# chmod +x /usr/local/bin/docker-compose
# docker-compose --version
docker-compose version 1.22.0, build f46880fe
```

2台構成用の設定ファイルを作成します。
サーバは以下の2台です。
・igniteserver1
・igniteserver2

```/tmp/default-config.xml 
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--
        Alter configuration below as needed.
    -->
    <bean id="grid.cfg" class="org.apache.ignite.configuration.IgniteConfiguration">
        <property name="discoverySpi">
            <bean class="org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi">
                <property name="ipFinder">
                    <bean class="org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder">
                        <property name="addresses">
                            <list>
                                <value>igniteserver1:47500..47509</value>
                                <value>igniteserver2:47500..47509</value>
                            </list>
                        </property>
                    </bean>
                </property>
            </bean>
        </property>
    </bean>
</beans>
```

次にIgniteを2台起動するdocker-compose.xmlを作成します。

```docker-compose.xml
version: "2"
services:
  ignite1:
    hostname: "igniteserver1"
    image: apacheignite/ignite
    ports:
      - "18100-18200:48100-48200"
      - "17100-17200:47100-47200"
      - "47501:47500"
      - "49121:49112"
      - "10801:10800"
    environment:
      TZ: Asia/Tokyo
      CONFIG_URI: /opt/ignite/apache-ignite-fabric/config/default-config.xml
    volumes:
      - /tmp/default-config.xml:/opt/ignite/apache-ignite-fabric/config/default-config.xml
    networks:
      testnet01:
        ipv4_address: 192.168.100.11

  ignite2:
    image: apacheignite/ignite
    hostname: "igniteserver2"
    ports:
      - "28100-28200:48100-48200"
      - "27100-27200:47100-47200"
      - "47502:47500"
      - "49122:49112"
      - "10802:10800"
    environment:
      TZ: Asia/Tokyo
      CONFIG_URI: /opt/ignite/apache-ignite-fabric/config/default-config.xml
    volumes:
      - /tmp/default-config.xml:/opt/ignite/apache-ignite-fabric/config/default-config.xml
    networks:
      testnet01:
        ipv4_address: 192.168.100.12

networks:
  testnet01:
    external: true
```

コンテナを起動しています。

```
docker-compose up -d
docker-compose exec ignite1 /bin/bash
```

「docker-compose exec ignite1 /bin/bash」でコンテナにログインしています。
Igniteクラスタに接続し、起動確認をしています。

```
# /opt/ignite/apache-ignite-fabric/bin/ignitevisorcmd.sh 
visor> open
Choose configuration file number ('c' to cancel) [0]: 0

+----------------------------------------------------------------------------------+
| Using configuration | /opt/ignite/apache-ignite-fabric/config/default-config.xml |
+----------------------------------------------------------------------------------+


visor> top
Hosts: 2
+=============================================================================================================================+
| Int./Ext. IPs  |   Node ID8(@)    | Node Type |                  OS                   | CPUs |       MACs        | CPU Load |
+=============================================================================================================================+
| 127.0.0.1      | 1: E1193E26(@n1) | Server    | Linux amd64 3.10.0-693.2.2.el7.x86_64 | 1    | 02:42:C0:A8:64:0C | 1.00 %   |
| 192.168.100.12 |                  |           |                                       |      |                   |          |
+----------------+------------------+-----------+---------------------------------------+------+-------------------+----------+
| 127.0.0.1      | 1: 83CC2290(@n0) | Server    | Linux amd64 3.10.0-693.2.2.el7.x86_64 | 1    | 02:42:C0:A8:64:0B | 0.67 %   |
| 192.168.100.11 |                  |           |                                       |      |                   |          |
+-----------------------------------------------------------------------------------------------------------------------------+

Summary:
+--------------------------------------+
| Active         | true                |
| Total hosts    | 2                   |
| Total nodes    | 2                   |
| Total CPUs     | 2                   |
| Avg. CPU load  | 0.83 %              |
| Avg. free heap | 89.00 %             |
| Avg. Up time   | 00:04:03            |
| Snapshot time  | 2018-10-27 18:54:38 |
+--------------------------------------+

```

ホスト数が2台になっていることが確認できます。


# Dockerfile

今回使用したDockerfileに以下にあります。

https://hub.docker.com/r/apacheignite/ignite/~/dockerfile/


```Dockerfile
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# Start from a Java image.
FROM openjdk:8

# Ignite version
ENV IGNITE_VERSION 2.3.0

# Ignite home
ENV IGNITE_HOME /opt/ignite/apache-ignite-fabric-${IGNITE_VERSION}-bin

# Do not rely on anything provided by base image(s), but be explicit, if they are installed already it is noop then
RUN apt-get update && apt-get install -y --no-install-recommends \
        unzip \
        curl \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /opt/ignite

RUN curl https://dist.apache.org/repos/dist/release/ignite/${IGNITE_VERSION}/apache-ignite-fabric-${IGNITE_VERSION}-bin.zip -o ignite.zip \
    && unzip ignite.zip \
    && rm ignite.zip

# Copy sh files and set permission
COPY ./run.sh $IGNITE_HOME/

RUN chmod +x $IGNITE_HOME/run.sh

CMD $IGNITE_HOME/run.sh

EXPOSE 11211 47100 47500 49112
```