1.glance介紹
Glance是Openstack項目中負責鏡像管理的模塊,其功能包括虛擬機鏡像的查找、注冊和檢索等。 Glance提供Restful API可以查詢虛擬機鏡像的metadata及獲取鏡像。 Glance可以將鏡像保存到多種后端存儲上,比如簡單的文件存儲或者對象存儲。
2.image介紹
1)在傳統 IT 環境下,安裝一個系統要么從安裝 CD 從頭安裝,要么用 Ghost 等克隆工具恢復。這兩種方式有如下幾個問題:
(1)如果要安裝的系統多了效率就很低
(2)時間長,工作量大
(3)安裝完還要進行手工配置,比如安裝其他的軟件,設置 IP 等
(4)備份和恢復系統不靈活
2)雲環境下需要更高效的方案,這就是 Image。 Image 是一個模板,里面包含了基本的操作系統和其他的軟件。
舉例來說,有家公司需要為每位員工配置一套辦公用的系統,一般需要一個 Windows系統再加 office 軟件。 OpenStack 是這么做的:
(1)先手工安裝好這么一個虛機
(2)然后對虛機執行 snapshot,這樣就得到了一個 image
(3)當有新員工入職需要辦公環境時,立馬啟動一個或多個該 image 的 instance(虛機)就可以了
在這個過程中,第 1 步跟傳統方式類似,需要手工操作和一定時間,但第 2、3 步非常快,全自動化,一般都是秒級別。而且 2、3 步可以循環做。 比如公司新上了一套 OA 系統,每個員工的 PC 上都得有客戶端軟件。 那么可以在某個現有虛機中先手工安裝好 OA 客戶端,然后執行 snapshot 操作,得到新的 image,以后可以就直接使用新 image 創建虛機了。另外,snapshot 還有備份的作用,能夠非常方便的恢復系統。
3)Image Service 的功能是管理 Image,讓用戶能夠發現、獲取和保存 Image。在 OpenStack 中,提供 Image Service 的是 Glance,其具體功能如下:
(1)提供 REST API 讓用戶能夠查詢和獲取 image 的元數據和 image 本身
(2)支持多種方式存儲 image,包括普通的文件系統、Swift、Amazon S3 等
(3)對 Instance 執行 Snapshot 創建新的 image
4)glance架構
(1)glance-api
glance-api 是系統后台運行的服務進程。 對外提供 REST API,響應 image 查詢、獲取和存儲的調用。
glance-api 不會真正處理請求。 如果操作是與 image metadata(元數據)相關,glance-api 會把請求轉發給glance-registry; 如果操作是與 image 自身存取相關,glance-api 會把請求轉發給該 image 的 store backend。
在控制節點上可以查看 glance-api 與 進程 ps aux | grep glance-api glance 7394 1.5 2.8 392164 81780 ? Ss 11:55 2:13 /usr/bin/python2 /usr/bin/glance-api glance 7414 0.0 2.8 399992 82164 ? S 11:55 0:00 /usr/bin/python2 /usr/bin/glance-api glance 7415 0.0 2.8 400524 83012 ? S 11:55 0:00 /usr/bin/python2 /usr/bin/glance-api root 10501 0.0 0.0 112664 972 pts/0 S+ 14:19 0:00 grep --color=auto glance-api
(2)glance-registry
glance-registry 是系統后台運行的服務進程。 負責處理和存取 image 的 metadata,例如 image 的大小和類型。
在控制節點上可以查看 glance-registry 進程 ps aux | grep glance-registry glance 7395 0.0 2.4 350724 72232 ? Ss 11:55 0:01 /usr/bin/python2 /usr/bin/glance-registry glance 7412 0.0 2.3 350724 67444 ? S 11:55 0:00 /usr/bin/python2 /usr/bin/glance-registry glance 7413 0.0 2.3 350724 67444 ? S 11:55 0:00 /usr/bin/python2 /usr/bin/glance-registry root 10538 0.0 0.0 112664 976 pts/0 S+ 14:20 0:00 grep --color=auto glance-registry
(3)Glance 支持多種格式的 image,包括
(4)Database
Image 的 metadata 會保持到 database 中,默認是 MySQL。 在控制節點上可以查看 glance 的 database 信息
(5)Store backend
Glance 自己並不存儲 image。 真正的 image 是存放在 backend 中的。 Glance 支持多種 backend,包括:
A directory on a local file system(這是默認配置)
GridFS
Ceph RBD
Amazon S3
Sheepdog
OpenStack Block Storage (Cinder)
OpenStack Object Storage (Swift)
VMware ESX
具體使用哪種 backend,是在 /etc/glance/glance-api.conf 中配置的。
其他 backend 的配置可參考http://docs.openstack.org/liberty/config-reference/content/configuring-image-service-backends.html。每個 image 在目錄下都對應有一個文件,文件以 image 的 ID 命名。
3.glance服務搭建
1)條件准備
(1)准備數據庫
create database glance;
grant all privileges on glance.* to 'glance'@'localhost' identified by 'GLANCE_DBPASS';
grant all privileges on glance.* to 'glance'@'%' identified by 'GLANCE_DBPASS';
(2)登錄openstack
source openrc
(3)創建用戶、服務
創建用戶
openstack user create --domain default --password=glance glance +---------------------+----------------------------------+ | Field | Value | +---------------------+----------------------------------+ | domain_id | default | | enabled | True | | id | 394ed512341541fa827181877505ef4a | | name | glance | | options | {} | | password_expires_at | None | +---------------------+----------------------------------+ openstack user list +----------------------------------+--------+ | ID | Name | +----------------------------------+--------+ | 394ed512341541fa827181877505ef4a | glance | | cd9ce1eb589b445e9b98c53a36bdc8d8 | admin | | e880cc125bb541128c9bc47233d91732 | demo | +----------------------------------+--------+
將glance用戶角色設置為admin
openstack role add --project service --user glance admin
創建glance服務
openstack service create --name glance --description "OpenStack Image" image +-------------+----------------------------------+ | Field | Value | +-------------+----------------------------------+ | description | OpenStack Image | | enabled | True | | id | d533cf98bd2f41f8958c2eadbad5d34f | | name | glance | | type | image | +-------------+----------------------------------+
(4)創建鏡像服務API的endpoints
openstack endpoint create --region RegionOne image public http://node1:9292 +--------------+----------------------------------+ | Field | Value | +--------------+----------------------------------+ | enabled | True | | id | dcc9722a128a43a58f23eb1e9c368292 | | interface | public | | region | RegionOne | | region_id | RegionOne | | service_id | d533cf98bd2f41f8958c2eadbad5d34f | | service_name | glance | | service_type | image | | url | http://node1:9292 | +--------------+----------------------------------+ openstack endpoint create --region RegionOne image internal http://node1:9292 +--------------+----------------------------------+ | Field | Value | +--------------+----------------------------------+ | enabled | True | | id | 3b48b8b801d1486988efed0059faa6ac | | interface | internal | | region | RegionOne | | region_id | RegionOne | | service_id | d533cf98bd2f41f8958c2eadbad5d34f | | service_name | glance | | service_type | image | | url | http://node1:9292 | +--------------+----------------------------------+ openstack endpoint create --region RegionOne image admin http://node1:9292 +--------------+----------------------------------+ | Field | Value | +--------------+----------------------------------+ | enabled | True | | id | 53eeb5d0e431444eb47de36a77ce04cb | | interface | admin | | region | RegionOne | | region_id | RegionOne | | service_id | d533cf98bd2f41f8958c2eadbad5d34f | | service_name | glance | | service_type | image | | url | http://node1:9292 | +--------------+----------------------------------+
2)安裝和配置glance服務
(1)安裝軟件包
yum install openstack-glance -y
(2)修改配置文件
vim /etc/glance/glance-api.conf [database] connection = mysql+pymysql://glance:GLANCE_DBPASS@node1/glance ... [keystone_authtoken] auth_uri = http://node1:5000 auth_url = http://node1:35357 memcached_servers = node1:11211 auth_type = password project_domain_name = default user_domain_name = default project_name = service username = glance password = GLANCE_PASS ... [paste_deploy] flavor = keystone ... [glance_store] stores = file,http default_store = file filesystem_store_datadir = /var/lib/glance/images/ vim /etc/glance/glance-registry.conf [database] connection = mysql+pymysql://glance:GLANCE_DBPASS@node1/glance ... [keystone_authtoken] auth_uri = http://node1:5000 auth_url = http://node1:35357 memcached_servers = node1:11211 auth_type = password project_domain_name = default user_domain_name = default project_name = service username = glance password = GLANCE_PASS . . . [paste_deploy] flavor = keystone
或者可以用如下方法

