OpenStack镜像服务就是Glance,可以让用户上传、导出、修改、删除虚拟机镜像。另外,openstack镜像服务支持将镜像文件存储在各种类型的存储中,如本地文件系统、Openstack对象存储服务swift、ceph等分布式存储中。

OpenStack镜像服务包括glance-api、glance-registry两个子服务。glance-api对外提供通信接口,与其他服务交互;glance-registry用于管理存储在硬盘或 glance 数据库中的对象。
修改好配置后,需要重启openstack-glance-api.service或openstack-glance-registry.service

1、使用数据库访问客户端以 root 用户身份连接到数据库服务器:
# mysql -uroot -p123
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 17
Server version: 10.3.20-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

2、创建glance数据库
MariaDB [(none)]> CREATE DATABASE glance default character set utf8;

3、授予对 glance 数据库的适当访问权限
MariaDB [(none)]> GRANT ALL PRIVILEGES ON glance.* TO 'glance'@'localhost' \
  IDENTIFIED BY 'GLANCE_DBPASS';
MariaDB [(none)]> GRANT ALL PRIVILEGES ON glance.* TO 'glance'@'%' \
  IDENTIFIED BY 'GLANCE_DBPASS';

4、创建 glance 用户

交互式:
# openstack user create --domain default --password-prompt glance

非交互式:
# openstack user create --domain default --password=glance glance

5、将 admin 角色添加到 glance 用户和 service 项目:
# openstack role add --project service --user glance admin

6、创建 glance 服务实体
# openstack service create --name glance \
  --description "OpenStack Image" image
 
7、创建Image服务 API 端点
# openstack endpoint create --region RegionOne \
  image public http://controller:9292
  
# openstack endpoint create --region RegionOne \
  image internal http://controller:9292
  
# openstack endpoint create --region RegionOne \
  image admin http://controller:9292

8、安装glance软件包,修改配置文件
# yum install openstack-glance -y

修改/etc/glance/glance-api.conf
# cd /etc/glance/ && mv glance-api.conf glance-api.conf.source && cat glance-api.conf.source |grep -Ev "^#|^$" > glance-api.conf && chown root:glance glance-api.conf
# vi /etc/glance/glance-api.conf

在 [database] “配置数据库访问”部分中:
[database]
# ...
connection = mysql+pymysql://glance:GLANCE_DBPASS@controller/glance

在 [keystone_authtoken] and [paste_deploy] 部分中,配置 Identity service 访问权限:
[keystone_authtoken]
# ...
www_authenticate_uri  = http://controller:5000
auth_url = http://controller:5000
memcached_servers = controller:11211
auth_type = password
project_domain_name = Default
user_domain_name = Default
project_name = service
username = glance
password = glance

[paste_deploy]
# ...
flavor = keystone

在本节 [glance_store] 中,配置文件映像文件的本地文件系统存储和位置:
[glance_store]
# ...
stores = file,http
default_store = file
filesystem_store_datadir = /var/lib/glance/images/

修改/etc/glance/glance-registry.conf
# cd /etc/glance/ && mv glance-registry.conf glance-registry.conf.source && cat glance-registry.conf.source |grep -Ev "^#|^$" > glance-registry.conf && chown root:glance glance-registry.conf
# vi /etc/glance/glance-registry.conf

在 [database] “配置数据库访问”部分中:
[database]
# ...
connection = mysql+pymysql://glance:GLANCE_DBPASS@controller/glance

在 [keystone_authtoken] and [paste_deploy] 部分中,配置 Identity service 访问权限:
[keystone_authtoken]
# ...
www_authenticate_uri  = http://controller:5000
auth_url = http://controller:5000
memcached_servers = controller:11211
auth_type = password
project_domain_name = Default
user_domain_name = Default
project_name = service
username = glance
password = glance

[paste_deploy]
# ...
flavor = keystone

9、同步glance服务数据库:
# su -s /bin/sh -c "glance-manage db_sync" glance

10、启动Glance服务并设置成开机自启动
# systemctl enable openstack-glance-api.service openstack-glance-registry.service
# systemctl start openstack-glance-api.service openstack-glance-registry.service

11、验证glance服务
# cd /var/lib/glance/images/

安装传输工具:
# yum install lrzsz -y

下载测试镜像cirros上传到Glance
# curl -O http://download.cirros-cloud.net/0.3.2/cirros-0.3.2-x86_64-disk.img
# openstack image create "cirros3.2" --file cirros-0.3.2-x86_64-disk.img --disk-format qcow2 --container-format bare --public

上传的镜像分私有镜像和公有镜像。私有镜像只有上传者才拥有权限使用;如果要设置成公有镜像,则只需要加上--public参数即可

12、其他命令

查看glance服务端点
# openstack endpoint list | grep glance

删除服务端点:
# openstack endpoint delete ID

查看镜像
# openstack image list  或者  glance image-list

查看镜像详细信息
# openstack image show ID

设置镜像为公有镜像
# openstack image set cirros3.2 --public

删除镜像
# openstack image delete ID