Puppet批量管理Nginx服务器

简介:

开发环境和测试环境均是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/








本文转自 抚琴煮酒 51CTO博客,原文链接:http://blog.51cto.com/yuhongchun/1624871,如需转载请自行联系原作者
目录
相关文章
|
7月前
|
缓存 负载均衡 JavaScript
Nginx:高性能Web服务器与反向代理利器
Nginx:高性能Web服务器与反向代理利器
366 110
|
7月前
|
缓存 负载均衡 前端开发
Nginx:高性能的Web服务器与反向代理利器
Nginx:高性能的Web服务器与反向代理利器
367 99
|
7月前
|
缓存 负载均衡 前端开发
Nginx:高性能Web服务器的核心引擎
Nginx:高性能Web服务器的核心引擎
250 47
|
6月前
|
Ubuntu 安全 应用服务中间件
详细指南:配置Nginx服务器在Ubuntu平台上
以上步骤涵盖了基本流程:从软件包管理器获取 Ngnix, 设置系统服务, 调整UFW规则, 创建并激活服务器块(也称作虚拟主机), 并进行了初步优化与加固措施。这些操作都是建立在命令行界面上,并假设用户具有必要权限(通常是root用户)来执行这些命令。每个操作都有其特定原因:例如,设置开机启动确保了即使重启后也能自动运行 Ngnix;而编辑server block则定义了如何处理进入特定域名请求等等。
362 18
|
6月前
|
Ubuntu 安全 应用服务中间件
详细指南:配置Nginx服务器在Ubuntu平台上
以上步骤涵盖了基本流程:从软件包管理器获取 Ngnix, 设置系统服务, 调整UFW规则, 创建并激活服务器块(也称作虚拟主机), 并进行了初步优化与加固措施。这些操作都是建立在命令行界面上,并假设用户具有必要权限(通常是root用户)来执行这些命令。每个操作都有其特定原因:例如,设置开机启动确保了即使重启后也能自动运行 Ngnix;而编辑server block则定义了如何处理进入特定域名请求等等。
579 17
|
5月前
|
弹性计算 运维 安全
阿里云轻量应用服务器与云服务器ECS啥区别?新手帮助教程
阿里云轻量应用服务器适合个人开发者搭建博客、测试环境等低流量场景,操作简单、成本低;ECS适用于企业级高负载业务,功能强大、灵活可扩展。二者在性能、网络、镜像及运维管理上差异显著,用户应根据实际需求选择。
422 10
|
5月前
|
运维 安全 Ubuntu
阿里云渠道商:服务器操作系统怎么选?
阿里云提供丰富操作系统镜像,涵盖Windows与主流Linux发行版。选型需综合技术兼容性、运维成本、安全稳定等因素。推荐Alibaba Cloud Linux、Ubuntu等用于Web与容器场景,Windows Server支撑.NET应用。建议优先选用LTS版本并进行测试验证,通过标准化镜像管理提升部署效率与一致性。
|
5月前
|
弹性计算 ice
阿里云4核8g服务器多少钱一年?1个月和1小时价格,省钱购买方法分享
阿里云4核8G服务器价格因实例类型而异,经济型e实例约159元/月,计算型c9i约371元/月,按小时计费最低0.45元。实际购买享折扣,1年最高可省至1578元,附主流ECS实例及CPU型号参考。
587 8
|
5月前
|
存储 监控 安全
阿里云渠道商:云服务器价格有什么变动?
阿里云带宽与存储费用呈基础资源降价、增值服务差异化趋势。企业应结合业务特点,通过阶梯计价、智能分层、弹性带宽等策略优化成本,借助云监控与预算预警机制,实现高效、可控的云资源管理。
|
5月前
|
弹性计算 运维 安全
区别及选择指南:阿里云轻量应用服务器与ECS云服务器有什么区别?
阿里云轻量应用服务器适合个人开发者、学生搭建博客、测试环境,易用且性价比高;ECS功能更强大,适合企业级应用如大数据、高流量网站。根据需求选择:轻量入门首选,ECS专业之选。
363 2

推荐镜像

更多