OpenStack镜像管理与制作(一)
Glance服务介绍
Glance服务是OpenStack平台的镜像服务,它提供了管理虚拟机镜像的功能。
在OpenStack Glance组件中,镜像(Image)是指虚拟机的模板,包含了预先配置的操作系统和应用程序。镜像是创建和部署虚拟机实例的基础。
Glance主要特点和功能:
镜像存储和管理:Glance提供了一个可靠的存储系统,用于持久化保存虚拟机镜像。它支持多种后端存储选项,如本地文件系统、分布式文件系统和对象存储。用户可以通过Glance API上传、删除、更新和查询镜像。
镜像加速和缓存:Glance支持镜像的加速和缓存,以提高虚拟机实例的启动速度和性能。通过将常用的镜像缓存在计算节点上,可以减少下载和传输时间。
镜像格式支持:Glance支持多种虚拟机镜像格式,包括常见的格式如QCOW2、VHD、VMDK和RAW等。这使得用户可以使用不同的镜像格式来满足其特定的需求。
元数据管理:Glance允许用户为每个镜像添加自定义元数据,包括镜像名称、描述、操作系统类型、版本等信息。这些元数据可以帮助用户更好地组织和搜索镜像库。
镜像共享:Glance支持镜像的共享功能,允许用户将自己的镜像分享给其他项目或用户。这样可以避免重复创建相同的镜像,提高资源的共享和重用。
Glance架构
Glance API:用户与 Glance 服务交互的接口,通过 RESTful API 提供了镜像管理的各种功能。用户可以通过 API 执行镜像的上传、下载、删除等操作。
Glance Registry:用于存储和管理镜像的元数据,如名称、格式、大小等。
Glance Store:Glance 的后端存储组件,负责实际的镜像文件的存储。它可以支持多种后端存储类型,如本地文件系统、Swift、Ceph、S3 等。根据配置,镜像可以存储在不同的后端存储中。
Database:数据库用于存储 Glance 服务的持久化数据,包括镜像的元数据、权限信息等。
Glance服务安装
作业与实验环境
超星网址
快照管理:控制节点选择快照项目6,计算节点不需要设置
安装与配置
执行命令安装Glance软件包:
[root@controller ~]# yum -y install openstack-glance安装结束后能够通过以下命令查看系统中的同名用户和用户组表示Glance组件安装完成:
[root@controller ~]# cat /etc/passwd |grep glance # 1. 查看glance用户 glance:x:161:161:OpenStack Glance Daemons:/var/lib/glance:/sbin/nologin [root@controller ~]# cat /etc/group |grep glance # 2. 查看glance用户组 glance:x:161:创建数据库并授权:
# 1. 进入数据库 [root@controller ~]# mysql -uroot -p000000 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 64 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. # 2. 创建glance数据库 MariaDB [(none)]> CREATE DATABASE glance; Query OK, 1 row affected (0.000 sec) # 3. 为glance数据库授予本地登录用户的权限 MariaDB [(none)]> GRANT ALL PRIVILEGES ON glance.* TO 'glance'@'localhost' IDENTIFIED BY '000000'; Query OK, 0 rows affected (0.001 sec) # 4. 为glance数据库授予远程登录用户的权限 MariaDB [(none)]> GRANT ALL PRIVILEGES ON glance.* TO 'glance'@'%' IDENTIFIED BY '000000'; Query OK, 0 rows affected (0.000 sec) # 5. 退出 MariaDB [(none)]> quit修改配置文件:
# 备份配置文件 [root@controller ~]# cp /etc/glance/glance-api.conf /etc/glance/glance-api.bak# 删除配置文件中的空行和注释行 [root@controller ~]# grep -Ev '^$|#' /etc/glance/glance-api.bak > /etc/glance/glance-api.conf打开配置文件依次编辑以下字段:
# 数据库地址 [database] connection = mysql+pymysql://glance:000000@controller/glance# 身份凭证配置 [keystone_authtoken] auth_url = http://controller:5000 memcached_servers = controller:11211 auth_type = password username = glance password = 000000 project_name = project user_domain_name = Default project_domain_name = Default# 身份凭证配置 [paste_deploy] flavor = keystone# 后端存储配置 [glance_store] stores = file default_store = file filesystem_store_datadir = /var/lib/glance/images/初始化Glance数据库
数据库同步
[root@controller ~]# su glance -s /bin/sh -c "glance-manage db_sync"查看同步后的数据表
[root@controller ~]# mysql -uroot -p000000 # 1. 数据库登录 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 270 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)]> use glance; # 2. 进入glance数据库 Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed MariaDB [glance]> show tables; # 3. 显示数据表 +----------------------------------+ | Tables_in_glance | +----------------------------------+ | alembic_version | | image_locations | | image_members | | image_properties | | image_tags | | images | | metadef_namespace_resource_types | | metadef_namespaces | | metadef_objects | | metadef_properties | | metadef_resource_types | | metadef_tags | | migrate_version | | task_info | | tasks | +----------------------------------+ 15 rows in set (0.000 sec) MariaDB [glance]> quit # 4. 退出
作业1:显示数据表后后截图上传
Glance初始化
创建Glance用户、分配角色
# 1. 利用环境变量登录 [root@controller ~]# source admin-login # 2. 创建用户 [root@controller ~]# openstack user create --domain default --password 000000 glance +---------------------+----------------------------------+ | Field | Value | +---------------------+----------------------------------+ | domain_id | default | | enabled | True | | id | 504c914efe01478a9da5968a88dad987 | | options | {} | | password_expires_at | None | +---------------------+----------------------------------+ # 3. 添加角色 [root@controller ~]# openstack role add --project project --user glance admin创建服务和端点
# 1. 创建服务 [root@controller ~]# openstack service create --name glance image +---------+----------------------------------+ | Field | Value | +---------+----------------------------------+ | enabled | True | | id | da84735108944b6690cbcf9fab56f5fd | | name | glance | | type | image | +---------+----------------------------------+ # 2. 创建公共用户使用的端点 [root@controller ~]# openstack endpoint create --region RegionOne glance public http://controller:9292 +--------------+----------------------------------+ | Field | Value | +--------------+----------------------------------+ | enabled | True | | id | a344eb651ebc41f7ae2b1b74b3e8fda3 | | interface | public | | region | RegionOne | | region_id | RegionOne | | service_id | da84735108944b6690cbcf9fab56f5fd | | service_name | glance | | service_type | image | | url | http://controller:9292 | +--------------+----------------------------------+ # 3. 创建内部组件用户使用的端点 [root@controller ~]# openstack endpoint create --region RegionOne glance internal http://controller:9292 +--------------+----------------------------------+ | Field | Value | +--------------+----------------------------------+ | enabled | True | | id | f1b7a33ab36047209e0130debf4dc500 | | interface | internal | | region | RegionOne | | region_id | RegionOne | | service_id | da84735108944b6690cbcf9fab56f5fd | | service_name | glance | | service_type | image | | url | http://controller:9292 | +--------------+----------------------------------+ # 4. 创建admin用户使用的端点 [root@controller ~]# openstack endpoint create --region RegionOne glance admin http://controller:9292 +--------------+----------------------------------+ | Field | Value | +--------------+----------------------------------+ | enabled | True | | id | c176c6ff1caa4eba9083105bd2b41261 | | interface | admin | | region | RegionOne | | region_id | RegionOne | | service_id | da84735108944b6690cbcf9fab56f5fd | | service_name | glance | | service_type | image | | url | http://controller:9292 | +--------------+----------------------------------+启动Glance服务并验证
# 1. 设置开机自启 [root@controller ~]# systemctl enable openstack-glance-api Created symlink from /etc/systemd/system/multi-user.target.wants/openstack-glance-api.service to /usr/lib/systemd/system/openstack-glance-api.service. # 2. 启动服务 [root@controller ~]# systemctl start openstack-glance-api [root@controller ~]# # 3. 查看服务状态 [root@controller ~]# systemctl status openstack-glance-api ● openstack-glance-api.service - OpenStack Image Service (code-named Glance) API server Loaded: loaded (/usr/lib/systemd/system/openstack-glance-api.service; enabled; vendor preset: disabled) Active: active (running) since Wed 2023-09-20 11:18:59 EDT; 13s ago Main PID: 5068 (glance-api) CGroup: /system.slice/openstack-glance-api.service ├─5068 /usr/bin/python2 /usr/bin/glance-api ├─5080 /usr/bin/python2 /usr/bin/glance-api └─5081 /usr/bin/python2 /usr/bin/glance-api Sep 20 11:18:59 controller systemd[1]: Started OpenStack Image Service (code-named Glance) API server. Sep 20 11:18:59 controller glance-api[5068]: /usr/lib/python2.7/site-packages/paste/deploy/loadwsgi.py:22: PkgRe...tely. Sep 20 11:18:59 controller glance-api[5068]: return pkg_resources.EntryPoint.parse("x=" + s).load(False) Hint: Some lines were ellipsized, use -l to show in full.创建镜像
# 1. 获取镜像文件到本地 [root@controller ~]# curl -O http://download.cirros-cloud.net/0.5.1/cirros-0.5.1-x86_64-disk.img % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 273 100 273 0 0 40 0 0:00:06 0:00:06 --:--:-- 65 # 2. 创建镜像 [root@controller ~]# openstack image create --file cirros-0.5.1-x86_64-disk.img --disk-format raw --container-format bare --public cirros # 3. 查看镜像列表 [root@controller ~]# openstack image list +--------------------------------------+--------+--------+ | ID | Name | Status | +--------------------------------------+--------+--------+G | cfd3a7d5-3339-4d11-8c18-18c6d703c03b | cirros | active | +--------------------------------------+--------+--------+作业2:显示镜像列表后截图上传
基于预制镜像定制OpenStack镜像
实验环境
控制节点和计算节点都是
快照项目12
Centos镜像创建
设置虚拟机和宿主机共享
控制节点关机状态右键->设置,选择选项->共享文件夹->总是启用并添加本地C盘内的镜像文件所在目录:学生机路径
C:\云计算上课环境\镜像-文档
安装vm-tools并挂载设备
[root@controller ~]# yum -y install open-vm-tools [root@controller ~]# vmhgfs-fuse .host:/ /mnt/hgfs -o subtype=vmhgfs-fuse,allow_other [root@controller ~]# file /mnt/hgfs/镜像-文档/CentOS-7-x86_64-GenericCloud-2003.qcow2 /mnt/hgfs/镜像/CentOS-7-x86_64-GenericCloud-2003.qcow2: QEMU QCOW Image (v3), 8589934592 bytes
创建镜像
[root@controller ~]# openstack image create --disk-format qcow2 --container-format bare --public --file /mnt/hgfs/镜像-文档/CentOS-7-x86_64-GenericCloud-2003.qcow2 centos7
创建虚拟机实例
通过浏览器
192.168.10.20访问Horizon服务的界面,设置域名Default、用户名admin、密码000000登录。创建实例类型、网络。
创建
实例类型和网络时设置项目为admin,否则没有权限。创建步骤可参考创建虚拟机实例创建网络。
设置
配置菜单后完成实例创建:#!/bin/bash passwd centos<<EOF 000000 000000 EOF这里配置的脚本用来在实例启动时设置centos账户的密码,否则不能登录控制台

定制镜像
在实例控制台中完成配置:
使用账号:centos;密码:000000登录控制台
sudo su命令切换root用户passwd root命令设置密码000000编辑文件
/etc/cloud/cloud.cfg修改第四行为disable_root:0重启实例使用root用户登录。
创建快照后基于新的快照创建实例,验证root用户直接登录控制台
作业3:登录控制台后截图上传
实例快照转换成镜像
# 1. 查看镜像列表获取ID [root@controller ~]# openstack image list +--------------------------------------+-------------+--------+ | ID | Name | Status | +--------------------------------------+-------------+--------+ | 87b290e8-57c9-4c27-bb77-9d312c6d96c2 | centos7 | active | | 26b59df3-4fbe-4368-abd9-a5e5b58efe50 | centos_test | active | | 3a211d53-30e2-4d42-a7cf-ee55786b176a | cirros | active | +--------------------------------------+-------------+--------+ # 2. 将本地快照文件转换成镜像 [root@controller ~]# openstack image create centos_test_image --file /var/lib/glance/images/26b59df3-4fbe-4368-abd9-a5e5b58efe50 --disk-format qcow2 --container-format bare