简体   繁体   English

如何从master获取minion的主机名

[英]How to get minion's hostname from master

I am using salt stack.我正在使用盐栈。 From the master (node01), I am using cmd.run to run docker-compose task on minions (node02).从主节点(node01),我使用cmd.run在 minions(node02)上运行 docker-compose 任务。

My docker-compose file needs to know the hostname of minion node02 but currently since the task is being run from master node01 , the docker-compose file receives the hostname of the master and not the minion.我的 docker-compose 文件需要知道 minion node02的主机名,但目前由于任务是从 master node01运行的,因此 docker-compose 文件接收到 master 的主机名而不是 minion。

node01 and node02 are on the same network node01node02在同一个网络上

Salt command run on node01node01上运行 salt 命令

 salt-cp -L node02 docker-compose.yml /my/path
 salt -L node02 cmd.run "docker-compose -f /my/path/docker-compose.yml pull &&
 docker-compose -f /my/path/docker-compose.yml down && 
 docker-compose -f /my/path/docker-compose.yml up -d" --timeout=600

In the following example Docker Compose file, I would like to pass the HOST_NAME to the command.在以下示例 Docker 编写文件中,我想将HOST_NAME传递给命令。 Right now, I have to create multiple different docker-compose files for each node as passing the hostname isn't working现在,我必须为每个节点创建多个不同的 docker-compose 文件,因为传递主机名不起作用

version: "3.3"
# interaction
services:
  consul:
    command: -node=HOST_NAME --join=192.168.0.30 --advertise=192.168.0.30
    image: progrium/consul:latest
    hostname: "consul"
    container_name: consul
    ports:
    - "8300:8300"
    - "8400:8400"
    - "8500:8500"
    - "8600:53/udp"
    - "8301:8301"
    - "8302:8302"
    network_mode: "host"
    restart: always

In my current setup, how do I pass the hostname of the minion to docker-compose when running the command from the master?在我当前的设置中,当从 master 运行命令时,如何将 minion 的主机名传递给 docker-compose?

If you're using a recipe it could be achieve easily with some jinja in your docker compose file:如果您使用的是配方,则可以通过 docker 撰写文件中的一些 jinja 轻松实现:

in your docker-compose.yml you will have this:在您的 docker-compose.yml 中,您将拥有:

version: "3.3"
# interaction
services:
  consul:
    command: -node={{ pillar['docker_hostname'] }} --join=192.168.0.30 --advertise=192.168.0.30
    image: progrium/consul:latest
    hostname: "consul"
    container_name: consul
    ports:
    - "8300:8300"
    - "8400:8400"
    - "8500:8500"
    - "8600:53/udp"
    - "8301:8301"
    - "8302:8302"
    network_mode: "host"
    restart: always

When doing a file.managed from the master to the minion, the jinja part will be replaced.当从 master 到 minion 执行 file.managed 时,将替换 jinja 部分。

You will have to put this file in the /srv/salt hierarchy.您必须将此文件放在 /srv/salt 层次结构中。 lets say /srv/salt/files/docker-compose.yml假设 /srv/salt/files/docker-compose.yml

then in /srv/salt/docker.sls:然后在 /srv/salt/docker.sls 中:

copy docker-compose:
  file.managed:
    - name: '/my/path/docker-compose.yml'
    - source: salt://files/docker-compose.yml.jinja
    - template: jinja

run docker:
  cmd.run:
    - name: "docker-compose -f /my/path/docker-compose.yml pull && docker-compose -f /my/path/docker-compose.yml down && docker-compose -f /my/path/docker-compose.yml up -d"

And after you juste have to use your recipe and to pass as a parameter the hostname (test in my example)在您必须使用您的配方并将主机名作为参数传递之后(在我的示例中进行测试)

salt 'node2' state.sls docker pillar='{"docker_hostname": "test"}'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 docker compose 中获取主机的主机名 - Get host's hostname in docker compose 当 K8S writer env 到 docker? 有没有办法从 dockerfile 获取真正的 HOSTNAME 或 POST_NAME? - When K8S writer env to docker? Is there a way to get real HOSTNAME or POST_NAME from dockerfile? 如何限制 Kubernetes 容器查看主机(K8s minion)操作系统上的块设备列表? - How to restrict Kubernetes containers to view block devices list on host (K8s minion) os? SaltStack master / minion管理容器内的数据库 - SaltStack master/minion to administrate my database that is inside a container Docker 中的 salt-master 和 salt-minion 连接问题 - Problem with salt-master and salt-minion connection in Docker 如何在没有环境变量的情况下从该主机上的 docker 容器内部获取 docker 主机的主机名 - How to get the hostname of the docker host from inside a docker container on that host without env vars 如何将新的Kubernetes minion添加到当前集群 - How to add new Kubernetes minion to current cluster 如何在Kube Minion上配置flanneld服务 - how to configure flanneld service on kube minion 将服务容器的主机名设置为主机的主机名 - Set hostname of service container to host's hostname 如何使Secure Gateway Client解析目标主机的主机名 - How to make Secure Gateway Client to resolve the hostname of the destination's host
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM