远程服务器批量管理

远程服务器批量管理,第1张

Puppet批量管理Nginx服务器

开发工具和接口测试是CentOS6.4x86_64,ngxin的模板文件是以“”结尾的文件。erb”,位于puppet控制模块下的模板文件目录中。puppet模板用于档案,比如各种服务项目的设备档案,同样的服务项目,不同的设备可以考虑应用模板档案。比如Nginx和Apache的虚拟主机配置可以考虑ERB模板。Nginx的安装建议在这里由第三方yum源码安装。如果nginx是nginx官方网络源码安装的,我们可以查询/etc/yum.repos.d/nginx.repo文件的内容,如下图:

[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,与源代码编译器安装的目录有很大不同。

我很关注ruby的版本和puppet的版本。数据显示如下:

[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

我这里忽略了布偶的安装等初中水平的知识点。我立即粘贴了文件的内容。/etc/puppet的文件结构如下:

|-- 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的文件内容如下:

import "nodes/*.pp" Package { allow_virtual => false, }

client.cn7788.com.pp文档的内容如下所示:

node 'client.cn7788.com' {   include nginx   nginx::vhost {'client.cn7788.com':   sitedomain => "client.cn7788.com" ,   rootdir => "client", } }

test.cn7788.com.pp文档的内容如下所示:

node 'test.cn7788.com' {   include nginx   nginx::vhost {'test.cn7788.com':   sitedomain => "test.cn7788.com" ,   rootdir => "test", } }

/etc/puppet/modules/nginx/manifests/init.PP文件的内容如下所示:

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的内容如下所示:

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的内容如下所示:

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的设备的实际效果。说明如下所示:

puppet agent --test --server server.cn7788.com

部分运行命令结果如下:

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}6f57d21ca18f7256ef7c7ccd068505dc' Notice: Finished catalog run in 15.47 seconds

为了方便大家免费下载阅读,我的github仓库里收录了这个控制模块。详细地址是https://github.com/yuhongchun/avaliablity/.

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zz/783632.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-04
下一篇 2022-05-04

发表评论

登录后才能评论

评论列表(0条)

保存