day40

作者: 1220 | 来源:发表于2019-04-25 23:55 被阅读0次
image.png

项目说明见:
老男孩教育_Linux58期-集群项目实战-03-三个项目实战大考察.pdf

管理配置好:
1、创建分发秘钥
2、安装ansible工具。
3、一键执行各服务脚本

具体服务一键实现的几个步骤:
1、计划要做。
2、单机安装好,步骤抽出来。
3、写成脚本,一键安装。
4、拿到管理机安装

1)一键完成rsync服务端安装。

剧本:
1)安装

yum install rsync -y

2)配置配置文件/etc/rsyncd.conf

cp /etc/rsyncd.conf{,.ori}
cat>/etc/rsyncd.conf<<EOF
uid = rsync
gid = rsync
use chroot = no
fake super = yes
max connections = 200
timeout = 600
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
ignore errors
read only = false
list = false
hosts allow = 172.16.1.0/24
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
[backup]
comment = welcome to oldboyedu backup!
path = /backup/
EOF

3)创建用户和备份目录

useradd rsync
id rsync
mkdir -p /backup
chown -R rsync.rsync /backup/
ls -ld /backup/

4)启动和检查

systemctl start rsyncd systemctl enable rsyncd systemctl status rsyncd
systemctl enable rsyncd
systemctl status rsyncd
ps -ef|grep sync|grep -v grep #检查进程
netstat -lntup|grep 873 #检查端口

5)配置密码文件

echo "rsync_backup:oldboy" > /etc/rsync.password
chmod 600 /etc/rsync.password
cat /etc/rsync.password
ls -l /etc/rsync.password

rsync服务端配置完成。
最终脚本路径/server/scripts/install_rsync_server.sh,需提前测试成功。

2)一键完成rsync客户端安装。

方法1:认证密码文件

echo "oldboy" > /etc/rsync.password
chmod 600 /etc/rsync.password
cat /etc/rsync.password
ls -l /etc/rsync.password
rsync -avz /etc/hosts rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password
最终脚本路径/server/scripts/install_rsync_client.sh,需提前测试成功。

编辑nfs配置文件

3)配置管理机61-m01:

1)实现批量分发秘钥,免秘钥管理

#!/bin/bash
yum install ansible -y                #含sshpass
[ ~/.ssh/id_rsa ]&& rm -fr ~/.ssh
ssh-keygen -f ~/.ssh/id_rsa  -P '' -q
for ip in 31 41 7 8
do
  sshpass -p666666 ssh-copy-id -f -i ~/.ssh/id_rsa.pub "-o StrictHostKeyChecking=no" 172.16.1.$ip
  ssh 172.16.1.$ip "ifconfig eth0"
done
#脚本路径/server/scripts/create_key.sh

4)实现文件分发和命令管理

方法1:脚本开发分发工具

[root@m01 /server/scripts]# cat fenfa.sh 
#!/bin/sh
. /etc/init.d/functions
if [ $# -ne 2 ]
then
    echo "usage:/bin/sh $0 localfile remotedir"
    exit 1
fi

for n in  `cat /etc/ssh/hosts`
do
   scp -P 22 -rp $1 root@$n:$2 &>/dev/null
   if [ $? -eq 0  ]
   then
        action "$n successful" /bin/true
   else
            action "$n failure" /bin/false
   fi
done

[root@m01 /server/scripts]# cat fenfa.sh 
#!/bin/sh

for n in  7 31 41
do
   scp -P 22 -rp $1 root@$n:$2 &>/dev/null
done

[root@m01 /server/scripts]# cat cmd.sh 
for n in 31 41 7
do
   echo "=====172.16.1.$n======"
   ssh 172.16.1.$n "$1"
done

方法2:使用ansible工具

#yum install ansible -y
[root@m01 /server/scripts]# cat /etc/ansible/hosts
[oldboy]
172.16.1.31
172.16.1.41
172.16.1.7

2)优化所有机器SSH

优化目标sshd_config
[root@m01 /server/scripts]# sed -n '17,22p' /etc/ssh/sshd_config 
####Start by oldboy#2020-04-26###
PermitEmptyPasswords no
UseDNS no
GSSAPIAuthentication no
#ListenAddress 172.16.1.7:22
####End by oldboy#2018-04-26###

方法1:脚本分发

[root@m01 /server/scripts]# sh fenfa.sh /etc/ssh/sshd_config /etc/ssh/
7 successful                                               [  确定  ]
31 successful                                              [  确定  ]
41 successful                                              [  确定  ]
[root@m01 /server/scripts]# 
[root@m01 /server/scripts]# 
[root@m01 /server/scripts]# 
[root@m01 /server/scripts]# sh cmd.sh "systemctl restart sshd"
=====172.16.1.31======
=====172.16.1.41======
=====172.16.1.7======

方法2:使用ansible分发

ansible oldboy -m copy -a "src=/etc/ssh/sshd_config dest=/etc/ssh/sshd_config backup=yes"
ansible oldboy -m shell -a "systemctl restart sshd"

从管理机实现一键安装 install_rsync_server.sh

ansible 172.16.1.41 -m script -a "/server/scripts/install_rsync_server.sh"
[root@m01 /server/scripts]# cat /etc/ansible/hosts
[oldboy]
172.16.1.31
172.16.1.41
172.16.1.7
172.16.1.8
[rsync_client]
172.16.1.31
172.16.1.8
ansible rsync_client -m script -a "/server/scripts/install_rsync_client.sh"
实现从管理机一键完成安装rsync服务端和客户端

项目实践作业:
rsync服务端写成脚本 r1.sh
rsync客户端写成脚本 r2.sh

nfs服务端写成脚本 n1.sh
nfs客户端写成脚本 n2.sh

sersync服务端写成脚本 s1.sh
sersync客户端写成脚本 s2.sh

/server/scripts/one_key_gaoding.sh
ansible r1 -m copy -a "src=/server/scripts/r1.sh dest=/server/scripts/ mode=ugo+x"
ansible r1 -m shell -a "sh /server/scripts/r1.sh"

ansible r1 -m copy -a "src=/server/scripts/r2.sh dest=/server/scripts/ mode=ugo+x"
ansible r2 -m shell -a "sh /server/scripts/r2.sh"

ansible n1 -m shell -a "sh /server/scripts/n1.sh"
ansible n2 -m shell -a "sh /server/scripts/n2.sh"
ansible s1 -m shell -a "sh /server/scripts/s1.sh"
ansible s2 -m shell -a "sh /server/scripts/s2.sh"

/bin/sh /server/scripts/one_key_gaoding.sh

也可以使用script模块,替代copy+shell模块

答案看《三大项目》

相关文章

网友评论

      本文标题:day40

      本文链接:https://www.haomeiwen.com/subject/solagqtx.html