Chinaunix首页 | 论坛 | 博客
  • 博客访问: 96940
  • 博文数量: 34
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 350
  • 用 户 组: 普通用户
  • 注册时间: 2014-07-04 22:47
文章分类

全部博文(34)

文章存档

2017年(34)

我的朋友

分类: 服务器与存储

2017-03-13 07:43:16

模块:目录结构
       module_name/
       manifests/
              init.pp:包含一个与模块名称同名的类
              *.pp:一个清单文件通常只包含一个类,而且建议清单文件名与类名相同:nginx::web,文件名web.pp;web/
              *.pp
              访问路径:module_name::mainfest_file_name,module_name::subdir_name::manifest_file_name
              files/
              访问路径: puppet:///modules/module_name/file_name
              nginx_web/
                    puppet:///modules/module_name/subdir/file_name
              templates/
                    *.erb:使用模板函数template()装载并运行其中模块语言,运行后会生成静态文件:
                    访问路径及方式:template('module_name/template_name')
              lib/
                    插件
              tests/
                    当前模块的使用说明和样例
              spec/
                    为lib目录中的插件提供使用说明和样例:

              在模块目录下,通常还应该具几个文档:
                    LICENSE
                    Modulefile
                    README


    
          类:  命名的代码块:可以继承;
                  class  class_name  {
                        ... puppet code ...
                  }
              
                 class parent_name::subclass_name inherits parent_name {
                 
                  }

                 class grand_name::parent_name::subclass_name inherits grand_name::parent_name {

                  }

                  =>: 在子类覆盖父类中的资源
                  +>: 在子类中为父类中的资源新增额外的属性

                  带参数类: 建议参数要有默认值
                 
                  声明类:
                        include
                        require
                        class {'class_name':
                             para1 => value1,
                             para2 => value2,
                        }

                    
                       nginx模块:
                             nginx
                                   nginx包
                             nginx::web
                                   file
                                   service
                             nginx::rproxy
                                    file
                                   service
                      
                         定义节点:也需要在清单文件,文件名后缀为.pp:在master/agent所有节点清单文件入口为site.pp
                                node  'node_name'  {
                                       节点专用变量
                                       类声明
                                }
                        建议一类节点使用一个清单文件,所有的清单文件都在site.pp中使用import包含进来:
                       
                 模板:
                         语法:
                                 <%= Ruby Expression %>: 替换为表达式的值;
                                           <%= @processorcount %>

                                <% ruby code %>: 仅执行代码,不做任何替换:常用语条件判断或循环语句、设定变量以及在输出之前对数据进行处理:

                                <%# commit %>: 注释 
                              
                                 <%%: 输出<%
                                  %%>:  显示%>
                                
                                  调用模块变量: 变量完全限定名称

                                  迭代和条件判断

                        注意: 使用模板生成文件时,使用的文件属性为content
                                   content          => template('module_name/template_file_name')
                       
                   
                
1、查看puppet模块默认的路径
[root@localhost ~]# puppet agent --configprint modulepath
/etc/puppet/modules:/usr/share/puppet/modules    

2、进入puppet模块的主目录,然后创建几个目录文件
cd /etc/puppet/modules

[root@localhost modules]# mkdir -pv nginx/{manifests,files,templates}
mkdir: 已创建目录 "nginx"
mkdir: 已创建目录 "nginx/manifests"
mkdir: 已创建目录 "nginx/files"
mkdir: 已创建目录 "nginx/templates"

3、进入manifests目录,通过类的方式安装nginx
[root@localhost modules]# cd nginx/manifests/

1)、创建第一个清单文件
 [root@localhost manifests]# vim init.pp      
class nginx {
        package {'nginx':
              ensure => installed,
        }
}      

2)、执行该类的清单文件
[root@localhost manifests]# puppet apply -d -v -e 'include nginx'

3)、查看nginx包的安装情况
[root@localhost manifests]# rpm -q nginx
nginx-1.10.2-1.el6.x86_64


4、使用node方式安装nginx

