puppet一鍵部署lnmthtml
安裝包下載路徑http://down.51cto.com/data/2290539java
yum install ./*.rpm
node
puppet模塊文件路徑nginx
mkdir -pv /etc/puppet/modules/{openjdk,nginx,tomcat,mariadb}/{manifests,files,templates,lib,tests,spec} cd /etc/puppet/modules
openjdk模塊web
vim openjdk/manifests/init.pp class openjdk($version='1.8.0') { package{"java-$version-openjdk-devel": ensure => installed, } file{'javahome': ensure => file, path => '/etc/profile.d/javahome.sh', source => 'puppet:///modules/openjdk/javahome.sh', } }
java運行路徑文件數據庫
vim /etc/puppet/modules/openjdk/files/javahome.sh export JAVA_HOME=/usr
puppet apply -v -e 'include openjdk'vim
這一步就提供完了openjdkcentos
Tomcat服務模塊瀏覽器
vim tomcat/manifests/init.pp class tomcat{ package{'tomcat': ensure => installed, } -> service{'tomcat': ensure => running, enable => true, restart => '/usr/bin/systemctl restart tomcat', } } class tomcat::tomcatweb inherits tomcat { package{'tomcat-webapps': ensure => installed, } package{'tomcat-admin-webapps': ensure => installed, } Service['tomcat']{ subscribe => [ Package['tomcat-webapps'],Package['tomcat-admin-webapps'] ], } }
puppet apply -v -e 'include tomcat::tomcatweb'tomcat
這一步這個執行完以後能夠打開瀏覽器訪問http://ip:8080,查看頁面
facter查看支持所有變量
文本文件中內嵌變量替換機制:
<%= @VARIABLE_NAME %>
獲取安裝包和配置文件
cd /etc/puppet/modules/nginx/files wget http://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.10.2-1.el7.ngx.x86_64.rpm yum install ./nginx-1.10.2-1.el7.ngx.x86_64.rpm
獲取default.conf配置文件,並修改代理配置
cp /etc/nginx/conf.d/default.conf ./ sed -i 's@.*/usr/share/nginx/html;@proxy_pass http://127.0.0.1:8080;@' nginx/files/default.conf
獲取nginx.conf.erb模板,並配置worker進程數量等於cpu的數量
cd ../templates/ cp /etc/nginx/nginx.conf ./ sed -i 's@worker_processes.*@worker_processes <%= processorcount %>;@' nginx.conf.erb
Nginx服務器模塊
vim nginx/manifests/init.pp class nginx{ file{'nginxpkg': path => '/tmp/nginx-1.10.2-1.el7.ngx.x86_64.rpm', ensure => file, } -> exec{'nginx': unless => '/usr/bin/rpm -q nginx', user => 'root', } -> file{'nginx.conf': path => '/etc/nginx/nginx.conf', ensure => file, content => template('nginx/nginx.conf.erb'), require => Exec['nginx'], } service{'nginx': ensure => running, enable => true, restart => '/usr/sbin/nginx -s reload', } } class nginx::proxy inherits nginx{ file{'default.conf': path => '/etc/nginx/conf.d/default.conf', ensure => file, source => 'puppet:///modules/nginx/default.conf', require => Exec['nginx'], } Service['nginx']{ subscribe => [ File['nginx.conf'],File['default.conf'] ], } }
puppet apply -v -e 'include nginx::proxy'
這一步就提供好了Nginx的反向代理,能夠打開瀏覽器訪問http://ip,查看頁面
數據庫節點模塊
vim mariadb/manifests/init.pp class mariadb{ package{'mariadb-server': ensure => installed, } -> service{'mariadb': ensure => running, enable => true, restart => '/usr/bin/yum restart mariadb.service', } }
puppet apply -v -e 'include mariadb'
部署大量節點配置
puppet是強依賴於域名解析的,個人hosts文件內容以下
vim /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 172.16.29.1 centos6.org c6 172.16.29.2 centos7.org c7 172.16.29.10 node1.org n1 172.16.29.20 node2.org n2 172.16.29.30 node3.org n3 172.16.29.40 node4.org n4
主節點
開啓服務器
systemctl start puppetmaster.service
添加配置文件
vim /etc/puppet/manifests/site.pp #這個配置能夠匹配一個主機centos7.oeg node 'centos7.org' { include nginx::proxy } #這個配置能夠配置多個node主機 node /node[12]\.org/' { include tomcat::tomcatweb }
從節點啓動從節點服務
puppet agent --server node3.org -v
主節點查看證書
puppet cert list
主節點簽署證書
puppet cert sign centos7.org puppet cert sign node1.org puppet cert sign node2.org
從節點重啓從節點服務
puppet agent --server node3.org -v
從節點永久自動鏈接master的方法,在agent配置段中添加一行
vim /etc/puppet/puppet.conf master = node3.org
多環境配置
多環境的意思是,打個比方一個公司有三個環境,一個開發人員測試本身程序的環境,一個測試人員測試程序的環境,還有一個是提供業務的環境。每一個環境的配置可能不同,這裏咱們使用puppet提供三種不一樣的環境。
master節點node3提供多環境須要的文件及目錄
cd /etc/puppet mkdir -pv environment/{production,development,testing}/{manifests,moudules} cp -a modules/* environments/production/moudules/ cp -a modules/* environments/development/moudules/ cp -a modules/* environments/testing/moudules/ #每一個環境使用模塊時,使用的是/etc/puppet/modules/目錄下的模塊,因此咱們定義模塊時要定義的靈活一些
master節點node3 在main配置段中添加以下內容,重啓puppetmaster
vim /etc/puppet/puppet.conf environmentpath = $confdir/environments systemctl restart puppetmaster.service
master節點提供安裝哪些模塊
vim environments/production/manifests/site.pp node 'node2.org' { include nginx::proxy } vim environments/development/manifests/site.pp node 'node2.org' { include openjdk include tomcat::tomcatweb } vim environments/development/manifests/site.pp node 'node1.org' { include openjdk include tomcat::tomcatweb } vim environments/testing/manifests/site.pp node 'centos7.org' { include mariadb }
node2的agent配置段中添加以下內容,並啓動
vim /etc/puppet/puppet.conf #指向主節點 server = node3.org listen = true systemctl start puppetagent.service
安裝後的截圖以下,只監聽了80端口說明,使用了production配置環境,若使用development環境會監聽80,8080
node1的agent配置段中添加以下內容,並啓動
vim /etc/puppet/puppet.conf #指向主節點 server = node3.org listen = true environment = development systemctl start puppetagent.service
centos7的agent配置段中添加以下內容,並啓動
vim /etc/puppet/puppet.conf #指向主節點 server = node3.org listen = true environment = testing systemctl start puppetagent.service
puppet的kick的使用
以前puppet工做時都是agent向master請求數據,而且是每30min請求一次,當咱們遇到緊急狀況,須要當即變動線上業務的時候,咱們就須要使用kick,由master主動通知agent,獲取配置信息。
從節點node2在path /以前添加一個認證配置段
vim /etc/puppet/auth.conf path /run method save auth any allow node3.org
從節點node2 在agent配置段中添加監聽
vim /etc/puppet/ puppet.conf [agent] listen = true server = node3.org
主節點編輯
vim environments/production/manifests/site.pp node 'node2.org' { include nginx::proxy include mariadb }
主節點推服務
puppet kick node2.org
node2又監聽3306
總結
puppetmater首先提供多個主機須要的模塊,各個模塊,每一個模塊中有多個類,類中定義多個服務;而後同時還提供每一個主機使用不一樣的模塊提供不一樣的服務,而且能夠把主機分類體提供配置。同時還可使用master使用kick主動通知agent安裝特定的模塊。