1.3. 例:アプリケーションの自動スケーリング


前述の機能も、アプリケーションのスケールアップにも使用できます。たとえば、一度に実行される複数のインスタンスの 1 つによって提供される動的な Web ページなどです。この場合、neutronLoad Balancing-as-a-Service を提供するように設定できます。これは、インスタンス間でトラフィックを均等に分散するのに均等に機能します。

以下の例では、Orchestration は Telemetry データを再度検査し、CPU 使用率が高いとインスタンスの数を増やすか、CPU 使用率がセット値を下回った場合にはインスタンスの数を減らします。

  1. ロードバランサー 環境のプロパティーを記述するテンプレートを作成します。/etc/heat/templates/lb-env.yaml に以下の値を入力します。

    heat_template_version: 2014-10-16
    description: A load-balancer server
    parameters:
      image:
        type: string
        description: Image used for servers
      key_name:
        type: string
        description: SSH key to connect to the servers
      flavor:
        type: string
        description: flavor used by the servers
      pool_id:
        type: string
        description: Pool to contact
      user_data:
        type: string
        description: Server user_data
      metadata:
        type: json
      network:
        type: string
        description: Network used by the server
    
    resources:
      server:
        type: OS::Nova::Server
        properties:
          flavor: {get_param: flavor}
          image: {get_param: image}
          key_name: {get_param: key_name}
          metadata: {get_param: metadata}
          user_data: {get_param: user_data}
          networks:
            - port: { get_resource: port }
    
      member:
        type: OS::Neutron::PoolMember
        properties:
          pool_id: {get_param: pool_id}
          address: {get_attr: [server, first_address]}
          protocol_port: 80
    
      port:
        type: OS::Neutron::Port
        properties:
          network: {get_param: network}
          security_groups:
            - base
    
    outputs:
      server_ip:
        description: IP Address of the load-balanced server.
        value: { get_attr: [server, first_address] }
      lb_member:
        description: LB member details.
        value: { get_attr: [member, show] }
    Copy to Clipboard Toggle word wrap
  2. Web アプリケーションを実行するインスタンス用に別のテンプレートを作成します。以下のテンプレートはロードバランサーを作成し、既存のネットワークを使用します。お使いの環境に応じてパラメーターを置き換え、テンプレートを /root/lb-webserver-rhel7.yaml などのファイルに保存します。

    heat_template_version: 2014-10-16
    description: AutoScaling RHEL 7 Web Application
    parameters:
      image:
        type: string
        description: Image used for servers
        default: RHEL 7
      key_name:
        type: string
        description: SSH key to connect to the servers
        default: admin
      flavor:
        type: string
        description: flavor used by the web servers
        default: m2.tiny
      network:
        type: string
        description: Network used by the server
        default: private
      subnet_id:
        type: string
        description: subnet on which the load balancer will be located
        default: 9daa6b7d-e647-482a-b387-dd5f855b88ef
      external_network_id:
        type: string
        description: UUID of a Neutron external network
        default: db17c885-77fa-45e8-8647-dbb132517960
    
    resources:
      webserver:
        type: OS::Heat::AutoScalingGroup
        properties:
          min_size: 1
          max_size: 3
          cooldown: 60
          desired_capacity: 1
          resource:
            type: file:///etc/heat/templates/lb-env.yaml
            properties:
              flavor: {get_param: flavor}
              image: {get_param: image}
              key_name: {get_param: key_name}
              network: {get_param: network}
              pool_id: {get_resource: pool}
              metadata: {"metering.stack": {get_param: "OS::stack_id"}}
              user_data:
                str_replace:
                  template: |
                    #!/bin/bash -v
    
                    yum -y install httpd php
                    systemctl enable httpd
                    systemctl start httpd
                    cat <<EOF > /var/www/html/hostname.php
                    <?php echo "Hello, My name is " . php_uname('n'); ?>
                    EOF
                  params:
                    hostip: 192.168.122.70
                    fqdn: sat6.example.com
                    shortname: sat6
    
      web_server_scaleup_policy:
        type: OS::Heat::ScalingPolicy
        properties:
          adjustment_type: change_in_capacity
          auto_scaling_group_id: {get_resource: webserver}
          cooldown: 60
          scaling_adjustment: 1
    
      web_server_scaledown_policy:
        type: OS::Heat::ScalingPolicy
        properties:
          adjustment_type: change_in_capacity
          auto_scaling_group_id: {get_resource: webserver}
          cooldown: 60
          scaling_adjustment: -1
    
      cpu_alarm_high:
        type: OS::Ceilometer::Alarm
        properties:
          description: Scale-up if the average CPU > 95% for 1 minute
          meter_name: cpu_util
          statistic: avg
          period: 60
          evaluation_periods: 1
          threshold: 95
          alarm_actions:
            - {get_attr: [web_server_scaleup_policy, alarm_url]}
          matching_metadata: {'metadata.user_metadata.stack': {get_param: "OS::stack_id"}}
          comparison_operator: gt
    
      cpu_alarm_low:
        type: OS::Ceilometer::Alarm
        properties:
          description: Scale-down if the average CPU < 15% for 1 minute
          meter_name: cpu_util
          statistic: avg
          period: 60
          evaluation_periods: 1
          threshold: 15
          alarm_actions:
            - {get_attr: [web_server_scaledown_policy, alarm_url]}
          matching_metadata: {'metadata.user_metadata.stack': {get_param: "OS::stack_id"}}
          comparison_operator: lt
    
      monitor:
        type: OS::Neutron::HealthMonitor
        properties:
          type: TCP
          delay: 5
          max_retries: 5
          timeout: 5
    
      pool:
        type: OS::Neutron::Pool
        properties:
          protocol: HTTP
          monitors: [{get_resource: monitor}]
          subnet_id: {get_param: subnet_id}
          lb_method: ROUND_ROBIN
          vip:
            protocol_port: 80
    
      lb:
        type: OS::Neutron::LoadBalancer
        properties:
          protocol_port: 80
          pool_id: {get_resource: pool}
    
      lb_floating:
        type: OS::Neutron::FloatingIP
        properties:
          floating_network_id: {get_param: external_network_id}
          port_id: {get_attr: [pool, vip, port_id]}
    
    outputs:
      scale_up_url:
        description: >
          This URL is the webhook to scale up the autoscaling group.  You
          can invoke the scale-up operation by doing an HTTP POST to this
          URL; no body nor extra headers are needed.
        value: {get_attr: [web_server_scaleup_policy, alarm_url]}
      scale_dn_url:
        description: >
          This URL is the webhook to scale down the autoscaling group.
          You can invoke the scale-down operation by doing an HTTP POST to
          this URL; no body nor extra headers are needed.
        value: {get_attr: [web_server_scaledown_policy, alarm_url]}
      pool_ip_address:
        value: {get_attr: [pool, vip, address]}
        description: The IP address of the load balancing pool
      website_url:
        value:
          str_replace:
            template: http://serviceip/hostname.php
            params:
              serviceip: { get_attr: [lb_floating, floating_ip_address] }
        description: >
          This URL is the "external" URL that can be used to access the
          website.
      ceilometer_query:
        value:
          str_replace:
            template: >
              ceilometer statistics -m cpu_util
              -q metadata.user_metadata.stack=stackval -p 60 -a avg
            params:
              stackval: { get_param: "OS::stack_id" }
        description: >
          This is a Ceilometer query for statistics on the cpu_util meter
          Samples about OS::Nova::Server instances in this stack.  The -q
          parameter selects Samples according to the subject's metadata.
          When a VM's metadata includes an item of the form metering.X=Y,
          the corresponding Ceilometer resource has a metadata item of the
          form user_metadata.X=Y and samples about resources so tagged can
          be queried with a Ceilometer query term of the form
          metadata.user_metadata.X=Y.  In this case the nested stacks give
          their VMs metadata that is passed as a nested stack parameter,
          and this stack passes a metadata of the form metering.stack=Y,
          where Y is this stack's ID.
    Copy to Clipboard Toggle word wrap
  3. Telemetry コレクションの間隔を更新します。デフォルトでは、Telemetry は CPU データに対して 10 分ごとにインスタンスをポーリングします。この例では、/etc/ceilometer/pipeline.yaml で間隔を 60 秒に変更します。

    - name: cpu_source
    interval: 60
    meters:
    - "cpu"
    sinks:
    - cpu_sink
    Copy to Clipboard Toggle word wrap
    注記

    実稼働環境では、ポーリング期間が 60 秒ほど推奨しません。ポーリング間隔が長くなると、コントロールプレーンの負荷が増加する可能性があるためです。

  4. すべての OpenStack サービスを再起動して、更新された Telemetry 設定を適用します。

    # openstack-service restart
    Copy to Clipboard Toggle word wrap
    注記

    このステップにより、OpenStack デプロイメントの停止が短くなります。

  5. オーケストレーションスクリプトを実行します。これにより、環境がビルドされ、テンプレートを使用してインスタンスをデプロイします。

    # heat stack-create webfarm -f /root/lb-webserver-rhel7.yaml
    Copy to Clipboard Toggle word wrap

    /root/lb-webserver-rhel7.yaml を実際のパスおよびファイル名に置き換えます。

