部署高可用WordPress网站

简介: 高可用服务是另外一个高频使用的场景,编写模板的流程和《部署单点WordPress网站》一样,但涉及的资源更多一些。本文以《部署高可用WordPress网站》为例,介绍高可用部署类的模板如何编写。

1. 搭建模板框架

高可用部署涉及到资源较多,包括负载均衡、数据库等资源,高可用部署也要求跨可用区部署,所以模板会相对复杂,但框架模板跟具体的应用一般来说关系不大,这里提供一个具体高可用的示例模板框架。

ROSTemplateFormatVersion: '2015-09-01'
Description:
  en: Deploy a highly available WordPress website.
  zh-cn: 部署高可用WordPress网站。
Parameters:
  CommonName:
    Type: String
    Default: highly-available
  ZoneId1:
    Type: String
    AssociationProperty: 'ALIYUN::ECS::Instance::ZoneId'
    AssociationPropertyMetadata:
      ExclusiveTo:
        - ZoneId2
    Label:
      en: Availability Zone
      zh-cn: 可用区1
  ZoneId2:
    Type: String
    AssociationProperty: 'ALIYUN::ECS::Instance::ZoneId'
    AssociationPropertyMetadata:
      ExclusiveTo:
        - ZoneId1
    Label:
      en: Availability Zone
      zh-cn: 可用区2
  InstanceType1:
    Type: String
    AssociationProperty: 'ALIYUN::ECS::Instance::InstanceType'
    AssociationPropertyMetadata:
      InstanceChargeType: PostPaid
      SystemDiskCategory: cloud_essd
      ZoneId: ${ZoneId}
    Label:
      en: Instance Type
      zh-cn: 实例规格1
  InstanceType2:
    Type: String
    AssociationProperty: 'ALIYUN::ECS::Instance::InstanceType'
    AssociationPropertyMetadata:
      InstanceChargeType: PostPaid
      SystemDiskCategory: cloud_essd
      ZoneId: ${ZoneId}
    Label:
      en: Instance Type
      zh-cn: 实例规格2
  InstancePassword:
    NoEcho: true
    Type: String
    Description:
      en: >-
        Server login password, Length 8-30, must contain three(Capital letters,
        lowercase letters, numbers, ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ Special
        symbol in)
      zh-cn: >-
        服务器登录密码,长度8-30,必须包含三项(大写字母、小写字母、数字、 ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/
        中的特殊符号)
    Label:
      en: Instance Password
      zh-cn: 实例密码
    ConstraintDescription:
      en: >-
        Length 8-30, must contain three(Capital letters, lowercase letters,
        numbers, ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ Special symbol in)
      zh-cn: '长度8-30,必须包含三项(大写字母、小写字母、数字、 ()`~!@#$%^&*_-+=|{}[]:;''<>,.?/ 中的特殊符号)'
    AssociationProperty: 'ALIYUN::ECS::Instance::Password'
    Default: null
  DBUserName:
    Type: String
    ConstraintDescription:
      en: >-
        Consist of 2 to 32 characters of lowercase letters, underline. 
        Must begin with a letter and be end with an alphanumeric character
      zh-cn: >-
        由 2 到 32 个小写字母组成,支持小写字母、数字和下划线,以小写字母开头。
    Label:
      zh-cn: RDS数据库账号
      en: RDS DB Username
    Default: wordpress
    AllowedPattern: '^[a-z][a-z0-9_]{0,31}$'
  DBPassword:
    NoEcho: true
    Type: String
    Description:
      en: >-
        RDS user password, Length 8-30, must contain three(Capital letters,
        lowercase letters, numbers, ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ Special
        symbol in)
      zh-cn: >-
        数据库账号密码,长度8-30,必须包含三项(大写字母、小写字母、数字、 ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/
        中的特殊符号)
    Label:
      en: RDS Instance Password
      zh-cn: RDS数据库密码
    ConstraintDescription:
      en: >-
        Length 8-30, must contain three(Capital letters, lowercase letters,
        numbers, ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ Special symbol in)
      zh-cn: '长度8-30,必须包含三项(大写字母、小写字母、数字、 ()`~!@#$%^&*_-+=|{}[]:;''<>,.?/ 中的特殊符号)'
    AssociationProperty: 'ALIYUN::ECS::Instance::Password'
  DBInstanceClass:
    Type: String
    Label:
      en: RDS Instance Class
      zh-cn: RDS实例规格
    AssociationProperty: ALIYUN::RDS::Instance::InstanceType
    AssociationPropertyMetadata:
      ZoneId:
        Ref: ZoneId
      EngineVersion: '8.0'
      DBInstanceStorageType: cloud_essd
      Engine: MySQL
      Category: Basic
    Default: mysql.n2e.small.1
