Puppet基础篇9-Puppetmaster多环境配置

扩充现有架构环境是对一个企业成长的见证

将基础环境模块部署到puppetmaster端之后就可以初始化所有节点了,接下来就是部署应用代码了。众所周知,一个企业中应用代码的编写并不是运维一个人完成的,而且代码的上线也不是一次性完成的。标准的架构应该由开发、测试、生产三个组成,对应到puppetmaster里面应该有3套代码才对。而且每套代码都应该对应到自己的环境中,而代码的变更更应该通过版本控制工具进行管理,比如svn、git等。
接下来我们为puppetmaster创造3个环境,它们分别是开发环境(kissdev)、测试环境(kissqa)、生产环境(kissprd).

1、配置puppet.conf

在标签[master]中添加environments环境,其次创建对应的环境标签及配置

  1. [root@puppetmaster ~]# vim /etc/puppet/puppet.conf
  2. [main]
  3. logdir = /var/log/puppet
  4. rundir = /var/run/puppet
  5. ssldir = $vardir/ssl
  6. [agent]
  7. classfile = $vardir/classes.txt
  8. localconfig = $vardir/localconfig
  9. server = puppetmaster.kisspuppet.com
  10. certname = puppetmaster_cert.kisspuppet.com
  11. [master]
  12. certname = puppetmaster.kisspuppet.com
  13. environments = kissdev,kisstmq,kissprd #添加三个环境的标签名称
  14. [kissdev]
  15. modulepath = $confdir/environments/kissdev/environment/modules:$confdir/environments/kissdev/application/modules #设置环境的搜索路径
  16. manifest = $confdir/environments/kissdev/manifests/site.pp #设置环境的site.pp文件位置
  17. fileserverconfig = /etc/puppet/fileserver.conf.kissdev #设置环境的fileserver
  18. [kissmq]
  19. modulepath = $confdir/environments/kissmq/environment/modules:$confdir/environments/kisstest/application/modules
  20. manifest = $confdir/environments/kisstest/manifests/site.pp
  21. fileserverconfig = /etc/puppet/fileserver.conf.kisstest
  22. [kissprd]
  23. modulepath = $confdir/environments/kissprd/environment/modules:$confdir/environments/kissprd/application/modules
  24. manifest = $confdir/environments/kissprd/manifests/site.pp
  25. fileserverconfig = /etc/puppet/fileserver.conf.kissprd

顺便解释一下:为什么在每个环境下会有environment和application两个目录,其中environment目录是存放基础环境模块的,比如puppet、yum等;而application目录是存在应用环境模块的,比如apache、mysql等。当然也可以放在同一个目录下,如果应用多的话还可以将application进行拆分,一切都是为了方便管理而考虑。

2、创建多环境目录结构

  1. [root@puppetmaster environments]# mkdir kissdev
  2. [root@puppetmaster environments]# mkdir kissdev/{application/modules,environment/modules} -p
  3. [root@puppetmaster environments]# tree .
  4. .
  5. └── kissdev
  6. ├── application
  7. └── modules #存放应用的模块
  8. └── environment
  9. └── modules #存放基础环境模块
  10. 5 directories, 0 files
  11. [root@puppetmaster environments]# cp kissdev kissmq -rp
  12. [root@puppetmaster environments]# cp kissdev kissprd -rp
  13. [root@puppetmaster environments]# tree .
  14. .
  15. ├── kissdev
  16. ├── application
  17. └── modules
  18. └── environment
  19. └── modules
  20. ├── kissmq
  21. ├── application
  22. └── modules
  23. └── environment
  24. └── modules
  25. └── kissprd
  26. ├── application
  27. └── modules
  28. └── environment
  29. └── modules
  30. 15 directories, 0 files

3、移动默认环境modules中的配置到kissprd对应的环境中

其中puppet和yum模块属于基础环境模块,motd属于应用环境模块

  1. [root@puppetmaster environments]# mv /etc/puppet/modules/puppet kissprd/environment/modules/
  2. [root@puppetmaster environments]# mv /etc/puppet/modules/yum kissprd/environment/modules/
  3. [root@puppetmaster environments]# mv /etc/puppet/modules/motd kissprd/application/modules/

4、复制manifests文件至kissprd环境中

  1. [root@puppetmaster environments]# cp /etc/puppet/manifests kissprd/ -r

