puppet自動化管理工具
基于ruby語言開發的自動化管理工具,以c/s模式獨立運作
puppet工作原理
1:用戶端puppetd調用facter,facter會探測出本機的一些資訊,然後puppetd把這些資訊發送到服務端
2:服務端的puppetmaster監測到用戶端的主機名,然後會到manifest裡面對應的node配置,然後對這段内容進行解析,facter送過來的資訊可以作為變量進行處理的,node牽涉到的
代碼才會解析,其他的代碼不會解析,解析過程:文法檢查,然後生成一個中間的僞代碼,然後在把僞代碼發給客戶機
3:用戶端收到僞代碼之後開始執行,用戶端在把執行結果發送給服務端
4:服務端再把用戶端的執行結果寫入日志
5:puppet服務端和用戶端是通過ssl隧道通信的,用戶端安裝完成後,需要向服務端申請證書
puppet主要資源
1:file主要負責管理檔案
2:package 軟體包的安裝管理
3:service 系統服務的管理
4:cron 配置計劃任務
5:exec 遠端執行運作指令
puppet安裝
192.168.110.130 test_server_110_130 (服務端)
192.168.110.131 test_server_110_131 (用戶端)
1:添加hosts檔案
192.168.110.130 test_server_110_130
192.168.110.131 test_server_110_131
2:服務端和用戶端
rpm -Uvh http://yum.puppetlabs.com/el/6/products/x86_64/puppetlabs-release-6-1.noarch.rpm
3:服務端
yum -y install puppet-server
/etc/init.d/puppetmaster restart
4:用戶端
yum -y install puppet
/etc/init.d/puppet restart
5:服務端配置檔案
cd /etc/puppet
auth.conf (認證配置檔案)
environments (環境變量的配置)
manifests (主配置目錄)
modules (主配置子產品)
puppet.conf (主配置檔案)
fileserver.conf (主要用于檔案傳輸)
6:用戶端配置檔案
7:用戶端向服務端請求證書
puppet agent --server 服務端主機名 --test
8:服務端向用戶端頒發證書
puppet cert --list(檢視沒有頒發的證書)
puppet cert --list --all(檢視頒發的證書)
puppet cert -s 用戶端主機名
file主要負責管理檔案
1:服務端
cd /etc/puppet/manifests
vi site.pp(配置檔案寫法)
node default {
file {
"/tmp/2016test.txt":
content => "hello world";
}
}
2:用戶端同步
package 軟體包的安裝管理
package {
["screen","ntp"]:
allow_virtual => false,
ensure => "installed(安裝)";
"pppoe(包名)":
ensure => "absent(解除安裝)";
}
service 系統服務的管理
service {
"httpd":
ensure => "running";
"nfs":
ensure => "stopped";
}
cron 配置計劃任務
1:伺服器
cron {
"ntpdate":
command => "/usr/sbin/ntpdate pool.ntp.org",
user => "root",
hour => "0",
minute => "0",
}
檔案推送
vi fileserver.conf
添加三行
[files]
path /etc/puppet/files (推送目錄,要先存在此目錄)
allow *
vi site.pp
"/data/sh/auto_ssh.sh":
source => "puppet://服務端主機名/files/auto_ssh.sh",
group => "root",
owner => "root",
mode => "755",
}
exec 遠端執行運作指令
exec {
"/tmp/auto_ssh.sh":
cwd => "/tmp",
path => ["/usr/bin","/usr/sbin","/bin","/bin/bash"],
不更新就不執行
exec {
"/linux/shell/echo.sh":
cwd => "/linux/shell",
path => ["/usr/bin","/usr/sbin","/bin","/bin/sh"],
subscribe => File["/linux/shell/echo.sh"], #定義資源和refreshonly一起使用,如果檔案更新才執行
refreshonly => true,
puppet管理以及維護
1)puppet自動認證
手動删除證書
用戶端
rm -rf /var/lib/puppet/ssl/*
服務端删除證書
puppet cert --clean test-server-31-131-web
在服務端puppet.conf的main配置下添加autosign = true(服務端自動頒發證書)
重新開機puppetmaster,用戶端直接同步
2)用戶端自動同步預設時間為半小時
vi /etc/sysconfig/puppet
PUPPET_SERVER=test-server-31-130-web 定義服務端
PUPPET_PORT=8140 用戶端監聽的端口
PUPPET_LOG=/var/log/puppet/puppet.log 用戶端puppet同步日志
PUPPET_EXTRA_OPTS=--waitforcert=500 證書傳回等待時間
将用戶端自動同步時間調整
在/etc/puppet/puppet.conf中agent下添加
runinterval = 30 (自動同步時間調整為30s)
重新開機puppet,觀察日志,可以看到自動同步已經生效
tail -f /var/log/puppet/puppet.log
Sat Mar 18 14:47:27 +0800 2017 Puppet (notice): Finished catalog run in 0.62 seconds
Sat Mar 18 14:47:57 +0800 2017 Puppet (notice): Finished catalog run in 0.77 seconds
Sat Mar 18 14:48:26 +0800 2017 Puppet (notice): Finished catalog run in 0.48 seconds
也可以設定成never(不主動同步)
3)服務端主動推送
啟動用戶端監聽端口
listen = true(服務端的監聽端口為8140,用戶端的監聽端口為8139)
在用戶端進行對服務端的授權,編輯配置檔案
vi /etc/puppet/namespaceauth.conf
[puppetrunner]
allow *
vi /etc/puppet/auth.conf(要添加在path /上邊)
path /run
method save
重新開機用戶端
在服務端進行推送
puppet kick -d test-server-31-131-web host host(可以推送多台機器,多台機器用空格分開,推送成功的提示資訊code 0)
也可以通過IP清單進行推送,将主機名加入到ip.list下(一行一個)
puppet kick -d `cat ip.list`
puppet自定義子產品
1:将同步時間的計劃任務更改為每五小時執行
在服務端/etc/puppet/modules/下建立ntp目錄
/etc/puppet/modules/ntp/manifests建立init.pp
class ntp { #class為自定義子產品名
path => "/bin:/sbin:/bin/sh:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin"
"auto change crontab ntp config":
command => "sed -i 's/0/*\/5/2' /var/spool/cron/root",
2:引用ntp子產品
在/etc/puppet/manifests下需要建立兩個檔案
2.1:cat modules.pp
import "ntp"
2.2:cat nodes.pp
include ntp
2.3:在site.pp裡導入這兩個子產品
cat site.pp
import "modules.pp"
import "nodes.pp"
puppet+svn自動送出部署
1:svn的建構
wget https://archive.apache.org/dist/subversion/subversion-1.7.10.tar.bz2
wget http://www.sqlite.org/sqlite-amalgamation-3071502.zip
安裝svn
需要安裝apr和apr-util,以及zlib zlib-devel
tar -jxf subversion-1.7.10.tar.bz2
unzip sqlite-amalgamation-3071502.zip
mv sqlite-amalgamation-3071502 subversion-1.7.10/sqlite-amalgamation
cd subversion-1.7.10
./configure --prefix=/usr/local/svn --with-apr=/usr/local/apr --with-apr-util=/usr/local/aprutil/
make && make install
添加環境變量
在/etc/profile添加
export PATH=/usr/local/svn/bin:$PATH
source /etc/profile
svn --version
svn, version 1.7.10 (r1485443)
建立svn版本庫目錄
mkdir -p /data/svn
建立svn版本庫關聯puppet
svnadmin create /data/svn/puppet
ls /data/svn/puppet/
conf db format hooks locks README.txt
修改版本庫配置檔案
vi /data/svn/puppet/conf/svnserve.conf
[general]
# 使非授權使用者無法通路
anon-access = none
# 使授權使用者有寫權限
auth-access = write
# 指明密碼檔案路徑
password-db = passwd
# 通路控制檔案
authz-db = authz
# 認證命名空間,subversion會在認證提示裡顯示,并且作為憑證緩存的關鍵字
realm = /data/svn/puppet
建立使用者并設定權限
vi /data/svn/puppet/conf/passwd
[users]
test1 = 123456
test2 = 123456
vi /data/svn/puppet/conf/authz
[/]
test1 = rw
test2 = rw
啟動svn服務
svnserve -d -r /data/svn/ --listen-port=8001 (指定監聽端口為8001)
netstat -ntlp|grep 8001
tcp 0 0 0.0.0.0:8001 0.0.0.0:* LISTEN 43851/svnserve
将/etc/puppet下的檔案導入到svn版本庫下
svn import /etc/puppet/ file:///data/svn/puppet -m 'version1'
将svn的版本庫遷移到/etc/puppet