构建实战之Nginx代理Tomcat
[root@node1 modules]# mkdir /etc/puppet/modules/{tomcat,nginx}/{manifests,files,templates,spec,tests,lib} -pv
Tomcat
编辑Tomcat模块
[root@node1 modules]# vim tomcat/manifests/init.pp
class tomcat {
package{'tomcat':
ensure => latest,
}
package{'tomcat-webapps':
ensure => latest,
}
file{'tomcat':
path => '/etc/sysconfig/tomcat',
source => 'puppet:///modules/tomcat/tomcat',
owner => root,
group => root,
mode => '644',
require => Package['tomcat'],
}
file{'server.xml':
path => '/etc/tomcat/server.xml',
source => 'puppet:///modules/tomcat/server.xml',
owner => root,
group => tomcat,
mode => '644',
require => Package['tomcat'],
}
service{'tomcat':
ensure => running,
enable => true,
subscribe => [ File['tomcat'], File['server.xml'] ],
}
}
[root@node1 modules]# vim tomcat/manifests/manager.pp
class tomcat::manager inherits tomcat {
package{'tomcat-admin-webapps':
ensure => latest
}
file{'tomcat-users.xml':
path => '/etc/tomcat/tomcat-users.xml',
source => 'puppet:///modules/tomcat/tomcat-users.xml',
owner => root,
group => tomcat,
mode => '640',
require => Package['tomcat']
}
Service['tomcat']{
subscribe +> File['tomcat-users.xml']
}
}
复制并编辑所需要的配置文件
[root@node1 modules]# scp 172.16.252.82:/etc/sysconfig/tomcat tomcat/files/
[root@node1 modules]# vim tomcat/files/tomcat 编辑修改tomcat的环境参数
JAVA_OPTS="-Xms512m -Xmx512M" 所使用的堆内存大小
[root@node1 modules]# scp 172.16.252.82:/etc/tomcat/{server.xml,tomcat-users.xml} tomcat/files/
[root@node1 modules]# vim tomcat/files/tomcat-users.xml \\定义manager的管理界面
<role rolename="manager-gui"/>
<user username="tomcat" password="tomcat" roles="manager-gui"/>
[root@node1 modules]# puppet apply -v -e 'include tomcat::manager'
Info: Loading facts in /etc/puppet/modules/puppi/lib/facter/last_run.rb
Info: Loading facts in /etc/puppet/modules/puppi/lib/facter/puppi_projects.rb
Info: Loading facts in /etc/puppet/modules/puppi/lib/facter/windows_common_appdata.rb
Info: Loading facts in /etc/puppet/modules/stdlib/lib/facter/facter_dot_d.rb
Info: Loading facts in /etc/puppet/modules/stdlib/lib/facter/package_provider.rb
Info: Loading facts in /etc/puppet/modules/stdlib/lib/facter/pe_version.rb
Info: Loading facts in /etc/puppet/modules/stdlib/lib/facter/puppet_settings.rb
Info: Loading facts in /etc/puppet/modules/stdlib/lib/facter/root_home.rb
Info: Loading facts in /etc/puppet/modules/stdlib/lib/facter/service_provider.rb
Notice: Compiled catalog for node1.danran.com in environment production in 0.18 seconds
Warning: The package type's allow_virtual parameter will be changing its default value from false to true in a future release. If you do not want to allow virtual packages, please explicitly set allow_virtual to false.
(at /usr/share/ruby/vendor_ruby/puppet/type.rb:816:in `set_default')
Info: Applying configuration version '1506014294'
Info: /Stage[main]/Tomcat::Manager/File[tomcat-users.xml]: Filebucketed /etc/tomcat/tomcat-users.xml to puppet with sum 5a9a6d35473573a12dc0d5da945907f4
Notice: /Stage[main]/Tomcat::Manager/File[tomcat-users.xml]/content: content changed '{md5}5a9a6d35473573a12dc0d5da945907f4' to '{md5}ea891a0415069c772378e1ba57326c1c'
Info: /Stage[main]/Tomcat::Manager/File[tomcat-users.xml]: Scheduling refresh of Service[tomcat]
Info: /Stage[main]/Tomcat/File[tomcat]: Filebucketed /etc/sysconfig/tomcat to puppet with sum 758c565e1e61a073e87294f9326e5e99
Notice: /Stage[main]/Tomcat/File[tomcat]/content: content changed '{md5}758c565e1e61a073e87294f9326e5e99' to '{md5}0371edad449a896cac5b7ea2149582ae'
Info: /Stage[main]/Tomcat/File[tomcat]: Scheduling refresh of Service[tomcat]
Notice: /Stage[main]/Tomcat/Service[tomcat]: Triggered 'refresh' from 2 events
Notice: Finished catalog run in 1.57 seconds
[root@node1 modules]# ss -ntl \\验证监听端口是否打开
浏览器键入http://172.16.252.184:8080/manager验证manager管理界面是否部署
编辑Nginx模块
[root@node1 modules]# vim nginx/manifests/init.pp
class nginx {
package{'nginx':
ensure => latest
} ->
service{'nginx':
ensure => running,
enable => true
}
}
nginx的web页面模块
[root@node1 modules]# vim nginx/manifests/web.pp
[root@node1 modules]# vim nginx/manifests/web.pp
class nginx::web($port=8088) inherits nginx {
file{'web.conf':
path => '/etc/nginx/conf.d/web.conf',
content => template('nginx/web.conf.erb')
}
file{'/ngxdata/html':
ensure => directory
}
file{'index.html':
ensure => file,
path => '/ngxdata/html/index.html',
source => 'puppet:///modules/nginx/index.html',
require => File['/ngxdata/html']
}
Service['nginx'] {
subscribe => File['web.conf']
}
}
nginx的proxy模块
[root@node1 modules]# vim nginx/manifests/proxy.pp
class nginx::proxy($proxy_port=8088) inherits nginx {
file{'proxy.conf':
path => '/etc/nginx/conf.d/proxy.conf',
content => template('nginx/proxy.conf.erb'),
}
Service['nginx'] {
subscribe => File['proxy.conf']
}
}
编辑Nginx web应用的配置文件的模板文件
[root@node1 modules]# vim nginx/templates/web.conf.erb
server {
listen <%= @port %>;
server_name <%= @fqdn %>;
location /
root /ngxdata/html;
}
}
编辑web的测试页
[root@node1 modules]# vim nginx/files/index.html
<h1> Nginx ok </h1>
编辑Nginx proxy应用的配置文件的模板文件
[root@node1 modules]# vim nginx/templates/proxy.conf.erb
server {
listen <%= @proxy_port %>;
server_name <%= @fqdn %>;
location / {
proxy_pass http://172.16.252.184:8080/;
}
}
调用nginx::proxy模块
[root@node1 modules]# puppet apply -v -e 'include nginx::proxy'
Info: Loading facts in /etc/puppet/modules/puppi/lib/facter/last_run.rb
Info: Loading facts in /etc/puppet/modules/puppi/lib/facter/puppi_projects.rb
Info: Loading facts in /etc/puppet/modules/puppi/lib/facter/windows_common_appdata.rb
Info: Loading facts in /etc/puppet/modules/stdlib/lib/facter/facter_dot_d.rb
Info: Loading facts in /etc/puppet/modules/stdlib/lib/facter/package_provider.rb
Info: Loading facts in /etc/puppet/modules/stdlib/lib/facter/pe_version.rb
Info: Loading facts in /etc/puppet/modules/stdlib/lib/facter/puppet_settings.rb
Info: Loading facts in /etc/puppet/modules/stdlib/lib/facter/root_home.rb
Info: Loading facts in /etc/puppet/modules/stdlib/lib/facter/service_provider.rb
Warning: Config file /etc/puppet/hiera.yaml not found, using Hiera defaults
Notice: Compiled catalog for node1.danran.com in environment production in 0.20 seconds
Warning: The package type's allow_virtual parameter will be changing its default value from false to true in a future release. If you do not want to allow virtual packages, please explicitly set allow_virtual to false.
(at /usr/share/ruby/vendor_ruby/puppet/type.rb:816:in `set_default')
Info: Applying configuration version '1506016338'
Notice: /Stage[main]/Nginx/Package[nginx]/ensure: created
Notice: /Stage[main]/Nginx::Proxy/File[proxy.conf]/ensure: defined content as '{md5}b43ab8721cac73255395cbfe5eba6be7'
Info: /Stage[main]/Nginx::Proxy/File[proxy.conf]: Scheduling refresh of Service[nginx]
Notice: /Stage[main]/Nginx/Service[nginx]/ensure: ensure changed 'stopped' to 'running'
Info: /Stage[main]/Nginx/Service[nginx]: Unscheduling refresh on Service[nginx]
Notice: Finished catalog run in 7.64 seconds
测试proxy.conf文件是否复制成功,且变量替换成功
[root@node1 modules]# cat /etc/nginx/conf.d/proxy.conf
server {
listen 8088;
server_name node1.danran.com;
location / {
proxy_pass http://172.16.252.184:8080/;
}
}
浏览器访问http://172.16.252.184:8088/也可正常访问
mariadb模块中的清单文件示例
class mariadb($datadir='/var/lib/mysql') {
package{'mariadb-server':
ensure => installed,
}
file{"$datadir":
ensure => directory,
owner => mysql,
group => mysql,
require => [ Package['mariadb-server'], Exec['createdir'], ],
}
exec{'createdir':
command => "mkdir -pv $datadir",
require => Package['mariadb-server'],
path => '/bin:/sbin:/usr/bin:/usr/sbin',
creates => “$datadir",
}
file{'my.cnf':
path => '/etc/my.cnf',
content => template('mariadb/my.cnf.erb'),
require => Package['mariadb-server'],
notify => Service['mariadb'],
}
service{'mariadb':
ensure => running,
enable => true,
require => [ Exec['createdir'], File["$datadir"], ],
}
}