linux ssh免密部署

根据如下要求,完成部署过程

1.恢复7、8、9、31、41所有机器的快照

7 8 9 web服务 nginx
172.16.1.xx
nfs-31 提供共享文件存储
rsync-41 提供数据备份的机器

2.在61机器,远程一键脚本化,部署这5台机器

master-61机器远程的,操作目标机器,让它安装好对应的服务
web-7
1.安装软件
2.修改配置文件
3.启动服务
4.挂载nfs
根据服务相关性,需要有先后的部署关系
rsync-41
1.安装软件
2.修改配置文件
3.创建rsync对应的数据目录,配置文件,授权
4.启动服务
nfs-31 + lrsync实时同步
1.安装软件
2.修改配置文件
3.创建nfs相关的数据目录,授权
3.启动服务
4.安装lsync
5.修改lsync配置文件
6.启动服务

3.检查整体应用可用性

1.从nginx作为入口,nginx默认的网页根目录
/usr/share/nginx/html 写入数据
2.同步到nfs机器上
3.同步到rsync机器上

Master-61建议登录的别名

alias sshweb7='ssh root@172.16.1.7 -p 22999'
alias sshweb8='ssh root@172.16.1.8 -p 22999'
alias sshweb9='ssh root@172.16.1.9 -p 22999'
alias sshnfs31='ssh root@172.16.1.31 -p 22999'
alias sshrsync41='ssh root@172.16.1.41 -p 22999'
[root@master-61 ~]#source /etc/profile
[root@master-61 ~]#alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias sshnfs31='ssh root@172.16.1.31 -p 22999'
alias sshrsync41='ssh root@172.16.1.41 -p 22999'
alias sshweb7='ssh root@172.16.1.7 -p 22999'
alias sshweb8='ssh root@172.16.1.8 -p 22999'
alias sshweb9='ssh root@172.16.1.9 -p 22999'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

windows部分

