本文是基于puppet master-agent 3.8来开发的,可参考此链接进行搭建:https://www.jianshu.com/p/31c433df4d3d
由于nginx反代tomcat需要我们先安装jdk和tomcat才能反代成功,因此我们需要在puppet 上先开发jdk及tomcat 的安装模块,系统版本为Centos 7.4。
一、jdk模块
首先创建模块目录,不建议直接在/etc/puppet/modules下对模块进行修改开发,因为生产环境中可能会有主机自动调用相关的模块配置,我们当然不希望agent调用未开发完成的模块,因此通常建议在其他路径下进行模块的开发。开发测试完成后,再复制到对应的模块路径下。
[root@puppet-master ~]# mkdir module/jdk/{manifests,files,spec,lib,templates,tests} -pv
1、创建配置install.pp文件
[root@puppet-master ~]# vim module/jdk/manifests/install.pp
class jdk::install {
file{'jdk.tar':
path => '/usr/local/src/jdk-8u181-linux-x64.tar.gz',
ensure => present,
source => 'puppet:///jdk-files/jdk-8u181-linux-x64.tar.gz',
} ->
exec{'jdk':
command => 'tar zxf /usr/local/src/jdk-8u181-linux-x64.tar.gz -C /usr/local/src/',
unless => 'ls /usr/local/src/jdk1.8.0_181/',
path => '/bin:/sbin:/usr/sbin:/usr/bin',
notify => File['jdk.link'],
} ->
file{'jdk.link':
path => '/usr/local/jdk',
ensure => link,
target => '/usr/local/src/jdk1.8.0_181/',
}
}
2、创建配置env.pp文件
[root@puppet-master ~]# vim module/jdk/manifests/env.pp
class jdk::env {
file{'jdk.sh':
path => '/etc/profile.d/jdk.sh',
ensure => present,
source => 'puppet:///jdk-files/jdk.sh',
owner => "root",
group => "root",
mode => "0755",
}
}
3、创建配置init.pp文件
[root@puppet-master ~]# vim module/jdk/manifests/init.pp
class jdk {
include jdk::install
include jdk::env
}
include jdk
4、修改/etc/puppet/fileserver.conf文件
[root@puppet-master ~]# vim /etc/puppet/fileserver.conf
[jdk-files] #用于允许客户端访问puppet master下指定路径下的文件
path /etc/puppet/modules/jdk/files
allow *
#指定完后,把相关的配置文件及压缩包复制到/etc/puppet/modules/jdk/files目录下
[root@puppet-master module]# cp /etc/profile.d/jdk.sh /etc/puppet/modules/jdk/files/
[root@puppet-master module]# cp /usr/local/src/jdk-8u181-linux-x64.tar.gz /etc/puppet/modules/jdk/files/
5、复制jdk模块到/etc/puppet/modules目录下并添加节点配置
[root@puppet-master ~]# cp -a module/jdk /etc/puppet/modules/
[root@puppet-master ~]# vim /etc/puppet/manifests/site.pp
node 'puppet-agent' {
include jdk
}
二、配置tomcat模块
1、在/root/module/目录下创建tomcat模块目录
[root@puppet-master ~]# mkdir -pv module/tomcat/{templates,files,tests,spec,lib,manifests}
2、创建install.pp
[root@puppet-master ~]# vim module/tomcat/manifests/install.pp
file{'tomcat.tar':
path => '/usr/local/src/apache-tomcat-8.5.33.tar.gz',
ensure => present,
source => 'puppet:///tomcat-files/apache-tomcat-8.5.33.tar.gz',
require => Class['jdk'], #需要jdk来安装
} ->
exec{'tomcat':
command => 'tar zxf /usr/local/src/apache-tomcat-8.5.33.tar.gz -C /usr/local/src/',
unless => 'ls /usr/local/src/apache-tomcat-8.5.33',
path => '/bin:/sbin:/usr/sbin:/usr/bin',
} ->
file{'tomcat.link':
path => '/usr/local/tomcat8',
ensure => link,
target => '/usr/local/src/apache-tomcat-8.5.33',
}
user{'tomcat':
ensure => present,
name => 'tomcat',
shell => '/sbin/nologin',
}
}
3、创建init.pp文件
[root@puppet-master ~]# vim module/tomcat/manifests/init.pp
class tomcat{
include tomcat::install
}
include tomcat
4、修改etc/puppet/fileserver.conf配置文件并复制相关文件到指定files目录下
[root@puppet-master ~]# vim /etc/puppet/fileserver.conf
[tomcat-files]
path /etc/puppet/modules/tomcat/files
allow *
[root@puppet-master ~]# cp /usr/local/src/apache-tomcat-8.5.33.tar.gz /etc/puppet/modules/tomcat/files/
5、复制tomcat 模块到/etc/puppet/modules目录下
[root@puppet-master ~]# cp -a module/tomcat/ /etc/puppet/modules/
三、配置tomcat1 实例模块
1、创建tomcat1模块目录
[root@puppet-master ~]# mkdir -pv module/tomcat1/{templates,files,tests,spec,lib,manifests}
2、创建install.pp文件
[root@puppet-master ~]# vim module/tomcat1/manifests/install.pp
class tomcat1::install inherits tomcat1 {
exec{'tomcat1 dir':
command => 'mkdir -pv /usr/local/tomcat1/bin',
path => '/bin:/sbin:/usr/sbin:/usr/bin',
require => Class['tomcat'], #需要tomcat类来安装
} ->
exec{'tomcat1 install':
command => 'cp -a /usr/local/tomcat8/{conf,webapps,temp,work,logs} /usr/local/tomcat1/',
onlyif => 'ls /usr/local/tomcat8',
path => '/bin:/sbin:/usr/sbin:/usr/bin',
} ->
file{'tomcat1 startup':
path => '/usr/local/tomcat1/bin/startup.sh',
source => 'puppet:///tomcat1-files/startup.sh',
mode => 0755,
ensure => present,
} ->
file {'tomcat1 shutdown':
path => '/usr/local/tomcat1/bin/shutdown.sh',
source => 'puppet:///tomcat1-files/shutdown.sh',
mode => 0755,
ensure => present,
} ->
# file {'tomcat1 init.d': #centos 6 的启动脚本
# path => '/etc/init.d/tomcat1',
# source => 'puppet:///tomcat1-files/tomcat1',
# mode => 0755,
# ensure => present,
# } ->
file {'tomcat1 systemd': #centos 通过systemd 的unit文件来管理启动
path => '/usr/lib/systemd/system/tomcat1.service',
source => 'puppet:///tomcat1-files/tomcat1.service',
ensure => present,
} ->
file{'tomcat1 server.xml':
path => '/usr/local/tomcat1/conf/server.xml',
ensure => present,
source => 'puppet:///tomcat1-files/server.xml',
require => Exec['tomcat1 install'],
notify => Service['tomcat1'],
}
}
3、创建service.pp文件
[root@puppet-master ~]# vim module/tomcat1/manifests/service.pp
class tomcat1::service inherits tomcat1 {
service{'tomcat1':
ensure => running,
enable => true,
name => 'tomcat1',
hasrestart => true,
# require => File['tomcat1 init.d'],
require => File['tomcat1 systemd'],
subscribe => Class['tomcat1::install'],
}
}
4、创建init.pp文件
[root@puppet-master ~]# vim module/tomcat1/manifests/init.pp
class tomcat1{
include tomcat1::install,tomcat1::service
}
include tomcat1
5、修改/etc/puppet/fileserver.conf并复制创建相关的配置文件到指定目录
[root@puppet-master ~]# vim /etc/puppet/fileserver.conf
[tomcat1-files]
path /etc/puppet/modules/tomcat1/files
allow *
[root@puppet-master ~]# cp /usr/local/src/apache-tomcat-8.5.33/conf/server.xml module/tomcat1/files/
其余shutdown.sh、startup.sh、tomcat1、tomcat1.service 配置文件可以参考下述链接来创建生成。http://64bf94f3.wiz03.com/share/s/1ALVjP2XtAvw2TBE3Q0AiVGt3I1MfQ3vwk7M2-z4K10klwpW
5、复制tomcat1模块到/etc/puppet/modules目录
[root@puppet-master ~]# cp -a module/tomcat1/ /etc/puppet/modules/
四、创建nginx模块
1、创建nginx模块目录
[root@puppet-master ~]# mkdir module/nginx/{templates,tests,lib,manifests,spec,files} -pv
2、创建install.pp文件
[root@puppet-master ~]# vim module/nginx/manifests/install.pp
class nginx::install{
package{'nginx':
ensure => latest,
provider => yum,
require => Package['epel']
}
package{'epel':
ensure => latest,
provider => yum,
name => 'epel-release',
}
}
3、创建webproxy.pp文件
[root@puppet-master ~]# vim module/nginx/manifests/webproxy.pp
class nginx::webproxy{
file{'webproxy.conf':
path => '/etc/nginx/conf.d/webproxy.conf',
ensure => present,
source => 'puppet:///nginx-files/webproxy.conf',
}
}
4、创建service.pp文件
[root@puppet-master ~]# vim module/nginx/manifests/service.pp
class nginx::service inherits nginx {
service {'nginx':
ensure => running,
enable => true,
name => 'nginx',
hasrestart => true,
subscribe => Class['nginx::webproxy'],
}
}
5、创建init.pp文件
[root@puppet-master ~]# vim module/nginx/manifests/init.pp
class nginx {
include nginx::install,nginx::webproxy,nginx::install
}
include nginx
6、修改/etc/puppet/fileserver.conf并复制相关文件到指定目录
[root@puppet-master ~]# vim /etc/puppet/fileserver.conf
[nginx-files]
path /etc/puppet/modules/nginx/files
allow *
7、复制nginx模块到/etc/puppet/modules目录
[root@puppet-master ~]# cp -a module/nginx/ /etc/puppet/modules/
配置开发完后,pupput-master的树状架构如下图:
[root@puppet-master ~]# tree /etc/puppet/modules/
/etc/puppet/modules/
├── jdk
│ ├── files
│ │ ├── jdk-8u181-linux-x64.tar.gz
│ │ └── jdk.sh
│ ├── lib
│ ├── manifests
│ │ ├── env.pp
│ │ ├── init.pp
│ │ └── install.pp
│ ├── spec
│ ├── templates
│ └── tests
├── nginx
│ ├── files
│ │ └── webproxy.conf
│ ├── lib
│ ├── manifests
│ │ ├── init.pp
│ │ ├── install.pp
│ │ ├── service.pp
│ │ └── webproxy.pp
│ ├── spec
│ ├── templates
│ └── tests
├── tomcat
│ ├── files
│ │ └── apache-tomcat-8.5.33.tar.gz
│ ├── lib
│ ├── manifests
│ │ ├── init.pp
│ │ └── install.pp
│ ├── spec
│ ├── templates
│ └── tests
└── tomcat1
├── files
│ ├── server.xml
│ ├── shutdown.sh
│ ├── startup.sh
│ ├── tomcat1
│ └── tomcat1.service
├── lib
├── manifests
│ ├── init.pp
│ ├── install.pp
│ └── service.pp
├── spec
├── templates
└── tests
28 directories, 21 files