Resources:
  Vpc:
    Type: 'ALIYUN::ECS::VPC'
    Properties:
      CidrBlock: 192.168.0.0/16
      VpcName:
        Fn::Sub: ${CommonName}-vpc
  VSwitch1:
    Type: 'ALIYUN::ECS::VSwitch'
    Properties:
      VpcId:
        Ref: Vpc
      CidrBlock: 192.168.1.0/24
      ZoneId:
        Ref: ZoneId1
      VSwitchName:
        Fn::Sub: ${CommonName}-vsw
  VSwitch2:
    Type: 'ALIYUN::ECS::VSwitch'
    Properties:
      VpcId:
        Ref: Vpc
      CidrBlock: 192.168.2.0/24
      ZoneId:
        Ref: ZoneId2
      VSwitchName:
        Fn::Sub: ${CommonName}-vsw
  SecurityGroup:
    Type: 'ALIYUN::ECS::SecurityGroup'
    Properties:
      VpcId:
        Ref: Vpc
      SecurityGroupName:
        Fn::Sub: ${CommonName}-sg
      SecurityGroupIngress:
        - PortRange: 22/22
          SourceCidrIp: 0.0.0.0/0
          IpProtocol: tcp
        - PortRange: 443/443
          SourceCidrIp: 0.0.0.0/0
          IpProtocol: tcp
        - PortRange: 80/80
          SourceCidrIp: 0.0.0.0/0
          IpProtocol: tcp
  EcsInstance1:
    Type: 'ALIYUN::ECS::InstanceGroup'
    Properties:
      VpcId:
        Ref: Vpc
      ZoneId:
        Ref: ZoneId1
      VSwitchId:
        Ref: VSwitch1
      SecurityGroupId:
        Ref: SecurityGroup
      ImageId: aliyun_3_9_x64_20G_alibase_
      InstanceName:
        Fn::Sub: ${CommonName}-ecs-1
      InstanceType:
        Ref: InstanceType1
      SystemDiskCategory: cloud_essd
      MaxAmount: 1
      InternetMaxBandwidthOut: 10
      Password:
        Ref: InstancePassword
  EcsInstance2:
    Type: 'ALIYUN::ECS::InstanceGroup'
    Properties:
      VpcId:
        Ref: Vpc
      ZoneId:
        Ref: ZoneId2
      VSwitchId:
        Ref: VSwitch2
      SecurityGroupId:
        Ref: SecurityGroup
      ImageId: aliyun_3_9_x64_20G_alibase_
      InstanceName:
        Fn::Sub: ${CommonName}-ecs-2
      InstanceType:
        Ref: InstanceType2
      SystemDiskCategory: cloud_essd
      MaxAmount: 1
      InternetMaxBandwidthOut: 10
      Password:
        Ref: InstancePassword
  Alb:
    Type: 'ALIYUN::ALB::LoadBalancer'
    Properties:
      LoadBalancerName:
        Fn::Sub: ${CommonName}-alb
      LoadBalancerEdition: Basic
      VpcId:
        Ref: Vpc
      LoadBalancerBillingConfig:
        PayType: PostPay
      AddressType: Internet
      ZoneMappings:
        - ZoneId:
            Ref: ZoneId1
          VSwitchId:
            Ref: VSwitch1
        - ZoneId:
            Ref: ZoneId2
          VSwitchId:
            Ref: VSwitch2
  AlbServerGroup:
    Type: 'ALIYUN::ALB::ServerGroup'
    Properties:
      VpcId:
        Ref: Vpc
      ServerGroupType: Instance
      ServerGroupName:
        Fn::Sub: ${CommonName}-server-group
      HealthCheckConfig:
        HealthCheckConnectPort: 80
        HealthCheckCodes:
          - http_2xx
          - http_3xx
        HealthCheckProtocol: HTTP
        HealthCheckEnabled: true
        HealthCheckPath: /
      StickySessionConfig:
        StickySessionEnabled: false
  AlbBackendServerAttachment:
    Type: 'ALIYUN::ALB::BackendServerAttachment'
    Properties:
      ServerGroupId:
        Ref: AlbServerGroup
      Servers:
        - ServerType: Ecs
          ServerId:
            Ref: EcsInstance1
          Port: 80
        - ServerType: Ecs
          ServerId:
            Ref: EcsInstance2
          Port: 80
  AlbListener:
    Type: 'ALIYUN::ALB::Listener'
    Properties:
      ListenerPort: 80
      DefaultActions:
        - Type: ForwardGroup
          ForwardGroupConfig:
            ServerGroupTuples:
              - ServerGroupId:
                  Ref: AlbServerGroup
      LoadBalancerId:
        Ref: Alb
      ListenerProtocol: HTTP
  RdsInstance:
    Type: 'ALIYUN::RDS::DBInstance'
    Properties:
      DBInstanceClass:
        Ref: DBInstanceClass
      ZoneId:
        Ref: ZoneId1
      DBInstanceStorage: 40
      Category: Basic
      DBInstanceStorageType: cloud_essd
      VSwitchId:
        Ref: VSwitch1
      Engine: MySQL
      VpcId:
        Ref: Vpc
      SlaveZoneIds:
        - Auto
      EngineVersion: '8.0'
      SecurityIPList: '192.168.0.0/16'
      MasterUsername:
        Ref: DBUserName
      MasterUserPassword:
        Ref: DBPassword
      MasterUserType: Super
  RdsDatabase:
    Type: ALIYUN::RDS::Database
    Properties:
      CharacterSetName: utf8mb4
      DBInstanceId:
        Ref: RdsInstance
      DBName: wordpress
