注:本文侧重于pupput使用git版本控制在线上环境的部署步骤详解。
1:线上puppet加入git版本控制
详细步骤:
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yydev/environment]root@pts/0 # git init ; cd ..; git clone --bar environment /var/puppet/environment.gitInitialized empty Git repository in /etc/puppet/multi-environment/yydev/environment/.git/Initialized empty Git repository in /var/puppet/environment.git/warning: You appear to have cloned an empty repository.pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yydev/application] root@pts/0 # git init Initialized empty Git repository in /etc/puppet/multi-environment/yydev/application/.git/warning: You appear to have cloned an empty repository.
将项目添加到git:
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yydev/application] 2015-04-15 17:37:53root@pts/0 # git status# On branch master## Initial commit## Untracked files:# (use "git add..." to include in what will be committed)## modules/nothing added to commit but untracked files present (use "git add" to track)pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yydev/application] 2015-04-15 17:37:56root@pts/0 # git add modulesroot@pts/0 # git commit -a -m "init repo"[master (root-commit) 8ef11ef] init repo Committer: root Your name and email address were configured automatically basedon your username and hostname. Please check that they are accurate.You can suppress this message by setting them explicitly: git config --global user.name "Your Name" git config --global user.email you@example.comIf the identity used for this commit is wrong, you can fix it with: git commit --amend --author='Your Name ' 2 files changed, 24 insertions(+), 0 deletions(-) create mode 100644 modules/httpd/files/1.txt create mode 100644 modules/httpd/manifests/init.pp
配置提交信息:
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yydev/application] 2015-04-15 17:38:47root@pts/0 # git config --global user.name "wangyanliang"pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yydev/application] 2015-04-15 17:39:10root@pts/0 # git config --global user.email "wangyl@youyuan.com"
配置git远程节点名称:
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yydev/environment] 2015-04-15 17:51:10root@pts/0 # git remote add origin /var/puppet/environment.gitpptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yydev/environment] 2015-04-15 17:52:17root@pts/0 # cd ../application pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yydev/application] 2015-04-15 17:52:35root@pts/0 # git remote add app_origin /var/puppet/application.gitroot@pts/0 # git remote -vapp_origin /var/puppet/application.git (fetch)app_origin /var/puppet/application.git (push)pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yydev/application] 2015-04-15 17:53:36root@pts/0 # cd ../environment/; git remote -vorigin /var/puppet/environment.git (fetch)origin /var/puppet/environment.git (push)pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yydev/environment] 2015-04-15 17:57:08root@pts/0 # git remote rm originpptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yydev/environment] 2015-04-15 17:57:18root@pts/0 # git remote add env_origin /var/puppet/environment.gitpptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yydev/environment] 2015-04-15 17:57:34root@pts/0 # git remote -venv_origin /var/puppet/environment.git (fetch)env_origin /var/puppet/environment.git (push)
将更改推送到git仓库:
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yydev/application] 2015-04-15 17:59:05root@pts/0 # git push app_origin master:masterCounting objects: 8, done.Delta compression using up to 2 threads.Compressing objects: 100% (3/3), done.Writing objects: 100% (8/8), 705 bytes, done.Total 8 (delta 0), reused 0 (delta 0)Unpacking objects: 100% (8/8), done.To /var/puppet/application.git * [new branch] master -> master
操作yytest目录:
2015-04-15 18:00:55root@pts/0 # git clone /var/puppet/environment.git environmentfatal: destination path 'environment' already exists and is not an empty directory.pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yytest] 2015-04-15 18:01:10root@pts/0 # rm -rf environmentpptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yytest] 2015-04-15 18:01:38root@pts/0 # git clone /var/puppet/environment.git environmentInitialized empty Git repository in /etc/puppet/multi-environment/yytest/environment/.git/root@pts/0 # git clone /var/puppet/application.git applicationInitialized empty Git repository in /etc/puppet/multi-environment/yytest/application/.git/pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yytest] 2015-04-15 18:02:56root@pts/0 # cd environment/modules/ ;lsssh testpptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yytest/environment] 2015-04-16 12:52:53root@pts/0 # git remote rm originpptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yytest/environment] 2015-04-16 12:53:02root@pts/0 # git remote add env_origin /var/puppet/environment.gitpptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yytest/environment] 2015-04-16 12:53:23root@pts/0 # cd ../application ; git remote rm origin; git remote add app_origin /var/puppet/application.gitpptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yytest/application] 2015-04-16 12:53:54root@pts/0 # git remote -vapp_origin /var/puppet/application.git (fetch)app_origin /var/puppet/application.git (push)
同样的操作yyprd目录:
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yytest]root@pts/0 # git clone /var/puppet/application.git applicationInitialized empty Git repository in /etc/puppet/multi-environment/yyprd/application/.git/root@pts/0 # git remote rm originpptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yyprd/application] 2015-04-16 12:48:31root@pts/0 # git remote add app_origin /var/puppet/application.gitroot@pts/0 # git clone /var/puppet/environment.git environmentInitialized empty Git repository in /etc/puppet/multi-environment/yyprd/environment/.git/pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yyprd] 2015-04-16 11:54:32root@pts/0 # ls environment/modules/ssh testroot@pts/0 # git remote rm originpptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yyprd/environment] 2015-04-16 12:49:19root@pts/0 # git remote add env_origin /var/puppet/environment.git
至此puppet线上部署完成:多环境配置统一,并加入git,包含开发,测试,生产。让我看看我们这个环境是怎么协同工作的吧!
2:协同开发
我们假设用户user1,user2
user1负责写crontab模块。
user2负责写httpd模块。
usr1的实施步骤:
进入开发环境建git开发特性分支 env/crontab
git branch env/crontab
root@pts/0 # git branch* masterpptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yydev/environment/modules] 2015-04-16 13:39:50root@pts/0 # git branch env/crontabroot@pts/0 # git branch -v env/crontab c10ee1d init env* master c10ee1d init envroot@pts/0 # git checkout env/crontabSwitched to branch 'env/crontab'pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yydev/environment/modules] 2015-04-16 13:42:55root@pts/0 # mkdir cron/{files,manifests,templates} -p
模块命令已经写好:(虽然错误百出,哈哈, 正确的在下面)
root@pts/0 # cat init.pp install.pp service.pp crontab.pp class cron { include cron::install,cron::service,cron::crontab}class cron::install { package { "crontabs": ensure => present, }}class cron::service { service { "crond": ensure => running,}class cron::crontab { cron {"ntp time": ensure => present, command => "/sbin/ntpdate 192.168.0.2", user => 'root', minute => 0, hour => '6-13/3', month => *;}
提交代码:
root@pts/0 # lscron ssh testpptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yydev/environment/modules] 2015-04-16 14:27:21root@pts/0 # git add * pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yydev/environment/modules] 2015-04-16 14:27:29root@pts/0 # git status# On branch env/cron# Changes to be committed:# (use "git reset HEAD..." to unstage)## new file: cron/manifests/crontab.pp# new file: cron/manifests/init.pp# new file: cron/manifests/install.pp# new file: cron/manifests/service.pp#pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yydev/environment/modules] 2015-04-16 14:27:46root@pts/0 # git commit -m "add cron" . [env/cron ef19965] add cron 4 files changed, 20 insertions(+), 0 deletions(-) create mode 100644 modules/cron/manifests/crontab.pp create mode 100644 modules/cron/manifests/init.pp create mode 100644 modules/cron/manifests/install.pp create mode 100644 modules/cron/manifests/service.pp
特性分支合并到主分支:
root@pts/0 # git checkout masterSwitched to branch 'master'root@pts/0 # git merge env/cron Already up-to-date.pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yydev/environment/modules] 2015-04-16 14:35:52root@pts/0 # lscron ssh test
删除特性分支:
root@pts/0 # git branch -v env/cron beb0b03 modify service.pp* master 4f5ff10 modify crontabpptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yydev/environment/modules] 2015-04-16 15:03:49root@pts/0 # git branch -d env/cronDeleted branch env/cron (was beb0b03).
开发环境测试:
1: 配置开发测试的主机:
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yydev] 2015-04-16 14:22:43root@pts/0 # lsapplication environment manifestspptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yydev] 2015-04-16 14:22:43root@pts/0 # cd manifests/nodes/ site.pp pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yydev] 2015-04-16 14:22:43root@pts/0 # cat manifests/site.pp import "nodes/*.pp"pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yydev] 2015-04-16 14:22:56root@pts/0 # cat manifests/nodes/test.pp class base { include ssh include cron}node 'ppttest_wangsu_bj_192.168.3.67.centos.yypuppet.com' { include base include test include httpd}
2: 开发测试主机上测试:
出错1:
root@pts/0 # puppet agent -t --environment=yydev --noop --no-daemonize --verbose err: Could not retrieve catalog from remote server: Error 400 on SERVER: Could not find class cron for ppttest_wangsu_bj_192.168.3.67.centos.yypuppet.com at /etc/puppet/multi-environment/yydev/manifests/nodes/test.pp:3 on node ppttest_wangsu_bj_192.168.3.67.centos.yypuppet.comwarning: Not using cache on failed catalogerr: Could not retrieve catalog; skipping run
错误排查:
root@pts/0 # git checkout masterSwitched to branch 'master'pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yydev/environment/modules/cron] 2015-04-16 14:42:44root@pts/0 # lsfiles templatespptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yydev/environment/modules/cron] 2015-04-16 14:42:44root@pts/0 # git checkout env/cronSwitched to branch 'env/cron'root@pts/0 # git merge masterAlready up-to-date.pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yydev/environment/modules/cron] 2015-04-16 14:43:40root@pts/0 # git checkout env/cronAlready on 'env/cron'pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yydev/environment/modules/cron] 2015-04-16 14:43:49root@pts/0 # lsfiles manifests templates
出错2:
root@pts/0 # puppet agent -t --environment=yydev --noop --no-daemonize --verboseerr: Could not retrieve catalog from remote server: Error 400 on SERVER: Syntax error at end of file; expected '}' at /etc/puppet/multi-environment/yydev/environment/modules/cron/manifests/service.pp:5 on node ppttest_wangsu_bj_192.168.3.67.centos.yypuppet.comwarning: Not using cache on failed catalogerr: Could not retrieve catalog; skipping run
报错排查,可见是语法错误:
service.pp少了个“}”,再次修复。
开发测试机成功。
root@pts/0 # puppet agent -t --environment=yydev --noop --no-daemonize --verboseinfo: Caching catalog for ppttest_wangsu_bj_192.168.3.67.centos.yypuppet.cominfo: Applying configuration version '1429167566'notice: /Stage[main]/Cron::Crontab/Cron[ntp time]/ensure: current_value absent, should be present (noop)notice: Class[Cron::Crontab]: Would have triggered 'refresh' from 1 eventsnotice: Stage[main]: Would have triggered 'refresh' from 1 eventsnotice: Finished catalog run in 1.07 seconds
2:测试:
root@pts/0 # crontab -lppttest_wangsu_bj_192.168.3.67.centos.yypuppet.com [~] 2015-04-16 15:10:57root@pts/0 # puppet agent -t --environment=yydev --no-daemonize --verbose info: Caching catalog for ppttest_wangsu_bj_192.168.3.67.centos.yypuppet.cominfo: Applying configuration version '1429168261'notice: /Stage[main]/Cron::Crontab/Cron[ntp time]/ensure: creatednotice: Finished catalog run in 1.10 secondsppttest_wangsu_bj_192.168.3.67.centos.yypuppet.com [~] 2015-04-16 15:11:04root@pts/0 # crontab -l# HEADER: This file was autogenerated at Thu Apr 16 15:11:03 +0800 2015 by puppet.# HEADER: While it can still be managed manually, it is definitely not recommended.# HEADER: Note particularly that the comments starting with 'Puppet Name' should# HEADER: not be deleted, as doing so could cause duplicate cron jobs.# Puppet Name: ntp time30 */2 * * * /sbin/ntpdate 192.168.0.2
提交代码:
root@pts/0 # git status# On branch master# Changed but not updated:# (use "git add..." to update what will be committed)# (use "git checkout -- ..." to discard changes in working directory)## modified: crontab.pp#no changes added to commit (use "git add" and/or "git commit -a")pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yydev/environment/modules/cron/manifests] 2015-04-16 15:01:04root@pts/0 # git commit -a -m "modify crontab" [master 4f5ff10] modify crontab 1 files changed, 7 insertions(+), 6 deletions(-)
最终版本:
root@pts/0 # cat init.pp install.pp service.pp crontab.pp class cron { include cron::install,cron::service,cron::crontab}class cron::install { package { "crontabs": ensure => present, }}class cron::service { service { "crond": ensure => running,}}class cron::crontab { cron {"ntp time": ensure => present, command => "/sbin/ntpdate 192.168.0.2", user => 'root', minute => 30, hour => '*/2',}}
提交代码到仓库中:
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yydev/environment/modules] 2015-04-16 15:33:36root@pts/0 # lscron ssh testpptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yydev/environment/modules] 2015-04-16 15:33:36root@pts/0 # git remote -venv_origin /var/puppet/environment.git (fetch)env_origin /var/puppet/environment.git (push)pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yydev/environment/modules] 2015-04-16 15:33:52root@pts/0 # git push env_originCounting objects: 36, done.Delta compression using up to 2 threads.Compressing objects: 100% (24/24), done.Writing objects: 100% (34/34), 2.84 KiB, done.Total 34 (delta 6), reused 0 (delta 0)Unpacking objects: 100% (34/34), done.To /var/puppet/environment.git c10ee1d..112c636 master -> master
yytest环境更新本地库:
root@pts/0 # git branch * masterpptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yytest/environment/modules] 2015-04-16 15:40:11root@pts/0 # git pull env_origin masterFrom /var/puppet/environment * branch master -> FETCH_HEADUpdating c10ee1d..112c636Fast-forward modules/cron/manifests/crontab.pp | 9 +++++++++ modules/cron/manifests/init.pp | 3 +++ modules/cron/manifests/install.pp | 5 +++++ modules/cron/manifests/service.pp | 5 +++++ 4 files changed, 22 insertions(+), 0 deletions(-) create mode 100644 modules/cron/manifests/crontab.pp create mode 100644 modules/cron/manifests/init.pp create mode 100644 modules/cron/manifests/install.pp create mode 100644 modules/cron/manifests/service.pp
创建tag
每一次开发完后打一个标签,可以很方便的回滚到上一个开发版本。
测试环境创建tag:
root@pts/0 # git branch* master testingpptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yytest/environment/modules] 2015-04-17 11:41:45root@pts/0 # git checkout testingSwitched to branch 'testing'pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yytest/environment/modules] 2015-04-17 11:41:50root@pts/0 # git tag -m 'first release to production' 1.0.0pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yytest/environment/modules] 2015-04-17 11:42:16root@pts/0 # git push --tags env_originCounting objects: 1, done.Writing objects: 100% (1/1), 170 bytes, done.Total 1 (delta 0), reused 0 (delta 0)Unpacking objects: 100% (1/1), done.To /var/puppet/environment.git * [new tag] 1.0.0 -> 1.0.0
生成环境:
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yyprd/environment/modules] 2015-04-17 11:43:48root@pts/0 # git fetch env_originremote: Counting objects: 37, done.remote: Compressing objects: 100% (25/25), done.remote: Total 35 (delta 6), reused 0 (delta 0)Unpacking objects: 100% (35/35), done.From /var/puppet/environment * [new branch] master -> env_origin/master * [new tag] 1.0.0 -> 1.0.0root@pts/0 # git checkout tags/1.0.0Note: checking out 'tags/1.0.0'.You are in 'detached HEAD' state. You can look around, make experimentalchanges and commit them, and you can discard any commits you make in thisstate without impacting any branches by performing another checkout.If you want to create a new branch to retain commits you create, you maydo so (now or later) by using -b with the checkout command again. Example: git checkout -b new_branch_nameHEAD is now at 112c636... finishpptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yyprd/environment/modules] 2015-04-17 11:44:20root@pts/0 # ls cron ssh test
user1的cron开发完毕!
下次上线后的回退:
git checkout tags/1.0.0
让我们来总结下这个过程:
1:user1建立一个特性分支env/cron。
2:user1在env/cron中写cron的代码,此时与主干分支是完全隔离的。
3:代码书写完毕,合并到master分支。
4:此时env/cron便可以删除了。
5:开发环境测试。修复cron的代码。
6:开发测试环境测试成功后,提交cron的代码到仓库中。
7: 测试环境测试及线上环境进行上线。
8: 打上tag,方便版本回退。
user2的实施步骤和user1步骤一致:
1:进入开发环境建立git开发分支 app/httpd
。。。
。。。
puppet模板文档:
http://docs.puppetlabs.com/references/latest/type.html#file