1. 环境准备与基础配置
在CentOS 7.9上部署OpenStack Train版的Glance服务前,需要确保基础环境配置正确。我通常会先检查系统版本和网络配置,这是后续所有操作的前提条件。
1.1 系统版本确认
首先通过以下命令确认系统版本:
bash复制cat /etc/redhat-release
uname -r
输出应该显示为:
code复制CentOS Linux release 7.9.2009 (Core)
3.10.0-1160.el7.x86_64
注意:OpenStack Train版官方推荐使用CentOS 7.9,其他小版本可能会出现兼容性问题。我在实际部署中发现7.6版本会有Python依赖冲突。
1.2 网络配置检查
OpenStack各组件需要稳定的网络通信,建议配置静态IP:
bash复制vi /etc/sysconfig/network-scripts/ifcfg-ens33
关键配置项:
code复制BOOTPROTO=static
ONBOOT=yes
IPADDR=192.168.100.10
NETMASK=255.255.255.0
GATEWAY=192.168.100.1
DNS1=8.8.8.8
重启网络服务:
bash复制systemctl restart network
1.3 基础软件包安装
安装必要的工具和依赖:
bash复制yum install -y epel-release
yum install -y python-pip git wget vim net-tools
pip install --upgrade pip
2. OpenStack基础环境搭建
2.1 数据库配置
Glance需要使用MySQL/MariaDB存储元数据信息:
bash复制yum install -y mariadb mariadb-server python2-PyMySQL
systemctl enable mariadb
systemctl start mariadb
安全配置:
bash复制mysql_secure_installation
创建Glance数据库和用户:
sql复制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';
FLUSH PRIVILEGES;
2.2 RabbitMQ消息队列
安装配置RabbitMQ:
bash复制yum install -y rabbitmq-server
systemctl enable rabbitmq-server
systemctl start rabbitmq-server
添加OpenStack用户:
bash复制rabbitmqctl add_user openstack RABBIT_PASS
rabbitmqctl set_permissions openstack ".*" ".*" ".*"
2.3 Memcached缓存服务
安装Memcached:
bash复制yum install -y memcached python-memcached
systemctl enable memcached
systemctl start memcached
3. Keystone认证服务集成
3.1 创建Glance服务实体
在Keystone中注册Glance服务:
bash复制openstack service create --name glance --description "OpenStack Image" image
3.2 创建API端点
设置Glance的API端点:
bash复制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
3.3 创建Glance用户和角色
bash复制openstack user create --domain default --password GLANCE_PASS glance
openstack role add --project service --user glance admin
4. Glance服务安装与配置
4.1 安装Glance软件包
bash复制yum install -y openstack-glance
4.2 配置glance-api.conf
编辑配置文件:
bash复制vi /etc/glance/glance-api.conf
关键配置项:
ini复制[database]
connection = mysql+pymysql://glance:GLANCE_DBPASS@controller/glance
[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_PASS
[paste_deploy]
flavor = keystone
[glance_store]
stores = file,http
default_store = file
filesystem_store_datadir = /var/lib/glance/images/
4.3 配置glance-registry.conf
bash复制vi /etc/glance/glance-registry.conf
配置内容:
ini复制[database]
connection = mysql+pymysql://glance:GLANCE_DBPASS@controller/glance
[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_PASS
[paste_deploy]
flavor = keystone
5. 数据库初始化与服务启动
5.1 同步数据库
bash复制su -s /bin/sh -c "glance-manage db_sync" glance
5.2 启动服务
bash复制systemctl enable openstack-glance-api openstack-glance-registry
systemctl start openstack-glance-api openstack-glance-registry
检查服务状态:
bash复制systemctl status openstack-glance-api openstack-glance-registry
6. 验证Glance服务
6.1 上传测试镜像
下载CirrOS测试镜像:
bash复制wget http://download.cirros-cloud.net/0.4.0/cirros-0.4.0-x86_64-disk.img
上传镜像:
bash复制openstack image create "cirros" \
--file cirros-0.4.0-x86_64-disk.img \
--disk-format qcow2 --container-format bare \
--public
6.2 查看镜像列表
bash复制openstack image list
预期输出:
code复制+--------------------------------------+--------+--------+
| ID | Name | Status |
+--------------------------------------+--------+--------+
| 38047887-5a97-4634-a4c1-3f8d1a2e53d9 | cirros | active |
+--------------------------------------+--------+--------+
7. 高级配置与优化
7.1 多存储后端配置
修改glance-api.conf支持多种存储后端:
ini复制[glance_store]
stores = file,http
default_store = file
filesystem_store_datadir = /var/lib/glance/images/
7.2 镜像缓存配置
ini复制[image_cache]
enabled = true
directory = /var/lib/glance/image-cache/
7.3 性能调优参数
ini复制[DEFAULT]
workers = 4
8. 常见问题排查
8.1 镜像上传失败
错误现象:
code复制Error: Failed to upload image
排查步骤:
- 检查存储目录权限:
bash复制ls -ld /var/lib/glance/images/
- 查看日志:
bash复制tail -f /var/log/glance/api.log
8.2 数据库连接问题
错误信息:
code复制OperationalError: (pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on 'controller'")
解决方法:
- 确认MySQL服务运行状态
- 检查防火墙设置
- 验证数据库用户权限
8.3 认证失败
错误信息:
code复制401 Unauthorized
解决方法:
- 检查Keystone服务状态
- 验证glance用户密码
- 确认端点URL正确
9. 安全加固建议
9.1 启用SSL加密
配置HTTPS访问:
ini复制[ssl]
enable = True
cert_file = /path/to/cert.pem
key_file = /path/to/key.pem
9.2 访问控制
配置防火墙规则:
bash复制firewall-cmd --permanent --add-port=9292/tcp
firewall-cmd --reload
9.3 日志审计
配置详细日志:
ini复制[DEFAULT]
debug = True
log_file = /var/log/glance/glance.log
10. 日常维护操作
10.1 镜像管理
删除镜像:
bash复制openstack image delete <image-id>
更新镜像属性:
bash复制openstack image set --property hw_disk_bus=scsi <image-id>
10.2 服务监控
检查服务健康状态:
bash复制openstack catalog list
glance-manage db version
10.3 备份与恢复
备份数据库:
bash复制mysqldump -u glance -p glance > glance_backup.sql
备份镜像存储:
bash复制tar czvf glance_images_backup.tar.gz /var/lib/glance/images/
在实际生产环境中部署Glance服务时,我发现以下几个经验特别重要:
-
存储选择:对于大规模部署,建议使用Swift或Ceph作为后端存储,而不是本地文件系统。我曾经在一个项目中因为使用本地存储导致镜像服务成为性能瓶颈。
-
镜像缓存:合理配置image_cache可以显著提高频繁访问镜像的响应速度。建议将缓存目录放在高性能存储设备上。
-
并发控制:根据服务器硬件配置调整workers参数。我的经验法则是CPU核心数的2倍,但不超过8个。
-
定期维护:每月执行一次
glance-manage db purge可以清理已删除镜像的残留数据,防止数据库膨胀。 -
监控指标:建议监控API响应时间、镜像上传成功率等关键指标,我在实际运维中发现这些指标最能反映服务健康状态。
