由于业务需要windows机器运行程序,沟通之后发现团队里大家并没有windows相关的运维经验,一番调研后决定以虚机+cygwin方式作为管理方案。

一、cygwin的安装部署 我们可以到Cygwin的官方网站下载Cygwin的安装程序,地址是:http://www.cygwin.com/ ,下载完成后,运行setup.exe程序,出现安装画面。直接点“下一步”即可。这里无需过多阐述。

注意这里有三种安装模式: Install from Internet,这种模式直接从Internet安装,适合网速较快的情况; Download Without Installing,这种模式只从网上下载Cygwin的组件包,但不安装; Install from Local Directory,这种模式与上面第二种模式对应,当你的Cygwin组件包已经下载到本地,则可以使用此模式从本地安装Cygwin。 从上述三种模式中选择适合你的安装模式,这里我们选择第一种安装模式,直接从网上安装,当然在下载的同时,Cygwin组件也保存到了本地,以便以后能够再次安装。

另外一个需要注意的点就是,cygwin的模块包安装,需要使用代理,推荐http://mirrors.163.com/cygwin/ 模块一般会选择sshd rsync cron等

二、服务的日常管理

  1. 基础脚本编写 在业务服务路径下,需要写好start.sh stop.sh restart.sh,作为服务的基本启停管理脚本
  2. 配置rsync
  3. 启动sshd
  4. 启动crond
  5. 这里重点讲下发布和远程批量重启 1)发布: #/bin/bash

#提醒用户,是否更新了要上线的代码列表文件 read -p "你是否已经更新了文件列表./file.list?确认请输入y或者Y,否则按其他任意键退出脚本。" c

if [ -z "$c" ] then exit 1 fi

#如果直接按回车,也会退出脚本 if [ $c == "y" -o $c == "Y" ] then echo "脚本将在2秒后,继续执行。" for i in 1 2 do echo -n "." sleep 1 done echo else exit 1 fi

#定义检测文件是否存在的函数 if_file_exist() { if [ ! -f $1 ] then echo "文件$1不存在,请检查。" exit 1 fi }

#ip.list为所有机器的ip列表 #file.list为要同步的文件列表 if_file_exist ./ip.list if_file_exist ./file.list

path=pwd for ip in cat ./ip.list do rsync -avR --files-from=./file.list $path $ip::odin/ done 2) 批量重启管理: #!/bin/bash #批量启动关闭远程机器上的服务

if [ $# != 1 ] ; then echo "USAGE: $0 start||stop||restart" exit 1; fi

ipfile=./ip.list

cat >> stop.exp <<EOF #!/usr/bin/expect set passwd [lindex $argv 0] set host [lindex $argv 1] spawn ssh Administrator@$host

expect { "yes/no" { send "yes\r"; exp_continue} "password:" { send "$passwd\r" } }

expect "$" send "stop.sh\r" expect "$" send "exit\r" EOF

cat >> start.exp <<EOF #!/usr/bin/expect set passwd [lindex $argv 0] set host [lindex $argv 1] spawn ssh Administrator@$host

expect { "yes/no" { send "yes\r"; exp_continue} "password:" { send "$passwd\r" } }

expect "$" send "start.sh\r" expect "$" send "exit\r" EOF

cat >> restart.exp <<EOF #!/usr/bin/expect set passwd [lindex $argv 0] set host [lindex $argv 1] spawn ssh administrator@$host

expect { "yes/no" { send "yes\r"; exp_continue} "password:" { send "$passwd\r" } }

expect "$" send "restart.sh\r" expect "$" send "exit\r" EOF

chmod a+x stop.exp chmod a+x start.exp chmod a+x restart.exp pw='xxxxxxxxx'

cat $ipfile | while read line do ip=echo $line|awk '{print $1}' if [ $1 == "start" ] then
echo "do start" ./start.exp $pw $ip elif [ $1 == "stop" ] then echo "do stop" ./stop.exp $pw $ip elif [ $1 == "restart" ] then echo "do restart" ./restart.exp $pw $ip else echo "Please make sure the positon variable is start or stop." fi done

rm -f ./start.exp rm -f ./restart.exp rm -f ./stop.exp 6. 开机自启动服务,编写启动bat脚本,添加到windows cron里面即可: C:\cygwin64\bin\run bash --login -c /start.sh 至此,服务的基本管理框架就有啦

三、虚机管理 制作完镜像,直接批量扩容即可。