开发环境和测试环境均是CentOS6.4 x86_64,ngxin的模板文件是在puppet模块下面templates目录中以”.erb”结尾的文件,puppet模板主要用于文件,例如各种服务的配置文件,相同的服务,不同的配置就可以考虑使用模板文件,例如Nginx和Apache的虚拟主机配置就可以考虑采用ERB模板,nginx的安装在这里建议用第三方yum源来安装,如果是用Nginx的官方源来安装nginx的话,我们可以查看下/etc/yum.repos.d/nginx.repo文件内容,如下所示:
1
2
3
4
5
|
[nginx] name=nginx repo baseurl=http: //nginx .org /packages/centos/ $releasever/$basearch/
gpgcheck=0 enabled=1 |
第二种方式就是通过createrepo自建自己的YUM源,这种方式更加宁活,我们可以在nginx官网去下载适合自己的rpm包,然后添加进自己的YUM源,在自动化运维要求严格的定制环境中,绝大多数运维同学都会选择这种方法。大家通过此种方式安装nginx以后会发现,确实比源码安装Nginx方便多了,像自动分配了运行nginx的用户nginx:nginx,Nginx的日志存放会自动保存在/var/log/nginx下,其工作目录为/etc/nginx,这一点跟源码编译安装的nginx区别很大,请大家在实验过程也注意甄别。
我比较关心的是ruby的版本和puppet的版本,结果显示如下:
1
2
3
4
|
[root@server manifests] # ruby -v
ruby 1.8.7 (2013-06-27 patchlevel 374) [x86_64-linux] [root@server manifests] # puppet -V
3.7.4 |
像Puppet的安装和其它初级知识点我这里就略过了,我直接贴上文件内容,/etc/puppet的文件结构如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
|-- auth.conf |-- fileserver.conf |-- manifests | |-- nodes | | |-- client.cn7788.com.pp | | `-- test .cn7788.com.pp
| `-- site.pp |-- modules | `-- nginx | |-- files | |-- manifests | | `-- init.pp | `-- templates | |-- nginx.conf.erb | `-- nginx_vhost.conf.erb `-- puppet.conf |
site.pp的文件内容如下:
1
2
3
4
|
import "nodes/*.pp"
Package { allow_virtual => false ,
} |
client.cn7788.com.pp的文件内容如下所示:
1
2
3
4
5
6
7
|
node 'client.cn7788.com' {
include nginx
nginx::vhost { 'client.cn7788.com' :
sitedomain => "client.cn7788.com" ,
rootdir => "client" ,
} } |
test.cn7788.com.pp的文件内容如下所示:
1
2
3
4
5
6
7
|
node 'test.cn7788.com' {
include nginx
nginx::vhost { 'test.cn7788.com' :
sitedomain => "test.cn7788.com" ,
rootdir => "test" ,
} } |
/etc/puppet/modules/nginx/manifests/init.pp文件内容如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
class nginx{ package{ "nginx" :
ensure =>present,
} service{ "nginx" :
ensure =>running,
require =>Package[ "nginx" ],
} file { "nginx.conf" :
ensure => present, mode => 644,owner => root,group => root, path => "/etc/nginx/nginx.conf" ,
content=> template( "nginx/nginx.conf.erb" ),
require=> Package[ "nginx" ],
} } define nginx::vhost($sitedomain,$rootdir) { file { "/etc/nginx/conf.d/${sitedomain}.conf" :
content => template( "nginx/nginx_vhost.conf.erb" ),
require => Package[ "nginx" ],
}
} |
/etc/puppet/modules/nginx/templates/nginx.conf.erb文件内容如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
user nginx; worker_processes 8; error_log /var/log/nginx/error .log warn;
pid /var/run/nginx .pid;
events { use epoll;
worker_connections 51200;
} http { include /etc/nginx/mime .types;
default_type application /octet-stream ;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"' ;
access_log /var/log/nginx/access .log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf .d/*.conf;
} |
/etc/puppet/modules/nginx/templates/nginx_vhost.conf.erb文件内容如下所示:
1
2
3
4
5
6
7
8
9
|
server { listen 80 ;
server_name <%= sitedomain %>; access_log / var /log/nginx/<%= sitedomain %>.access.log;
location / { root / var /www/<%= rootdir %>;
index index.php index.html index.htm; } } |
最后我们可以在节点名为client.cn7788.com和test.cn7788.com的机器验证效果,命令如下所示:
1
|
puppet agent -- test --server server.cn7788.com
|
部分执行命令结果如下:
1
2
3
4
5
|
Info: Computing checksum on file /etc/nginx/nginx .conf
Info: FileBucket got a duplicate file {md5}f7984934bd6cab883e1f33d5129834bb
Info: /Stage [main] /Nginx/File [nginx.conf]: Filebucketed /etc/nginx/nginx .conf to puppet with sum f7984934bd6cab883e1f33d5129834bb
Notice: /Stage [main] /Nginx/File [nginx.conf] /content : content changed '{md5}f7984934bd6cab883e1f33d5129834bb' to '{md5}6f57d21ca18f7256ef6c6ccd068505dc'
Notice: Finished catalog run in 15.47 seconds
|
为了方便大家下载和阅读,此模块我特的收录进了我的github仓库,地址为https://github.com/yuhongchun/avaliablity/