Glance是Openstack項目中負責鏡像管理的模塊,其功能包括虛擬機鏡像的查找、註冊和檢索等。 Glance提供Restful API能夠查詢虛擬機鏡像的metadata及獲取鏡像。 Glance能夠將鏡像保存到多種後端存儲上,好比簡單的文件存儲或者對象存儲。html
1)在傳統 IT 環境下,安裝一個系統要麼從安裝 CD 從頭安裝,要麼用 Ghost 等克隆工具恢復。這兩種方式有以下幾個問題:node
(1)若是要安裝的系統多了效率就很低python
(2)時間長,工做量大mysql
(3)安裝完還要進行手工配置,好比安裝其餘的軟件,設置 IP 等redis
(4)備份和恢復系統不靈活sql
2)雲環境下須要更高效的方案,這就是 Image。 Image 是一個模板,裏面包含了基本的操做系統和其餘的軟件。數據庫
舉例來講,有家公司須要爲每位員工配置一套辦公用的系統,通常須要一個 Windows系統再加 office 軟件。 OpenStack 是這麼作的:vim
(1)先手工安裝好這麼一個虛機後端
(2)而後對虛機執行 snapshot,這樣就獲得了一個 imageapi
(3)當有新員工入職須要辦公環境時,立馬啓動一個或多個該 image 的 instance(虛機)就能夠了
在這個過程當中,第 1 步跟傳統方式相似,須要手工操做和必定時間,但第 二、3 步很是快,全自動化,通常都是秒級別。並且 二、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