Running a command on remote machine without ssh de

2019-08-13 01:47发布

问题:

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.

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.

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

回答1:

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

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

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.

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).