如何在 Docker Swarm 中的全局服务中设置主机名

sad*_*k-f 6 docker docker-swarm elastic-stack metricbeat

我有一个服务作为全局服务部署到我的 Docker Swarm 集群 (ELK Metricbeat)。

我希望每个服务都有一个与正在运行的节点(主机)的主机名相同的主机名?

换句话说,我如何在 yml 文件中获得相同的结果,例如:

 docker run -h `hostname` elastic/metricbeat:5.4.1
Run Code Online (Sandbox Code Playgroud)

这是我的 yml 文件:

metricbeat:
  image: elastic/metricbeat:5.4.1
  command: metricbeat -e -c /etc/metricbeat/metricbeat.yml -system.hostfs=/hostfs
  hostname: '`hostname`'
  volumes:
    - /proc:/hostfs/proc:ro
    - /sys/fs/cgroup:/hostfs/sys/fs/cgroup:ro
    - /:/hostfs:ro
    - /var/run/docker.sock:/var/run/docker.sock
  networks:
    - net
  user: root
  deploy:
    mode: global
Run Code Online (Sandbox Code Playgroud)

我努力了:

  hostname: '`hostname`'
  hostname: '${hostname}'
Run Code Online (Sandbox Code Playgroud)

但没有成功。

有什么解决办法吗?

先感谢您。

sad*_*k-f 2

我通过在下面安装主机主机名文件/etc/nodehostname并更改服务容器以使用读取文件并替换 metricbeat.yml 中的变量(名称)的入口点解决了该问题

docker-entrypoint.sh

export NODE_HOSTNAME=$(eval cat /etc/nodehostname)

envsubst '$NODE_HOSTNAME' </etc/metricbeat/metricbeat.yml.tpl > /etc/metricbeat/metricbeat.yml
Run Code Online (Sandbox Code Playgroud)