让windows可以免密登录master-61机器
1. windows创建公私钥,在windows中下载一个支持使用linux命令的工具
git-bash工具
生成公私钥对儿
63556@DESKTOP-KE05A15 MINGW64 ~/Desktop
$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/63556/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/63556/.ssh/id_rsa
Your public key has been saved in /c/Users/63556/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:/A31nX8Gu0USifqnyQcpoXxaHaP0hY5MyGlAOLLF+e4 63556@DESKTOP-KE05A15
The key's randomart image is:
+---[RSA 3072]----+
| . +. |
| . * . . . |
| + o o o o.o |
| . ..= +o+..o.|
| . oS=oB =+.o|
| . o.B+* =.|
| . +..o.o =|
| E . . +.+.|
| +.. |
+----[SHA256]-----+
63556@DESKTOP-KE05A15 MINGW64 ~/.ssh
$ ls
id_rsa id_rsa.pub known_hosts(存放目标机器的指纹公钥,意义在于?当你下次连接该目标机器的时候,就无序再确认机器的指纹了)
发送windows的公钥,给需要免密登录的机器上,目标机器 git-bash执行
ssh-copy-id (这个命令,等于把本地的公钥,写入到目标机器的~/.ssh/)
63556@DESKTOP-KE05A15 MINGW64 ~/.ssh
$ ssh-copy-id root@10.0.0.61
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/c/Users/63556/.ssh/id_rsa.pub"
The authenticity of host '10.0.0.61 (10.0.0.61)' can't be established.
ECDSA key fingerprint is SHA256:ZHEB5JjEbyRm1YIMwrA8pLk4jkcZC9GUIk899JDanpQ.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@10.0.0.61's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@10.0.0.61'"
and check to make sure that only the key(s) you wanted were added.
分别检查,客户端,服务端的,密钥文件信息
windows客户端的,目标机器的公钥
63556@DESKTOP-KE05A15 MINGW64 ~/.ssh
$ cat known_hosts
10.0.0.61 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBKupWZ7FEFns/ajVcMsMfzd4nGQ+EFpdPiWXPCAdxzgzV3ZxPXg4cuUNIQ6c3A3LLWtQHU1mBdsT2rzgjNbqrJg=
linux服务端,可以看到windows机器的,公钥信息
[root@master-61 ~]#cat ~/.ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDcfnzhqCXfO4IV9cslAQJ2stq3k+crMhjr7dkRo+jUNWGC4GyP6El3XirKX4A2mr6wPWz683I9ERTyIXONiQRaVVJ2TtstAKC5S1oZoHa9Nj863arOU5HztXCLhzsByvlNbJX/nhBJqmylzSN3EgEBUzJcCkLlOT2jq7K3FoFJIZc6hJU9tVy+zJzjWjePKqfSyIwf6COw6uL04EmrDHYC+u7T9BNhLDuLHsuHnEJOZtXE4TL4JSpxxvNJsmKmcvxV0T9VcWU++uNEo+dRYRmxtARfK8XXcQCXz6P9IaR7IIRGP8pWyAoD+OiXzI50C5Pp9h+zg78IYIDi4BfOFXPEhpzbeqKLCG0Ww80de9vZnRJPpvQKmBUFaHY9ucAKAdvarePS7Mhq0MYmw/3rWfeWMWGPRg18ImgYgmlHadDSCpDmnIunLmdB+PEyKZ0qqDliTQZIvdQ/G32sTFdE8MK/AOEAXOazMEcWEaenAzMhg/h4AayNqTIwVXU7qTzWC8k= 63556@DESKTOP-KE05A15
这个信息就和windows的 id_rsa.pub一样
63556@DESKTOP-KE05A15 MINGW64 ~/.ssh
$ cat id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDcfnzhqCXfO4IV9cslAQJ2stq3k+crMhjr7dkRo+jUNWGC4GyP6El3XirKX4A2mr6wPWz683I9ERTyIXONiQRaVVJ2TtstAKC5S1oZoHa9Nj863arOU5HztXCLhzsByvlNbJX/nhBJqmylzSN3EgEBUzJcCkLlOT2jq7K3FoFJIZc6hJU9tVy+zJzjWjePKqfSyIwf6COw6uL04EmrDHYC+u7T9BNhLDuLHsuHnEJOZtXE4TL4JSpxxvNJsmKmcvxV0T9VcWU++uNEo+dRYRmxtARfK8XXcQCXz6P9IaR7IIRGP8pWyAoD+OiXzI50C5Pp9h+zg78IYIDi4BfOFXPEhpzbeqKLCG0Ww80de9vZnRJPpvQKmBUFaHY9ucAKAdvarePS7Mhq0MYmw/3rWfeWMWGPRg18ImgYgmlHadDSCpDmnIunLmdB+PEyKZ0qqDliTQZIvdQ/G32sTFdE8MK/AOEAXOazMEcWEaenAzMhg/h4AayNqTIwVXU7qTzWC8k= 63556@DESKTOP-KE05A15

master-61管理机

1.修改ssh端口为22999
2.关闭用户名密码登录
3.开启通过公私钥登录

被管理机

1.修改ssh端口为22999
2.关闭用户名密码登录
3.开启通过公私钥登录
4.指定监听内网地址,172.16.1.xx

要求部署效果

1.master-61机器只能通过公私钥登录,禁止用户密码连接
2.所有主机的ssh端口全都是22999
3.被管理的机器只能通过内网、且使用公私钥连接。

阶段2:脚本部署ssh

阶段1的ssh环境部署,是手动操作;

现在需要实现脚本一键部署;

1.管理机自动创建公私钥
2.管理机自动分发公钥到备管理机
3.远程修改被管理机的ssh连接端口为22999,监听地址是172.16.1.xx
4.远程修改被管理机不允许密码登录,只能是密钥登录
5.修改完毕后,验证是否生效,远程查看所有被管理主机的主机名

批量修改配置文件

