首页 文章 精选 留言 我的

精选列表

搜索[Agent集群],共10000篇文章
优秀的个人博客,低调大师

搭建一个高可用负载均衡的集群架构(第三部分)

1.使用脚本编写一个简单的文件分发系统即可,首先expect编写一个脚本文件rsync.expect,这个脚本是实现文件同步的脚本,内容如下: expect编写一个脚本文件rsync.expect,这个脚本是实现文件同步的脚本,内容如下: [root@localhost ~/syncList]# vim syncFile.expect #!/usr/bin/expect set host [lindex $argv 0] set file [lindex $argv 1] set passwd [lindex $argv 2] # 核心命令,同步多个文件 spawn rsync -avR --files-from=$file / root@$host:/ expect { "yes/no" { send "yes\r"} "password:" { send "$passwd\r" } } expect eof [root@localhost ~/syncList]# chmod a+x syncFile.expect 提示:如果你的rsync命令没有加-R选项的话,就需要确保目标机器也有这个文件列表中所定义的目录路径,不然就会报错。而且存放文件路径列表的文件内容不能有空格,不然会把这个空格看作为文件路径的一部分。 2.然后再编辑一个文本文件,这个文件用来放需要同步的文件列表,例如我这里需要同步以下几个文件: [root@localhost ~/syncList]$ cat /tmp/fileList.txt /usr/local/nginx/conf/vhost/dedecms.com.conf /usr/local/nginx/conf/vhost/discuz.com.conf /usr/local/nginx/conf/vhost/zrlog.com.conf /usr/local/sbin/nginx_log_rotate.sh /var/spool/cron/root [root@localhost ~/syncList]$ 3.还需要编辑一个ip.txt文件,用于存放需要同步的目标机器的IP地址,例如我需要将文件都同步这几个IP的机器上: [root@localhost ~/syncList]$ cat /root/webServerIP.txt 192.168.200.153 192.168.200.154 192.168.200.155 192.168.200.156 192.168.200.157 192.168.200.158 192.168.200.159 192.168.200.160 4.再编写一个shell脚本syncFile.sh,这个脚本比较简单,只是遍历出ip.list文件内容然后交给syncFile.expect脚本去执行而已,示例: #!/bin/bash ipList="$1" fileList="$2" password="$3" for ip in `cat $ipList` do # 第二个参数就是需要同步的文件列表 ./syncFile.expect $ip $fileList $password done 5.最后我们只需要执行syncFile.sh脚本即可实现批量同步多个文件: [root@localhost ~/syncList]$ sh ./syncFile.sh "/root/webServerIP.txt" "/tmp/fileList.txt" "lri35krJF;ba" 我运行完之后,没有报错,文件也正常同步了,这样我们就实现了一个简单的文件分发系统,这个需求就实现了。 11 制定合理的mysql数据备份方案,并写备份脚本,要求把备份数据传输到备份服务器 这个mysql的主从已经可以满足备份的需求了,如果需要通过脚本另外备份,可以参考以下代码: 假定,我们知道mysql root账号的密码,要备份的库为discuz,本地备份目录为/bak/mysql, 远程服务器ip为192.168.123.30,远程提供了一个rsync服务,备份的地址是 192.168.200.149::backup . 写完脚本后,需要加入到cron中,每天凌晨3点执行。 #! /bin/bash ### backup mysql data PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/mysql/bin d1=`data +%w` d2=`date +%d` pass="123456" bakdir=/bak/mysql r_bakdir=192.168.200.149::module/data/mysqlBackup exec 1>/var/log/mysqlbak.log 2>/var/log/mysqlbak.log echo "mysql backup begin at `date +"%F %T"`." mysqldump -uroot -p$pass –default-character-set=utf8 ultrax >$bakdir/ultrax$d1.sql mysqldump -uroot -p$pass –default-character-set=utf8 DedeCMS >$bakdir/DedeCMS$d1.sql mysqldump -uroot -p$pass –default-character-set=utf8 zrlog >$bakdir/zrlog$d1.sql rsync -az $bakdir/ultrax$d1.sql $r_bakdir/ultrax$d2.sql rsync -az $bakdir/DedeCMS$d1.sql $r_bakdir/DedeCMS$d2.sql rsync -az $bakdir/zrlog$d1.sql $r_bakdir/zrlog$d2.sql echo "mysql backup end at `date +"%F %T"`." 接着加入cron: 0 3* /bin/bash /usr/local/sbin/mysqlbak.sh 然后就会每天凌晨3点进行备份了。 12 制定代码、静态文件的备份方案,并写备份脚本,要求备份13 编写数据恢复文档,能保证当数据丢失在2小时内恢复所有数据 以上这两个需求可以一起实现,结合起来就是两个小时同步一次文件,因为提到需要恢复所有数据,所以需要备份整个站点目录,备份的脚本修改一下分发系统那个expect脚本就可以了,代码如下: [root@localhost ~]$ vim syncList/syncWebFile.expect #!/usr/bin/expect set host [lindex $argv 0] set file [lindex $argv 1] set passwd [lindex $argv 2] spawn rsync -az $file root@$host:$file expect { "yes/no" { send "yes\r"} "password:" { send "$passwd\r" } } interact [root@localhost ~]$ chmod a+x /root/syncList/syncWebFile.expect ## shell 脚本 [root@localhost ~]$ vim /root/syncList/webBak.sh #!/bin/bash ip="$1" webDir="$2" password=`cat /etc/sync.passwd` d=`date +%F" "%T` echo "webdir backup begin at `date +"%F %T"`." for dir in `cat $webDir` do /root/syncList/syncWebFile.expect $ip $dir $password done echo "backup = ip:$ip time:$d;" >> /tmp/webSync.log echo "webdir backup end at `date +"%F %T"`." 创建密码文件: [root@localhost ~]$ vim /etc/sync.passwd lri35krJF;ba [root@localhost ~]$ chmod 600 /etc/sync.passwd 创建文件存放需要同步的目录路径列表: [root@localhost ~]$ vim /tmp/webDir.txt /data/wwwroot/discuz.com /data/wwwroot/dedecms.com /data/wwwroot/zrlog.com 在备份服务上创建目录: mkdir -p /data/wwwroot/ 8.测试一下能否进行同步: [root@localhost ~]$ /bin/bash /root/syncList/webBak.sh "192.168.200.149" "/tmp/webDir.txt" webdir backup begin at 2017-12-22 16:53:07. spawn rsync -az -e ssh -i .ssh/id_rsa /data/wwwroot/discuz.com user1@192.168.200.149:/data/wwwroot/discuz.com Enter passphrase for key '.ssh/id_rsa': spawn rsync -az -e ssh -i .ssh/id_rsa /data/wwwroot/dedecms.com user1@192.168.200.149:/data/wwwroot/dedecms.com Enter passphrase for key '.ssh/id_rsa': spawn rsync -az -e ssh -i .ssh/id_rsa /data/wwwroot/zrlog.com user1@192.168.200.149:/data/wwwroot/zrlog.com Enter passphrase for key '.ssh/id_rsa': webdir backup end at 2017-12-22 16:53:16. [root@localhost ~]$ 9.执行完脚本后到备份服务器上确认一下: [root@localhost ~]$ ls /data/wwwroot/ dedecms.com discuz.com zrlog.com [root@localhost ~]$ ls /data/wwwroot/dedecms.com/ dedecms.com [root@localhost ~]$ ls /data/wwwroot/dedecms.com/dedecms.com/ a data dede favicon.ico images include index.php logs m member plus robots.txt tags.php templets uploads [root@localhost ~]$ ls /data/wwwroot/zrlog.com/zrlog.com/ admin assets attached error favicon.ico include install logs META-INF WEB-INF [root@localhost ~]$ ls /data/wwwroot/discuz.com/discuz.com/ admin.php api.php config cp.php data fileList.txt forum.php home.php member.php plugin.php robots.txt source template uc_server api archiver connect.php crossdomain.xml favicon.ico filePermission.sh group.php index.php misc.php portal.php search.php static uc_client userapp.php [root@localhost ~]$ 10.看看日志文件里有没有记录每次同步的时间: [root@localhost ~]$ cat /tmp/webSync.log backup = ip:192.168.200.149 time:2017-12-22 17:13:18; [root@localhost ~]$ 11.添加到任务计划里: 0 */2 * * * /bin/bash /root/syncList/webBak.sh "192.168.200.149" "/tmp/webDir.txt" 7 所有服务器要求只能普通用户登录,而且只能密钥登录,root只能普通用户sudo 1.首先使用visudo命令编辑配置文件,设置用户的alias: User_Alias ADMINS = user1, user2, user3 然后批量执行useradd命令在全部服务器上添加user1、user2、user3用户 2.找到Allow root to run any commands anywhere,在这行下面添加以下内容: ADMINS ALL=(ALL) NOPASSWD: /usr/bin/su, /usr/bin/ls, /usr/bin/cat, /usr/bin/mkdir 3.修改sshd.config配置文件,修改以下内容: [root@localhost ~]$ vim /etc/ssh/sshd_config PermitRootLogin no PubkeyAuthentication yes PasswordAuthentication no 重启服务: systemctl restart sshd.service 4.新建/root/.ssh 文件夹,然后生成密钥 [root@localhost ~]$ mkdir /root/.ssh/ [root@localhost ~]$ ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): # 直接回车 Enter passphrase (empty for no passphrase): # 设置密钥的密码 Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: 95:e1:79:b3:a0:4b:a0:d8:83:77:4e:88:96:8b:af:56 root@localhost.localdomain The key's randomart image is: +--[ RSA 2048]----+ | . | | . + | | . * o | | * o . o o o | | * * o S . | | o E = . . | |. o . . | | o | |o.. | +-----------------+ [root@localhost ~]$ 查看生成的密钥文件: [root@localhost ~]$ cd .ssh/ [root@localhost ~/.ssh]$ ls id_rsa id_rsa.pub [root@localhost ~/.ssh]$ 把公钥的文件名修改成authorized_keys,并复制一份到user1用户的家目录下: mv id_rsa.pub authorized_keys mkdir /home/user1/.ssh/ touch /home/user1/.ssh/authorized_keys cat /root/.ssh/authorized_keys > /home/user1/.ssh/authorized_keys 设置权限: chmod 700 /home/user1/.ssh/ chmod 600 /home/user1/.ssh/authorized_keys chown -R user1:user1 /home/user1/.ssh/ 5.将id_rsa文件下载到本地。 6.连接时选择这个文件,或者导入Xshell中: 登录成功: 7.然后把/etc/sudoers、/etc/ssh/sshd_config配置文件同步到所有机器上,密钥也一并同步,同步完之后记得重启sshd服务。 完成以上操作后,无论使用密钥或者是密码都无法远程登录root用户,只能登录普通用户了。 因为我们禁止了root远程登录,并且普通用户也只能密钥登录,所以之前写的那个备份文件的脚本需要进行更改,改成通过ssh指定密钥进行连接远程机器。 1.给刚刚生成的密钥文件授予700权限: chmod 700 .ssh/id_rsa 2.使用ssh测试一下看看能否进行同步文件(user1的密钥文件我已经同步到所有机器上了): [root@localhost ~]$ rsync -av -e "ssh -i .ssh/id_rsa" /tmp/fileList.txt user1@192.168.200.149:/tmp/fileList.txt Enter passphrase for key '.ssh/id_rsa': ## 这是生成密钥时设置的密码 sending incremental file list fileList.txt sent 269 bytes received 31 bytes 28.57 bytes/sec total size is 189 speedup is 0.63 [root@localhost ~]$ 从结果可以看到,能够成功同步文件。 好,接下来修改之前的脚本,首先修改的是密码文件,将之前的登录密码改为生成密钥时设置的密码: [root@localhost ~]$ vim /etc/sync.passwd 123456 [root@localhost ~]$ shell脚本不用修改,修改expect脚本如下: [root@localhost ~]$ vim syncList/syncWebFile.expect #!/usr/bin/expect set host [lindex $argv 0] set file [lindex $argv 1] set passwd [lindex $argv 2] spawn rsync -az -e "ssh -i .ssh/id_rsa" $file user1@$host:$file expect { "yes/no" { send "yes\r"} "'.ssh/id_rsa':" { send "$passwd\r" } } expect eof 到备份服务器上给/data/wwwroot/目录授予777权限: chmod 777 /data/wwwroot/ 然后测试一下该脚本: [root@localhost ~]$ /bin/bash /root/syncList/webBak.sh "192.168.200.149" "/tmp/webDir.txt" webdir backup begin at 2017-12-22 16:53:07. spawn rsync -az -e ssh -i .ssh/id_rsa /data/wwwroot/discuz.com user1@192.168.200.149:/data/wwwroot/discuz.com Enter passphrase for key '.ssh/id_rsa': spawn rsync -az -e ssh -i .ssh/id_rsa /data/wwwroot/dedecms.com user1@192.168.200.149:/data/wwwroot/dedecms.com Enter passphrase for key '.ssh/id_rsa': spawn rsync -az -e ssh -i .ssh/id_rsa /data/wwwroot/zrlog.com user1@192.168.200.149:/data/wwwroot/zrlog.com Enter passphrase for key '.ssh/id_rsa': webdir backup end at 2017-12-22 16:53:16. [root@localhost ~]$ 可以看到没有报错,同步成功,到备份服务器上看看: [root@localhost ~]$ ls /data/wwwroot/ dedecms.com discuz.com zrlog.com [root@localhost ~]$ ls /data/wwwroot/dedecms.com/ dedecms.com [root@localhost ~]$ ls /data/wwwroot/dedecms.com/dedecms.com/ a data dede favicon.ico images include index.php logs m member plus robots.txt tags.php templets uploads [root@localhost ~]$ ls /data/wwwroot/zrlog.com/zrlog.com/ admin assets attached error favicon.ico include install logs META-INF WEB-INF [root@localhost ~]$ ls /data/wwwroot/discuz.com/discuz.com/ admin.php api.php config cp.php data fileList.txt forum.php home.php member.php plugin.php robots.txt source template uc_server api archiver connect.php crossdomain.xml favicon.ico filePermission.sh group.php index.php misc.php portal.php search.php static uc_client userapp.php [root@localhost ~]$ 没毛病 ,到目前为止这个项目就告一段落了,其中有一些细节还需慢慢处理,这些都是日后维护的问题了。 本文转自 ZeroOne01 51CTO博客,原文链接:http://blog.51cto.com/zero01/2052429,如需转载请自行联系原作者

