背景:
大约有20台的生产和测试主机(Centos5.3),有6个大模块,以前都是用手动安装的,费时费力,有时也会手动操作出错。
目标:
希望用puppet来实现部署的自动化
下面以其中的一个模块的自动化部署尝试来讲解如何实现用puppet的部署自动化。
前提:
在一台测试机上安装puppet master,一台装puppet agent.具体的安装和配置过程就不再讲述了,网上的资料一堆堆的。
现在着重讲述如何配置puppet的类。
- 创建一个模块,叫ppe
/etc/puppet/modules/ppe/{manifests,templates,files} - 编辑ppe的类文件:/etc/puppet/modules/ppe/manifests/ini.pp
内容如下:
- 检查war包放在/nishome/ppeuser001/installation/frontend/的目录下面
file { ‘frontend.war‘:
path => ‘/nishome/ppeuser001/installation/frontend/frontend.war‘,
ensure => present,
} - 备份现在的部署包
exec { ‘ppefe_install_bp‘:
path => ‘/bin‘,
command => ‘cp -R /nishome/ppeuser001/frontend.war /tmp/frontend_backup‘,
require => File[‘frontend.war‘],
notify => Exec[‘ppefe_install_cp‘],
} - 将包拷贝到安装目录(出于演示的目的,安装目录采用了/tmp)
exec { ‘ppefe_install_cp‘:
path => ‘/bin‘,
command => ‘cp /nishome/ppeuser001/installation/frontend/frontend.war /tmp‘,
require => Exec[‘ppefe_install_bp‘],
} - 执行安装脚本test.sh,加了provider这个参数,否则运行SHELL脚本会失败。
exec { ‘ppefe_install‘:
path => ‘/nishome/ppeuser001/‘,
command => ‘test.sh‘,
provider => ‘shell‘,
require => Exec[‘ppefe_install_cp‘],
notify => Exec[‘ppefe_install_conf‘],
} - 恢复配置文件(因为安装包里面不提供环境的配置文件,所以需要手动恢复)。
exec { ‘ppefe_install_conf‘:
path => ‘/bin‘,
command => ‘cp /nishome/ppeuser001/installation/frontend/properties /tmp ‘,
require => Exec[‘ppefe_install‘],
notify => Exec[‘ppefe_install_post‘],
}
6. 修改文件目录的属性(因为puppet默认都是以root的用户来执行,所以所有涉及到的文件的owner和group都是root,所以在部署后需要修改属性)。最后的notify提示语法错误,不知何故。注释掉后可以运行。
exec { ‘ppefe_install_post‘:
path => ‘/bin‘,
command => ‘chown -R ppeuser001:ppeuser001 /tmp/frontend.war ‘,
require => Exec[‘ppefe_install_conf‘],
# notify { "Deployment is complete" },
}
- 将模块添加到节点配置文件:
node ‘alihzppe002.ha.cekasp.cn‘ {
include ppe
}
$pwd
/etc/puppet/manifests/nodes
- 将测试节点载入到site.pp
vim /etc/puppet/manifests/site.pp
import "nodes/alihzppe002.ha.cekasp.cn"
-
在puppet agent端执行:sudo puppet agent --test --server alihzppe001.ha.cekasp.cn --debug
执行的输出:
Debug: /Stage[main]/Ppe/Exec[ppefe_install_cp]/require: requires Exec[ppefe_install_bp]
Debug: /Stage[main]/Ppe/Exec[ppefe_install_post]/require: requires Exec[ppefe_install_conf]
Debug: /Stage[main]/Ppe/Exec[ppefe_install_bp]/require: requires File[frontend.war]
Debug: /Stage[main]/Ppe/Exec[ppefe_install_bp]/notify: subscribes to Exec[ppefe_install_cp]
Debug: /Stage[main]/Ppe/Exec[ppefe_install_conf]/require: requires Exec[ppefe_install]
Debug: /Stage[main]/Ppe/Exec[ppefe_install_conf]/notify: subscribes to Exec[ppefe_install_post]
Debug: /Stage[main]/Ppe/Exec[ppefe_install]/require: requires Exec[ppefe_install_cp]
Debug: /Stage[main]/Ppe/Exec[ppefe_install]/notify: subscribes to Exec[ppefe_install_conf]
Info: Applying configuration version ‘1404266209‘
Debug: Exec[ppefe_install_bp](provider=posix): Executing ‘cp -R /nishome/ppeuser001/frontend.war /tmp/frontend_backup‘
Debug: Executing ‘cp -R /nishome/ppeuser001/frontend.war /tmp/frontend_backup‘
Notice: /Stage[main]/Ppe/Exec[ppefe_install_bp]/returns: executed successfully
Info: /Stage[main]/Ppe/Exec[ppefe_install_bp]: Scheduling refresh of Exec[ppefe_install_cp]
Debug: /Stage[main]/Ppe/Exec[ppefe_install_bp]: The container Class[Ppe] will propagate my refresh event
Debug: Exec[ppefe_install_cp](provider=posix): Executing ‘cp /nishome/ppeuser001/installation/frontend/frontend.war /tmp‘
Debug: Executing ‘cp /nishome/ppeuser001/installation/frontend/frontend.war /tmp‘
Notice: /Stage[main]/Ppe/Exec[ppefe_install_cp]/returns: executed successfully
Debug: /Stage[main]/Ppe/Exec[ppefe_install_cp]: The container Class[Ppe] will propagate my refresh event
Debug: Exec[ppefe_install_cp](provider=posix): Executing ‘cp /nishome/ppeuser001/installation/frontend/frontend.war /tmp‘
Debug: Executing ‘cp /nishome/ppeuser001/installation/frontend/frontend.war /tmp‘
Notice: /Stage[main]/Ppe/Exec[ppefe_install_cp]: Triggered ‘refresh‘ from 1 events
Debug: /Stage[main]/Ppe/Exec[ppefe_install_cp]: The container Class[Ppe] will propagate my refresh event
Debug: Exec[ppefe_install](provider=shell): Executing ‘/bin/sh-ctest.sh‘
Debug: Executing ‘/bin/sh -c test.sh‘
Notice: /Stage[main]/Ppe/Exec[ppefe_install]/returns: executed successfully
Debug: /Stage[main]/Ppe/Exec[ppefe_install]: The container Class[Ppe] will propagate my refresh event
Info: /Stage[main]/Ppe/Exec[ppefe_install]: Scheduling refresh of Exec[ppefe_install_conf]
Debug: Exec[ppefe_install_conf](provider=posix): Executing ‘cp /nishome/ppeuser001/installation/frontend/properties /tmp ‘
Debug: Executing ‘cp /nishome/ppeuser001/installation/frontend/properties /tmp ‘
Notice: /Stage[main]/Ppe/Exec[ppefe_install_conf]/returns: executed successfully
Info: /Stage[main]/Ppe/Exec[ppefe_install_conf]: Scheduling refresh of Exec[ppefe_install_post]
Debug: /Stage[main]/Ppe/Exec[ppefe_install_conf]: The container Class[Ppe] will propagate my refresh event
Debug: Exec[ppefe_install_conf](provider=posix): Executing ‘cp /nishome/ppeuser001/installation/frontend/properties /tmp ‘
Debug: Executing ‘cp /nishome/ppeuser001/installation/frontend/properties /tmp ‘
Notice: /Stage[main]/Ppe/Exec[ppefe_install_conf]: Triggered ‘refresh‘ from 1 events
Info: /Stage[main]/Ppe/Exec[ppefe_install_conf]: Scheduling refresh of Exec[ppefe_install_post]
Debug: /Stage[main]/Ppe/Exec[ppefe_install_conf]: The container Class[Ppe] will propagate my refresh event
Debug: Exec[ppefe_install_post](provider=posix): Executing ‘chown -R ppeuser001:ppeuser001 /tmp/frontend.war ‘
Debug: Executing ‘chown -R ppeuser001:ppeuser001 /tmp/frontend.war ‘
Notice: /Stage[main]/Ppe/Exec[ppefe_install_post]/returns: executed successfully
Debug: /Stage[main]/Ppe/Exec[ppefe_install_post]: The container Class[Ppe] will propagate my refresh event
Debug: Exec[ppefe_install_post](provider=posix): Executing ‘chown -R ppeuser001:ppeuser001 /tmp/frontend.war ‘
Debug: Executing ‘chown -R ppeuser001:ppeuser001 /tmp/frontend.war ‘
Notice: /Stage[main]/Ppe/Exec[ppefe_install_post]: Triggered ‘refresh‘ from 2 events
Debug: /Stage[main]/Ppe/Exec[ppefe_install_post]: The container Class[Ppe] will propagate my refresh event
Debug: Class[Ppe]: The container Stage[main] will propagate my refresh event
Debug: Finishing transaction 70208250166440
Debug: Storing state
Debug: Stored state in 0.02 seconds
Notice: Finished catalog run in 2.96 seconds
从结果来看是成功的,所以用puppet来实现应用的部署自动化是可行的,但是问题是用puppet来管理20台机子的部署效率到底有没有得到提升,值不值得用这个工具来实现?
而且puppet本身是一个系统管理员的工具,用到应用层面,好象就不是一把“利剑”了,比如上述的六个部署步骤,改用shell脚本来直接运行,效率肯定更高,是吧?
仅个人观点。
对于puppet exec命令的讲解,推荐:http://blog.chinaunix.net/uid-20639775-id-3317595.html
一次puppet部署自动化的尝试,布布扣,bubuko.com