这个sshpass命令只存在master-61机器上即可
实现了公钥面交互分发的命令如下
在master-61机器上执行
ssh-copy-id命令,分发公钥,但是默认需要输入远程机器的密码
使用 sshpass即可面交互输入密码
以及面指纹确认的参数 -o StrictHostKeyChecking=no
sshpass -p '123123' ssh-copy-id 172.16.1.${ip} -o StrictHostKeyChecking=no > /tmp/create_ssh.log 2>&1

脚本创建

安装sshpass命令
yum install sshpass -y
[root@master-61 ~/install_ssh]#cat install.sh
#1.管理机自动创建公私钥
echo "正在创建公私钥..."
if [ -f /root/.ssh/id_rsa ]
then
echo "密钥对已经存在,请检查! "
else
ssh-keygen -f /root/.ssh/id_rsa -N '' > /tmp/create_ssh.log 2>&1
fi
echo '================================================='
#2.管理机自动分发公钥到被管理机
echo "正在分发公钥中...分发的机器列表是{7,8,9,31,41}"
for ip in {7,8,9,31,41}
do
sshpass -p '123456' ssh-copy-id 172.16.1.${ip} -o StrictHostKeyChecking=no > /tmp/create_ssh.log 2>&1
echo "正在验证免密登录结果中...."
echo "远程获取到主机名: $(ssh 172.16.1.${ip} hostname)"
done
echo '===================================================='
#3.远程修改被管理机的ssh连接端口为22999,监听地址是172.16.1.xx
for ip in {7,8,9,31,41}
do
echo "修改172.16.1.${ip}的ssh端口中..."
ssh root@172.16.1.${ip} "sed -i '/Port 22/c Port 22999' /etc/ssh/sshd_config"
done
echo '================================================='
#4.远程修改被管理机不允许密码登录,只能是密钥登录
for ip in {7,8,9,31,41}
do
echo "禁止密码登录参数修改中...当前操作的机器是172.16.1.${ip}"
ssh root@172.16.1.${ip} "sed -i '/^PasswordAuthentication/c PasswordAuthentication no' /etc/ssh/sshd_config"
echo "允许公钥登录参数修改中...当前操作的机器是172.16.1.${ip}"
ssh root@172.16.1.${ip} "sed -i '/PubkeyAuthentication/c PubkeyAuthentication yes' /etc/ssh/sshd_config"
done
echo '==================================================='
#5.修改监听内网地址
for ip in {7,8,9,31,41}
do
echo "修改监听地址中...当前操作的机器是172.16.1.${ip}"
ssh root@172.16.1.${ip} "sed -i '/ListenAddress 0.0.0.0/c ListenAddress 172.16.1.${ip}' /etc/ssh/sshd_config"
done
echo '====================================================='
#6.批量验证ssh修改情况
for ip in {7,8,9,31,41}
do
echo "当前查看的机器是172.16.1.${ip}"
ssh root@172.16.1.${ip} "grep -Ei '^(port|passwordauthentication|pubkeyauthentication|listenaddress)' /etc/ssh/sshd_config"
done
echo '=================================================='

脚本执行

