样书配置

用于创建基本负载平衡配置的样书

在本节中,您将设计一个样书来创建负载平衡配置,其中包括负载平衡虚拟服务器、服务组和服务列表。样书将服务绑定到服务组并将服务组绑定到虚拟服务器。

要创建基本的负载平衡配置,请使用您在样书中创建的示例样书,lb-vserver创建负载平衡虚拟服务器并将其另存为 basic-lb-config.yaml

标头

要构建此样书,请更新标题部分。此部分类似于您在样书中创建负载平衡虚拟服务器的标题部分。

name 的值更改为基本的 lb-config。更新描述显示名称以描述样书。不必更改 namespaceversion 值。因为您已经更改了名称,所以在系统中创建了这个样书的唯一标识符。

name: basic-lb-config
namespace: com.example.stylebooks
version: "0.1"
display-name: Load Balancing Configuration
description: This StyleBook defines a simple load balancing configuration.
schema-version: "1.0"
<!--NeedCopy-->

导入样书

import-stylebooks 部分保持不变。它引用 netscaler.nitro.config 命名空间以使用 NITRO 配置对象。

import-stylebooks:
 -
 namespace: netscaler.nitro.config
 prefix: ns
 version: "10.5"
<!--NeedCopy-->

参数

更新参数部分以再添加两个参数。

参数定义服务或服务器列表以及服务侦听的端口。前三个参数 nameiplb-alg 保持不变。

parameters:
 -
  name: name
  type: string
  label: Application Name
  description: Give a name to the application configuration.
  required: true
 -
  name: ip
  type: ipaddress
  label: Application Virtual IP (VIP)
  description: The Application VIP that clients access
  required: true
 -
  name: lb-alg
  type: string
  label: LoadBalancing Algorithm
  description: Choose the loadbalancing algorithm (method) used for loadbalancing client requests between the application servers.
  allowed-values:
     - ROUNDROBIN
     - LEASTCONNECTION
  default: ROUNDROBIN
 -
  name: svc-servers
  type: ipaddress[]
  label: Application Server IPs
  description: The IP addresses of all the servers of this application
  required: true
 -
  name: svc-port
  type: tcp-port
  label: Server Port
  description: The TCP port open on the application servers to receive requests.
  default: 80
<!--NeedCopy-->

添加 svc-servers 参数是为了接受应用程序后端服务器的 IP 地址列表。svc-servers 是必填参数,如 required: true 所示。

第二个参数 svc-port 表示服务器侦听的端口号。除非用户指定,否则默认端口号为 80。

组件

您还必须更新组件部分以定义更多组件,以便它们使用两个新参数并构建完整的负载平衡配置。

例如,必须按如下所示编写 components 部分:

components:
 -
  name: lbvserver-comp
  type: ns::lbvserver
  properties:
   name: $parameters.name + "-lb"
   servicetype: HTTP
   ipv46: $parameters.ip
   port: 80
   lbmethod: $parameters.lb-alg

components:
 -
  name: svcg-comp
  type: ns::servicegroup
  properties:
   name: $parameters.name + "-svcgrp"
   servicetype: HTTP
  
  components:
    -
      name: lbvserver-svg-binding-comp
      type: ns::lbvserver_servicegroup_binding
      properties:
          name: $parent.parent.properties.name
          servicegroupname: $parent.properties.name
    -
      name: members-svcg-comp
      type: ns::servicegroup_servicegroupmember_binding
      repeat: $parameters.svc-servers
      repeat-item: srv
      properties:
         ip: $srv
         port: str($parameters.svc-port)
         servicegroupname: $parent.properties.name
<!--NeedCopy-->

在此示例中,原始组件 lbvserver-comp (来自前面的示例)现在有一个名为 svcg-comp的子组件。svcg-comp 组件中还有两个子组件。通过在一个组件里面嵌套另一个组件,嵌套的组件可以通过引用父组件中的属性来创建配置对象。嵌套的组件可以为父组件中创建的每个对象创建一个或多个对象。

svcg-comp 组件用于使用为资源属性提供的值在 NetScaler 实例上创建服务组 servicegroup。在此示例中,您正在为 servicetype 指定静态值,而 name 从输入参数获取其值。使用 $parameters.name + “-svcgrp” 表示法引用参数部分中定义的参数名称,其中 -svcgrp 附加(连接)到用户定义的名称后。

组件 svcg-comp 有两个子组件, lbvserver-svg-binding-compmembers-svcg-comp

第一个子组件 lbvserver svg-binding-comp用于在父组件创建的服务组和父组件创建的负载平衡虚拟服务器 (lbvserver) 之间绑定配置对象。$parent 表示法(也称为父引用)用于引用父组件中的实体。例如,servicegroupname: $parent.properties.name 指由父组件 svcg-comp创建的服务组, 名称:$parent.parent.properties.nam e 指由父组件 lbvserver-comp创建的虚拟服务器。

成员 svcg 组件用于将服务列表之间的配置对象绑定到父组件创建的服务组。绑定配置对象的创建是通过使用样书的重复结构遍历参数 svc-servers 中指定的服务器列表来实现的。在迭代期间,此样书组件为服务组中的每个服务(在 repeat-item 构造中称为 srv)创建一个 servicegroup_servicegroupmember_binding 类型的 NITRO 配置对象,并将每个 NITRO 配置对象中的 ip 属性设置为相应服务器的 IP 地址。

通常,可以在组件中使用 重复和重复 项 构造来使该组件构建多个相同类型的配置对象。例如,您可以为 repeat-item 构造指定变量名称,例如 srv,以指定迭代中的当前值。此变量名称在相同组件的属性或子组件中引用$<varname>,例如 $srv。

