Application Delivery Management

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

在上一个示例中,您已构建了一个基本样本来创建负载平衡虚拟服务器。可以用不同的名称保存此样本,然后对其更新以包含用于基本负载平衡配置的其他参数和组件。将此样本文件保存为 basic-lb-config.yaml

在本节中,将设计一个新样本,用于创建由负载平衡虚拟服务器、服务组和一组服务组成的负载平衡配置。它还将服务绑定至服务组,并将服务组绑定至虚拟服务器。

标题

要构建此样本,必须先更新标头部分。此部分与为负载平衡虚拟服务器样本创建的标头部分类似。在标头部分中,将 name 值更改为 basic-lb-config。此外,更新 descriptiondisplay-name 以适当说明此样本。不必更改 namespaceversion 值。因为已更改了名称,所以 name、namespace 和 version 组合构成了此样本在系统中的唯一标识符。

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

导入样本

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

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

参数

必须更新 parameters 部分以添加两个其他参数来定义一组服务或服务器以及服务侦听的端口。前三个参数 name、ip 和 lb-alg 保持不变。

parameters:
 -
 name: name
 type: string
 label: Application Name
 description: Name of the application configuration
 required: true
  -
   name: ip
   type: ipaddress
   label: Application Virtual IP (VIP)
   description: Application VIP that the clients access
   required: true
  -
   name: lb-alg
   type: string
   label: LoadBalancing Algorithm
   description: Choose the load balancing algorithm used for load balancing 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 地址列表。这是必需参数,由 required: true 指明。第二个参数 svc-port 表示服务器侦听的端口号。如果用户未指定,svc-port 参数的默认端口号是 80。

组件

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

例如,必须按如下所示编写 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 组件用于通过使用为资源 “服务组” 属性提供的值在 Citrix ADC 实例上创建服务组。在此示例中,为 servicetype 指定静态值,而 name 从输入参数获取其值。您可以通过使用 **$ 参数部分中定义的参数名称。name + “-svcgrp” 表示法来**引用参数名称,其中-svcgrp附加(连接)到用户定义的名称。

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

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

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

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

在上述示例中,使用了在组件中相互嵌套组件来轻松构造此配置。在此特殊情况下,嵌套组件不是唯一的配置构建方式。您可以在没有嵌套的情况下实现相同的结果,如下所示:

components:
 -
  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: $components.svcg-comp.properties.name
 -
  name: lbvserver-svg-binding-comp
  type: ns::lbvserver_servicegroup_binding
  properties:
   name: $components.lbvserver-comp.properties.name
   servicegroupname: $components.svcg-comp.properties.name
 -
  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:
  name: $parameters.name + "-svcgrp"
  servicetype: HTTP  
<!--NeedCopy-->

在这里,所有组件都处于相同级别(也就是说,它们不嵌套),但实现的结果(生成的 Citrix ADC 配置)与之前使用的嵌套组件相同。此外,样本中组件的声明顺序不影响配置对象的创建顺序。在此示例中,组件 svcg-complbvserver-comp尽管最后声明,但必须在构建第二个组件 lbvserver-svg-bindingcomp之前构建,因为在第二个组件中存在对这些组件的前向引用。

注意

按约定,样本的名称、parameters、substitutions、components 和 outputs 采用小写。如果它们包含多个词语,词语以“-”字符分隔。例如“lb-bindings”、“app-name”、“rewrite-config”等。另一个约定是为组件名称添加后缀“-comp”字符串。

输出

可以添加到新样本的最后一个部分是 outputs 部分,在其中指定在此样本用于创建配置后,此样本向其用户呈现的内容(或在其他样本中)。例如,可以在 outputs 部分指定要呈现将由此样本创建的 lbvserver 和 servicegroup 配置对象。

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-->

样书的 outputs 部分是可选的。样书不必返回输出。但是,如果将一些内部组件作为输出返回,导入此样本的任何样本就可以有更大的灵活性,这在创建复合样本时可以看到。

注意

最好在 outputs 部分呈现样本的整个组件,而不仅仅是组件的单个属性(例如,呈现整个 $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

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-->

要开始使用样本创建配置,您必须将其导入 Citrix ADM,然后使用它。有关详细信息,请参阅 如何使用用户定义的样本

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

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