Metadata:
  'ALIYUN::ROS::Interface':
    ParameterGroups:
      - Parameters:
          - ZoneId1
          - ZoneId2
        Label:
          default:
            zh-cn: 可用区配置
            en: ZoneId Configuration
      - Parameters:
          - InstanceType1
          - InstanceType2
          - InstancePassword
        Label:
          default:
            zh-cn: ECS 实例规格配置
            en: ECS InstanceType Configuration
      - Parameters:
          - DBUserName
          - DBPassword
          - DBInstanceClass
        Label:
          default:
            zh-cn: RDS 数据库配置
            en: RDS Configuration
    TemplateTags:
      - acs:developer:gitee:1234-在ECS实例上搭建WordPress博客平台
    Hidden:
      - CommonName

image.png

1.1 模板内容解析

ROSTemplateFormatVersion是固定值,不用修改。

Description模板描述信息,我们用enzh-cn表示多语言。

Parameters表示参数信息,我们把可能影响到库存的可用区和实例规格等信息提取出来作为参数,实际部署时用户可以在前端选择有库存的地域和规格,保障部署的成功率

Resources表示创建的资源,在高可用的架构中,我们一般会创建 1 个VPC、2 个不同可用区的 VSwitch、一个安全组、2 个 ECS ;为了实现负载均衡,我们会创建 1 个 ALB 实例、1 个服务器组、一个监听并且将2个ECS加到服务器组中;2 个 ECS 会共用一个数据库,所以我们创建了 1 个RDS 实例和一个数据库。

Metadata中定义了参数的顺序、标签和不需要用户感知的参数。

2. 搭建应用运行环境(可选)

在高可用的架构中,我们需要为每个 ECS 实例都搭建运行环境,本次示例中,我们为上述的 2 台 ECS 都安装 Docker 运行环境。

Resources:
  ModuleInstallDocker:
    Version: default
    Type: 'MODULE::ACS::OOS::Extension'
    Properties:
      # 与单机版本不同的是这里指定了 2 个 ECS 实例
      EcsInstanceIds:
        - Ref: EcsInstance1
        - Ref: EcsInstance2
      PackageName: ACS-Extension-DockerCE-1853370294850618

3. 利用 Shell 脚本完成应用的部署

