pdsh(Parallel Distributed Shell)是一个用于并行执行命令的工具,可以在多个远程主机上同时运行相同的命令。它对于需要在多台服务器上执行批量操作的系统管理员和开发人员非常有用。
我们在使用pdsh前做些基础的操作,设置每台主机的hostname,绑定hosts解析,设置root密码,密钥登录以及节点免密操作
1.设置hostname为指定或规范的名字
每台根据设计的名字进行设置,我这里准备了三台主机,分别是master,node1,node2
hostnamectl set-hostname master
hostnamectl set-hostname node1
hostnamectl set-hostname node2
2.绑定hosts和内网IP
每台机器都用hosts绑定内网IP,这样就能用host名字登录和连接登操作
cat >>/etc/hosts<<EOF
10.0.3.146 master
10.0.2.185 node1
10.0.3.231 node2
EOF
3.每台机器设置root密码
避免交互,使用chpasswd
echo "root:admin123" | chpasswd
4.修改sshd_config配置
每台机器修改sshd_config配置,现备份一下,然后按照下面修改,注意如果无法登录,最后一行的sed修改密码认证关闭最后做。cat /etc/ssh/sshd_config | grep -iE -v '^(#|$)' > /etc/ssh/sshd_config_bak
echo "PermitRootLogin yes" >>/etc/ssh/sshd_config
echo "PubkeyAuthentication yes">>/etc/ssh/sshd_config
echo "AuthorizedKeysFile .ssh/authorized_keys">>/etc/ssh/sshd_config
sed -i "/PasswordAuthentication/s/yes/no/g" /etc/ssh/sshd_config
修改完以后重启下sshd服务使其生效
systemctl restart sshd
5.生成master节点公钥
在master生成密钥ssh-keygen -t rsa
chmod 700 ~/.ssh
touch ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
6.给所有节点安装pdsh
先在master安装下pdsh
apt install pdsh -y
接着我们给其他节点安装pdsh
pdsh -w ssh:node[1-2] 'apt install pdsh -y'
上述多个节点名如果不连续,我们也可以使用逗号隔开,写法如下:
pdsh -w ssh:node1,ssh:node2 'apt install pdsh -y'
#也可以这样写
pdsh -w ssh:node[1,2] 'apt install pdsh -y'
7.在所有节点批量编译nginx
接下来我们做点复制的操作,我们拷贝nginx源码包,然后在每台节点去编译nginx
这其中会用到pdcp命令,我们来看看具体命令的撰写。
export NGINX_VERSION='1.28.0'
wget https://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz
pdcp -w ssh:node[1-2] nginx-${NGINX_VERSION}.tar.gz /usr/local/src/
我们再撰写一个编译脚本自动去编译nginx,脚本内容如下:
cat >./install_nginx.sh<<EOF
#!/bin/bash
set -e
apt install libpcre2-dev zlib1g zlib1g-dev openssl libssl-dev build-essential -y
cd /usr/local/src/
tar -zxvf nginx-${NGINX_VERSION}.tar.gz
cd nginx-${NGINX_VERSION}
mkdir -p /usr/local/nginx
groupadd www
useradd -g www www -s /sbin/nologin
./configure --prefix=/usr/local/nginx --without-http_memcached_module --user=www --group=www --with-http_stub_status_module --without-http-cache --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-pcre
make && make install
EOF
然后复制安装脚本到远程目录
pdcp -w ssh:node[1-2] install_nginx.sh /usr/local/src/
最后执行批量安装命令
pdsh -w ssh:node[1-2] 'apt update -y'
pdsh -w ssh:node[1-2] 'bash /usr/local/src/install_nginx.sh'
可以看到编译完成,然后登录每台主机上查看/usr/local/nginx目录,发现均编译成功并安装。
内容版权声明:除非注明,否则皆为本站原创文章。
转载注明出处:https://sulao.cn/post/1089
评论列表