探索 OpenStack 之(16):計量模塊 Ceilometer 介紹及優化

0. 背景

0.1 爲什麼要有 Ceilometer?

通常雲,特別是公有云在計費方面有三個層次:

  • 計量 (Metering): 收集資源的使用數據,其數據信息主要包括:使用對象(what), 使用者(who), 使用時間(when)和 用量(how much)。
  • 計費 (Rating):將資源使用數據按照商務規則轉化爲可計費項目並計算費用
  • 結算 (Billing):收錢開票

Ceilometer 的目標是 計量 Metering 方面,爲上層的計費、結算或者監控應用提供統一的資源使用數據收集功能。

"To provide an infrastructure to collect any information needed within all OpenStack projects so that rating engines could use this single point to transform events into bill items, which we tagged “metering“."

0.2 歷史

  • 項目開始於2012年四五月份,由 Julien Danjou, Dreamhost 和 Canonical等發起。
  • 2012年的10月份(Folsom版本),Ceilometer發佈了它的v1.0版本,在 第一個版本中,Ceilometer主要實現了對一些重要數據的計量,包括Compute, Network, Memory, CPU, Image, Volume等,並且提供了REST API。
  • 2013年2月,Ceilometer完成了由Incubation到Integrated的轉變,這意味着Ceilometer將作爲OpenStack 發行版的一部分而發佈。
  • Grizzly 中,Ceilometer添加了對 Swift 的支持,增加了SQLAlchemy作爲Storage Backend,開發了Multi Publisher, 並且發佈了V2版本的API。
  • Havana 中,Ceilometer主要增加了 HBase 作爲Storage Backend,Alarm功能也基本完成, 並且增加了UDP Publisher 作爲取代RPC發送消息的第二選擇,更加高效。還增加了DB2作爲Storage Backend.
  • IceHouse 中,主要是分離collector,增加vmware vcenter server支持等。參考鏈接
  • Juno 中,主要是進行優化(還之前留下的技術債),以及添加Temptest。參考鏈接。

 1. 概念 

Ceilometer 的主要概念包括:

  • Meter:計量項
  • Sample:某Resource 某時刻某 Meter 的值
  • Statistics:某區間 Samples 的聚合值
  • Alarm:某區間 Statistics 滿足給定條件後發出的告警 

1.1 Meter

資源使用的某個計量項,它的屬性包括:名稱(name)、單位 (unit)、類型 (cumulative:累計值,delta:變化值、gauge:離散或者波動值)以及對應的資源屬性等。

(1)在 這裏 可以看到所有的 Ceilometer meter 列表和說明。

(2)使用CLI “ceilometer meter-list” 來獲取所有的meters。例如:

s1@controller:~$ ceilometer meter-list
+---------------------+------------+----------+--------------------------------------+----------------------------------+----------------------------------+
| Name                | Type       | Unit     | Resource ID                                                 | User ID                                                | Project ID                       |
+---------------------+------------+----------+--------------------------------------+----------------------------------+----------------------------------+
| cpu                 | cumulative | ns       | 440e4e2c-f255-43b3-8150-c6bc6b061ef7   | 1dc0db32a936496ebfc50be54924a7cc | fa2046aaead44a698de8268f94759fc1 |
| cpu_util            | gauge      | %        | 440e4e2c-f255-43b3-8150-c6bc6b061ef7   | 1dc0db32a936496ebfc50be54924a7cc | fa2046aaead44a698de8268f94759fc1 |
| cpu_util            | gauge      | %        | 49618cae-dd28-41a0-ae97-e98899d717eb | 8f4f734443674afcbbb57b9909d5a07f  | d6feddb5279a42f4854b93a729470448 |
| image               | gauge      | image   | 1e7b0a5a-7b78-4673-8d56-3abff7b491ae  | None                                                    | fa2046aaead44a698de8268f94759fc1 |

tips:
(a)meter 列表結果和被計量對象關聯。該CLI只列出的當前存在的計量對象的meter 列表。
(b)meter 列表結果和 samples 關聯。沒有 samples 的meter 不出現在列表中。 

1.2 Sample

某時刻某個 resource 的某個 meter 的值。Sample 的收集有區間概念,即收集數據的時間間隔。它的屬性出了meter屬性外,還有 timestampe(採樣時間)和 Volume (採樣值)。