在本次示例中,我们在 Docker 中运行 WordPress,可以在 RunCommand 资源中指定 2 台 ECS。这里需要注意,此脚本的运行依赖上一步的 Docker 环境,所以在资源中我们通过 DependsOn来表示依赖关系,在创建过程中就会先部署 Docker 环境,再执行我们的脚本。

Resources:
  InstallWordPress:
    Type: 'ALIYUN::ECS::RunCommand'
    # 这里我们设置依赖关系,先安装 Docker
    DependsOn: ModuleInstallDocker
    Properties:
      InstanceIds:
        - Ref: EcsInstance1
        - Ref: EcsInstance2
      Type: RunShellScript
      Sync: true
      Timeout: 3600
      CommandContent:
        Fn::Sub: |-
          #!/bin/bash
                    
          # script exit code:
          # 0 - success
          # 1 - unsupported system
          # 2 - failed to deploy WordPress
          
          # 环境变量配置
          export PATH=/usr/local/bin:$PATH
          
          function log_info() {
              printf "%s [INFO] %s\n" "$(date '+%Y-%m-%d %H:%M:%S')" "$1"
          }
          
          function log_error() {
              printf "%s [ERROR] %s\n" "$(date '+%Y-%m-%d %H:%M:%S')" "$1"
          }
          
          function log_fatal() {
              printf "\n========================================================================\n"
              printf "%s [FATAL] %s\n" "$(date '+%Y-%m-%d %H:%M:%S')" "$2"
              printf "\n========================================================================\n"
              exit $1
          }
          
          function debug_exec(){
              local cmd="$@"
              log_info "$cmd"
              eval "$cmd"
              ret=$?
              echo ""
              log_info "$cmd, exit code: $ret"
              return $ret
          }
          
          
          function unsupported_system() {
              log_fatal 1 "Unsupported System: $1"
          }
          
          # 安装 WodPress 函数
          function deploy_wordpress() {
              log_info "start deploy WordPress"
              wget 'https://ros-public-templates.oss-cn-hangzhou.aliyuncs.com/file/wordpress-5.6.tar'
              docker load -i wordpress-5.6.tar
              mkdir /wordpress
              docker run --name my-blog --restart=always -v /wordpress:/var/www/html -p 80:80 -d \
              --env WORDPRESS_DB_HOST=${RdsInstance.InnerConnectionString} \
              --env WORDPRESS_DB_USER=${DBUserName} \
              --env WORDPRESS_DB_PASSWORD=${DBPassword} \
              --env WORDPRESS_DB_NAME=wordpress wordpress:5.6
          }
          
          log_info "System Information:"
          if ! lsb_release -a; then
              unsupported_system
          fi;
          echo ""
          
          if ! debug_exec deploy_wordpress; then
              log_fatal 3 "failed to deploy WordPress"
          fi

4. 添加输出信息(可选)

在高可用区的架构中,用户是通过 ALB 来访问我们部署在 ECS 的应用的,所以我们在输出中定义 ALB 的域名,以便用户可以轻松访问 WordPress 网站。

Outputs:
  WebUrl:
    Description:
      zh-cn: Web 访问地址。
      en: The Addresses of Web.
    Value:
      'Fn::Sub': 'http://${Alb.DNSName}'

5. 完整模板示例

ROSTemplateFormatVersion: '2015-09-01'
Description:
  en: Deploy a highly available WordPress website.
  zh-cn: 部署高可用WordPress网站。
