Kolla是Openstack的容器化部署项目。
在Kolla中使用ansible启动容器,服务运行于各容器内。
很多情况下,我们需要自定义服务启动的某些参数,这就需要我们对Kolla中服务的启动有详细的了解。
服务启动流程
以下是对Kolla中一个完整服务的分析,以horizon为例。
1. ansible/roles/horizon/templates/horizon.json.j2
其中有:
"command": "/usr/sbin/{{ apache_cmd }} -DFOREGROUND", |
其中apache_cmd
是根据当前平台为redhat系还是ubuntu系,分别设置为'httpd'
/'apache2'
。
2. ansible/roles/horizon/tasks/config.yml
其中有name为“Copying horizon JSON configuration file”的task:
- name: Copying horizon JSON configuration file |
以上task将1中定义的horizon.json.j2
拷贝渲染为horizon/config.json
。
3. docker/base/set_configs.py
其中的load_config
函数具体内容:
with open('/run_command', 'w+') as f: |
读取horizon/config.json
中的command
配置,并写入到/run_command
文件。
4. docker/base/Dockerfile.j2
Dockerfile的模板文件中有:
COPY start.sh /usr/local/bin/kolla_start |
build镜像时会将start.sh
拷贝至/usr/local/bin/kolla_start
, 后者会在容器启动时被执行。
5. docker/base/start.sh内容
CMD=$(cat /run_command) |
执行/run_command
中所有命令,而从3得知,/run_command
从config.json的command配置得来,而config.json由horizon.json.j2得来。
整个流程概括
容器启动时执行start.sh
中定义的命令,其中包含horizon.json.j2
的command
字段定义的命令。
至此,整个服务的配置及运行流程走通。
extend_start
除了start.sh
以外,很多容器还会自动执行extend_start。它主要用其来做服务启动前的准备工作,如数据库同步。
下面仍以horizon为例进行实际分析。
extend_start.sh
主要执行一些准备工作,如dbsync。社区horizon未使用数据库,因此无此步骤。
###horizon/Dockerfile.j2
COPY extend_start.sh /usr/local/bin/kolla_extend_start |
镜像中会拷贝extend_start.sh
至/usr/local/bin/kolla_extend_start
,并添加可执行权限。
base/Dockerfile.j2
所有服务的Dockerfile最终都继承自base,其Dockerfile中包含如下内容:
COPY set_configs.py /usr/local/bin/kolla_set_configs |
确保/usr/local/bin/kolla_extend_start
文件存在并添加可执行权限。
base/start.sh中
if [[ ! "${!KOLLA_SKIP_EXTEND_START[@]}" ]]; then |
检测未配置KOLLA_SKIP_EXTEND_START
变量的情况下,使用source执行/usr/local/bin/kolla_extend_start
。
至此,整个extend_start的执行流程打通。
添加自定义服务步骤
添加自定义服务时,默认情况下,只需要编写:
*.json.js
文件,command中指定启动命令;- 编写相关task,拷贝
*.json.js
文件至指定目录,并且使用指定名称。 - 其余使用kolla即可。