1)、定义一个node1
[root@node1 ~]# vim local.pp
node 'node1.shamereedwine.com' {
           include nginx
}

2)、应用此local.pp
[root@node1 ~]# puppet apply local.pp
Notice: Compiled catalog for node1.shamereedwine.com in environment production in 0.74 seconds
Notice: /Stage[main]/Nginx/Package[nginx]/ensure: created
Notice: Finished catalog run in 20.81 seconds

3)、查看nginx是否安装上,看到已经安装上
[root@node1 ~]# rpm -q nginx
nginx-1.10.2-1.el6.x86_64

5、使用子类的方式安装并启动nginx
1)、创建配置文件
[root@node1 ~]# cd  /etc/puppet/modules/nginx/
[root@node1 nginx]# cp /etc/nginx/nginx.conf files/nginx.web.conf
[root@node1 nginx]# cp /etc/nginx/nginx.conf files/nginx.rproxy.conf

2)、修改子类的配置文件
cd  /etc/puppet/modules/nginx/files
[root@node1 files]# vim nginx.web.conf
把worker_connections改为10240

[root@node1 files]# vim nginx.rproxy.conf
把worker_connections改为2048

3)、创建第一个子类
cd  /etc/puppet/modules/nginx/manifests

vim web.pp
class nginx::web inherits nginx {
        file {'nginx.conf':
                ensure  => file,
                source  => "puppet:///modules/nginx/nginx.web.conf",
                path    => '/etc/nginx/nginx.conf',
                require => Package['nginx'],
                mode    => '0644',
                notify  => Service['nginx'],
        }
 
        service {'nginx':
                ensure => true,
                enable => true,
                restart => '/etc/init.d/nginx reload',
        }
}

4)、先卸载nginx和查看80端口是否被占用
[root@node1 manifests]# yum remove nginx

[root@node1 manifests]# ss -tnlp|grep 80

5)、应用此子类
[root@node1 manifests]# puppet apply -d -v -e 'include nginx::web'

6)、查看nginx进程已经启动
[root@node1 manifests]# ps -ef|grep nginx
root       7575      1  0 07:09 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx      7576   7575  0 07:09 ?        00:00:00 nginx: worker process                   
nginx      7577   7575  0 07:09 ?        00:00:00 nginx: worker process                   
root       7589   6722  0 07:11 pts/0    00:00:00 grep nginx

7)、查看配置文件nginx.conf配置文件是否被替换,从下面看配置文件已经被替换
cd /etc/nginx
[root@node1 nginx]# cat nginx.conf|grep worker_connections
    worker_connections  10240;

8)、定义第二个子类,

cd  /etc/puppet/modules/nginx/manifests

cp web.pp rproxy.pp

class nginx::rproxy inherits nginx {
        file {'nginx.conf':
                ensure  => file,
                source  => "puppet:///modules/nginx/nginx.rproxy.conf",
                path    => '/etc/nginx/nginx.conf',
                require => Package['nginx'],
                mode    => '0644',
                notify  => Service['nginx'],
        }
 
        service {'nginx':
                ensure => true,
                enable => true,
                restart => '/etc/init.d/nginx reload',
        }
}

9)、手动停用nginx并删除nginx和删除nginx配置文件的目录

[root@node1 manifests]# service nginx stop
停止 nginx:                                               [确定]
[root@node1 manifests]# yum remove nginx
[root@node1 manifests]# rm -rf /etc/nginx/

10)、使用站点清单的方式
vim local.pp
node 'node1.shamereedwine.com' {
           include nginx::rproxy
}

11)、应用此站点清单
[root@node1 ~]# puppet apply -d -v local.pp

12)、查看服务启动情况和配置文件
[root@node1 ~]# service nginx status
nginx (pid  7877) 正在运行...

[root@node1 ~]# cat /etc/nginx/nginx.conf|grep worker_connections
    worker_connections  2048;

6、使用模板安装nginx

1)、删除web和proxy的两个配置文件
cd  /etc/puppet/modules/nginx/files

