
使用部署 REST API 部署 Liberty Docker 容器
您可以使用 DeployService REST API 部署 Docker 容器中的 Liberty 服务器作为集合体的成员。
开始之前
确定要安装 Docker 映像的目标主机。将 Docker 安装在目标主机上。每个目标主机都需要 RXA 或 SSH。请参阅设置 RXA 以执行 Liberty 集合体操作。
观看:WASdev 文章中的管理包含 WebSphere Liberty 集合体的内部部署多语种环境视频对本主题中的过程进行了补充。
过程
- 创建 Docker 映像。
- 使用 Dockerfile 创建 Liberty 映像。
以下 Dockerfile 示例可创建 Liberty Network Deployment 映像。Docker 主数据中心上提供的映像无法访问 Network Deployment 功能部件,例如 scalingMember-1.0。可以使用此文件来创建 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/注: The Docker image has a joinMember script that joins the member server in the Docker container to a collective controller:
该 Docker 映像还具有 removeMember 脚本,该脚本用于从集合体中除去 Docker 容器中的成员服务器:#!/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 - 配置 server.xml 文件,以使其与 Dockerfile 配合使用;例如:
<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 构建命令以创建映像。
- 将 Dockerfile joinMember 和 removeMember 脚本以及 Dockerfile 中指定的 server.xml 文件复制到安装了 Docker 的计算机。将这些文件放在同一目录中。
- 在保存这些文件的目录中打开命令窗口。
- 运行 Docker 命令以创建映像:
docker build -t image_name - 验证映像创建。如果创建成功,那么以下命令会返回映像名称。
docker images
- 可选: 在 Docker 主数据中心上发布 Docker 映像。
- 使用 Telnet 或 SSH 之类的协议登录主机。
- 登录 Docker 注册表。
- 通过运行以下命令之类的命令将 Docker 映像上载到该注册表:
docker push image_name
- 使用 Dockerfile 创建 Liberty 映像。
- 向集合体控制器注册目标 Docker 主机。
注册主机使得集合体控制器可以将 Docker 映像从注册表拉取到该主机,以及访问该主机上的文件、命令和其他资源。可以使用 registerHost 命令来注册目标主机。请查看控制器的 server.xml 文件以查找 --host、--port、--user 和 --password 参数的值。要避免使用 SSH 专用密钥文件(例如,对于 Linux 或 Windows 操作系统上的目标主机),请通过设置 --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
