注:本文侧重于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.git Initialized 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:53 root@pts/0 # git status # On branch master # # Initial commit # # Untracked files: # (use "git add <file>..." 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:56 root@pts/0 # git add modules root@pts/0 # git commit -a -m "init repo" [master (root-commit) 8ef11ef] init repo Committer: root <root@pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com> Your name and email address were configured automatically based on 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 [email protected] If the identity used for this commit is wrong, you can fix it with: git commit --amend --author='Your Name <[email protected]>' 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:47 root@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:10 root@pts/0 # git config --global user.email "[email protected]"
配置git远程节点名称:
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yydev/environment] 2015-04-15 17:51:10 root@pts/0 # git remote add origin /var/puppet/environment.git pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yydev/environment] 2015-04-15 17:52:17 root@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:35 root@pts/0 # git remote add app_origin /var/puppet/application.git root@pts/0 # git remote -v app_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:36 root@pts/0 # cd ../environment/; git remote -v origin /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:08 root@pts/0 # git remote rm origin pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yydev/environment] 2015-04-15 17:57:18 root@pts/0 # git remote add env_origin /var/puppet/environment.git pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yydev/environment] 2015-04-15 17:57:34 root@pts/0 # git remote -v env_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:05 root@pts/0 # git push app_origin master:master Counting 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:55 root@pts/0 # git clone /var/puppet/environment.git environment fatal: 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:10 root@pts/0 # rm -rf environment pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yytest] 2015-04-15 18:01:38 root@pts/0 # git clone /var/puppet/environment.git environment Initialized empty Git repository in /etc/puppet/multi-environment/yytest/environment/.git/root@pts/0 # git clone /var/puppet/application.git application Initialized 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:56 root@pts/0 # cd environment/modules/ ;ls ssh test pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yytest/environment] 2015-04-16 12:52:53 root@pts/0 # git remote rm origin pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yytest/environment] 2015-04-16 12:53:02 root@pts/0 # git remote add env_origin /var/puppet/environment.git pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yytest/environment] 2015-04-16 12:53:23 root@pts/0 # cd ../application ; git remote rm origin; git remote add app_origin /var/puppet/application.git pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yytest/application] 2015-04-16 12:53:54 root@pts/0 # git remote -v app_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 application Initialized empty Git repository in /etc/puppet/multi-environment/yyprd/application/.git/ root@pts/0 # git remote rm origin pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yyprd/application] 2015-04-16 12:48:31 root@pts/0 # git remote add app_origin /var/puppet/application.git root@pts/0 # git clone /var/puppet/environment.git environment Initialized 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:32 root@pts/0 # ls environment/modules/ ssh test root@pts/0 # git remote rm origin pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yyprd/environment] 2015-04-16 12:49:19 root@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 * master pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yydev/environment/modules] 2015-04-16 13:39:50 root@pts/0 # git branch env/crontab root@pts/0 # git branch -v env/crontab c10ee1d init env * master c10ee1d init env root@pts/0 # git checkout env/crontab Switched 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:55 root@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 # ls cron ssh test pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yydev/environment/modules] 2015-04-16 14:27:21 root@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:29 root@pts/0 # git status # On branch env/cron # Changes to be committed: # (use "git reset HEAD <file>..." 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:46 root@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 master Switched 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:52 root@pts/0 # ls cron ssh test
删除特性分支:
root@pts/0 # git branch -v env/cron beb0b03 modify service.pp * master 4f5ff10 modify crontab pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yydev/environment/modules] 2015-04-16 15:03:49 root@pts/0 # git branch -d env/cron Deleted 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:43 root@pts/0 # ls application environment manifests pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yydev] 2015-04-16 14:22:43 root@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:43 root@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:56 root@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.com warning: Not using cache on failed catalog err: Could not retrieve catalog; skipping run
错误排查:
root@pts/0 # git checkout master Switched 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:44 root@pts/0 # ls files templates pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yydev/environment/modules/cron] 2015-04-16 14:42:44 root@pts/0 # git checkout env/cron Switched to branch 'env/cron' root@pts/0 # git merge master Already 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:40 root@pts/0 # git checkout env/cron Already 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:49 root@pts/0 # ls files manifests templates
出错2:
root@pts/0 # puppet agent -t --environment=yydev --noop --no-daemonize --verbose err: 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.com warning: Not using cache on failed catalog err: Could not retrieve catalog; skipping run
报错排查,可见是语法错误:
service.pp少了个“}”,再次修复。
开发测试机成功。
root@pts/0 # puppet agent -t --environment=yydev --noop --no-daemonize --verbose info: Caching catalog for ppttest_wangsu_bj_192.168.3.67.centos.yypuppet.com info: 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 events notice: Stage[main]: Would have triggered 'refresh' from 1 events notice: Finished catalog run in 1.07 seconds
2:测试:
root@pts/0 # crontab -l ppttest_wangsu_bj_192.168.3.67.centos.yypuppet.com [~] 2015-04-16 15:10:57 root@pts/0 # puppet agent -t --environment=yydev --no-daemonize --verbose info: Caching catalog for ppttest_wangsu_bj_192.168.3.67.centos.yypuppet.com info: Applying configuration version '1429168261' notice: /Stage[main]/Cron::Crontab/Cron[ntp time]/ensure: created notice: Finished catalog run in 1.10 seconds ppttest_wangsu_bj_192.168.3.67.centos.yypuppet.com [~] 2015-04-16 15:11:04 root@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 time 30 */2 * * * /sbin/ntpdate 192.168.0.2
提交代码:
root@pts/0 # git status # On branch master # Changed but not updated: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." 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:04 root@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:36 root@pts/0 # ls cron ssh test pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yydev/environment/modules] 2015-04-16 15:33:36 root@pts/0 # git remote -v env_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:52 root@pts/0 # git push env_origin Counting 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 * master pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yytest/environment/modules] 2015-04-16 15:40:11 root@pts/0 # git pull env_origin master From /var/puppet/environment * branch master -> FETCH_HEAD Updating c10ee1d..112c636 Fast-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 testing pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yytest/environment/modules] 2015-04-17 11:41:45 root@pts/0 # git checkout testing Switched 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:50 root@pts/0 # git tag -m 'first release to production' 1.0.0 pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yytest/environment/modules] 2015-04-17 11:42:16 root@pts/0 # git push --tags env_origin Counting 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:48 root@pts/0 # git fetch env_origin remote: 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.0 root@pts/0 # git checkout tags/1.0.0 Note: checking out 'tags/1.0.0'. You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout. If you want to create a new branch to retain commits you create, you may do so (now or later) by using -b with the checkout command again. Example: git checkout -b new_branch_name HEAD is now at 112c636... finish pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com [/etc/puppet/multi-environment/yyprd/environment/modules] 2015-04-17 11:44:20 root@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