Orchestration Stacks Webfarm で、Dashboard でスタックの作成をモニターできます。スタックが作成されると、以下のような、有用な情報が複数表示されます。

  • 手動スケールアップまたはスケールダウンイベントをトリガーするために使用できる URL。
  • Web サイトの IP アドレスである Floating IP アドレス。
  • スタックの CPU 負荷を表示する Telemetry コマンド。スケーリングが予想通りに機能しているかどうかを確認することができます。

ダッシュボードでページは次のようになります。

heat stack output

Network Load Balancers を開いて、ロードバランサーを表示します。

load balancer

Members をクリックします。このページには、負荷分散プールのメンバーが表示されます。これらは、Web サイトのトラフィックを分散できるインスタンスです。対応するインスタンスが作成され、Apache がインストールおよび設定されるまで、メンバーは Active ステータスにならないことに注意してください。

Web サーバーを起動すると、インスタンスはロードバランサーのアクティブなメンバーとして表示されます。

load balancer member active

これで、http://IP/hostname.php で Web アプリケーションにアクセスできるようになりました。以下のような出力が表示されるはずです。

Hello, My name is we-zrwm-t4ezkpx34gxu-qbg5d7dqbc4j-server-mzdvigk2jugl
Copy to Clipboard Toggle word wrap

