###### tags: `AWS`,`IBM MQ`
# AWS上にIBM MQを構築する
---
参考: 『AWS公式 [「IBM MQ on AWS(英語版)」](https://aws.amazon.com/jp/quickstart/architecture/ibm-mq/),[「導入ガイド」](https://aws-quickstart.s3.amazonaws.com/quickstart-ibm-mq/doc/ibm-mq-on-the-aws-cloud.pdf)』
参考: [『IBM MQ_MQ on Container(英語版)](https://developer.ibm.com/messaging/learn-mq/mq-tutorials/mq-connect-to-queue-manager/#docker-get-docker-image)』
---
【導入方法】
・ EC2上にDocker Containerを作成
→ Test環境の為、EC2インスタンスは1つ。
・ 構築イメージ

---
## EC2インスタンスを作成する/DockerContainerの準備
**1. 以下を参考にEC2を作成する。(EC2インスタンスの作成:項番4まで実施でOKかと)**
[・ カスタムVPC〜EC2インスタンスの作成](https://hackmd.io/lqWbbYBDQ-6PJ9xYem_5dQ)
**2. DockerをInstallする**
```
sudo yum update -y
sudo yum install docker -y
エラーが出ない事
```
**3. Docker起動**
```
sudo service docker start
```
**4. docker runコマンドでイメージ取得/コンテナ実行【確認作業】**
```
sudo docker run hello-world
```
**・ 実行結果の確認**
```
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:d58e752213a51785838f9eed2b7a498ffa1cb3aa7f946dda11af39286c3db9a9
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
```
> 【豆知識】
> <span style="color:blue;">dockerコマンドはsudo付加しないで実行するとパーミッションで拒否されます。
> が、dockerグループに所属させれば問題解決です。(セキュリティの為にsudo付加じゃなきゃ嫌だという方はここは無視してください)</span>
> ```
> 対象ソケットのファイルパーミッションは以下のようになっている
> srw-rw---- 1 root docker 0 5月 19 03:23 /var/run/docker.sock
>
> #ec2-userをグループ:dockerに追加
> sudo gpasswd -a ec2-user docker
>
> #一度、exitして再ログイン(変更反映)
> ```
>
**5. コンテナ/イメージの削除【確認作業の後片付け】**
```
#コンテナID確認
docker ps -a
# コンテナ削除:docker ps -a コマンドで確認したコンテナIDを指定
docker rm [コンテナID]
[コンテナID] # 削除したコンテナIDが表示される
# イメージを削除する
docker rmi hello-world
```
**6. MQサーバーイメージの取得**
EC2で以下のコマンドを実行
```
docker pull ibmcom/mq:latest
```
pull completeしたら以下のコマンドでDockerイメージを確認する
```
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ibmcom/mq latest 382f6ba8110a 2 months ago 896MB
```
**7. イメージからコンテナの実行**
・ ボリュームを作成する
dockerのファイルシステムはdockerを削除すると一緒に削除されてしまうので
コンテナ自体を削除してもキューデータなどが存続できるように(パーシステント)ボリュームを作成する
```
docker volume create qm1data
```
・ 2つのコンテナ(MQサーバー用とMQクライアントデモ用)が通信に使用できるネットワークを作成する
```
docker network create mq-demo-network
```
・ MQサーバーコンテナの実行
```
# yourpassword箇所をアプリケーション接続用の独自パスワードに変更して実行する
docker run --env LICENSE=accept --env MQ_QMGR_NAME=QM1 --volume qm1data:/mnt/mqm --publish 1414:1414 --publish 9443:9443 --network mq-demo-network --network-alias qmgr --detach --env MQ_APP_PASSWORD=【yourpassword】 ibmcom/mq:latest
> 【コマンド説明】
> キューマネージャー名:QM1
> キューマネージャー用リスナーポート:1414
> MQコンソール用ポート:9443
```
・ Dockerコンテナの確認
```
docker ps
以下のように出力されれば起動している
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
【コンテナID英数字】 ibmcom/mq:latest "runmqdevserver" 5 minutes ago Up 5 minutes 0.0.0.0:1414->1414/tcp, 0.0.0.0:9443->9443/tcp, 9157/tcp hopeful_jang
```
・ MQバージョンの確認
```
# コンテナ内にアクセスしてコマンドラインを実行。
docker exec -ti 【コンテナID英数字】 /bin/bash
# プロンプトに対してコマンドを実行(MQバージョン表示)
dspmqver
【表示結果】
Name: IBM MQ
Version: 9.1.5.0
Level: p915-ifix-L200325.DE
BuildType: IKAP - (Production)
Platform: IBM MQ for Linux (x86-64 platform)
Mode: 64-bit
O/S: Linux 4.14.177-139.254.amzn2.x86_64
O/S Details: Red Hat Enterprise Linux 8.1 (Ootpa)
InstName: Installation1
InstDesc: IBM MQ V9.1.5.0 (Unzipped)
Primary: N/A
InstPath: /opt/mqm
DataPath: /mnt/mqm/data
MaxCmdLevel: 915
LicenseType: Developer
# プロンプトに対してコマンドを実行(キューマネージャの状態表示)
dspmq
【表示結果】
QMNAME(QM1) 〜 STATUS(Running)
# コンテナから抜ける
exit
```
# アプリケーション(デモ用)を動かす
1. デモ用Dockerfileをダウンロード(wget)する
```
wget https://raw.githubusercontent.com/ibm-messaging/mq-dev-samples/master/gettingStarted/mq-demo-client-application/Dockerfile
```
---
2. Dockerイメージをビルドする
```
docker build -t mq-demo .
```
---
3. アプリケーション(コンテナ)の実行
```
docker run --network mq-demo-network -ti mq-demo
```
---
以下の画面が表示される
```
Extracting to /tmp/mq-demo...
/usr/local/bin/mq-demo: line 48: cd: /root/Downloads: No such file or directory
Initializing...
5724-H72 (C) Copyright IBM Corp. 2017.
__ __ ___ ___
| \/ |/ _ \ | \ ___ _ __ ___
| |\/| | (_) | | |) / -_) ' \/ _ \
|_| |_|\__\_\ |___/\___|_|_|_\___/
Welcome to this demo application for IBM MQ!
The app will allow you to connect to a queue manager and start sending and receiving messages.
```
**↓ ここからは対話的に設定項目を入力する**
【キューマネージャー名】
```
Type the name of your queue manager (leave blank for 'QM1'):
```
<span style="color:blue;">→ 何も入力せずenterキー押下 → デフォルト:QM1に設定</span>
【ホスト名】
```
Type the host name or IP address for your queue manager:
```
<span style="color:red;">→ HOST名:[qmgr]を入力しenterキー押下 → qmgrに設定</span>
`qmgr`
【リスナーポート】
```
Type the listener port of your queue manager (leave blank for '1414'):
```
<span style="color:blue;">→ 何も入力せずenterキー押下 :デフォルト:1414に設定 </span>
【アプリケーションチャネル名】
```
Type the name of the client connection channel (leave blank for 'DEV.APP.SVRCONN'):
```
<span style="color:blue;">→ 何も入力せずenterキー押下 → デフォルト:DEV.APP.SVRCONNに設定</span>
【キュー名】
```
Type the name of the queue (leave blank for 'DEV.QUEUE.1'):
```
<span style="color:blue;">→ 何も入力せずenterキー押下 → デフォルト:DEV.QUEUE.1に設定 </span>
【アプリケーションユーザ名】
```
Type the application user name (leave blank for 'app'):
```
<span style="color:blue;">→ 何も入力せずenterキー押下 → デフォルト:appに設定 </span>
【アプリケーションユーザのパスワード】
```
Please type the password for this user:
```
<span style="color:red;">→ MQサーバーのDockerコンテナーを実行したときに設定したMQ_APP_PASSWORDを入力</span>
**設定が終了すると接続される**
```
Connecting to queue manager 'QM1'
at address 'qmgr(1414)'
through channel 'DEV.APP.SVRCONN'
...
Connected!
*** YOU ARE NOW CONNECTED TO THE QUEUE MANAGER! ***
Type PUT [number] to put a given number of messages to the queue
Type GET [number] to get a given number of messages from the queue
Type AUTO [seconds] to put and get messages automatically for a given number of seconds
Type EXIT to quit
Type HELP to display this message
```
4. キュー間でのメッセージ送受信
```
【例:メッセージを20個キューにPUTする】
put 20
【出力メッセージ】
Attempting to put 20 messages to the queue...
Successfully put 20 messages
Check the MQ Console to see that these 20 messages have been added to the queue
```
```
【例:メッセージを18個キューからGETする】
get 18
【出力メッセージ】
Attempting to get 18 messages from the queue...
Successfully retrieved 18 messages
Check the MQ Console to see that these 18 messages have been removed from the queue
```
```
【例:メッセージを6秒間自動でキューからPUT/GETする】
auto 6
【出力メッセージ】
The application will put and get messages for 6 seconds
Check the MQ Console to see these messages being added and removed to and from the queue
PUT: 4, GET: 3, elapsed: 5s
Terminated
```
```
【終了する】
exit
【出力メッセージ】
Exiting...
Successfully disconnected from queue manager.
Cleaning up...
```
---