在前面的示例中,组件的嵌套有助于轻松构造配置。您也可以在不嵌套的情况下构建配置,如以下示例所示:

components:
 -
  name: lbvserver-comp
  type: ns::lbvserver
  properties:
   name: $parameters.name + "-lb"
   servicetype: HTTP
   ipv46: $parameters.ip
   port: 80
   lbmethod: $parameters.lb-alg
 -
  name: svcg-comp
  type: ns::servicegroup
  properties:
    servicegroupname: $parameters.name + "-svcgrp"
    servicetype: HTTP
 -
  name: lbvserver-svg-binding-comp
  type: ns::lbvserver_servicegroup_binding
  properties:
   name: $components.lbvserver-comp.properties.name
   servicegroupname: $components.svcg-comp.properties.servicegroupname
 -
  name: members-svcg-comp
  type: ns::servicegroup_servicegroupmember_binding
  repeat: $parameters.svc-servers
  repeat-item: srv
  properties:
   ip: $srv
   port: 80
   servicegroupname: $components.svcg-comp.properties.servicegroupname  
<!--NeedCopy-->

在这里,尽管组件未嵌套,但生成的 NetScaler 配置与之前使用的嵌套组件相同。

在样书中声明组件的顺序不影响配置对象的创建顺序。在此示例中,尽管最后声明了组件 svcg-comp 和 lbvserver-comp,但必须在构建第二个组件 lbvserver-svg-binding-comp 之前构建,因为第二个组件中有对这些组件的前向引 用。

注意

按照惯例,样书、参数、替换、组件和输出的名称均为小写。当它们包含许多单词时,它们由“-”字符分隔。例如 lb-bindingsapp-namerewrite-config 等。另一个惯例是用 -comp 字符串作为组件名称的后缀。

输出

在本节中,您需要指定样书在创建配置后向用户(或在其他样书中)显示的内容。例如,您可以指定公开由此样书创建的 lbvserverservicegroup 配置对象。

outputs:
-
  name: lbvserver-comp
  value: $components.lbvserver-comp
  description: The component that builds the Nitro lbvserver configuration object
-
  name: servicegroup-comp
  value: $components.svcg-comp
  description: The component that builds the Nitro servicegroup configuration object
<!--NeedCopy-->

样书的输出部分是可选的。样书不必返回输出。但是,通过返回一些内部组件作为输出,它允许任何导入此样书的样书具有更大的灵活性。此功能在创建复合样书时很有用。

注意

最好在输出部分公开样书的整个组件,而不仅仅是组件的单个属性(例如,公开整个 $components.lbvserver-comp,而不仅仅是名称 $components.lbvserver-comp.properties.name)。另外在输出中添加说明,解释特定输出表示的内容。

构建您的样书

现在已定义了此样书的所有必要部分,将它们全部汇聚在一起即可构建您的第二个样书。已将此样书文件保存为 basic-lb-config.yaml。Citrix 建议您使用样书中的内置 YAML 验证程序来验证和导入 YAML 内容。

basic-lb-config.yaml 文件的完整内容转载如下:

name: basic-lb-config
namespace: com.example.stylebooks
version: "0.1"
display-name: Load Balancing Configuration
description: This StyleBook defines a simple load balancing configuration.
schema-version: "1.0"

import-stylebooks:
 -
  namespace: netscaler.nitro.config
  version: "10.5"
  prefix: ns
parameters:
 -
  name: name
  type: string
  label: Application Name
  description: Give a name to the application configuration.
  required: true
 -
  name: ip
  type: ipaddress
  label: Application Virtual IP (VIP)
  description: The Application VIP that clients access
  required: true
 -
  name: lb-alg
  type: string
  label: LoadBalancing Algorithm
  description: Choose the loadbalancing algorithm (method) used for loadbalancing client requests between the application servers.
  allowed-values:
     - ROUNDROBIN
     - LEASTCONNECTION
  default: ROUNDROBIN
 -
  name: svc-servers
  type: ipaddress[]
  label: Application Server IPs
  description: The IP addresses of all the servers of this application
  required: true
 -
  name: svc-port
  type: tcp-port
  label: Server Port
  description: The TCP port open on the application servers to receive requests.
  default: 80

components:
 -
  name: lbvserver-comp
  type: ns::lbvserver
  properties:
   name: $parameters.name + "-lb"
   servicetype: HTTP
   ipv46: $parameters.ip
   port: 80
   lbmethod: $parameters.lb-alg
 -
  name: svcg-comp
  type: ns::servicegroup
  properties:
    servicegroupname: $parameters.name + "-svcgrp"
    servicetype: HTTP
 -
  name: lbvserver-svg-binding-comp
  type: ns::lbvserver_servicegroup_binding
  properties:
   name: $components.lbvserver-comp.properties.name
   servicegroupname: $components.svcg-comp.properties.servicegroupname
 -
  name: members-svcg-comp
  type: ns::servicegroup_servicegroupmember_binding
  repeat: $parameters.svc-servers
  repeat-item: srv
  properties:
   ip: $srv
   port: 80
   servicegroupname: $components.svcg-comp.properties.servicegroupname

outputs:
-
  name: lbvserver-comp
  value: $components.lbvserver-comp
  description: The component that builds the Nitro lbvserver configuration object
-
  name: servicegroup-comp
  value: $components.svcg-comp
  description: The component that builds the Nitro servicegroup configuration object
<!--NeedCopy-->

要开始使用样书创建配置,必须将其导入到 NetScaler 控制台,然后再使用。有关详细信息,请参阅如何使用用户定义的样书

还可以将此样书导入其他样书并使用其属性,如下一节中所述。

用于创建基本负载平衡配置的样书