Parameters:
  CommonName:
    Type: String
    Default: highly-available
  ZoneId1:
    Type: String
    AssociationProperty: 'ALIYUN::ECS::Instance::ZoneId'
    AssociationPropertyMetadata:
      ExclusiveTo:
        - ZoneId2
    Label:
      en: Availability Zone
      zh-cn: 可用区1
  ZoneId2:
    Type: String
    AssociationProperty: 'ALIYUN::ECS::Instance::ZoneId'
    AssociationPropertyMetadata:
      ExclusiveTo:
        - ZoneId1
    Label:
      en: Availability Zone
      zh-cn: 可用区2
  InstanceType1:
    Type: String
    AssociationProperty: 'ALIYUN::ECS::Instance::InstanceType'
    AssociationPropertyMetadata:
      InstanceChargeType: PostPaid
      SystemDiskCategory: cloud_essd
      ZoneId: ${ZoneId}
    Label:
      en: Instance Type
      zh-cn: 实例规格1
  InstanceType2:
    Type: String
    AssociationProperty: 'ALIYUN::ECS::Instance::InstanceType'
    AssociationPropertyMetadata:
      InstanceChargeType: PostPaid
      SystemDiskCategory: cloud_essd
      ZoneId: ${ZoneId}
    Label:
      en: Instance Type
      zh-cn: 实例规格2
  InstancePassword:
    NoEcho: true
    Type: String
    Description:
      en: >-
        Server login password, Length 8-30, must contain three(Capital letters,
        lowercase letters, numbers, ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ Special
        symbol in)
      zh-cn: >-
        服务器登录密码,长度8-30,必须包含三项(大写字母、小写字母、数字、 ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/
        中的特殊符号)
    Label:
      en: Instance Password
      zh-cn: 实例密码
    ConstraintDescription:
      en: >-
        Length 8-30, must contain three(Capital letters, lowercase letters,
        numbers, ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ Special symbol in)
      zh-cn: '长度8-30,必须包含三项(大写字母、小写字母、数字、 ()`~!@#$%^&*_-+=|{}[]:;''<>,.?/ 中的特殊符号)'
    AssociationProperty: 'ALIYUN::ECS::Instance::Password'
    Default: null
  DBUserName:
    Type: String
    ConstraintDescription:
      en: >-
        Consist of 2 to 32 characters of lowercase letters, underline. 
        Must begin with a letter and be end with an alphanumeric character
      zh-cn: >-
        由 2 到 32 个小写字母组成,支持小写字母、数字和下划线,以小写字母开头。
    Label:
      zh-cn: RDS数据库账号
      en: RDS DB Username
    Default: wordpress
    AllowedPattern: '^[a-z][a-z0-9_]{0,31}$'
  DBPassword:
    NoEcho: true
    Type: String
    Description:
      en: >-
        RDS user password, Length 8-30, must contain three(Capital letters,
        lowercase letters, numbers, ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ Special
        symbol in)
      zh-cn: >-
        数据库账号密码,长度8-30,必须包含三项(大写字母、小写字母、数字、 ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/
        中的特殊符号)
    Label:
      en: RDS Instance Password
      zh-cn: RDS数据库密码
    ConstraintDescription:
      en: >-
        Length 8-30, must contain three(Capital letters, lowercase letters,
        numbers, ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ Special symbol in)
      zh-cn: '长度8-30,必须包含三项(大写字母、小写字母、数字、 ()`~!@#$%^&*_-+=|{}[]:;''<>,.?/ 中的特殊符号)'
    AssociationProperty: 'ALIYUN::ECS::Instance::Password'
  DBInstanceClass:
    Type: String
    Label:
      en: RDS Instance Class
      zh-cn: RDS实例规格
    AssociationProperty: ALIYUN::RDS::Instance::InstanceType
    AssociationPropertyMetadata:
      ZoneId:
        Ref: ZoneId
      EngineVersion: '8.0'
      DBInstanceStorageType: cloud_essd
      Engine: MySQL
      Category: Basic
    Default: mysql.n2e.small.1
