部署高可用WordPress网站

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


搭建模板框架

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

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

模板内容解析

ROSTemplateFormatVersion是固定值,不用修改。

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

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

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

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


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

在高可用的架构中,我们需要为每个 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

利用 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"
              docker pull docker.io/library/wordpress:5.6
              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


添加输出信息(可选)

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

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


完整模板示例

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"
              docker pull docker.io/library/wordpress:5.6
              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




相关文章
|
1月前
|
安全
SiteGround如何设置WordPress网站自动更新
iteGround Autoupdate功能会自动帮我们更新在他们这里托管的所有WordPress网站,这样做是为了保证网站安全,并且让它们一直保持最新状态。他们会根据我们选择的设置自动更新不同版本的WordPress,包括主要版本和次要版本。在每次自动更新之前,他们都会为我们的网站做一个完整的备份,这样如果有什么问题,我们可以轻松地恢复到之前的状态。在本文中,我们将介绍如何在SiteGround中设置WordPress网站自动更新。
54 0
SiteGround如何设置WordPress网站自动更新
|
1月前
|
关系型数据库 MySQL Apache
怎么在树莓派上搭建WordPress博客网站,并发布到外网可访问?
怎么在树莓派上搭建WordPress博客网站,并发布到外网可访问?
|
1月前
|
PHP
菊子曰备份Wordpress网站的博客内容出错的解决办法(原创)
菊子曰备份Wordpress网站的博客内容出错的解决办法(原创)
17 2
|
3天前
|
弹性计算 Java Linux
部署单点WordPress网站
在特定场景下编写模板的流程比较固定,本篇文章以《部署单点 WordPress 博客平台》为例,讲述如何完成一个部署成功率高、适配场景广的模板。大多数在 ECS 上部署应用的模板都可以参考此教程来编写。
|
1天前
|
关系型数据库 Serverless PHP
一元建站-基于函数计算 + wordpress 构建 serverless 网站
一元建站-基于函数计算 + wordpress 构建 serverless 网站
|
1月前
|
域名解析 搜索推荐 网络安全
小白教程Cloudways搭建WordPress网站
得益于开源的CMS建站系统的兴起,如今搭建网站已经变得非常简单,即使是不懂编程的人也能轻松搭建自己的网站。本文将以最受欢迎的WordPress开源建站系统为例,向大家介绍如何使用CloudWays搭建WordPress网站。首先你需要注册域名(推荐NameSilo)和购买主机服务器(推荐Cloudways),然后注册Cloudways账号,接下来是创建Cloudways主机并安装WordPress、设置域名解析、域名绑定、安装SSL证书。完成这些操作后你就可以访问WordPress后台了,接着就是安装WordPress主题和WordPress插件
77 0
|
1月前
|
存储 XML 前端开发
信息收集 -- WordPress网站
信息收集 -- WordPress网站
40 0
|
1月前
WordPress网站更换域名后如何重新激活elementor
本文讲解WordPress网站更换域名后如何重新激活elementor。首先你需要在WordPress后台elementor下点击Disconnect断开原elementor连接,然后登录elementor官网,在后台解除原网站授权;接着在WordPress后台重新连接并激活elementor。
81 2
WordPress网站更换域名后如何重新激活elementor
|
1月前
给WordPress网站增加一个带时间的led广告牌
给WordPress网站增加一个带时间的led广告牌
38 3
|
1月前
|
域名解析 弹性计算 数据安全/隐私保护
阿里云ECS免费搭建WordPress个人博客网站
阿里云ECS免费搭建WordPress个人博客网站
569 2
阿里云ECS免费搭建WordPress个人博客网站