复制完成后整个环境如下

  1. [root@puppetmaster environments]# tree kissprd/
  2. kissprd/
  3. ├── application
  4. └── modules
  5. └── motd
  6. ├── files
  7. └── etc
  8. └── motd
  9. ├── manifests
  10. └── init.pp
  11. └── templates
  12. ├── environment
  13. └── modules
  14. ├── puppet
  15. ├── files
  16. ├── manifests
  17. ├── config.pp
  18. ├── init.pp
  19. ├── install.pp
  20. ├── params.pp
  21. └── service.pp
  22. └── templates
  23. └── puppet.conf.erb
  24. └── yum
  25. ├── files
  26. ├── etc
  27. └── yum.conf
  28. └── PM-GPG-KEY
  29. ├── RPM-GPG-KEY-puppet-release
  30. ├── RPM-GPG-KEY-redhat-release-rhel5
  31. └── RPM-GPG-KEY-redhat-release-rhel6
  32. ├── manifests
  33. ├── config.pp
  34. ├── init.pp
  35. ├── install.pp
  36. └── params.pp
  37. └── templates
  38. └── manifests
  39. └── site.pp
  40. 20 directories, 17 files

5、删除掉默认环境manifests中site.pp文件内容

因为模块已经移除,其次默认环境production已经不再使用了。

  1. [root@puppetmaster environments]# >/etc/puppet/manifests/site.pp

6、创建fileserverconfig文件

  1. [root@puppetmaster ~]# cp /etc/puppet/fileserver.conf{,.kissdev}
  2. [root@puppetmaster ~]# cp /etc/puppet/fileserver.conf{,.kissqa}
  3. [root@puppetmaster ~]# cp /etc/puppet/fileserver.conf{,.kissprd}
  4. [root@puppetmaster ~]# ll /etc/puppet/
  5. total 88
  6. -rw-r--r-- 1 root root 2569 Jan 7 07:51 auth.conf
  7. -rw-r--r-- 1 root root 17 Mar 9 17:54 autosign.conf.bak
  8. drwxr-xr-x 5 root root 4096 Mar 27 22:33 environments
  9. -rw-r--r-- 1 root root 381 Jan 7 07:49 fileserver.conf
  10. -rw-r--r-- 1 root root 381 Mar 27 22:46 fileserver.conf.kissdev #指向kissdev环境
  11. -rw-r--r-- 1 root root 381 Mar 27 22:46 fileserver.conf.kissprd #指向kissmq环境
  12. -rw-r--r-- 1 root root 381 Mar 27 22:46 fileserver.conf.kissqa #指向kissdev环境
  13. drwxr-xr-x 2 root root 4096 Mar 25 05:23 manifests
  14. drwxr-xr-x 2 root root 4096 Mar 27 22:40 modules
  15. -rw-r--r-- 1 root root 1063 Mar 27 21:55 puppet.conf
  16. -rw-r--r-- 1 root root 853 Mar 9 00:48 puppet.conf.bak
  17. -rw-r--r-- 1 root root 42031 Mar 9 03:25 puppet.conf.out

7、重启puppetmaster服务

  1. [root@puppetmaster ~]# /etc/init.d/puppetmaster restart
  2. Stopping puppetmaster: [ OK ]
  3. Starting puppetmaster: [ OK ]

8、节点测试验证

  1. [root@agent1 ~]# >/etc/motd
  2. You have new mail in /var/spool/mail/root
  3. [root@agent1 ~]# puppet agent -t #默认请求的是production环境,由于此环境里面没有模块所有不更新
  4. info: Caching catalog for agent1_cert.kisspuppet.com
  5. info: Applying configuration version '1395931884'
  6. notice: Finished catalog run in 0.02 seconds
  7. [root@agent1 ~]# puppet agent -t --environment=kissprd #环境指向kissprd
  8. info: Caching catalog for agent1_cert.kisspuppet.com
  9. info: Applying configuration version '1395931962'
  10. notice: /Stage[main]/Motd/File[/etc/motd]/content:
  11. --- /etc/motd 2014-03-27 22:52:27.000000000 +0800
  12. +++ /tmp/puppet-file20140327-26204-29bst1-0 2014-03-27 22:52:44.000000000 +0800
  13. @@ -0,0 +1,3 @@
  14. +-- --
  15. +--------puppet test---------
  16. +-- --
  17. info: FileBucket got a duplicate file {md5}d41d8cd98f00b204e9800998ecf8427e
  18. info: /Stage[main]/Motd/File[/etc/motd]: Filebucketed /etc/motd to puppet with sum d41d8cd98f00b204e9800998ecf8427e
  19. notice: /Stage[main]/Motd/File[/etc/motd]/content: content changed '{md5}d41d8cd98f00b204e9800998ecf8427e' to '{md5}87ea3a1af8650395038472457cc7f2b1'
  20. notice: Finished catalog run in 0.68 seconds
  21. [root@agent1 ~]# cat /etc/motd
  22. -- --
  23. --------puppet test---------
  24. -- --

