puppet-glance模块

  1. 项目简介 - 理解Glance
  2. 先睹为快
  3. 核心代码讲解 - 如何管理Glance服务?
  4. 动手练习 - 光看不练假把式

0. 项目简介

Glance是OpenStack Image Service项目,用于注册、管理和检索虚拟机镜像。
Glance并不负责实际的镜像存储。它提供了对接简单文件系统,对象存储,块存储等多种存储后端的能力。除了磁盘镜像信息,它还能够存储描述镜像的元数据和状态信息。

1.先睹为快

不想看下面大段的代码解析,已经跃跃欲试了?

OK,我们开始吧!

创建puppet_glance.pp文件并输入:

  1. class { 'glance::api':
  2. verbose => true,
  3. keystone_tenant => 'services',
  4. keystone_user => 'glance',
  5. keystone_password => '12345',
  6. database_connection => 'mysql://glance:12345@127.0.0.1/glance',
  7. }
  8. class { 'glance::registry':
  9. verbose => true,
  10. keystone_tenant => 'services',
  11. keystone_user => 'glance',
  12. keystone_password => '12345',
  13. database_connection => 'mysql://glance:12345@127.0.0.1/glance',
  14. }
  15. class { 'glance::backend::file': }
  16. class { 'glance::db::mysql':
  17. password => '12345',
  18. allowed_hosts => '%',
  19. }
  20. class { 'glance::keystone::auth':
  21. password => '12345'
  22. email => 'glance@example.com',
  23. public_address => '127.0.0.1',
  24. admin_address => '127.0.0.1',
  25. internal_address => '172.17.1.3',
  26. region => 'example-west-1',
  27. }
  28. rabbitmq_user { 'glance':
  29. admin => true,
  30. password => 'an_even_bigger_secret',
  31. provider => 'rabbitmqctl',
  32. require => Class['::rabbitmq'],
  33. }
  34. rabbitmq_user_permissions { 'glance@/':
  35. configure_permission => '.*',
  36. write_permission => '.*',
  37. read_permission => '.*',
  38. provider => 'rabbitmqctl',
  39. require => Class['::rabbitmq'],
  40. }

在终端执行以下命令:

  1. $ puppet apply -v puppet_glance.pp

2.核心代码讲解

2.1 class glance

class glance用于管理Glance软件包和Openstackclient软件包:

  1. include ::glance::params
  2. if ( $glance::params::api_package_name == $glance::params::registry_package_name ) {
  3. package { $::glance::params::api_package_name :
  4. ensure => $package_ensure,
  5. name => $::glance::params::api_package_name,
  6. tag => ['openstack', 'glance-package'],
  7. }
  8. include '::openstacklib::openstackclient'
  9. }

2.2 class glance::api

glance::api类用于管理以下配置:

  1. policy\db\logging\cache
    1. include ::glance::policy
    2. include ::glance::api::db
    3. include ::glance::api::logging
    4. include ::glance::cache::logging
  2. /etc/glance/glance-api.conf
    1. # basic service config
    2. glance_api_config {
    3. 'DEFAULT/bind_host': value => $bind_host;
    4. 'DEFAULT/bind_port': value => $bind_port;
    5. 'DEFAULT/backlog': value => $backlog;
    6. 'DEFAULT/show_image_direct_url': value => $show_image_direct_url;
    7. ...
    8. 'DEFAULT/image_cache_dir': value => $image_cache_dir;
    9. 'DEFAULT/auth_region': value => $auth_region;
    10. 'glance_store/os_region_name': value => $os_region_name;
    11. }
    3.管理/etc/glance/glance-cache.conf

在Glance-api中,启用Glance的缓存功能可以加速镜像的二次下载速度(注:在使用Ceph作为Glance, Cinder, Nova的后端时,此功能无效)

  1. glance_cache_config {
  2. 'DEFAULT/image_cache_stall_time': value => $image_cache_stall_time;
  3. 'DEFAULT/image_cache_max_size': value => $image_cache_max_size;
  4. 'glance_store/os_region_name': value => $os_region_name;
  5. }

