在docker swarm服务中执行命令 [英] execute a command within docker swarm service

查看:215
本文介绍了在docker swarm服务中执行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 初始化群模式:

  1. Initialize swarm mode:

root@ip-172-31-44-207:/home/ubuntu# docker swarm init --advertise-addr 172.31.44.207

Swarm initialized: current node (4mj61oxcc8ulbwd7zedxnz6ce) is now a manager.

To add a worker to this swarm, run the following command:


  • 加入第二个节点:

  • Join the second node:

    docker swarm join \
    --token SWMTKN-1-4xvddif3wf8tpzcg23tem3zlncth8460srbm7qtyx5qk3ton55-6g05kuek1jhs170d8fub83vs5 \
    172.31.44.207:2377
    


  • 要将管理员添加到该群集,请运行 docker swarm join-token manager并按照说明进行操作。

    To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

    # start 2 services
    docker service create continuumio/miniconda3 
    
    docker service create --name redis redis:3.0.6
    
    
    root@ip-172-31-44-207:/home/ubuntu# docker service ls
    ID            NAME        REPLICAS  IMAGE                   COMMAND
    2yc1xjmita67  miniconda3  0/1       continuumio/miniconda3
    c3ptcf2q9zv2  redis       1/1       redis:3.0.6
    

    如上所示,redis是它的副本,而miniconda却不是复制

    As shown above, redis has it's replica while miniconda does not seem to be replicated.

    我通常会登录到miniconda容器以键入以下命令:

    I do usually log-in to miniconda container to type these commands:

    /opt/conda/bin/conda install jupyter -y --quiet && mkdir /opt/notebooks && /opt/conda/bin/jupyter notebook --notebook-dir=/opt/notebooks --ip='*' --port=8888 --no-browser
    

    问题是 docker exec -it XXX bash 命令不适用于群体模式。

    The problem is that docker exec -it XXX bash command does not work with swarm mode.

    推荐答案

    有一个衬套可用于访问本地主机服务的相应实例:

    There is one liner for accessing corresponding instance of the service for localhost:

    docker exec -ti stack_myservice.1.$(docker service ps -f 'name=stack_myservice.1' stack_myservice -q --no-trunc | head -n1) /bin/bash
    

    已在PowerShell上进行了测试,但bash应该相同。 oneliner访问第一个实例,但在两个位置中用要访问的实例编号替换 1以获得另一个实例。

    It is tested on PowerShell, but bash should be the same. The oneliner accesses the first instance, but replace '1' with the number of the instance you want to access in two places to get other one.

    更复杂的示例是分布式案例:

    More complex example is for distributed case:

    #! /bin/bash
    
    set -e
    
    exec_task=$1
    exec_instance=$2
    
    strindex() { 
      x="${1%%$2*}"
      [[ "$x" = "$1" ]] && echo -1 || echo "${#x}"
    }
    
    parse_node() {
      read title
      id_start=0
      name_start=`strindex "$title" NAME`
      image_start=`strindex "$title" IMAGE`
      node_start=`strindex "$title" NODE`
      dstate_start=`strindex "$title" DESIRED`
      id_length=name_start
      name_length=`expr $image_start - $name_start`
      node_length=`expr $dstate_start - $node_start`
    
      read line
      id=${line:$id_start:$id_length}
      name=${line:$name_start:$name_length}
      name=$(echo $name)
      node=${line:$node_start:$node_length}
      echo $name.$id
      echo $node
    }
    
    if true; then 
       read fn 
       docker_fullname=$fn
       read nn
       docker_node=$nn 
    fi < <( docker service ps -f name=$exec_task.$exec_instance --no-trunc -f desired-state=running $exec_task | parse_node )
    
    echo "Executing in $docker_node $docker_fullname" 
    
    eval `docker-machine env $docker_node`
    
    docker exec -ti $docker_fullname /bin/bash
    

    此脚本以后可以用作:

    swarm_bash stack_task 1
    

    它只是在所需节点上执行bash。

    It just execute bash on required node.

    这篇关于在docker swarm服务中执行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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