使用docker-compose扩展服务时如何在卷路径中指定迭代器? [英] How to specify an iterator in the volume path when using docker-compose to scale up service?

查看:157
本文介绍了使用docker-compose扩展服务时如何在卷路径中指定迭代器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:我正在使用docker-compose来将tomcat服务放置到docker swarm集群中,但目前我正为如何使用日志目录而苦苦挣扎。



考虑(显然)组成的docker-compose,它简单地启动了tomcat并在其中安装了一个日志文件系统,以扩大服务规模并保留日志目录的唯一性。捕获日志。



版本: 2
服务:
tomcat:
图片: tomcat :latest
主机名:tomcat-example
命令:/start.sh
卷:
- / data / container / tomcat / logs:/ opt / tomcat / logs, z



版本




  • docker 1.11

  • docker-compose 1.7.1

  • API版本1.21



问题:我想了解如何将变量插入卷日志路径,以便t扩展服务的每个实例的日志目录都是唯一的



例如,

 卷:
- / data / container / tomcat / $ {container_name} / logs:/ opt / tomcat / logs,z

我看到基于项目名称(或我所在的目录)实际上是已知的容器名称,所以我可以使用它吗?



例如,将项目名称设置为 tomcat并运行 docker-compose scale tomcat = 2 ,我会看到以下容器。




  • 主机名 / tomcat_1

  • 主机名 / tomcat_2



那么我有什么办法可以利用它作为日志记录量中的变量,欢迎其他建议或方法。我意识到我可以指定一个相对路径,然后让container_id来处理这个问题,但是现在,如果我将splunk或logstash附加到日志记录设备,则需要知道哪些容器确实是日志记录设备,而不是基本容器f / s。但是理想情况下,我希望在此处使用特定的绝对路径。



多谢码头工人!
R。

解决方案

您实际上不应登录到文件系统,而应使用诸如graylog /用logstash / splunk / ...代替。要么使用特定的附加程序在Tomcat中配置日志记录框架,要么登录sysout并在Docker中配置日志记录驱动程序以将您的日志重定向到外部目标。



如果您真的想使用文件系统方式,只需使用一个常规的未命名卷,然后在容器上调用 docker inspect 即可在文件系统上找到该卷的路径:

  [... snip ...] 
安装:[
{
类型:卷,
名称: b8c ... SomeHash ... 48d6e,
源: /var/lib/docker/volumes/b8c...SomeHash。 ..48d6e / _data,
Destination: / opt / tomcat / logs,
[... snip ...]

如果要在特定位置使用漂亮的名称,请使用脚本创建符号链接。



<但是,我仍然对此解决方案充满怀疑,尤其是在多主机群环境中。登录用例的外部方法是使用外部服务。


Background: I'm using docker-compose in order to place a tomcat service into a docker swarm cluster but I'm presently struggling with how I would approach the logging directory given that I want to scale the service up yet retain the uniqueness of the logging directory.

Consider the (obviously) made up docker-compose which simply starts tomcat and mounts a logging filesystem in which to capture the logs.

version: '2' services: tomcat: image: "tomcat:latest" hostname: tomcat-example command: /start.sh volumes: - "/data/container/tomcat/logs:/opt/tomcat/logs,z"

Versions

  • docker 1.11
  • docker-compose 1.7.1
  • API version 1.21

Problem: I'm looking to understand how I would approach inserting a variable into the 'volume' log path so that the log directory is unique for each instance of the scaled service

say,

volumes:
    - "/data/container/tomcat/${container_name}/logs:/opt/tomcat/logs,z"

I see that based on project name (or directory I'm in) the container name is actually known, so could I use this ?

eg, setting the project name to 'tomcat' and running docker-compose scale tomcat=2 I would see the following containers.

  • hostname/tomcat_1
  • hostname/tomcat_2

So is there any way I could leverage this as a variable in the logging volume, Other suggestions or approaches welcome. I realise that I could just specify a relative path and let the container_id take care of this, but now if I attach splunk or logstash to the logging devices I'd need to know which ones are indeed logging devices as opposed to the base containers f/s. However Ideally I'm looking use a specific absolute path here.

Thanks in advance dockers! R.

解决方案

You should really NOT log to the filesystem, and use a specialized log management tool like graylog/logstash/splunk/... instead. Either configure your logging framework in Tomcat with a specific appender, or log to sysout and configure a logging driver in Docker to redirect your logs to the external destination.

This said, if you really want to go the filesystem way, simply use a regular unnamed volume, and then call docker inspect on your container to find the volume's path on the filesystem :

[...snip...]
"Mounts": [
    {
        "Type": "volume",
        "Name": "b8c...SomeHash...48d6e",
        "Source": "/var/lib/docker/volumes/b8c...SomeHash...48d6e/_data",
        "Destination": "/opt/tomcat/logs",
[...snip...]

If you want to have nice-looking names in a specific location, use a script to create symlinks.

Yet, I'm still doubtfull on this solution, especially in a multi-host swarm context. Logging to an external, specialized service is the way to go in your use case.

这篇关于使用docker-compose扩展服务时如何在卷路径中指定迭代器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