[root@master-61 ~/install_ssh]#bash install.sh
正在创建公私钥...
=================================================
正在分发公钥中...分发的机器列表是{7,8,9,31,41}
正在验证免密登录结果中....
远程获取到主机名: web-7
正在验证免密登录结果中....
远程获取到主机名: web-8
正在验证免密登录结果中....
远程获取到主机名: web-9
正在验证免密登录结果中....
远程获取到主机名: nfs-31
正在验证免密登录结果中....
远程获取到主机名: rsync-41
====================================================
修改172.16.1.7的ssh端口中...
修改172.16.1.8的ssh端口中...
修改172.16.1.9的ssh端口中...
修改172.16.1.31的ssh端口中...
修改172.16.1.41的ssh端口中...
=================================================
禁止密码登录参数修改中...当前操作的机器是172.16.1.7
允许公钥登录参数修改中...当前操作的机器是172.16.1.7
禁止密码登录参数修改中...当前操作的机器是172.16.1.8
允许公钥登录参数修改中...当前操作的机器是172.16.1.8
禁止密码登录参数修改中...当前操作的机器是172.16.1.9
允许公钥登录参数修改中...当前操作的机器是172.16.1.9
禁止密码登录参数修改中...当前操作的机器是172.16.1.31
允许公钥登录参数修改中...当前操作的机器是172.16.1.31
禁止密码登录参数修改中...当前操作的机器是172.16.1.41
允许公钥登录参数修改中...当前操作的机器是172.16.1.41
===================================================
修改监听地址中...当前操作的机器是172.16.1.7
修改监听地址中...当前操作的机器是172.16.1.8
修改监听地址中...当前操作的机器是172.16.1.9
修改监听地址中...当前操作的机器是172.16.1.31
修改监听地址中...当前操作的机器是172.16.1.41
=====================================================
当前查看的机器是172.16.1.7
Port 22999
ListenAddress 172.16.1.7
PubkeyAuthentication yes
PasswordAuthentication no
当前查看的机器是172.16.1.8
Port 22999
ListenAddress 172.16.1.8
PubkeyAuthentication yes
PasswordAuthentication no
当前查看的机器是172.16.1.9
Port 22999
ListenAddress 172.16.1.9
PubkeyAuthentication yes
PasswordAuthentication no
当前查看的机器是172.16.1.31
Port 22999
ListenAddress 172.16.1.31
PubkeyAuthentication yes
PasswordAuthentication no
当前查看的机器是172.16.1.41
Port 22999
ListenAddress 172.16.1.41
PubkeyAuthentication yes
PasswordAuthentication no
==================================================

还缺少远程的批量重启sshd服务,让sshd_config配置生效

1.批量重启sshd服务

重启服务,单独拆分为了一个脚本,作用就是重启服务

[root@master-61 ~/install_ssh]#cat install_restart.sh
for ip in {7,8,9,31,41}
do
echo "重启sshd服务中,当前操作的机器是172.16.1.${ip}"
ssh root@172.16.1.${ip} "systemctl restart sshd"
echo '==================================='
done
执行脚本
[root@master-61 ~/install_ssh]#bash install_restart.sh
重启sshd服务中,当前操作的机器是172.16.1.7
===================================
重启sshd服务中,当前操作的机器是172.16.1.8
===================================
重启sshd服务中,当前操作的机器是172.16.1.9
===================================
重启sshd服务中,当前操作的机器是172.16.1.31
===================================
重启sshd服务中,当前操作的机器是172.16.1.41
===================================

重启完毕了服务,验证下修改的结果是否正确,远程查看配置文件信息

2.远程查看主机信息

这个脚本,作用就是远程查看主机的配置文件信息