[root@node1 files]# rm -i nginx.rproxy.conf  nginx.web.conf
rm:是否删除普通文件 "nginx.rproxy.conf"?y
rm:是否删除普通文件 "nginx.web.conf"?y

2)、复制nginx的两个默认的配置文件到当前的文件夹里并重新命名
[root@node1 files]# cp /etc/nginx/conf.d/default.conf nginx.web.conf
[root@node1 files]# cp /etc/nginx/conf.d/default.conf nginx.rproxy.conf

3)、编辑nginx.web.conf,修改server_name参数
[root@node1 files]# vim nginx.web.conf

[root@node1 files]# cat nginx.web.conf |grep server_name
    server_name  node1.shamereedwine.com;

4)、编辑nginx.rproxy.conf,修改proxy_pass参数
[root@node1 files]# cat nginx.rproxy.conf |grep proxy_pass
        proxy_pass

5)、定义一个主配置文件
[root@node1 nginx]# cp /etc/nginx/nginx.conf   templates/

6)、修改主配置文件,把worker_processes定义为模板
cd  templates
vim nginx.conf

[root@node1 templates]# cat nginx.conf |grep worker_processes
worker_processes <%= @processorcount %>;

7)、把该模板改为erb文件的格式
[root@node1 templates]# mv nginx.conf nginx.conf.erb

8)、回到manifests清单当中
[root@node1 nginx]# cd manifests/
[root@node1 manifests]# ls
init.pp  rproxy.pp  web.pp

9)、定义父类,init.pp
[root@node1 manifests]# vim init.pp
class nginx {
        package {'nginx':
              ensure => installed,
        }

        file {'nginx.conf':
               ensure => file,
               content => template('nginx/nginx.conf.erb'),
               path   => '/etc/nginx/nginx.conf',
               require => Package['nginx'],
               mode    => '0644',
        }

}

10)、定义web.pp
[root@node1 manifests]# vim web.pp
class nginx::web inherits nginx {
        file {'nginx.web.conf':
                ensure  => file,
                source  => "puppet:///modules/nginx/nginx.web.conf",
                path    => '/etc/nginx/conf.d/default.conf',
                require => Package['nginx'],
                mode    => '0644',
        }
 
        service {'nginx':
                ensure => true,
                enable => true,
                restart => '/etc/init.d/nginx reload',
                subscribe     => File['nginx.conf','nginx.web.conf'],
        }
}

11)、定义rproxy.pp

[root@node1 manifests]# vim rproxy.pp
class nginx::rproxy inherits nginx {
        file {'nginx.rproxy.conf':
                ensure  => file,
                source  => "puppet:///modules/nginx/nginx.rproxy.conf",
                path    => '/etc/nginx/conf.d/default.conf',
                require => Package['nginx'],
                mode    => '0644',
        }
 
        service {'nginx':
                ensure => true,
                enable => true,
                restart => '/etc/init.d/nginx reload',
                subscribe => File['nginx.conf','nginx.rproxy.conf'],
        }
}

12)、停用并删除nginx包和目录

[root@node1 manifests]# service nginx stop
停止 nginx:                                               [确定]

[root@node1 manifests]# yum remove nginx

[root@node1 manifests]# rm -rf /etc/nginx/

13)、应用定义的模板

[root@node1 manifests]# puppet apply -d -v -e 'include nginx::web'

14)、查看安装并启动的nginx

[root@node1 manifests]# service nginx status
nginx (pid  2610) 正在运行...
[root@node1 manifests]# ps -ef|grep nginx
root       2610      1  0 05:12 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx      2611   2610  0 05:12 ?        00:00:00 nginx: worker process                   
nginx      2612   2610  0 05:12 ?        00:00:00 nginx: worker process                   
root       2630   1613  3 05:13 pts/0    00:00:00 grep nginx

15)、查看配置文件是否已被修改
[root@node1 conf.d]# cat /etc/nginx/conf.d/default.conf |grep server_name
    server_name  node1.shamereedwine.com;









阅读(1242) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~