
デプロイメント REST API を使用した Liberty Docker コンテナーのデプロイ
DeployService REST API を使用して、Docker コンテナー内の Liberty サーバーを、集合のメンバーとしてデプロイできます。
始める前に
Docker イメージのインストール場所となるターゲット・ホストを決定します。それらのターゲット・ホストに Docker をインストールします。 ターゲット・ホストごとに RXA または SSH が必要です。『Liberty 集合操作用 RXA のセットアップ』を参照してください。
ご覧ください: このトピックの手順を補足する説明が、WASdev 記事内のビデオ「Managing an On-Premise, Polyglot Environment with WebSphere Liberty Collectives」に示されています。
手順
- Docker イメージを作成します。
- Dockerfile を使用して Liberty イメージを作成します。
以下の Dockerfile サンプルを使用すると、Liberty Network Deployment イメージを作成できます。Docker Hub で入手できるイメージは、scalingMember-1.0 などの Network Deployment フィーチャーにアクセスできません。このファイルを使用して Liberty Network Deployment イメージを作成します。最終行を使用して、使用するアプリケーションを Docker イメージにコピーできます。
# # Dockerfile to create a Liberty Network Deployment image # # This Dockerfile assumes: # - The collective enabling scripts (joinMember, removeMember) will be placed in # /opt/ibm/docker in the Docker container # - WebSphere Liberty will be installed at /opt/ibm/wlp # - The WebSphere Liberty server will be "defaultServer" # (for example, /opt/ibm/wlp/usr/servers/defaultServer) # - The Docker container runs using --net=host so that no ports are explicitly # exported in this file FROM websphere-liberty # # Auto-start the Liberty server with the container # ENTRYPOINT ["/opt/ibm/wlp/bin/server", "run", "defaultServer"] # # # # Add collective enabling scripts # ADD joinMember /opt/ibm/docker/ RUN chmod +x /opt/ibm/docker/joinMember ADD removeMember /opt/ibm/docker/ RUN chmod +x /opt/ibm/docker/removeMember # # Update the operating system # RUN apt-get update # # Add the additional required features # RUN installUtility install --acceptLicense scalingMember-1.0 clusterMember-1.0 collectiveMember-1.0 ADD server.xml /opt/ibm/wlp/usr/servers/defaultServer/ # # Insert your application file name. This statement copies the application # onto the Docker image. # ADD application_file /opt/ibm/wlp/usr/servers/defaultServer/dropins/注: この Docker イメージでは、Docker コンテナー内のメンバー・サーバーを集合コントローラーに結合する、次のような joinMember スクリプトを使用しています。
また、この Docker イメージでは、Docker コンテナー内のメンバー・サーバーを集合から除去する、次のような removeMember スクリプトも使用しています。#!/bin/bash # # Script to join a Liberty Docker container to a collective controller. # # Required: <containerName> - positional parameter that is the name # of this Docker container # --host - collective controller hostname # --port - collective controller https port # --user - collective controller admin user # --password - collective controller admin user password # --keystorePassword - to be used for the member keystore # --dockerHost - hostname of the Docker host this container resides on # # Optional: --clusterName - cluster name for the member configuration. # If specified, it adds the cluster and scaling # member features also. # # Set initial values containerName=$1 # Parse the command line failure="false" if [[ -z $1 ]]; then # No values received echo "Error: no parameters specified" failure="true" else TEMP=`getopt --long host:,port:,user:,password:,keystorePassword:,dockerHost:,clusterName:: -- "$@"` eval set -- "$TEMP" OPTIND=-1 while true ; do case "$1" in --host) controllerHost=$2; shift 2;; --port) controllerPort=$2; shift 2;; --user) controllerUser=$2; shift 2;; --password) controllerPassword=$2; shift 2;; --keystorePassword) memberKeystorePassword=$2; shift 2;; --dockerHost) dockerHost=$2; shift 2;; --clusterName) clusterName=$2; shift 2;; *) break;; esac done # Validate the required parameters if [[ -z "$containerName" ]]; then echo "Error: no containerName specified, validation suspended" failure="true" fi # if a containerName is not specified, bash uses the # 1st "--" parameter as the containerName if [[ $containerName == --* ]]; then echo "Error: no containerName specified, validation suspended" failure="true" fi # When the positional parameter containerName fails, # suspend validation reporting if [[ $failure == false ]]; then if [[ -z "${controllerHost// }" ]]; then echo "Error: no host specified" failure="true" fi if [[ -z "${controllerPort// }" ]]; then echo "Error: no port specified" failure="true" fi if [[ -z "${controllerUser// }" ]]; then echo "Error: no user specified" failure="true" fi if [[ -z "${controllerPassword// }" ]]; then echo "Error: no password specified" failure="true" fi if [[ -z "${memberKeystorePassword// }" ]]; then echo "Error: no keystorePassword specified" failure="true" fi if [[ -z "${dockerHost// }" ]]; then echo "Error: no dockerHost specified" failure="true" fi fi fi if [[ $failure == false ]]; then # Create the env.properties file for the Liberty Docker member echo containerName=$containerName > /opt/ibm/docker/env.properties echo containerHost=$dockerHost >> /opt/ibm/docker/env.properties # Invoke the collective utility /opt/ibm/wlp/bin/collective join defaultServer --host=$controllerHost --port=$controllerPort --user=$controllerUser --password=$controllerPassword --hostName=$dockerHost --createConfigFile --keystorePassword=$memberKeystorePassword --autoAcceptCertificates --genDeployVariables # Build additional config dropin snippets if [[ $clusterName = *[!\ ]* ]]; then echo \<?xml version=\"1.0\" encoding=\"UTF-8\" ?\> > /opt/ibm/wlp/usr/servers/defaultServer/cfgfeatures.xml echo \<server\> >> /opt/ibm/wlp/usr/servers/defaultServer/cfgfeatures.xml echo $'\t'\<featureManager\> >> /opt/ibm/wlp/usr/servers/defaultServer/cfgfeatures.xml echo $'\t'$'\t'\<feature\>clusterMember-1.0\</feature\> >> /opt/ibm/wlp/usr/servers/defaultServer/cfgfeatures.xml echo $'\t'$'\t'\<feature\>scalingMember-1.0\</feature\> >> /opt/ibm/wlp/usr/servers/defaultServer/cfgfeatures.xml echo $'\t'\</featureManager\> >> /opt/ibm/wlp/usr/servers/defaultServer/cfgfeatures.xml echo \</server\> >> /opt/ibm/wlp/usr/servers/defaultServer/cfgfeatures.xml echo \<?xml version=\"1.0\" encoding=\"UTF-8\" ?\> > /opt/ibm/wlp/usr/servers/defaultServer/cluster-config.xml echo \<server\> >> /opt/ibm/wlp/usr/servers/defaultServer/cluster-config.xml echo $'\t'\<clusterMember name=\"$clusterName\" /\> >> /opt/ibm/wlp/usr/servers/defaultServer/cluster-config.xml echo \</server\> >> /opt/ibm/wlp/usr/servers/defaultServer/cluster-config.xml else echo "No clusterName provided, the container will not be configured as a cluster or scaling member." fi # Move config snippets to the proper location for consumption mkdir -p /opt/ibm/wlp/usr/servers/defaultServer/configDropins/overrides mv /opt/ibm/wlp/usr/servers/defaultServer/c*.xml /opt/ibm/wlp/usr/servers/defaultServer/configDropins/overrides if [ -f /opt/ibm/wlp/usr/servers/defaultServer/user-collective-include.xml ] then cp /opt/ibm/wlp/usr/servers/defaultServer/user-collective-include.xml /opt/ibm/wlp/usr/servers/defaultServer/configDropins/overrides fi else # At least one required parameter was missing, # notify the user of the correct syntax printf "Usage: joinMember <containerName> \n\t--host <controller host> \n\t--port <controller https port> \n\t--user <controller user> \n\t--password <controller password> \n\t--keystorePassword <member keystore password> \n\t--dockerHost <hostname of the Docker host> \n\t[--clusterName <cluster name for the member>]\n" fi#!/bin/bash # Script to remove a Liberty Docker container from a collective controller. # # Required: <containerName> - positional parameter that is the # name of this Docker container # --host - collective controller hostname # --port - collective controller https port # --user - collective controller admin user # --password - collective controller admin user password # --dockerHost - hostname of the Docker host this container resides on # # # Set initial values containerName=$1 # Parse the command line failure="false" if [[ -z $1 ]]; then # No values received echo "Error: no parameters specified" failure="true" else TEMP=`getopt --long host:,port:,user:,password:,dockerHost:: -- "$@"` eval set -- "$TEMP" OPTIND=-1 while true ; do case "$1" in --host) controllerHost=$2; shift 2;; --port) controllerPort=$2; shift 2;; --user) controllerUser=$2; shift 2;; --password) controllerPassword=$2; shift 2;; --dockerHost) dockerHost=$2; shift 2;; *) break;; esac done # Validate the required parameters if [[ -z "$containerName" ]]; then echo "Error: no containerName specified, validation suspended" failure="true" fi # if a containerName is not specified, bash uses the # 1st "--" parameter as the containerName if [[ $containerName == --* ]]; then echo "Error: no containerName specified, validation suspended" failure="true" fi # When the positional parameter containerName fails, # suspend validation reporting if [[ $failure == false ]]; then if [[ -z "${controllerHost// }" ]]; then echo "Error: no host specified" failure="true" fi if [[ -z "${controllerPort// }" ]]; then echo "Error: no port specified" failure="true" fi if [[ -z "${controllerUser// }" ]]; then echo "Error: no user specified" failure="true" fi if [[ -z "${controllerPassword// }" ]]; then echo "Error: no password specified" failure="true" fi if [[ -z "${dockerHost// }" ]]; then echo "Error: no dockerHost specified" failure="true" fi fi fi # Invoke the collective utility if [[ $failure == false ]]; then /opt/ibm/wlp/bin/collective remove defaultServer --host=$controllerHost --port=$controllerPort --user=$controllerUser --password=$controllerPassword --hostName=$dockerHost --autoAcceptCertificates else # At least one required parameter was missing, # notify the user of the correct syntax printf "Usage: removeMember <containerName> \n\t--host=<controller host> \n\t--port=<controller https port> \n\t--user=<controller user> \n\t--password=<controller password> \n\t--dockerHost=<docker host> \n" fi - この Dockerfile で使用する server.xml ファイルを構成します。以下に例を示します。
<server description="Default Server"> <featureManager> <feature>webProfile-6.0</feature> </featureManager> <httpEndpoint id="defaultHttpEndpoint" httpPort="${memberHttpPort}" httpsPort="${memberHttpsPort}" /> </server> - 集合コントローラーの server.xml ファイルには、必ず、コントローラーのユーザー名とパスワード、およびデプロイメント変数を定義するステートメントを含めてください。以下に例を示します。
<collectiveController user="admin" password="adminpwd" /> ... <deployVariable name="memberHttpPort" value="9090" increment="1"/> <deployVariable name="memberHttpsPort" value="9453" increment="1"/>デプロイメント変数は、各メンバーがターゲット・ホスト上で固有のポート番号を持てるようにする値を設定するものでなければなりません。 『deployVariable (deployVariable)』を参照してください。
- Docker の build コマンドを実行してイメージを作成します。
- Dockerfile、joinMember スクリプトと removeMember スクリプト、および Dockerfile 内に指定した server.xml ファイルを、Docker がインストールされているコンピューターにコピーします。 これらのファイルは、同じディレクトリーに入れてください。
- これらのファイルが保持されているディレクトリーで、コマンド・ウィンドウを開きます。
- 次のように、Docker コマンドを実行してイメージを作成します。
docker build -t image_name . - イメージの作成を検証します。次のコマンドを実行すると、正常に作成されていればイメージ名が返されます。
docker images
- オプション: Docker イメージを Docker Hub にポストします。
- Telnet や SSH などを使用して、ホストにログインします。
- Docker レジストリーにログインします。
- 次のようなコマンドを実行して Docker イメージをレジストリーにアップロードします。
docker push image_name
- Dockerfile を使用して Liberty イメージを作成します。
- ターゲット Docker ホストを集合コントローラーに登録します。
ホストを登録すると、集合コントローラーは Docker イメージをレジストリーからそのホストにプルできるほか、 そのホスト上のファイル、コマンド、およびその他のリソースにアクセスできるようになります。 registerHost コマンドを使用して、ターゲット・ホストを登録します。 コントローラーの server.xml ファイルで、 --host、--port、--user、および --password の各パラメーターの値を見つけます。 Linux や Windows などのオペレーティング・システム上のターゲット・ホストの場合などに、SSH 秘密鍵ファイルを使用しない場合は、 --rpcUser と --rpcUserPassword のパラメーターを設定して、オペレーティング・システム・ログイン・ユーザーとパスワードを含めます。 --rpcUser で指定するユーザーには、ターゲット・デプロイメント・ロケーションへのオペレーティング・システムの権限が必要です。
wlp/bin/collective registerHost targetHost --host=controllerHost --port=controllerHTTPSPort --user=controllerAdmin --password=controllerAdminPassword --rpcUser=osUser --rpcUserPassword=osUserPasswordホストが既に登録されている場合は、updateHost コマンドを使用して登録情報をリセットすることができます。 詳細については、『ホスト・コンピューターの Liberty 集合への登録』を参照してください。
- デプロイする Docker イメージが、デプロイ先の Docker ホスト上でローカルに使用できるのではなく、レジストリーに保管されている場合は、そのレジストリーにアクセスするように集合コントローラーを構成できます。 『集合コントローラーが Docker レジストリーを使用できるようにするための構成』を参照してください。
- 使用可能なデプロイメント・ルールを確認します。
- 使用可能なデプロイメント・ルールを返す REST API を起動します。
GET https://controller_host:controller_port/ibm/api/collective/v1/deployment/ruleこの起動により JSON 構造のデプロイメント・ルールが返されます。以下に例を示します。
{"rules": [ {"id":"Liberty Docker Rule","description":"Deploy rule for Liberty containers.","type":"docker","deployCommands":"docker run -d -e LICENSE=accept --net=host --name=${containerName} ${imageName} ; docker exec -t ${containerName} bash /opt/ibm/docker/joinMember ${containerName} --clusterName=${clusterName} --dockerHost=${wlp.deploy.host} --host=${wlp.admin.host} --port=${wlp.admin.port} --user=${wlp.admin.user} --password=${wlp.admin.password} --keystorePassword=${containerName}","undeployCommands":"docker rm ${containerName} ; docker rmi ${imageName}","startCommands":"docker start ${containerName}","stopCommands":"docker stop ${containerName}","retartCommands":" ","inputVariables":[ {"name":"containerName","type":"String","description":"Name of Liberty Docker container.","defaultValue":"${imageName}_container"}, {"name":"imageName","type":"String","description":"Name of Liberty Docker image."}, {"name":"clusterName","type":"String","description":"Name of Liberty Docker container cluster.","defaultValue":"${imageName}_cluster"} ]} - 必要であれば、DeployRules をコントローラー構成に追加することで、独自のデプロイメント・ルールを作成します。 『デプロイ・ルール (deployRule)』を参照してください。
- 使用可能なデプロイメント・ルールを返す REST API を起動します。
- Docker イメージをデプロイします。
デプロイメント・ルールには、使用する Docker イメージや、作成するコンテナー名など、操作の入力値の指定に使用できる変数が含まれています。戻り本文には、状況や結果の要求に使用できるトークンが含まれています。
POST https://controller_host:controller_port/ibm/api/collective/v1/deployment/deploy { "rule": "Liberty Docker Rule", "hosts": [ "myHostName" ], "variables": [ { "name":"imageName", "value":"myImageName" }, { "name":"containerName", "value":"myContainerName" }, { "name":"clusterName", "value":"myClusterName" }, { "name":"keystorePassword", "value":"thePassword" } ] }例えば、戻りトークンが {"id":3} であれば、これを使用して状況や結果を取得できます。
次のタスク
デプロイメントの状況や結果を要求して確認します。
- 要求したデプロイメント操作のトークンのリストを取得します。
GET https://controller_host:controller_port/ibm/api/collective/v1/deployment/deploy - デプロイメント操作の簡略状況を取得します。{token} には、ステップ 4 で得られたトークンを使用します。したがって、ステップ 4 での戻りトークンが {"id":3} の場合、{token} には 3 を使用します。
デプロイメント中であれば、API は次のように IN_PROGRESS を返します。GET https://controller_host:controller_port/ibm/api/collective/v1/deployment/{token}/status
デプロイメント後であれば、API は次のように FINISHED を返します。{"status":{"target_host":"IN_PROGRESS"}}{"status":{"target_host":"FINISHED"}} - デプロイメント操作の完全な結果を取得します。{token} には、ステップ 4 で得られたトークンを使用します。したがって、ステップ 4 での戻りトークンが {"id":3} の場合、{token} には 3 を使用します。
デプロイメント完了後であれば、結果には操作に関する情報が含まれています。以下の例は、FINISHED の場合の結果を示しています。GET https://controller_host:controller_port/ibm/api/collective/v1/deployment/{token}/results{"results":[{"host_name":"target_host","status":"FINISHED","result":0}]}
- コンテナーをアンデプロイします。
POST https://controller_host:controller_port/ibm/api/collective/v1/deployment/undeploy { "host": "myHostName", "userDir":"Docker", "serverName":"myContainerName" } - アンデプロイメント操作用のトークンのリストを取得します。
状況や結果を取得するには、デプロイメントと同様のステップに従います。GET https://controller_host:controller_port/ibm/api/collective/v1/deployment/undeploy
タスク・トピック
最終更新: Friday, 16 September 2016http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-dist&topic=twlp_deployservice_docker
ファイル名: twlp_deployservice_docker.html
