Hello everyone, I am Glacier~~
Before Ice River maintained a server cluster composed of thousands of servers, it would be too troublesome if you had to manually log in to each server to perform operations every time you need to execute a command on the server. Think about it, if in a cluster of thousands of servers, each server only needs to simply execute the same command, let alone execute the command, it will let you manually log in to thousands of servers in turn, that is enough for you Accepted. It is estimated that you log in to thousands of servers one by one, and you may not be able to log in after three days, so what should I do? Is there any good way to solve this problem?
Don't worry, we are here to solve this problem 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? That's right, it's because I'm lazy! I am lazy to log in, and log in to so many servers in turn, the whole person will crash.
So, I figured out whether I could write a script, let this script receive the commands I want to execute, and then distribute the commands to all the servers in the cluster for execution. Wouldn't this solve the problem? Just do it.
However, here, there is one thing to note: that is: the correspondence between the host name and IP address of each server in the cluster needs to be configured in advance, the host names can be used to communicate with each other, and SSH password-free login is configured. This is not enough to worry about, as long as the operation and maintenance is planned and allocated when the server is planned, there is no need to log in to the server in sequence later.
In order to facilitate the understanding of the friends, here we assume that there are 1024 servers in the cluster, and the host name of each server is binghe1~binghe1024. Each server can communicate through the host name. Next, I wrote a script named distribute_command.sh, the content is as follows.
#!/bin/bash
pcount=$#
if (( pcount<1 )) ; then
echo no args;
exit;
fi
#先在本机上执行命令
echo ------------binghe$host-----------------
$@
#循环在集群中的远程节点上执行命令
for (( host=1 ; host<=1024; host=host+1)) ; do
echo ------------binghe$host-----------------
ssh binghe$host $@
done;
The meaning of this script is: receive the command passed in, distribute the command to the server with the host name binghe1~binghe1024 for execution, that is to say, using this script we can do: execute the same command on the cluster server at the same time .
Next, give executable permissions to the distribute_command.sh script as shown below.
chmod a+x ./distribute_command.sh
The format used is as follows:
./distribute_command.sh 在服务器上执行的完整命令
usage example
- Create a hello.txt file in the /home directory of each server in the cluster, with the content hello world
./distribute_command.sh echo "hello world" >> /home/hello.txt
- View the content of the hello.txt file on each server in the cluster
./distribute_command.sh cat /home/hello.txt
- Delete the hello.txt file on each server in the cluster
./distribute_command.sh rm -rf /home/hello.txt
very simple? So, sometimes, don't implement it blindly. Many times, before doing things, we must first think about whether there is a better solution, whether there is a more efficient and more efficient solution. For example, as mentioned in this article, executing a command on thousands of servers, if you manually log in to each server to execute the command in turn, it is estimated that it will take three days to get it; if we write a script, it is estimated to be 1 It's done in minutes. Therefore, efficiency and quality are the goals to be pursued in doing things.
Alright, come here today, I’m Glacier, see you in the next issue~~
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。