Resources:
  Vpc:
    Type: 'ALIYUN::ECS::VPC'
    Properties:
      CidrBlock: 192.168.0.0/16
      VpcName:
        Fn::Sub: ${CommonName}-vpc
  VSwitch1:
    Type: 'ALIYUN::ECS::VSwitch'
    Properties:
      VpcId:
        Ref: Vpc
      CidrBlock: 192.168.1.0/24
      ZoneId:
        Ref: ZoneId1
      VSwitchName:
        Fn::Sub: ${CommonName}-vsw
  VSwitch2:
    Type: 'ALIYUN::ECS::VSwitch'
    Properties:
      VpcId:
        Ref: Vpc
      CidrBlock: 192.168.2.0/24
      ZoneId:
        Ref: ZoneId2
      VSwitchName:
        Fn::Sub: ${CommonName}-vsw
  SecurityGroup:
    Type: 'ALIYUN::ECS::SecurityGroup'
    Properties:
      VpcId:
        Ref: Vpc
      SecurityGroupName:
        Fn::Sub: ${CommonName}-sg
      SecurityGroupIngress:
        - PortRange: 22/22
          SourceCidrIp: 0.0.0.0/0
          IpProtocol: tcp
        - PortRange: 443/443
          SourceCidrIp: 0.0.0.0/0
          IpProtocol: tcp
        - PortRange: 80/80
          SourceCidrIp: 0.0.0.0/0
          IpProtocol: tcp
  EcsInstance1:
    Type: 'ALIYUN::ECS::InstanceGroup'
    Properties:
      VpcId:
        Ref: Vpc
      ZoneId:
        Ref: ZoneId1
      VSwitchId:
        Ref: VSwitch1
      SecurityGroupId:
        Ref: SecurityGroup
      ImageId: aliyun_3_9_x64_20G_alibase_
      InstanceName:
        Fn::Sub: ${CommonName}-ecs-1
      InstanceType:
        Ref: InstanceType1
      SystemDiskCategory: cloud_essd
      MaxAmount: 1
      InternetMaxBandwidthOut: 10
      Password:
        Ref: InstancePassword
  EcsInstance2:
    Type: 'ALIYUN::ECS::InstanceGroup'
    Properties:
      VpcId:
        Ref: Vpc
      ZoneId:
        Ref: ZoneId2
      VSwitchId:
        Ref: VSwitch2
      SecurityGroupId:
        Ref: SecurityGroup
      ImageId: aliyun_3_9_x64_20G_alibase_
      InstanceName:
        Fn::Sub: ${CommonName}-ecs-2
      InstanceType:
        Ref: InstanceType2
      SystemDiskCategory: cloud_essd
      MaxAmount: 1
      InternetMaxBandwidthOut: 10
      Password:
        Ref: InstancePassword
  Alb:
    Type: 'ALIYUN::ALB::LoadBalancer'
    Properties:
      LoadBalancerName:
        Fn::Sub: ${CommonName}-alb
      LoadBalancerEdition: Basic
      VpcId:
        Ref: Vpc
      LoadBalancerBillingConfig:
        PayType: PostPay
      AddressType: Internet
      ZoneMappings:
        - ZoneId:
            Ref: ZoneId1
          VSwitchId:
            Ref: VSwitch1
        - ZoneId:
            Ref: ZoneId2
          VSwitchId:
            Ref: VSwitch2
  AlbServerGroup:
    Type: 'ALIYUN::ALB::ServerGroup'
    Properties:
      VpcId:
        Ref: Vpc
      ServerGroupType: Instance
      ServerGroupName:
        Fn::Sub: ${CommonName}-server-group
      HealthCheckConfig:
        HealthCheckConnectPort: 80
        HealthCheckCodes:
          - http_2xx
          - http_3xx
        HealthCheckProtocol: HTTP
        HealthCheckEnabled: true
        HealthCheckPath: /
      StickySessionConfig:
        StickySessionEnabled: false
  AlbBackendServerAttachment:
    Type: 'ALIYUN::ALB::BackendServerAttachment'
    Properties:
      ServerGroupId:
        Ref: AlbServerGroup
      Servers:
        - ServerType: Ecs
          ServerId:
            Ref: EcsInstance1
          Port: 80
        - ServerType: Ecs
          ServerId:
            Ref: EcsInstance2
          Port: 80
  AlbListener:
    Type: 'ALIYUN::ALB::Listener'
    Properties:
      ListenerPort: 80
      DefaultActions:
        - Type: ForwardGroup
          ForwardGroupConfig:
            ServerGroupTuples:
              - ServerGroupId:
                  Ref: AlbServerGroup
      LoadBalancerId:
        Ref: Alb
      ListenerProtocol: HTTP
  RdsInstance:
    Type: 'ALIYUN::RDS::DBInstance'
    Properties:
      DBInstanceClass:
        Ref: DBInstanceClass
      ZoneId:
        Ref: ZoneId1
      DBInstanceStorage: 40
      Category: Basic
      DBInstanceStorageType: cloud_essd
      VSwitchId:
        Ref: VSwitch1
      Engine: MySQL
      VpcId:
        Ref: Vpc
      SlaveZoneIds:
        - Auto
      EngineVersion: '8.0'
      SecurityIPList: '192.168.0.0/16'
      MasterUsername:
        Ref: DBUserName
      MasterUserPassword:
        Ref: DBPassword
      MasterUserType: Super
  RdsDatabase:
    Type: ALIYUN::RDS::Database
    Properties:
      CharacterSetName: utf8mb4
      DBInstanceId:
        Ref: RdsInstance
      DBName: wordpress
  ModuleInstallDocker:
    Version: default
    Type: 'MODULE::ACS::OOS::Extension'
    Properties:
      EcsInstanceIds:
        - Ref: EcsInstance1
        - Ref: EcsInstance2
      PackageName: ACS-Extension-DockerCE-1853370294850618
  InstallWordPress:
    Type: 'ALIYUN::ECS::RunCommand'
    DependsOn: ModuleInstallDocker
    Properties:
      InstanceIds:
        - Ref: EcsInstance1
        - Ref: EcsInstance2
      Type: RunShellScript
      Sync: true
      Timeout: 3600
      CommandContent:
        Fn::Sub: |-
          #!/bin/bash
                    
          # script exit code:
          # 0 - success
          # 1 - unsupported system
          # 2 - failed to deploy ...
          
          # 环境变量配置
          export PATH=/usr/local/bin:$PATH
          
          function log_info() {
              printf "%s [INFO] %s\n" "$(date '+%Y-%m-%d %H:%M:%S')" "$1"
          }
          
          function log_error() {
              printf "%s [ERROR] %s\n" "$(date '+%Y-%m-%d %H:%M:%S')" "$1"
          }
          
          function log_fatal() {
              printf "\n========================================================================\n"
              printf "%s [FATAL] %s\n" "$(date '+%Y-%m-%d %H:%M:%S')" "$2"
              printf "\n========================================================================\n"
              exit $1
          }
          
          function debug_exec(){
              local cmd="$@"
              log_info "$cmd"
              eval "$cmd"
              ret=$?
              echo ""
              log_info "$cmd, exit code: $ret"
              return $ret
          }
          
          
          function unsupported_system() {
              log_fatal 1 "Unsupported System: $1"
          }
          
          
          function deploy_wordpress() {
              log_info "start deploy wordpress"
              wget 'https://ros-public-templates.oss-cn-hangzhou.aliyuncs.com/file/wordpress-5.6.tar'
              docker load -i wordpress-5.6.tar
              mkdir /wordpress
              docker run --name my-blog --restart=always -v /wordpress:/var/www/html -p 80:80 -d \
              --env WORDPRESS_DB_HOST=${RdsInstance.InnerConnectionString} \
              --env WORDPRESS_DB_USER=${DBUserName} \
              --env WORDPRESS_DB_PASSWORD=${DBPassword} \
              --env WORDPRESS_DB_NAME=wordpress wordpress:5.6
          }
          
          log_info "System Information:"
          if ! lsb_release -a; then
              unsupported_system
          fi;
          echo ""
          
          if ! debug_exec deploy_wordpress; then
              log_fatal 3 "failed to deploy wordpress"
          fi
Outputs:
  WebUrl:
    Description:
      zh-cn: Web 访问地址。
      en: The Addresses of Web.
    Value:
      'Fn::Sub': 'http://${Alb.DNSName}'
Metadata:
  'ALIYUN::ROS::Interface':
    ParameterGroups:
      - Parameters:
          - ZoneId1
          - ZoneId2
        Label:
          default:
            zh-cn: 可用区配置
            en: ZoneId Configuration
      - Parameters:
          - InstanceType1
          - InstanceType2
          - InstancePassword
        Label:
          default:
            zh-cn: ECS 实例规格配置
            en: ECS InstanceType Configuration
      - Parameters:
          - DBUserName
          - DBPassword
          - DBInstanceClass
        Label:
          default:
            zh-cn: RDS 数据库配置
            en: RDS Configuration
    TemplateTags:
      - acs:developer:gitee:1234-在ECS实例上搭建WordPress博客平台
    Hidden:
      - CommonName


点击此处或扫描下方二维码,加入「开源上云」,一起把开源项目「做大做强,再创辉煌」!


海报.jpg



作者介绍
目录