优秀的个人博客,低调大师

实战案例:构建docker容器集群 (解决方案一:使用自定义网桥连接跨主机容器)

一、实验环境 虚拟机a两块网卡 eth0 eth1 (IP地址static或者dhcp) 虚拟机b两块网卡 eth0 eth1 (IP地址static或者dhcp) 二、在两台虚拟机上配置网桥 虚拟机a操作: apt-get install bridge-utils vim /etc/network/interfaces auto eth0 iface eth0 inet dhcp auto eth1 iface eth1 inet dhcp auto br0 iface br0 inet static address 192.168.200.4 network 192.168.200.0 netmask 255.255.255.0 broadcast 192.168.200.255 gateway 192.168.200.1 dns-nameservers 192.168.89.2 bridge_ports eth0 bridge_stp off bridge_fd 0 bridge_maxwait 0 bridge_maxage 12 虚拟机b操作: apt-get install bridge-utils vim /etc/network/interfaces auto eth0 iface eth0 inet dhcp auto eth1 iface eth1 inet dhcp auto br0 iface br0 inet static address 192.168.200.3 network 192.168.200.0 netmask 255.255.255.0 broadcast 192.168.200.255 gateway 192.168.200.1 dns-nameservers 192.168.89.2 bridge_ports eth0 bridge_stp off bridge_fd 0 bridge_maxwait 0 bridge_maxage 12 配置好后重启即可!!! 三、验证网桥(ping一下) root@docker-test:~# ping 192.168.200.3 PING 192.168.200.3 (192.168.200.3) 56(84) bytes of data. 64 bytes from 192.168.200.3: icmp_seq=1 ttl=64 time=0.714 ms 64 bytes from 192.168.200.3: icmp_seq=2 ttl=64 time=0.549 ms root@docker-test:~# ping 192.168.200.4 PING 192.168.200.4 (192.168.200.4) 56(84) bytes of data. 64 bytes from 192.168.200.4: icmp_seq=1 ttl=64 time=0.036 ms 64 bytes from 192.168.200.4: icmp_seq=2 ttl=64 time=0.078 ms 虚拟机a验证 root@docker-test:~# brctl show bridge name bridge id STP enabled interfaces br0 8000.000c29f87a89 no eth0 #网桥br0连接到物理网卡eth0 docker0 8000.56847afe9799 no 虚拟机b验证: bridge name bridge id STP enabled interfaces br0 8000.000c298c2483 no eth0 #网桥br0连接到物理网卡eth0 docker0 8000.56847afe9799 no 四、开始修改docker相关配置文件(将容器绑定到指定的网桥上) 虚拟机a操作: vim /etc/default/docker 在最后一行添加 DOCKER_OPTS="-b=br0" 虚拟机b操作: vim /etc/default/docker 在最后一行添加 DOCKER_OPTS="-b=br0" 修改完之后,重启docker服务 service docker restart 五、创建容器 虚拟机1操作: docker run -dti -p 5000:5000 --name web1 training/webapp python app.py #创建并在后台运行容器 root@docker-test:~# docker ps -a 查看网卡 ifconfig命令, 发现多了一块刚才创建的容器的虚拟网卡:vethbe760cd root@docker-test:~# brctl show bridge name bridge id STP enabled interfaces br0 8000.000c29f87a89 no eth0 vethbe760cd 进入容器: docker exec -ti web1 /bin/bash 进入容器后,ping物理网络,是否可以连通 虚拟机2操作: docker run -dti -p 5000:5000 --name web2 training/webapp python app.py #创建并在后台运行容器 root@docker2:~# docker ps -a 查看网卡 ifconfig命令, 发现多了一块刚才创建的容器的虚拟网卡:veth09f70b6 root@docker2:~# brctl show bridge name bridge id STP enabled interfaces br0 8000.000c298c2483 no eth0 veth09f70b6 进入容器: docker exec -ti web1 /bin/bash 进入容器后,ping物理网络,是否可以连通 注意:这样的实现方式,主要问题是需要知道容器所在物理主机(或者虚拟机)的IP地址 那么,如果用Ambassador容器机制,可以让互联的容器无需知道所在物理主机的IP地址即可互联 本文转自 TtrToby 51CTO博客,原文链接:http://blog.51cto.com/freshair/1958355

资源下载

更多资源
优质分享App

优质分享App

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册
修改密码
忘记密码