s1@controller:~$ ceilometer sample-list -m cpu_util
+--------------------------------------+----------+-------+---------------+------+---------------------+
| Resource ID                                                | Name     | Type   | Volume              | Unit | Timestamp |
+--------------------------------------+----------+-------+---------------+------+---------------------+

| 631507ed-598c-4e6d-8582-9fd7490e7805 | cpu_util | gauge | 22.5263157895 | % | 2015-03-29T13:22:10 |
| 49618cae-dd28-41a0-ae97-e98899d717eb | cpu_util | gauge | 0.0                     | % | 2015-03-29T13:22:10 |
| 631507ed-598c-4e6d-8582-9fd7490e7805 | cpu_util | gauge | 21.85                 | % | 2015-03-29T13:21:51 |

1.3 Statistics

一個時間段(Period)內的 samples 聚合值,包括計數(Count)、最大(Max)、最小(Min)、平均 (Avg)、求和(Sum)等。例如:
 
s1@controller:~$ ceilometer statistics -m cpu -p 10000000
+------------+---------------------+---------------------+-----+-----+-----+-----+-------+----------+---------------------+---------------------+
| Period      | Period Start                 | Period End                | Max | Min | Avg | Sum | Count | Duration | Duration Start      | Duration End        |
+------------+---------------------+---------------------+-----+-----+-----+-----+-------+----------+---------------------+---------------------+
| 10000000 | 2015-03-29T05:56:38 | 2046-12-05T07:43:18 | 0.0 | 0.0 | 0.0 | 0.0 | 17    | 11273.0  | 2015-03-29T05:56:38 | 2015-03-29T09:04:31 |
+------------+---------------------+---------------------+-----+-----+-----+-----+-------+----------+---------------------+---------------------+
 這裏的Period表示當前該查詢的區間,使用 -p 參數指定;Duration 表示 samples 的區間。
 
需要注意的是,你可以使用 ”-q“ 指定統計的目標範圍。當不指定的時候,表示對當前租戶(tenant)內的所有虛機的 sample 做統計。比如指定 resource_id 來只統計某一個虛機: ceilometer statistics -m cpu_util -p 60 -q resource_id=d7ce68d4-3d58-404c-85a6-f9c19fe9d96c。

1.4 Alarm:告警

在 Havana 版本中引入。參考文檔

1.4.1 類型