cd /etc/glance/ cp glance-api.conf glance-api.conf.bak -a cp glance-registry.conf glance-registry.conf.bak -a vim glance-api.conf [DEFAULT] [cors] [cors.subdomain] [database] connection = mysql+pymysql://glance:GLANCE_DBPASS@node1/glance [glance_store] stores = file,http default_store = file filesystem_store_datadir = /var/lib/glance/images/ [image_format] [keystone_authtoken] auth_uri = http://node1:5000 auth_url = http://node1:35357 memcached_servers = node1:11211 auth_type = password project_domain_name = default user_domain_name = default project_name = service username = glance password = glance [matchmaker_redis] [oslo_concurrency] [oslo_messaging_amqp] [oslo_messaging_kafka] [oslo_messaging_notifications] [oslo_messaging_rabbit] [oslo_messaging_zmq] [oslo_middleware] [oslo_policy] [paste_deploy] flavor = keystone [profiler] [store_type_location_strategy] [task] [taskflow_executor] vim glance-registry.conf [DEFAULT] [database] connection = mysql+pymysql://glance:GLANCE_DBPASS@node1/glance [keystone_authtoken] auth_uri = http://node1:5000 auth_url = http://node1:35357 memcached_servers = node1:11211 auth_type = password project_domain_name = default user_domain_name = default project_name = service username = glance password = glance [matchmaker_redis] [oslo_messaging_amqp] [oslo_messaging_kafka] [oslo_messaging_notifications] [oslo_messaging_rabbit] [oslo_messaging_zmq] [oslo_policy] [paste_deploy] flavor = keystone [profiler]
(3)同步數據庫
su -s /bin/sh -c "glance-manage db_sync" glance
(4)設置開機自啟
systemctl restart openstack-glance-api.service openstack-glance-registry.service
systemctl enable openstack-glance-api.service openstack-glance-registry.service
3)導入鏡像進行驗證
(1)下載鏡像
wget http://download.cirros-cloud.net/0.3.5/cirros-0.3.5-x86_64-disk.img
(2)上傳鏡像
openstack image create "cirrors" \ > --file cirros-0.3.3-x86_64-disk.img \ > --disk-format qcow2 --container-format bare \ > --public +------------------+------------------------------------------------------+ | Field | Value | +------------------+------------------------------------------------------+ | checksum | 133eae9fb1c98f45894a4e60d8736619 | | container_format | bare | | created_at | 2019-07-30T06:12:37Z | | disk_format | qcow2 | | file | /v2/images/cf5c5c98-30a2-494a-b24d-379511491a28/file | | id | cf5c5c98-30a2-494a-b24d-379511491a28 | | min_disk | 0 | | min_ram | 0 | | name | cirrors | | owner | 7d3f0cd56a104c60a36196675f62428e | | protected | False | | schema | /v2/schemas/image | | size | 13200896 | | status | active | | tags | | | updated_at | 2019-07-30T06:12:37Z | | virtual_size | None | | visibility | public | +------------------+------------------------------------------------------+
(3)查看鏡像
openstack image list
+--------------------------------------+---------+--------+
| ID | Name | Status |
+--------------------------------------+---------+--------+
| cf5c5c98-30a2-494a-b24d-379511491a28 | cirrors | active |
+--------------------------------------+---------+--------+
(4)刪除鏡像
openstack image delete cf5c5c98-30a2-494a-b24d-379511491a28