OpenStack计费Billing功能前瞻(一)

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: OpenStack计费Billing功能前瞻(一)

前言:



OpenStack有个比较年轻的费用统计项目CloudKitty,但是它需要与Ceilometer进行结合。在实际的生产环境中,若整个集群资源不大(10台服务器以下)且服务器性能较低,不建议上ceilometer,我的想法主要是mongoDB数据量太大,且会影响前端dashboard拿ceilometer的监控数据,国内主流做OpenStack的公司上ceilometer项目的较少,大部分使用collectd等主流轻量级监控插件。

鉴于此情况,本文主要阐述在没有安装Cloudkitty以及Ceilometer情况下计费功能的实现。


核心思想:


本文描述的计费功能主要是通过虚拟机的vcpu、ram、disk来统计费用。


Nova(Mitaka):

在开始代码之前我们来了解一下nova写表的机制:每一次通过client对云主机进行操作即使用nova命令时,/usr/lib/python2.7/site-packages/nova/objects/instance.py文件中的save方法进行参数处理,部分代码如下:

def save(self, expected_vm_state=None,
             expected_task_state=None, admin_state_reset=False):
        """Save updates to this instance
        Column-wise updates will be made based on the result of
        self.what_changed(). If expected_task_state is provided,
        it will be checked against the in-database copy of the
        instance before updates are made.
        :param:context: Security context
        :param:expected_task_state: Optional tuple of valid task states
        for the instance to be in
        :param:expected_vm_state: Optional tuple of valid vm states
        for the instance to be in
        :param admin_state_reset: True if admin API is forcing setting
        of task_state/vm_state
        """
        # Store this on the class because _cell_name_blocks_sync is useless
        # after the db update call below.
        self._sync_cells = not self._cell_name_blocks_sync()
        context = self._context
        cell_type = cells_opts.get_cell_type()
        if cell_type is not None:
            stale_instance = self.obj_clone()
        cells_update_from_api = (cell_type == 'api' and self.cell_name and
                                 self._sync_cells)
        if cells_update_from_api:
            def _handle_cell_update_from_api():
                cells_api = cells_rpcapi.CellsAPI()
                cells_api.instance_update_from_api(context, stale_instance,
                            expected_vm_state,
                            expected_task_state,
                            admin_state_reset)
        updates = {}
        changes = self.obj_what_changed()
        for field in self.fields:
            # NOTE(danms): For object fields, we construct and call a
            # helper method like self._save_$attrname()
            if (self.obj_attr_is_set(field) and
                    isinstance(self.fields[field], fields.ObjectField)):
                try:
                    getattr(self, '_save_%s' % field)(context)
                except AttributeError:
                    LOG.exception(_LE('No save handler for %s'), field,
                                  instance=self)
                except db_exc.DBReferenceError as exp:
                    if exp.key != 'instance_uuid':
                        raise
                    # NOTE(melwitt): This will happen if we instance.save()
                    # before an instance.create() and FK constraint fails.
                    # In practice, this occurs in cells during a delete of
                    # an unscheduled instance. Otherwise, it could happen
                    # as a result of bug.
                    raise exception.InstanceNotFound(instance_id=self.uuid)
            elif field in changes:
                if (field == 'cell_name' and self[field] is not None and
                        self[field].startswith(cells_utils.BLOCK_SYNC_FLAG)):
                    updates[field] = self[field].replace(
                            cells_utils.BLOCK_SYNC_FLAG, '', 1)
                else:
                    updates[field] = self[field]


即每对instance进行一次操作,save方法都会对这次操作即update进行一次处理并写入mariaDB表,而改变的状态则在obj_what_change中:

310abc753915299c2c0a1d2d08b3ae5c.png

由此可见,我们只需要在client对instance进行操作时,nova在将update信息写入mariadb之前将信息过滤并进行处理同样写入一张billing表即可!


接下去就很简单了


如何实现??


1.在mariadb中创billing的表及用户

mysql -uroot -pMARIADB_PWD <<EOF
drop database if exists billing;
create database billing;
grant all privileges on billing.* to "billing"@"localhost" identified by "BILLING_PWD";
grant all privileges on billing.* to "billing"@"%" identified by "BILLING_PWD";
flush privileges;
EOF

2.初始化表

在这里使用了python的mysql工具包sqlalchemy(http://www.sqlalchemy.org),可以直接通过python语言来定义mysql库及表。

a.init_db.py(DB中数据的状态记录以及使用记录初始化)

1d7410040183d7ee9209859117b280ac.png

b.utils.py(主要是从billing表中拿数据并计算写入billing表)

1509b15271ea840214330994689bd3ae.jpg

在/etc/nova/nova.conf配置一下billing的参数:

db_connection = mysql://billing:openstack@127.0.0.1/billing
pool_recycle = 600

c.modules.py(定义billing表的title即里面数据的声明,三张表分别是Rate(单价)和Usage(使用量)和StatusRecord(状态记录))

87c5eafb6720c10416879e167d8647d1.jpg


3.Analysis

总体的设计思路是-----

    1.部署时先python utils.py跑一遍代码,之后每通过client对云主机进行关机、删除、创建、迁移操作时会记录到billing表中;


    2.可以在前端自定义费用模板(也可以直接写进billing.rate表中),后端可以拿到前端传过来的模板信息(rate.cpu、rate.ram、rate.disk)进行计算整合写入表并传给前端


4.WorkFlow

94104b3f80b499052aae4d9a166e9a22.png


5.DB

billing中有三张表,对应的是价格、状态记录和使用量

71a0914df83f1b461e3ea12312937548.png


7a3aec2a3202023f7b9ea50a454e7c96.png


139189448cc65e22f365e80eabb5ce4b.png


本文主要对billing功能进行前瞻,下一节主要讲计算api   utils.py文件

计费功能最重要的是理清楚nova中虚拟机状态的变化,create, update,delete,resized等等,只要搞清楚里面的逻辑,对计费的计算一下就能搞清楚了。

相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
监控 关系型数据库 API
Openstack组件实现原理 — Keystone认证功能
目录 目录 前言 Keystone安装列表 Keystone架构 Keystone的管理对象 一个理解Keystone管理对象功能的例子 Keystone管理对象之间的关系 Keystone V3的新特性 V3的改进 Authorization授权功能的应用 Authentication认证功能的应用过程 前言 Keystone实现始终围绕着Keystone所实现的功能来展开,所以在理解其实现之前,建议大家尝试通过安装Keystone这一个过程来感受Keystone在Openstack架构中所充当的角色。
1955 0
|
API 安全 存储
Openstack组件部署 — Keystone功能介绍与认证实现流程
目录 目录 前文列表 Keystone认证服务 Keystone认证服务中的概念 Keystone的验证过程 简单来说 前文列表 Openstack组件部署 — Overview和前期环境准备 Openstack组建部署 — Environment of Controller Node Keystone认证服务 Keystone是Identity Service认证服务的Alias。
2195 0
|
监控 测试技术 API
OpenStack ceilometer部署安装监控,计费数据抓取测试Ok
OpenStack core components service 之 service entry服务入口即API endpoint 创建及关联集成 语句; glance image-download image-Name > storName
776 0
|
网络协议 Linux 网络安全
openstack 云平台一体化部署(超详细)
openstack 云平台一体化部署(超详细)
1231 0
openstack 云平台一体化部署(超详细)
|
2月前
|
消息中间件 缓存 Shell
跟我一起来学OpenStack部署
跟我一起来学OpenStack部署
222 0