ダッシュボード のスタックの概要から Telemetry コマンドを実行して、スタックの CPU パフォーマンスデータを表示できるようになりました。コマンドは以下のようになります。

# ceilometer statistics -m cpu_util -q metadata.user_metadata.stack=8f86c3d5-15cf-4a64-b9e8-70215498c046 -p 60 -a avg
Copy to Clipboard Toggle word wrap

1.3.1. 自動スケーリングアプリケーションのテスト

アプリケーションのスケーリングを手動でトリガーするには、Dashboard のスタック概要から REST スケールアップ URL を使用するか、最初にデプロイされたインスタンスでリソース集約型コマンドを実行して負荷を生成します。

  • REST API を使用するには、HTTP POST リクエストを実行できるツールが必要です。たとえば、REST Easy Firefox add oncurl などです。スケールアップ URL をコピーし、REST Easy の形式で貼り付けます。

    scale up

    または、curl コマンドラインでパラメーターとして使用します。

    $ curl -X POST "scale-up URL"
    Copy to Clipboard Toggle word wrap
  • 負荷を人為的に生成するには、インスタンスに Floating IP を確保し、SSH でログインし、CPU がビジー状態になるコマンドを実行します。以下に例を示します。

    $ dd if=/dev/zero of=/dev/null &
    Copy to Clipboard Toggle word wrap
    重要

    たとえば top コマンドを使用して、CPU 使用率が 95% を超えるかどうかを確認します。CPU 使用率が十分に高くない場合は、dd コマンドを並行して実行するか、別の方法を使用して CPU をビジー状態に維持します。

次回 Telemetry がスタックから CPU データを収集すると、スケールアップイベントがトリガーされ、Orchestration Stacks Webfarm Events に表示されます。新しい Web サーバーインスタンスが作成され、ロードバランサーに追加されます。これが完了すると、インスタンスはアクティブになり、Web サイト URL がロードバランサー経由でスタックの両方のインスタンスにルーティングされることがわかります。

注記

インスタンスの起動には数分かかることがあります。なぜなら、インスタンスの初期化、Apache のインストールと設定、およびアプリケーションをデプロイする必要があるためです。これは HAProxy によってモニターされます。これにより、Web サイトがアクティブとしてマークされる前にインスタンスで利用可能になります。

新規インスタンスの作成中の Dashboard で、負荷分散プールのメンバーのリストは次のようになります。

scale up load balancer

重要

追加のインスタンスを作成するかどうかを決定する際には、heat スタックのインスタンスの平均 CPU 使用率が考慮されます。2 番目のインスタンスは通常の CPU 使用率を持つ可能性が最も高いため、最初のインスタンスのバランスを取り除きます。ただし、2 番目のインスタンスもビジーになり、最初のインスタンスと 2 番目のインスタンスの平均 CPU 使用率が 95% を超えると、別のインスタンス(3 番目の)インスタンスが作成されます。

1.3.2. アプリケーションの自動スケーリング

これは、スタックの CPU 使用率が事前定義の値を下回ると、スケールダウンポリシーが実行される点で 「自動スケーリングアプリケーションのテスト」 と似ています。これは、「自動スケーリングアプリケーションのテスト」 で説明されている例の 15% です。さらに、インスタンスがスタックから削除されると、ロードバランサーからも自動的に削除されます。Web サイトのトラフィックは、残りのインスタンスに自動的に分散されます。

トップに戻る
Red Hat logoGithubredditYoutubeTwitter

詳細情報

試用、購入および販売

コミュニティー

Red Hat ドキュメントについて

Red Hat をお使いのお客様が、信頼できるコンテンツが含まれている製品やサービスを活用することで、イノベーションを行い、目標を達成できるようにします。 最新の更新を見る.

多様性を受け入れるオープンソースの強化

Red Hat では、コード、ドキュメント、Web プロパティーにおける配慮に欠ける用語の置き換えに取り組んでいます。このような変更は、段階的に実施される予定です。詳細情報: Red Hat ブログ.

会社概要

Red Hat は、企業がコアとなるデータセンターからネットワークエッジに至るまで、各種プラットフォームや環境全体で作業を簡素化できるように、強化されたソリューションを提供しています。

Theme

© 2025 Red Hat