OpenStack镜像的构建
目录
概述
OpenStack镜像的构建有许多种方法,可以直接利用别人构建好的镜像,也可以手动构建,也可以使用镜像构建工具实现镜像构建的自动化,这里主要讲后面两种。
获取镜像
关于如何获取镜像,OpenStack官网上已经说的足够清楚了,这里就不赘述了,可以直接去官网查看。
手动构建镜像
这里以构建centos 7的镜像为例子,我的环境也是centos7。
1 | yum -y install qemu-kvm libvirt virt-install bridge-utils |
diskimage-builder(简称为DIB)是一个用于自动构建自定义的操作系统镜像的工具,以便在云和其他环境中使用。
构建docker容器
利用docker容器来搭建镜像的环境,这里选择centos镜像,同时将本地目录和容器内的文件目录做一个映射。
1 | docker run -itd --privileged=true --name centos -v $PWD:/root centos:7 /sbin/init |
安装
依赖环境
进入容器内安装DIB的依赖环境。
1 | docker exec -it centos bash |
安装qemu-img和kpartx。
1 | apt-get install qemu-utils kpartx squashfs-tools e4fsprogs # ubuntu |
DIB
从github将DIB下载下来。
1 | git clone https://github.com/openstack/diskimage-builder.git |
切换到最近的tag上,我这里是3.7.0。
1 | cd diskimge-builder |
设置虚环境
1 | pip install virtualenv |
安装diskimage-builder。
1 | cd /root/diskimage-builder && /root/venv/diskimage-builder/bin/pip install -e . |
测试
1 |
|
创建ubuntu镜像。
1 | /root/venv/diskimage-builder/bin/disk-image-create -a amd64 -o ubuntu-amd64 vm ubuntu |
高级
Elements
DIB中的elements
DIB中有大量的元件。DIB的元件在diskimage-builder项目中的/diskimage_builder/elements的目录下。
自定义elements
可以自行编写elements来满足构建镜像的要求。
1 |
|
- Trove中的elements
除了DIB提供的元件,Trove也提供自己所支持的数据库的元件。可以在trove项目下的integration/scripts/files/elements中找到trove的相关元件。
Trove镜像构建
下载Trove
从github上将trove项目下载到本地,位置和DIB存放的一致,都在/root下。
1 | git clone https://git.openstack.org/openstack/trove.git |
设置变量
1 |
|
设置变量
1 |
|
- 说明
- HOST_USERNAME
HOST_USERNAME is the name of the user on the Trove host machine. It is used to identify the location of the authorized_keys, id_rsa, and id_rsa_pub files that are to be in guest image creation.
- HOST_SCP_USERNAME
HOST_SCP_USERNAME is the name of the user on the Trove host machine used to connect from the guest while copying the guest agent code during the upstart process.
- GUEST_USERNAME
GUEST_USERNAME is the name of the user on the Trove guest who will run the guest agent and perform a number of other jobs. This user is created during the image build process if it does not exist.
- CONTROLLER_IP
CONTROLLER_IP
- TROVESTACK_SCRIPTS
TROVESTACK_SCRIPTS is the pointer to files in the trove project.
- SERVICE_TYPE
SERVICE_TYPE
- PATH_TROVE
PATH_TROVE is the path to the Trove source code and this is used in the rsync of code to the guest.
- ESCAPED_PATH_TROVE
ESCAPED_PATH_TROVE is the escaped version of PATH_TROVE and is used for much the same purpose as PATH_TROVE.
- SSH_DIR
SSH_DIR is a path to the .ssh directory for the user on the host and is used in the image creation process to obtain the authorized_keys, id_rsa and id_rsa_pub files.
- GUEST_LOGDIR
GUEST_LOGDIR is the location on the guest where the Trove log file is to be stored.
- ESCAPED_GUEST_LOGDIR
ESCAPED_GUEST_LOGDIR is the escaped version of GUEST_LOGDIR.
- DIB_CLOUD_INIT_DATASOURCES
DIB_CLOUD_INIT_DATASOURCES . The DIB element cloud-init-datasources uses this value to determine the data sources that must be queried during first boot to obtain instance metadata.
- DATASTORE_PKG_LOCATION
DATASTORE_PKG_LOCATION is not used in the creation of the Mysql instance but is used by some databases (currently DB2 and Vertica) to identify the location of a downloaded package containing the database. Other databases merely obtain this using apt-get or appropriate package management command. This variable is used for databases that do not allow this, and for example, require the user to click on a license agreement in order to obtain the database software.
- BRANCH_OVERRIDE
BRANCH_OVERRIDE
- RELEASE
RELEASE是我自己后加入的,不加的话会有报错,后面会提到。
构建镜像
构建镜像使用的命令是disk-image-create。
1 | (diskimage-builder) wkt@controller-1:~$ disk-image-create --help |
一旦设置好前面的变量可以用下面的命令创建Mysql数据库的Trove Guest镜像。
1 | disk-image-create -a amd64 -o ubuntu-xenial-mysql -x --qemu-img-options compat=1.1 ubuntu vm cloud-init-datasources ubuntu-guest ubuntu-xenial-guest ubuntu-mysql ubuntu-xenial-mysql |
这会默认生成qcow2格式的镜像,并储存在当前路径下。
出错的话,可以查看出现问题。
注册镜像
构建了镜像就需要在Glance和Trove中注册。首先,在Glance注册一个Guest镜像。获得镜像的id
1 | openstack image create ubuntu-xenial-mysql --file ubuntu-xenial-mysql.qcow2 --disk-format qcow2 --container-format bare --public --property image_type=distribution --property image_label=Test --property expected_size=10 --property hw_qemu_guest_agent='yes’ |
进入到trove-api的Docker中。
1 | root@controller-1:~# docker ps | grep trove |
注册新的数据库类型
1 | trove-manage --config-file=/etc/trove/trove.conf datastore_update mysql '' |
已经存在数据库类型可跳过此步。
更新mysql数据库的版本信息
1 | trove-manage --config-file=/etc/trove/trove.conf datastore_version_update mysql <版本> mysql <镜像id> '' 1 |
第一个参数
指的是数据库的类型第二个参数
指的是数据库的版本号第三个参数
指的是数据库管理类的名字,可以根据类名关联到Trove guest agent内的特定数据库类型。对Mysql而言,其对应的管理类就是trove.guestagent.datastore.mysql.manager.Manager。Trove支持的每一个数据库都有相应的Manage类,其类名可以在各种Trove配置文件中通过参数进行配置。第四个参数
指的是镜像的id。此id是之前的openstack image create命令输出中提供的。第五个参数
是传递到guest agent的prepare()信息中的安装包名称列表,通常用于guest agent安装最新的软件包或更新软件包到最新版本。这里为空。第六个参数
将数据库版本标记为活跃。
可以通过执行trove-manage datastore_version_update -h命令获取所有的帮助信息。
出现问题
command not found
- 执行代码
1
disk-image-create -a amd64 -o ubuntu-xenial-mysql -x --qemu-img-options compat=1.1 ubuntu vm cloud-init-datasources ubuntu-guest ubuntu-xenial-guest ubuntu-mysql ubuntu-xenial-mysql
- 出错日志在/opt/stack/trove/integration/scripts/files/elements/ubuntu-mysql/pre-install.d找到10-percona-apt-key文件出错代码
1
22018-09-11 02:34:15.980 | + die 'RELEASE must be set to a valid Ubuntu release (e.g. trusty)'
2018-09-11 02:34:15.999 | /tmp/in_target.d/pre-install.d/10-percona-apt-key: line 10: die: command not found1
10 [ -n "${RELEASE}" ] || die "RELEASE must be set to a valid Ubuntu release (e.g. trusty)"
- 解决问题
在设置变量时添加RELEASE=xenial
apt-update失败
执行代码
1
disk-image-create -a amd64 -o ubuntu-xenial-mysql -x --qemu-img-options compat=1.1 ubuntu vm cloud-init-datasources ubuntu-guest ubuntu-xenial-guest ubuntu-mysql ubuntu-xenial-mysql
出错日志
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
502018-09-11 02:40:15.989 | dib-run-parts Running /tmp/in_target.d/pre-install.d/10-percona-apt-key
2018-09-11 02:40:15.996 | + '[' -n root ']'
2018-09-11 02:40:15.997 | + '[' -n xenial ']'
2018-09-11 02:40:15.997 | + mkdir -p /home/root/.gnupg
2018-09-11 02:40:15.998 | + get_key_robust 1C4CBDCDCD2EFD2A
2018-09-11 02:40:15.998 | + KEY=1C4CBDCDCD2EFD2A
2018-09-11 02:40:15.998 | + set +e
2018-09-11 02:40:15.998 | + apt-key adv --keyserver hkp://keys.gnupg.net --recv-keys 1C4CBDCDCD2EFD2A
2018-09-11 02:40:16.121 | Executing: /tmp/tmp.qhaS3an1yi/gpg.1.sh --keyserver
2018-09-11 02:40:16.121 | hkp://keys.gnupg.net
2018-09-11 02:40:16.121 | --recv-keys
2018-09-11 02:40:16.121 | 1C4CBDCDCD2EFD2A
2018-09-11 02:40:16.127 | gpg: requesting key CD2EFD2A from hkp server keys.gnupg.net
2018-09-11 02:40:20.249 | gpg: key CD2EFD2A: public key "Percona MySQL Development Team <mysql-dev@percona.com>" imported
2018-09-11 02:40:20.249 | gpg: Total number processed: 1
2018-09-11 02:40:20.249 | gpg: imported: 1
2018-09-11 02:40:20.375 | + '[' 0 -ne 0 ']'
2018-09-11 02:40:20.375 | + set -e
2018-09-11 02:40:20.375 | + get_key_robust 9334A25F8507EFA5
2018-09-11 02:40:20.375 | + KEY=9334A25F8507EFA5
2018-09-11 02:40:20.375 | + set +e
2018-09-11 02:40:20.375 | + apt-key adv --keyserver hkp://keys.gnupg.net --recv-keys 9334A25F8507EFA5
2018-09-11 02:40:20.469 | Executing: /tmp/tmp.NgGbibfTaA/gpg.1.sh --keyserver
2018-09-11 02:40:20.469 | hkp://keys.gnupg.net
2018-09-11 02:40:20.469 | --recv-keys
2018-09-11 02:40:20.469 | 9334A25F8507EFA5
2018-09-11 02:40:20.472 | gpg: requesting key 8507EFA5 from hkp server keys.gnupg.net
2018-09-11 02:40:21.076 | gpg: key 8507EFA5: public key "Percona MySQL Development Team (Packaging key) <mysql-dev@percona.com>" imported
2018-09-11 02:40:21.076 | gpg: Total number processed: 1
2018-09-11 02:40:21.076 | gpg: imported: 1 (RSA: 1)
2018-09-11 02:40:21.191 | + '[' 0 -ne 0 ']'
2018-09-11 02:40:21.191 | + set -e
2018-09-11 02:40:21.191 | + cat
2018-09-11 02:40:21.192 | + apt-get update
2018-09-11 02:40:21.712 | Hit:1 http://security.ubuntu.com/ubuntu xenial-security InRelease
2018-09-11 02:40:22.429 | Hit:2 http://archive.ubuntu.com/ubuntu xenial InRelease
2018-09-11 02:40:22.700 | Hit:3 http://archive.ubuntu.com/ubuntu xenial-updates InRelease
2018-09-11 02:40:22.733 | Get:4 http://repo.percona.com/apt xenial InRelease [16.0 kB]
2018-09-11 02:40:22.968 | Hit:5 http://archive.ubuntu.com/ubuntu xenial-backports InRelease
2018-09-11 02:40:23.683 | Get:6 http://203.187.160.133:9011/repo.percona.com/c3pr90ntc0td/apt xenial/main Sources [6942 B]
2018-09-11 02:40:23.684 | Ign:6 http://203.187.160.133:9011/repo.percona.com/c3pr90ntc0td/apt xenial/main Sources
2018-09-11 02:40:24.022 | Get:7 http://203.187.160.132:9011/repo.percona.com/c3pr90ntc0td/apt xenial/main amd64 Packages [21.1 kB]
2018-09-11 02:40:24.023 | Ign:7 http://203.187.160.132:9011/repo.percona.com/c3pr90ntc0td/apt xenial/main amd64 Packages
2018-09-11 02:40:24.189 | Err:6 http://203.187.160.133:9011/repo.percona.com/c3pr90ntc0td/apt xenial/main Sources
2018-09-11 02:40:24.189 | Writing more data than expected (8870 > 6942)
2018-09-11 02:40:24.452 | Get:7 http://203.187.160.132:9011/repo.percona.com/c3pr90ntc0td/apt xenial/main amd64 Packages [115 kB]
2018-09-11 02:40:25.250 | Fetched 131 kB in 4s (32.5 kB/s)
2018-09-11 02:40:26.762 | Reading package lists...
2018-09-11 02:40:26.797 | E: Failed to fetch http://repo.percona.com/apt/dists/xenial/main/source/Sources Writing more data than expected (8870 > 6942)
2018-09-11 02:40:26.797 | E: Some index files failed to download. They have been ignored, or old ones used instead.解决问题
将trove/integration/scripts/files/elements/ubuntu-mysql/pre-install.d/10-percona-apt-key中的1
2
3
4
5
6
7
8
9get_key_robust 1C4CBDCDCD2EFD2A
get_key_robust 9334A25F8507EFA5
# Add Percona repo
# Creates the percona sources list
cat <<EOL > /etc/apt/sources.list.d/percona.list
deb http://repo.percona.com/apt $RELEASE main
deb-src http://repo.percona.com/apt $RELEASE main
EOL改为
1
2
3
4
5
6
7
8
9
10
11get_key_robust 1C4CBDCDCD2EFD2A
get_key_robust 9334A25F8507EFA5
apt-get install -y apt-transport-https
# Add Percona repo
# Creates the percona sources list
cat <<EOL > /etc/apt/sources.list.d/percona.list
deb https://repo.percona.com/apt $RELEASE main
deb-src https://repo.percona.com/apt $RELEASE main
EOL
其他错误
1 | Device or resource busy |
1 |
|
1 + 2 + 1 = 4
docker run -itd –name trove_monitor
-v /project/boncloud-trove/:/root/trove:rw
-v /etc/kolla/trove-monitor:/etc/trove/:rw
172.16.58.14:4000/bonc/centos-source-trove-api:7.0.1
bash