我们有使用.NET 堆栈的集群Windows 服务。目前,每个集群角色仅托管在两个节点上。部署和升级过程通过Ansible 执行。以下 sn-ps 仅涵盖升级部分。
服务部署使用Nuget 包。使用的.nuspec 如下所示。所以包代表.zip档案,其中包含根目录中的所有内容。
<?xml version="1.0"?>
<package>
<metadata>
<id>$Id$</id>
<version>$Version$</version>
<authors>$Authors$</authors>
<description> $Description$ </description>
<releaseNotes>$ReleaseNotes$</releaseNotes>
</metadata>
<files>
<file src="$PackageInput$" target=" "/>
</files>
</package>
当一个集群角色包含多个资源时,下面描述的角色可用于复合集群角色。
- name: 'Copy the cluster_role.ps1 to all hosts'
win_copy:
src : 'cluster_role.ps1'
dest: 'cluster_role.ps1'
需要此任务将PowerShell 脚本复制到所有主机,该脚本是检测所有者节点和在节点之间移动角色所必需的。
param([String]$ClusterRoleName, [String]$ExcludeNode)
# Task: Define the owner node
if ($ClusterRoleName -ne [string]::Empty -and $ExcludeNode -eq [string]::Empty)
{
Get-ClusterResource -Name $ClusterRoleName | Format-List -Property OwnerNode
exit
}
# Task: Move the cluster role
if ($ClusterRoleName -ne [string]::Empty -and $ExcludeNode -ne [string]::Empty)
{
Move-ClusterGroup $ClusterRoleName (Get-ClusterNode | Where-Object { $_.State -eq 'Up' -and $_.Name -ne $ExcludeNode })
exit
}
- name: 'Define the owner node'
win_shell: 'cluster_role.ps1 -ClusterRoleName {{ cluster_role }}'
register: owner_node
run_once: True
when: 'cluster_role is defined'
- name: 'Define the owner node metadata'
set_fact:
owner_node_host: '{{ owner_node.stdout.split(":")[1] | trim }}.{{ windows_domain }}'
owner_node_name: '{{ owner_node.stdout.split(":")[1] | trim }}'
run_once: True
when: 'cluster_role is defined'
需要这些任务来检测所有者节点。第一个任务返回所有者节点,例如:s001srv000。第二个任务创建以下类型的两个变量:
owner_node_host : s001srv.domain.net
owner_node_name: s001srv000
- name: 'Apply the application roles on the inactive nodes'
include_role:
name: '{{ item }}'
when : 'cluster_role is defined and (cluster_sets is defined or cluster_full is defined) and owner_node_host != inventory_hostname'
with_items: '{{ dependencies }}'
这些任务包括其他角色,例如下载新版本包,根据环境生成服务配置等。在非活动节点上执行。
- pause:
prompt : 'A manual failover must be manually performed'
minutes: 30
run_once : True
when: 'cluster_full is defined and environment_type == "prod"'
- name: 'Move the cluster role'
win_shell: 'cluster_role.ps1 -ClusterRoleName {{ cluster_role }} -ExcludeNode {{ owner_node_name }}'
run_once : True
when: 'cluster_move is defined or cluster_full is defined'
需要这些任务来控制升级流程,如果当前环境是STG,则升级将自动执行,否则在暂停时手动故障转移。
- name: 'Apply the application roles on the nodes which were active a moment ago'
include_role:
name: '{{ item }}'
when : 'cluster_role is defined and cluster_full is defined and owner_node_host == inventory_hostname'
这些任务与'Apply the application roles on the inactive nodes' 相同,但针对的是刚刚处于活动状态的节点。