1. 程式人生 > >Yarn 3.1.1 - Yarn 服務 - 快速開始

Yarn 3.1.1 - Yarn 服務 - 快速開始

ecif min json mas name sed try 數量 support

快速開始

本文描述了如何用 Yarn 服務框架在 Yarn 上部署服務。

配置和啟動 HDFS 和 Yarn 組件

首先啟動 HDFS 和 Yarn 的各個組件。為啟用 Yarn 服務框架,添加以下參數到 yarn-site.xml 文件中並重啟 ResourceManager,或在 ResourceManager 啟動前就設置號。該參數是啟用 Yarn 服務框架的 CLI 和 REST API 所必需的。

<property>
  <description>
    在 ResourceManager 啟用 Yarn 服務框架的 REST API。
  </description>
  <name>yarn.webapp.api-service.enable</name>
  <value>true</value>
</property>

示例服務

以下是一個簡單的服務定義,它通過一個簡單的說明文件在 Yarn 上啟動容器執行睡眠命令,整個過程無需寫任何代碼。

{
  "name": "sleeper-service",
  "components" : 
    [
      {
        "name": "sleeper",
        "number_of_containers": 1,
        "launch_command": "sleep 900000",
        "resource": {
          "cpus": 1, 
          "memory": "256"
       }
      }
    ]
}

執行以下命令,用戶可以簡單地運行一個預先提供的樣例服務:

yarn app -launch <service-name> <example-name>

舉例來說,以下命令在 Yarn 上啟動了一個 sleeper 服務並命名為 my-sleeper。

yarn app -launch my-sleeper sleeper

如果要使用 Yarn 服務框架啟動基於 Docker 的服務,請參閱 API 文檔。

通過 CLI 管理 Yarn 上的服務

以下步驟實現用 CLI 部署一個 Yarn 服務。請參閱 Yarn 命令集以獲得完整的命令列表。

部署一個服務

yarn app -launch ${SERVICE_NAME} ${PATH_TO_SERVICE_DEF_FILE}

參數 SERVICE_NAME 在某個用戶名下必須是唯一的。參數 PATH_TO_SERVICE_DEF 是 Json 格式的服務定義文件夾的路徑。

舉例來說:

yarn app -launch sleeper-service /path/to/local/sleeper.json

伸縮一個服務的組件

為一個組件增加或減少容器數量:

yarn app -flex ${SERVICE_NAME} -component ${COMPONENT_NAME} ${NUMBER_OF_CONTAINERS}

距離來說,對於一個名叫 sleeper-service 的服務,設置其名為 sleeper 的組件的容器數量為 2 個(絕對值):

yarn app -flex sleeper-service -component sleeper 2

在伸縮命令中,${NUMBER_OF_CONTAINERS} 同樣可以是相對值,如 +2 或 -2。

停止一個服務

停止一個服務將停止該服務的所有容器和關聯的 ApplicationMaster,但不會刪除服務的狀態,如在 HDFS 上的服務的根目錄。

yarn app -stop ${SERVICE_NAME}

重啟一個停止的服務

重啟一個停止的服務非常容易,只需調用啟動命令!

yarn app -start ${SERVICE_NAME}

銷毀一個服務

在停止服務之外,它還會刪除該服務在 HDFS 上的根目錄以及在 Yarn Service Registry 裏的記錄。

yarn app -destroy ${SERVICE_NAME}

通過 REST API 管理 Yarn 上的服務

當 yarn.webapp.api-service.enable 為 true時,YARN API Server REST API 作為 ResourceManager 的一部分被激活。

可以通過 ResourceManager 的網絡入口在 Yarn 上部署服務。

參閱 API 文檔以獲得更多 API 說明。

部署一個服務

POST 提交前面提供的樣例服務的定義到 ResourceManager API 服務入口:

POST  http://localhost:8088/app/v1/services

獲取服務的狀態

GET  http://localhost:8088/app/v1/services/${SERVICE_NAME}

伸縮服務的組件

PUT  http://localhost:8088/app/v1/services/${SERVICE_NAME}/components/${COMPONENT_NAME}

PUT 請求體:

{
  "name": "${COMPONENT_NAME}",
  "number_of_containers": ${COUNT}
}

舉例來說:

{
  "name": "sleeper",
  "number_of_containers": 2
}

停止一個服務

停止一個服務將停止該服務的所有容器和關聯的 ApplicationMaster,但不會刪除服務的狀態,如在 HDFS 上的服務的根目錄。

PUT  http://localhost:8088/app/v1/services/${SERVICE_NAME}

PUT 請求體:

{
  "name": "${SERVICE_NAME}",
  "state": "STOPPED"
}

重啟一個停止的服務

重啟一個停止的服務非常容易:

PUT  http://localhost:8088/app/v1/services/${SERVICE_NAME}

PUT 請求體:

{
  "name": "${SERVICE_NAME}",
  "state": "STARTED"
}

銷毀一個服務

在停止服務之外,它還會刪除該服務在 HDFS 上的根目錄以及在 Yarn Service Registry 裏的記錄。

DELETE  http://localhost:8088/app/v1/services/${SERVICE_NAME}

嵌入 Yarn UI2 的服務 UI 和 Timeline Service v2

A new service tab is added in the YARN UI2 specially to show YARN Services in a first class manner. The services framework posts the data into TimelineService and the service UI reads data from TimelineService to render its content.

Enable Timeline Service v2

Please refer to TimeLineService v2 doc for how to enable Timeline Service v2.

Enable new YARN UI

Set below config in yarn-site.xml and start ResourceManager. If you are building from source code, make sure you use -Pyarn-ui in the mvn command - this will generate the war file for the new YARN UI.

  <property>
    <description>To enable RM web ui2 application.</description>
    <name>yarn.webapp.ui2.enable</name>
    <value>true</value>
  </property>

安全地運行

YARN service framework supports running in a secure (kerberized) environment. User needs to specify the kerberos principal name and keytab when they launch the service. E.g. A typical configuration looks like below:

{
  "name": "sample-service",
  ...
  ...
  "kerberos_principal" : {
    "principal_name" : "hdfs-demo/[email protected]",
    "keytab" : "file:///etc/security/keytabs/hdfs.headless.keytab"
  }
}

Note that _HOST is required in the principal_name field because Hadoop client validates that the server’s (in this case, the AM’s) principal has hostname present when communicating to the server. * principal_name : the principal name of the user who launches the service * keytab : URI of the keytab. Currently supports only files present on the bare host. * URI starts with file:// - A path on the local host where the keytab is stored. It is assumed that admin pre-installs the keytabs on the local host before AM launches.

用 Docker 運行

The above example is only for a non-docker container based service. YARN Service Framework also provides first-class support for managing docker based services. Most of the steps for managing docker based services are the same except that in docker the Artifact type for a component is DOCKER and the Artifact id is the name of the docker image. For details in how to setup docker on YARN, please check Docker on YARN.

With docker support, it also opens up a set of new possibilities to implement features such as discovering service containers on YARN with DNS. Check ServiceDiscovery for more details.

Yarn 3.1.1 - Yarn 服務 - 快速開始