Alarm 包括 threshold alarm (閾值告警)和 combination (組合告警)兩種類型。

  • Threshold alarm 根據監控指標的閾值去判斷alarm的狀態,它只是針對某一個監控指標建立alarm。它包括幾個要素:
    • 一個靜態閾值和比較方法 (static threshold value & comparison operator
    • 指定的 meter statistic (against which a selected meter statistic is compared
    • 比較的時間窗 (over an evaluation window of configurable length into the recent past.)
  • Combination alarm 根據多個alarm的狀態來判斷自己的狀態的,多個alarm之間是or/and的關係,這相當於是對多個監控指標建立了一個alarm。比如
$ ceilometer alarm-combination-create --name meta \
  --alarm_ids ALARM_ID1 \
  --alarm_ids ALARM_ID2 \
  --operator or \
  --alarm-action 'http://example.org/notify' 

1.4.2 CLI

Ceilometer Alarm CLI 支持一下Alarm的操作:

  • create:[POST ] /alarms
  • list: [GET ] /alarms
  • get:[GET ] /alarms/<alarm>
  • update:[PUT ] /alarms/<alarm>
  • delete:[DELETE] /alarms/<alarm>
  • histroy:GET /v2/alarms/{alarm_id}/history

使用 CLI 創建一個名爲 "cpu_high" 的 Threshold Alarm “當連續 3 個 10 分鐘內 某 instance 的 cpu_util 值超過70 的時候產告警,並其內容被寫入日誌文件”:

ceilometer alarm-threshold-create --name cpu_high --description 'instance running hot'  --meter-name cpu_util  --threshold 70.0 --comparison-operator gt  --statistic avg --period 600 --evaluation-periods 3 --alarm-action 'log://' --query resource_id=INSTANCE_ID
  • name: 告警名稱
  • meter-name:meter 名稱
  • threshold: 閾值
  • comparison_operator: 這個參數確定了怎麼和閾值進行比較,有6個可選:lt, le, eq, ne, ge, gt,默認是eq
  • statistic: 這個參數確定了使用什麼數據去和 threshold 比較,有5種可選:max, min, avg, sum, count,默認是avg
  • period: 這個參數其實有兩個作用,一個是確定了獲取該監控指標的監控數據的時間範圍,和下面的 evaluation_periods 配合使用,另外一個作用就是它確定了兩個點之間的時間間隔,默認是60s
  • evaluation_periods: 表示連續的監控間隔數目。和 period 參數相乘,可以確定獲取監控數據的時間範圍,默認是1。
  • alarm-action:告警產生後的反應。
  • query: 該參數一般用於過濾到監控指標下的某個資源,默認是[]

1.4.3 Alarm 的狀態

  • ALARM (告警狀態):

      {"current": "alarm", "alarm_id": "742873f0-97f0-4d99-87da-b5f7c7829b7f", "reason": "Remaining as alarm due to 1 samples outside threshold, most recent: 0.138333333333", "previous": "alarm"}

  • OK (數據充足,未告警):

      {"current": "ok", "alarm_id": "742873f0-97f0-4d99-87da-b5f7c7829b7f", "reason": "Remaining as ok due to 1 samples inside threshold, most recent: 0.138333333333", "previous": "ok"}

  • Insufficient Data (默認狀態 - 數據不足):

      {"current": "insufficient data", "alarm_id": "742873f0-97f0-4d99-87da-b5f7c7829b7f", "reason": "1 datapoints are unknown", "previous": "ok"}

1.4.4 Alarm Action

使用Ceilometer alarm-threshold/combination-create CLI 的如下屬性來爲不同狀態的下的 Alarm 定義不同的 Action:

  • --ok-action 
  • --alarm-action 
  • --insufficient-data-action 

Ceilometer 支持兩種Action:

  • 'log://':Alarm 被寫入 Log 文件
  • Webhook URL: 這是一個 HTTP(S) endpoint 的URL,例如 'http://130.56.250.199:8080/alarm/instances_TOO_MANY'。Alarm 的內容會以 JSON 的格式被 POST 到該URL 中。

參考:http://blog.csdn.net/hackerain/article/details/38172941

2. Ceilometer 的數據處理

Ceilometer 的功能就是對上面各種概念的對象的處理:

 

功能名稱 功能描述 提供功能Ceilometer 模塊
Collect Meters 數據收集

ceilometer-agent-compute

ceilometer-agent-central

ceilometer-agent-notification

ceilometer-collector

Transform Meters 數據轉換  
Publish Meters 數據發佈  
Store Meters 數據保存  
Read Meters 數據訪問 ceilometer-api
Alarm 告警
ceilometer-alarm-notifier
ceilometer-alarm-evaluator

 

總體架構如下:

 

 

2.1 Meters數據的收集

Ceilometer 有兩種數據收集方式:

  • Poller:
    • Compute agent (ceilometer-agent-compute)運行在每個 compute 節點上,以輪詢的方式通過調用 Image 的 driver 來獲取資源使用統計數據。
    • Central agent (ceilometer-agent-central)運行在 management server 上,以輪詢的方式通過調用 OpenStack 各個組件(包括 Nova、Cinder、Glance、Neutron、Swift 等)的 API 收集資源使用統計數據。
  • Notificaiton:Collector (ceilometer-collector)是一個運行在一個或者多個management server上的數據收集程序,它會監控 OpenStack 各組件的消息隊列。隊列中的 notification 消息會被它處理並轉化爲計量消息,再發回到消息系統中。計費消息會被直接保存到存儲系統中。

除了監控這些對象以外,Ceilometer 還可以監控 Neutron 的 Bandwidth, 以及 hardware。關於數據收集的細節,會有另一篇文章來闡述。

2.2 Meters 數據處理

Meters 數據的處理使用 Pipeline 的方式,即Metes 數據依次經過(零個或者多個) Transformer 和 (一個或者多個)Publisher 處理,最後達到(一個或者多個)Receiver。其中Recivers 包括 Ceilometer Collector 和 外部系統。

2.2.1 Pipeline

Ceilometer 根據配置文件 /etc/ceilometer/pipeline.yaml 來配置 meters 所使用的 transformers 和 publishers。以 cpu meter 爲例:

複製代碼
sources: A source is a producer of samples
......
  - name: cpu_source
    interval: 600
    meters:
        - "cpu"
    sinks:
        - cpu_sink

......
sinks: A sink on the other hand is a chain of handlers of samples
......
  - name: cpu_sink
    transformers:
      - name: "rate_of_change"
        parameters:
                target:
                    name: "cpu_util"
                    unit: "%"
                    type: "gauge"
                    scale: "100.0 / (10**9 * (resource_metadata.cpu_number or1))"
     publishers:
         - notifier://
複製代碼

這段代碼定義了 cpu meter 的 :

  • interval: 600:Poller 獲取 cpu samples 的間隔爲 10 分鐘
  • cpu meter 的 transformer 爲 "rate_of_change"
  • cpu meter 的 publisher 爲 notifier://,它使用默認的配置經過 AMQP 使用 oslo.messaging 發出數據

該配置文件的詳細信息可參考 http://docs.openstack.org/developer/ceilometer/configuration.html

2.2.1 Transformer 轉換器

Transformer 即 Sample 的轉換器。常見的 transformer 包括:

  • unit_conversion: 單位轉換器,比如溫度從°F 轉換成°C
  • rate_of_change: 計算方式轉換器,比如根據一定的計算規則來轉換一個sample。例如:

        name: "rate_of_change"
           parameters:
               target:
                   name: "cpu_util"
                   unit: "%"
                   type: "gauge"
                   scale: "100.0 / (10**9 * (resource_metadata.cpu_number or 1))"

  • accumulator: 累計器。如下圖示例:

 

2.2.2 Publisher 分發器

參考文檔: http://docs.openstack.org/admin-guide-cloud/content/section_telemetry-publishers.html

Ceilometer 支持如下幾種 Publishers:

Publisher 格式 說明 配置項 示例
Notifier notifier://?option1=value1&option2=value2 samples 數據被髮到 AMQP 系統,然後被 Ceilometer collecter 接收。默認的 AMQP Queue 是 metering_topic=metering。這默認的方式。

[publisher_notifier]
metering_driver = messagingv2
metering_topic = metering

notifier://?policy=drop&max_queue_length=512
RPC rpc://?option1=value1&option2=value2 與 notifier類似,同樣經過 AMQP, 不過是同步操作,因此可能有性能問題。  

[publisher_rpc]
metering_topic = metering

rpc://?per_meter_topic=1
UDP udp://<host>:<port>/ 經過 UDP port 發出。默認的 UDP 端口是 4952  udp_port=4952
udp://10.0.0.2:1234
File file://path?option1=value1&option2=value2 發送到文件保存    

 

可以在 /etc/ceilometer/pipeline.yaml 中爲某個 meter 配置多個 publisher。比如增加一個file publisher:

sinks:
    - name: meter_sink
      transformers:
      publishers:
          - notifier://
          - file:///var/log/ceilometer/ceilometer-file-publisher #新增的file publisher

那麼在該文件中你會看到如下的 sample:

{'user_id': None, 'name': 'image', 'resource_id': u'bb8838d5-06b5-4f7e-b6ef-87c908f04cc7', 'timestamp': '2015-03-29T15:39:05Z', 'resource_metadata': {'status': u'active', 'name': u'cinderimg', 'deleted': False, 'container_format': u'bare', 'created_at': u'2015-01-21T17:15:56', 'disk_format': u'qcow2', 'updated_at': u'2015-01-21T17:15:56', 'protected': True, 'min_ram': 0, 'checksum': u'64d7c1cd2b6f60c92c14662941cb7913', 'min_disk': 0, 'is_public': False, 'deleted_at': None, 'properties': {}, 'size': 13167616}, 'volume': 1, 'source': 'openstack', 'project_id': u'fa2046aaead44a698de8268f94759fc1', 'type': 'gauge', 'id': 'bb2b4142-d629-11e4-925a-080027ff4b45', 'unit': 'image'}

 

2.3 數據保存

參考文檔:http://docs.openstack.org/admin-guide-cloud/content/section_telemetry-storing-data.html

Ceilometer Collector 從 AMQP 接收到數據後,會原封不動地通過一個或者多個分發器(dispatchers)將它保存到指定位置。目前它支持的分發器:

  • 文件分發器:保存到文件 - 添加配置項dispatcher = file 和 [dispatcher_file] 部分的配置項
  • HTTP 分發器:保存到外部的 HTTP target - 添加配置項 dispatcher = http
  • 數據庫分發器:保存到數據庫 - 添加配置項 dispatcher = database。參考文檔
    • MongoDB:默認DB。
    • SQL DB:支持 mysql、postgreSQL 和 IBM DB2等。
    • HBase DB

Ceilometer 支持同時配置多個分發器,將數據保存到多個目的位置。比如在 ceilometer.conf 中做如下配置使得同時使用 file 和 database dispatcher:

複製代碼
[DEFAULT]
dispatcher = database
dispatcher = file

[dispatcher_file]
backup_count = 5
file_path = /var/log/ceilometer/ceilometer-samples
max_bytes = 100000
複製代碼

在  /var/log/ceilometer/ceilometer-samples 文件中將收到如下類似的 samples 數據:

複製代碼
[{u'counter_name': u'cpu_util', u'user_id': u'8f4f734443674afcbbb57b9909d5a07f', u'message_signature': u'21fdc2cbf50b4da39746eba47ac0a1b742c759a1bb42e17c00e293413c356a38', u'timestamp': u'2015-03-29T14:47:10Z', u'resource_id': u'49618cae-dd28-41a0-ae97-e98899d717eb', u'message_id': u'7ad714f6-d622-11e4-8f83-080027df9b16', u'source': u'openstack', u'counter_unit': u'%', u'counter_volume': 0.0, u'project_id': u'd6feddb5279a42f4854b93a729470448', u'resource_metadata': {u'status': u'shutoff', u'cpu_number': 1, u'ramdisk_id': None, u'display_name': u'vm-1-for-user-one', u'name': u'instance-0000000e', u'disk_gb': 1, u'kernel_id': None, u'image': None, u'ephemeral_gb': 0, u'host': u'5d9f88849c5458f5b903fbc7a7d19bb90c3a4b0c492c5180434d216d', u'memory_mb': 100, u'instance_type': u'7124c366-3e56-4923-b32a-124ee31abaf7', u'vcpus': 1, u'root_gb': 1, u'image_ref': None, u'flavor': {u'name': u'tiny', u'links': [{u'href': u'http://controller:8774/e5defbf994694519b1261fa855a058ae/flavors/7124c366-3e56-4923-b32a-124ee31abaf7', u'rel': u'bookmark'}], u'ram': 100, u'ephemeral': 0, u'vcpus': 1, u'disk': 1, u'id': u'7124c366-3e56-4923-b32a-124ee31abaf7'}, u'OS-EXT-AZ:availability_zone': u'nova', u'image_ref_url': None}, u'counter_type': u'gauge'}]
複製代碼

 

2.4 數據訪問

外部系統通過 ceilometer-api 模塊提供的 Ceilometer REST API 來訪問保存在數據庫中的數據。API 有 V1 和 V2 兩個版本,現在使用的是 V2.

API Service 默認在 8777 端口監聽 (#port=8777)。V2 API 列表在這裏

2.5 告警

2.5.1 架構

(1)ceilometer-alarm-evaluator 使用 Ceilometer REST API 獲取 statistics 數據

(2)ceilometer-alarm-evaluator 生成 alarm 數據, 並通過 AMQP 發給 ceilometer-alarm-notifer

(3)ceilometer-alarm-notifer 會通過指定方式把 alarm 發出去。

2.5.2 Heat 和 Ceilometer 通過 Ceilometer Alarm 進行交互來實現 Instance auto-scaling

參考文檔:http://superuser.openstack.org/articles/simple-auto-scaling-environment-with-heat

示意圖:

 

步驟:

 

3. 總體架構

3.1 總體架構

 

3.2 特點

(1)Ceilometer 的架構的開放性應該說非常好,處處都留有和外部系統交互的接口。

(2)其性能可能是個問題,特別是大的生產系統中計量數據量非常大的情況下。據說京東對該模塊有很大的修改後纔用到其生產系統中。

 3.3 京東對 Ceilometer 的優化 (摘自京東架構師在2015年初OpenStack meetup 上的材料)

  • 消息跟蹤:集成 Stacktach 到 Ceilometer,跟蹤message
  • 性能優化:
    • 增加數據處理服務 + cache,提升ceilometer-api 和 alarm 性能
    • 自研 filter 功能,直接 hook 在 collector 進程上面,做到及時告警
    • 去 MQ,使用自研 RPC 傳輸監控 sample,並採用 gzip 壓縮
  • 功能增強:增加郵件告警功能,做到告警及時通知

 參考文檔:

發佈了21 篇原創文章 · 獲贊 88 · 訪問量 31萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章