4.glance-api服务的管理

  1. service { 'glance-api':
  2. ensure => $service_ensure,
  3. name => $::glance::params::api_service_name,
  4. enable => $enabled,
  5. hasstatus => true,
  6. hasrestart => true,
  7. tag => 'glance-service',
  8. }

5.验证glance-api服务部署是否成功
通过调用glance image-list命令来验证glance-api的返回值是否符合预期。

  1. if $validate {
  2. $defaults = {
  3. 'glance-api' => {
  4. 'command' => "glance --os-auth-url ${auth_uri} --os-tenant-name ${keystone_tenant} --os-username ${keystone_user} --os-password ${keystone_password} image-list",
  5. }
  6. }
  7. $validation_options_hash = merge ($defaults, $validation_options)
  8. create_resources('openstacklib::service_validation', $validation_options_hash, {'subscribe' => 'Service[glance-api]'})
  9. }

2.3 Class glance::registry

glance::registry用于安装和配置glance-registry服务,其代码结构与glance::api类似,在此不做赘述。

Class glance::notify::rabbitmq

在glance-api和glance-registry中启用notifications功能可以在创建镜像,更新镜像源数据等事件发生时发送通知到rabbitmq给其他服务使用。

调用puppet-oslo来配置glance-api.conf和glance-registry

  1. oslo::messaging::rabbit { ['glance_api_config', 'glance_registry_config']:
  2. rabbit_password => $rabbit_password,
  3. rabbit_userid => $rabbit_userid,
  4. rabbit_host => $rabbit_host,
  5. rabbit_port => $rabbit_port,
  6. rabbit_hosts => $rabbit_hosts,
  7. rabbit_virtual_host => $rabbit_virtual_host,
  8. rabbit_ha_queues => $rabbit_ha_queues,
  9. heartbeat_timeout_threshold => $rabbit_heartbeat_timeout_threshold,
  10. heartbeat_rate => $rabbit_heartbeat_rate,
  11. rabbit_use_ssl => $rabbit_use_ssl,
  12. kombu_ssl_ca_certs => $kombu_ssl_ca_certs,
  13. kombu_ssl_certfile => $kombu_ssl_certfile,
  14. kombu_ssl_keyfile => $kombu_ssl_keyfile,
  15. kombu_ssl_version => $kombu_ssl_version,
  16. kombu_reconnect_delay => $kombu_reconnect_delay,
  17. amqp_durable_queues => $amqp_durable_queues,
  18. kombu_compression => $kombu_compression,
  19. }
  20. oslo::messaging::notifications { ['glance_api_config', 'glance_registry_config']:
  21. driver => $notification_driver,
  22. topics => $rabbit_notification_topic,
  23. }

2.4 Class glance::backend::rbd

Glance支持多种存储后端,比如cinder,swift,file,ceph,s3,本节将介绍如何使用glance::backend::rbd配置Ceph作为Glance后端存储:

  1. #修改glance_store下的配置项
  2. glance_api_config {
  3. 'glance_store/rbd_store_ceph_conf': value => $rbd_store_ceph_conf;
  4. 'glance_store/rbd_store_user': value => $rbd_store_user;
  5. 'glance_store/rbd_store_pool': value => $rbd_store_pool;
  6. 'glance_store/rbd_store_chunk_size': value => $rbd_store_chunk_size;
  7. 'glance_store/rados_connect_timeout': value => $rados_connect_timeout;
  8. }
  9. if !$multi_store {
  10. glance_api_config { 'glance_store/default_store': value => 'rbd'; }
  11. if $glare_enabled {
  12. glance_glare_config { 'glance_store/default_store': value => 'rbd'; }
  13. }
  14. }
  15. #安装python-ceph软件包
  16. package { 'python-ceph':
  17. ensure => $package_ensure,
  18. name => $::glance::params::pyceph_package_name,
  19. }

3.动手练习

  1. 配置Glance使用Swift作为存储后端
  2. 设置token的缓存时间为5min