2018年4月21日土曜日

LXDでH2 Databaseがインストールされたコンテナ(Ubuntu16.04)を構築する

H2 Databaseはjava製のRDBです。

〇H2DBのコンソール画面


〇コンテナの構築
LXDで以下のコマンドを実行して、CouchDBがインストールされたコンテナを構築します。
lxc init ubuntu:16.04 h2db
lxc config set h2db user.user-data - < config.yml
lxc start h2db

config.yml
#cloud-config

package_upgrade: true

hostname: h2db
manage_etc_hosts: true

write_files:
  - path: /etc/systemd/system/h2.service
    content: |
      [Unit]
      Description=H2 database
      After=syslog.target network.target
      [Service]
      Type=simple
      EnvironmentFile=/opt/h2/bin/h2env
      WorkingDirectory=/opt/h2
      ExecStart=/usr/bin/java -cp "/opt/h2/bin/h2-1.4.197.jar:\\$H2CUSTOMJARS" org.h2.tools.Server -tcp -tcpAllowOthers -web -webAllowOthers -pg -pgAllowOthers -baseDir /opt/h2/data
      ExecStop=/usr/bin/kill -3 \\${MAINPID}
      [Install]
      WantedBy=multi-user.target
runcmd:
  - 'apt-get update'
  - 'apt-get -y install openjdk-8-jdk'
  - 'apt-get -y install unzip'
  - 'wget http://www.h2database.com/h2-2018-03-18.zip'
  - 'unzip h2-2018-03-18.zip'
  - 'mv h2 /opt'
  - mkdir /opt/h2/data
  - 'echo "H2CUSTOMJARS=" >> /opt/h2/bin/h2env'
  - systemctl enable h2.service
  - systemctl start h2.service
final_message: "completed."

〇ホストマシンの外部からコンテナにアクセスしたい場合
以下のコマンドを実行します。
PORT=9092 PUBLIC_IP=<ホストのIP> CONTAINER_IP=<コンテナのIP> sudo -E bash -c 'iptables -t nat -I PREROUTING -i eth0 -p TCP -d $PUBLIC_IP --dport $PORT -j DNAT --to-destination $CONTAINER_IP:$PORT -m comment --comment "container"'
PORT=5435 PUBLIC_IP=<ホストのIP> CONTAINER_IP=<コンテナのIP> sudo -E bash -c 'iptables -t nat -I PREROUTING -i eth0 -p TCP -d $PUBLIC_IP --dport $PORT -j DNAT --to-destination $CONTAINER_IP:$PORT -m comment --comment "container"'
PORT=8082 PUBLIC_IP=<ホストのIP> CONTAINER_IP=<コンテナのIP> sudo -E bash -c 'iptables -t nat -I PREROUTING -i eth0 -p TCP -d $PUBLIC_IP --dport $PORT -j DNAT --to-destination $CONTAINER_IP:$PORT -m comment --comment "container"'

○コンソール画面へのアクセス
ブラウザからhttp://<ホストのIP>:8092/にアクセスします。
JDBCURLには、「jdbc:h2:tcp://<コンテナのIP>/test」を指定し
ユーザ名sa、パスワードなしを指定します。

〇コンテナのIPを調べる
コンテナのIPは以下のコマンドで調べることができます。
lxc list

〇コンテナの停止
lxc stop h2db

〇コンテナの削除
lxc delete h2db


○関連情報
・H2 Databaseに関する他の記事はこちらを参照してください。

0 件のコメント:

コメントを投稿