在没有ssh延迟的情况下在远程计算机上运行命令 [英] Running a command on remote machine without ssh delay

查看:89
本文介绍了在没有ssh延迟的情况下在远程计算机上运行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用负载均衡器建立一个API服务器.我将使用一台计算机作为主机,它将以轮循方式将任务分配给2台从属计算机.所有计算机都托管在AWS中.

I want to establish an API server with a load balancer. I will use one machine as the master which will assign tasks in a round robin manner to 2 slave machines. All machines are hosted in AWS.

为了处理请求,我需要运行python脚本.当主服务器收到请求时,它可以使用ssh在其中一个从服务器上触发命令,但这将使处理时间增加几秒钟的延迟.

In order to process the request, I need to run a python script. When the master receives the request, it can trigger the command on one of the slaves using ssh but this adds an extra couple of seconds delay to the processing time.

有没有办法减少/消除ssh延迟?

Is there a way to reduce/remove the ssh delay?

推荐答案

不确定是否实施了某些东西,或者只是收集了想法.

Not sure if you have something implemented or you just collect the thoughts.

Wikibooks 中描述了基本用例,但是最简单的方法是设置公共密钥身份验证和ssh_config(machine2的配置几乎相同):

The basic use case is described on wikibooks, but the easiest is to set up public key authentication and ssh_config (config for machine2 would be almost the same):

Host machine1
  HostName machine1.example.org
  ControlPath ~/.ssh/controlmasters/%r@%h:%p
  ControlMaster auto
  ControlPersist yes
  IdentityFile ~/.ssh/id_rsa-something

然后像这样调用远程脚本:

And then call the remote script like this:

ssh machine1 ./remote_script.py

第一次ssh调用将启动连接(并且会花费更长的时间),其他每个脚本调用都将使用现有的连接,并且几乎立即执行.

First ssh call will initiate the connection (and will take a bit longer), every other script call will use the existing connection and will be almost immediate.

如果您使用的是Python,则如果要升级一个级别(但实际上取决于用例),则可以使用paramiko甚至ansible来实现类似的行为.

If you are using Python, the similar behaviour you can achieve using paramiko or even ansible if you want to step one level up (but really depends on use case).

这篇关于在没有ssh延迟的情况下在远程计算机上运行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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