Hello everyone, I am Glacier ~~
Before the ice Age, you maintained a cluster of thousands of servers, and it would have been too cumbersome to manually log in to each server every time you needed to run a command on it. If you’re in a cluster of thousands of servers and you simply need to execute the same command on each server, then you’re going to have to manually log in to thousands of servers in turn. There are probably thousands of servers that you can log in to in three days, so what do you do? Is there any good way to solve this problem?
Don’t worry. That’s what we’re here to solve today.
To be honest, when I was maintaining a cluster of thousands of servers, I didn’t manually log in to each server in turn. Why? Yeah, because I’m lazy! I am lazy to log in, and in turn logged in so many servers, the whole person will crash.
So I figured out if I could write a script that would receive the commands I wanted to execute and then distribute them to all the servers in the cluster, wouldn’t that solve the problem? No sooner said than done.
However, here, there is a point to note: you need to configure the mapping between the host name and IP address of each server in the cluster, communicate with each other using the host name, and configure SSH password-free login. There is no need to worry about this, as long as the operation and maintenance plan and allocate the server, the plan is good, there is no need to log in to the server later.
For the convenience of small friends to understand, here we assume that there are 1024 servers in the cluster, each server host name binghe1~ Binghe1024. Each server can communicate by host name, so I wrote a script named distribute_command-sh, which looks like this.
#! /bin/bash
pcount=$#
if (( pcount<1 )) ; then
echo no args;
exit;
fi
Execute the command on the local machine first
echo ------------binghe$host-----------------
$@
# Loop to execute commands on remote nodes in the cluster
for (( host=1 ; host<=1024; host=host+1)) ; do
echo ------------binghe$host-----------------
ssh binghe$host $@
done;
Copy the code
This script is used to receive incoming commands and distribute them to servers with host names binghe1~binghe1024 for execution. This script is used to execute the same commands on all servers in the cluster at the same time.
Next, grant executable permission to the distribute_command-sh script, as shown below.
chmod a+x ./distribute_command.sh
Copy the code
The format is as follows:
./ distribute_command-. sh The complete command executed on the serverCopy the code
Use the sample
- Create a hello. TXT file containing hello World in the /home directory of each server in the cluster
./distribute_command.sh echo "hello world" >> /home/hello.txt
Copy the code
- View the contents of the hello. TXT file on each server in the cluster
./distribute_command.sh cat /home/hello.txt
Copy the code
- Delete the hello. TXT file from each server in the cluster
./distribute_command.sh rm -rf /home/hello.txt
Copy the code
Isn’t that easy? So, sometimes, don’t do it blindly. A lot of times, before you do something, you have to think about whether there is a better solution, whether there is a more efficient solution. As mentioned in this article, to execute a command on thousands of servers, it would take three days to manually log in to each server in turn and execute the command. If we had written a script, it would have been done in less than a minute. Therefore, efficiency and quality are the goals of doing things.
Well, that’s all for today, I’m Glacier, and I’ll see you next time