[root@master-61 ~/install_ssh]#cat show_config.sh
for ip in {7,8,9,31,41}
do
echo "远程获取主机名,当前操作的机器是172.16.1.${ip}"
ssh -p 22999 root@172.16.1.${ip} "hostname"
echo "远程获取主机sshd配置信息,当前操作的机器是172.16.1.${ip}"
ssh -p 22999 root@172.16.1.${ip} "grep -Ei '^(port|passwordauthentication|pubkeyauthentication|listenaddress)' /etc/ssh/sshd_config"
echo "远程查看sshd端口情况,当前操作的机器是172.16.1.${ip}"
ssh -p 22999 root@172.16.1.${ip} "netstat -tunlp |grep sshd |grep -v grep"
echo '========================================='
done
执行脚本
[root@master-61 ~/install_ssh]#bash show_config.sh
远程获取主机名,当前操作的机器是172.16.1.7
web-7
远程获取主机sshd配置信息,当前操作的机器是172.16.1.7
Port 22999
ListenAddress 172.16.1.7
PubkeyAuthentication yes
PasswordAuthentication no
远程查看sshd端口情况,当前操作的机器是172.16.1.7
tcp 0 0 172.16.1.7:22999 0.0.0.0:* LISTEN 11740/sshd
=========================================
远程获取主机名,当前操作的机器是172.16.1.8
web-8
远程获取主机sshd配置信息,当前操作的机器是172.16.1.8
Port 22999
ListenAddress 172.16.1.8
PubkeyAuthentication yes
PasswordAuthentication no
远程查看sshd端口情况,当前操作的机器是172.16.1.8
tcp 0 0 172.16.1.8:22999 0.0.0.0:* LISTEN 1945/sshd
=========================================
远程获取主机名,当前操作的机器是172.16.1.9
web-9
远程获取主机sshd配置信息,当前操作的机器是172.16.1.9
Port 22999
ListenAddress 172.16.1.9
PubkeyAuthentication yes
PasswordAuthentication no
远程查看sshd端口情况,当前操作的机器是172.16.1.9
tcp 0 0 172.16.1.9:22999 0.0.0.0:* LISTEN 11592/sshd
=========================================
远程获取主机名,当前操作的机器是172.16.1.31
nfs-31
远程获取主机sshd配置信息,当前操作的机器是172.16.1.31
Port 22999
ListenAddress 172.16.1.31
PubkeyAuthentication yes
PasswordAuthentication no
远程查看sshd端口情况,当前操作的机器是172.16.1.31
tcp 0 0 172.16.1.31:22999 0.0.0.0:* LISTEN 11737/sshd
=========================================
远程获取主机名,当前操作的机器是172.16.1.41
rsync-41
远程获取主机sshd配置信息,当前操作的机器是172.16.1.41
Port 22999
ListenAddress 172.16.1.41
PubkeyAuthentication yes
PasswordAuthentication no
远程查看sshd端口情况,当前操作的机器是172.16.1.41
tcp 0 0 172.16.1.41:22999 0.0.0.0:* LISTEN 11763/sshd
=========================================

此时还剩下master-61机器未修改了

web-7
web-8
web-9
nfs-31
rsync-41
全部完成了 sshd的配置文件修改了
端口
监听地址
禁止密码登录
允许公私钥登录
修改master-61机
禁止密码登录,允许公钥登录
[root@master-61 ~/install_ssh]#grep -Ei '^(port|passwordauthentication|pubkeyauthentication)' /etc/ssh/sshd_config
Port 22999
PubkeyAuthentication yes
PasswordAuthentication no
重启sshd服务
[root@master-61 ~/install_ssh]#systemctl restart sshd

此时,master-61,以及所有的目标机器以及全部配置好了ssh环境,可以进行服务安装 了

阶段3:远程一键安装综合备份架构

上述的阶段2,一键搭建好了sshd的安全连接环境

只要编写一键安装服务的脚本即可

注意服务的启动顺序

rsync服务(rsync-41机器)

[root@master-61 ~/install_ssh]#cat rsync.sh
# 1.安装
yum install rsync -y
#2.修改配置文件
cat > /etc/rsyncd.conf << 'EOF'
uid = www
gid = www
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
igonre errors
read only = false
list = false
auth users = rsync_lwj
secrets file = /etc/rsync.lwjpass
log file = /var/log/rsyncd.log
################################################
[lwj]
comment = yuchao.cn about rsync
path = /lwj
EOF
#3.创建用户
groupadd www -g 666
useradd www -g 666 -u 666 -M -s /sbin/nologin
#4.创建目录,授权
mkdir -p /lwj
chown -R www.www /lwj
#5.创建密码文件,授权
echo 'rsync_lwj:123456' > /etc/rsync.lwjpass
chmod 600 /etc/rsync.lwjpass
#6.启动服务
systemctl start rsyncd
systemctl enable rsyncd
#7.检查服务
netstat -tunlp |grep rsync

远程拷贝,远程安装

传送脚本到rsync-41机器
[root@master-61 ~/install_ssh]#scp -P 22999 rsync.sh root@172.16.1.41:/opt/
rsync.sh 100% 780 1.1MB/s 00:00
远程执行传过去的脚本
[root@master-61 ~/install_ssh]#ssh -p 22999 root@172.16.1.41 "bash /opt/rsync.sh"
远程检查rsync部署操作
[root@master-61 ~/install_ssh]#sshrsync41 "cat /etc/rsync.lwjpass;ls -ld /lwj;id www"
rsync_lwj:123456
drwxr-xr-x 2 www www 6 May 5 16:20 /lwj
uid=666(www) gid=666(www) groups=666(www)