9、节点更改环境

如果节点是主动同步的方式,应该在puppet.conf文件中添加environment配置

  1. [root@agent1 ~]# vim /etc/puppet/puppet.conf
  2. ### config by puppet ###
  3. [main]
  4. logdir = /var/log/puppet
  5. rundir = /var/run/puppet
  6. ssldir = $vardir/ssl
  7. [agent]
  8. classfile = $vardir/classes.txt
  9. localconfig = $vardir/localconfig
  10. server = puppetmaster.kisspuppet.com
  11. certname = agent1_cert.kisspuppet.com
  12. runinterval = 10
  13. environment =kissprd #添加默认环境为kissprd

10、继续测试

  1. [root@agent1 ~]# puppet agent -t
  2. info: Caching catalog for agent1_cert.kisspuppet.com
  3. info: Applying configuration version '1395931962'
  4. notice: /Stage[main]/Motd/File[/etc/motd]/content:
  5. --- /etc/motd 2014-03-27 22:55:43.000000000 +0800
  6. +++ /tmp/puppet-file20140327-30010-8ada2g-0 2014-03-27 22:56:19.000000000 +0800
  7. @@ -0,0 +1,3 @@
  8. +-- --
  9. +--------puppet test---------
  10. +-- --
  11. info: FileBucket got a duplicate file {md5}d41d8cd98f00b204e9800998ecf8427e
  12. info: /Stage[main]/Motd/File[/etc/motd]: Filebucketed /etc/motd to puppet with sum d41d8cd98f00b204e9800998ecf8427e
  13. notice: /Stage[main]/Motd/File[/etc/motd]/content: content changed '{md5}d41d8cd98f00b204e9800998ecf8427e' to '{md5}87ea3a1af8650395038472457cc7f2b1'
  14. notice: /Stage[main]/Puppet::Config/File[/etc/puppet/puppet.conf]/content:
  15. --- /etc/puppet/puppet.conf 2014-03-27 22:56:14.000000000 +0800
  16. +++ /tmp/puppet-file20140327-30010-cmjg48-0 2014-03-27 22:56:19.000000000 +0800
  17. @@ -10,4 +10,3 @@
  18. server = puppetmaster.kisspuppet.com
  19. certname = agent1_cert.kisspuppet.com
  20. runinterval = 10
  21. - environment =kissprd
  22. info: FileBucket got a duplicate file {md5}43df60b1aa2638c5f10aa7e6be892b77
  23. info: /Stage[main]/Puppet::Config/File[/etc/puppet/puppet.conf]: Filebucketed /etc/puppet/puppet.conf to puppet with sum 43df60b1aa2638c5f10aa7e6be892b77
  24. notice: /Stage[main]/Puppet::Config/File[/etc/puppet/puppet.conf]/content: content changed '{md5}43df60b1aa2638c5f10aa7e6be892b77' to '{md5}8c67cb8c039bb6436556b91f0c6678c4'
  25. info: /Stage[main]/Puppet::Config/File[/etc/puppet/puppet.conf]: Scheduling refresh of Class[Puppet::Service]
  26. info: Class[Puppet::Service]: Scheduling refresh of Service[puppet]
  27. notice: /Service[puppet]/ensure: ensure changed 'stopped' to 'running'
  28. notice: /Service[puppet]: Triggered 'refresh' from 1 events
  29. notice: Finished catalog run in 0.68 seconds
  30. [root@agent1 ~]# cat /etc/motd
  31. -- --
  32. --------puppet test---------
  33. -- --

备注: 记得设置puppet模块中的puppet.conf.erb模板,否则会被还原哦。

后续问题

1、puppetmaster端有三套环境,那么如何管理呢,接下来就应该考虑版本控制系统了,这里已经有写了http://rsyslog.org/2013/11/16/svn-puppet/

2、后面讲的hiear中关于设置的变量对应到每个环境中是如何解决的。

关于多环境的部署有不理解的还可以参考书籍《精通Puppet配置管理工具》或者官网