nfs服务(nfs-31机器)

[root@master-61 ~/install_ssh]#cat nfs.sh
# 1.安装服务
yum install nfs-utils rpcbind -y
# 2.创建nfs限定的用户,组
groupadd www -g 666
useradd www -g 666 -u 666 -M -s /sbin/nologin
# 3.创建共享目录,修改权限
mkdir /nfs-lwj-nginx
chown -R www.www /nfs-lwj-nginx
# 4.创建配置文件
cat > /etc/exports << EOF
/nfs-lwj-nginx 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
EOF
# 5.启动服务
systemctl start nfs
# 6.检查服务
showmount -e 127.0.0.1

远程安装

1.远程发送脚本文件
[root@master-61 ~/install_ssh]#scp -P 22999 nfs.sh root@172.16.1.31:/opt/
nfs.sh 100% 450 522.5KB/s 00:00
2.远程执行
[root@master-61 ~/install_ssh]#ssh -p 22999 root@172.16.1.31 "bash /opt/nfs.sh"

nfs+lsyncd服务

[root@master-61 ~/install_ssh]#cat lsyncd.sh
# 1.安装服务
yum install lsyncd -y
# 2.生成配置文件
cat >/etc/lsyncd.conf <<EOF
settings {
logfile ="/var/log/lsyncd/lsyncd.log",
statusFile ="/var/log/lsyncd/lsyncd.status",
inotifyMode = "CloseWrite",
maxProcesses = 8,
}
sync {
default.rsync,
source = "/nfs-lwj-nginx",
target = "rsync_lwj@172.16.1.41::lwj",
delete= true,
exclude = {".*"},
delay=1,
rsync = {
binary = "/usr/bin/rsync",
archive = true,
compress = true,
verbose = true,
password_file="/etc/rsync.lwjpass",
_extra={"--bwlimit=200"}
}
}
EOF
# 3.创建密码文件
echo "123456" > /etc/rsync.lwjpass
chmod 600 /etc/rsync.lwjpass
# 4.启动
systemctl start lsyncd
# 5.检查服务
ps -ef |grep lsyncd |grep -v grep

远程安装lsyncd

1.远程发送脚本文件
[root@master-61 ~/install_ssh]#scp -P 22999 lsyncd.sh root@172.16.1.31:/opt/
lsyncd.sh 100% 828 946.3KB/s 00:00
2.远程执行脚本
[root@master-61 ~/install_ssh]#ssh -p 22999 root@172.16.1.31 "bash /opt/lsyncd.sh"

测试rsync+nfs

远程在31共享目录下创建一个文件
[root@master-61 ~/install_ssh]#ssh -p 22999 root@172.16.1.31 "touch /nfs-lwj-nginx/杰杰666.png"
远程查看41机器是否备份
[root@master-61 ~/install_ssh]#ssh -p 22999 root@172.16.1.41 "ls /lwj"
杰杰666.png

web-7 8 9机器

[root@master-61 ~/install_ssh]#cat nginx.sh
# 1.安装服务
yum install nginx -y
# 2.创建配置文件
cat >/etc/nginx/nginx.conf <<EOF
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
server {
listen 81;
server_name localhost;
location / {
root html;
index index.html;
}
}
}
EOF
# 3.启动服务
systemctl start nginx
# 4.检查服务
netstat -tunlp |grep nginx
#5.挂载目录
yum install nfs-utils -y
mount -t nfs 172.16.1.31:/nfs-lwj-nginx /usr/share/nginx/html

远程部署三台机器 web-7 8 9

通过脚本把上一个脚本一起发送到三台机器
[root@master-61 ~/install_ssh]#cat server.sh
for server in {7,8,9}
do
scp -P 22999 nginx.sh root@172.16.1.${server}:/opt/
ssh -p 22999 root@172.16.1.${server} "bash /opt/nginx.sh"
done

检查三台机器的挂载情况

[root@master-61 ~/install_ssh]#for web_ip in {7,8,9};do ssh -p 22999 root@172.16.1.${web_ip} "df -h";done
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 17G 1.6G 16G 10% /
devtmpfs 899M 0 899M 0% /dev
tmpfs 911M 0 911M 0% /dev/shm
tmpfs 911M 9.6M 902M 2% /run
tmpfs 911M 0 911M 0% /sys/fs/cgroup
/dev/sda1 1014M 142M 873M 14% /boot
tmpfs 183M 0 183M 0% /run/user/0
172.16.1.31:/nfs-lwj-nginx 17G 1.6G 16G 9% /usr/share/nginx/html
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 17G 1.6G 16G 10% /
devtmpfs 899M 0 899M 0% /dev
tmpfs 911M 0 911M 0% /dev/shm
tmpfs 911M 9.6M 902M 2% /run
tmpfs 911M 0 911M 0% /sys/fs/cgroup
/dev/sda1 1014M 142M 873M 14% /boot
tmpfs 183M 0 183M 0% /run/user/0
172.16.1.31:/nfs-lwj-nginx 17G 1.6G 16G 9% /usr/share/nginx/html
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 17G 1.6G 16G 10% /
devtmpfs 899M 0 899M 0% /dev
tmpfs 911M 0 911M 0% /dev/shm
tmpfs 911M 9.6M 902M 2% /run
tmpfs 911M 0 911M 0% /sys/fs/cgroup
/dev/sda1 1014M 142M 873M 14% /boot
tmpfs 183M 0 183M 0% /run/user/0
172.16.1.31:/nfs-lwj-nginx 17G 1.6G 16G 9% /usr/share/nginx/html

最终测试

1.在共享存储中,创建网页数据,提供给所有web机器使用
[root@master-61 ~/install_ssh]#cat >index.html<<EOF
> <meta charset=utf8>
> 部署脚本的一天,加油年轻人!
> EOF
[root@master-61 ~/install_ssh]#cat index.html
<meta charset=utf8>
部署脚本的一天,加油年轻人!
远程操作发送到nfs-31机器的共享目录下
[root@master-61 ~/install_ssh]#scp -P 22999 index.html root@172.16.1.31:/nfs-lwj-nginx/
index.html 100% 63 91.8KB/s 00:00
2.检查数据备份情况
[root@master-61 ~/install_ssh]#sshrsync41 "ls -l /lwj"
total 4
-rw-r--r-- 1 www www 63 May 5 17:45 index.html
-rw-r--r-- 1 www www 0 May 5 17:16 杰杰666.png
[root@master-61 ~/install_ssh]#for nginx_ip in {7,8,9};do ssh -p 22999 172.16.1.${nginx_ip} "netstat -tunlp|grep nginx|grep -v grep";done
tcp 0 0 0.0.0.0:81 0.0.0.0:* LISTEN 12325/nginx: master
tcp 0 0 0.0.0.0:81 0.0.0.0:* LISTEN 2386/nginx: master
tcp 0 0 0.0.0.0:81 0.0.0.0:* LISTEN 12081/nginx: master
浏览器访问
10.0.0.7:81
10.0.0.8:81
10.0.0.9:81
下载静态图片到web-9机器
[root@master-61 ~/install_ssh]#sshweb9 wget -O /usr/share/nginx/html/haizei.png https://tx-free-imgs.acfun.cn/content/2020_10_13/1.6025727344597871E9.jpeg?imageslim
再次修改页面,查看数据
[root@master-61 ~/install_ssh]#cat index.html
<meta charset=utf8>
部署脚本的一天,加油年轻人!
<img src="haizei.png">
再远程发送到nfs-31机器的共享目录下
[root@master-61 ~/install_ssh]#scp -P 22999 index.html root@172.16.1.31:/nfs-lwj-nginx/
index.html 100% 86 88.8KB/s 00:00
浏览器查看web-7 8 9 三台机器

posted @   并不是那么牛  阅读(192)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
点击右上角即可分享
微信分享提示