Puppet部署於應用node
一、工做流程linux
1)客戶端puppet調用facter(facter是經過ssl加密收集及檢測分析客戶端配置信息的一個工具),facter探測出主機的一些變量,如主機名,內存大小,ip地址等。Puppet把這些信息經過ssl鏈接發送到服務器器端vim
2)服務器端的puppetmaster經過facter工具分析檢測客戶端的主機名,而後找到項目主配置文件mainfest裏面對應的node配置,並對該部份內容進行解析。Facter發送過來的信息能夠做爲變量處理,node牽扯到的代碼才解析,其餘沒牽涉的代碼不解析,解析分爲幾個階段,首先進行語法檢查,若是語法沒錯,就繼續解析,解析的結果生成一箇中間的「僞代碼」,而後把僞代碼發送給客戶端。centos
3)客戶端接收到僞代碼,並執行,客戶端把執行的結果發送給服務器。安全
4)服務器把客戶端的執行結果寫入日誌。ruby
Puppet工做過程有如下兩點值得注意:bash
1)爲了保證安全,client和master之間是基於ssl和證書的,只有經master證書認證的client能夠與master通訊。服務器
2)Puppet會讓系統保持在人們所指望的某種狀態並一直維持下去,例如:檢測某個文件並保證其一直存在,保證ssh服務始終開啓,若是文件被刪除了或者ssh服務被關閉了,puppet下次執行時(默認30分鐘),會從新建立該文件或者啓動ssh服務。ssh
實驗環境:tcp
實驗目標:
1.經過puppetmaster管理clinet端的ssh服務
2.客戶端能夠主動拉取
3.master能夠主動推送
1、搭建ntp server(已經安裝)
server 127.127.1.0
fudge 127.127.1.0 stratum 8
[root@centos4 ~]# service ntpd start
[root@centos4 ~]# chkconfig ntpd on
2、搭建puppetmaster
1.規劃服務器主機名
[root@centos1 ~]# vim /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=master.test.com
[root@centos1 ~]# hostname master.test.com
[root@centos1 ~]# bash
2.添加主機名解析;在互聯網中能夠搭建DNS服務
[root@centos1 ~]# vim /etc/hosts
192.168.1.1 master.test.com
192.168.1.2 client1.test.com
192.168.1.3 client2.test.com
2.服務器時間同步
[root@master ~]#yum -y install ntp( 默認已經安裝)
[root@master ~]# ntpdate 192.168.1.4
3.安裝ruby,先安裝compat-readline5(系統盤中),也能夠一併安裝
因爲puppet是採用ruby語言開發,因此不管是源碼仍是RPM方式安裝puppet,咱們都必需要先安裝ruby語言環境
[root@master ~]# rpm -ivh /mnt/Packages/compat-readline5-5.2-17.1.el6.x86_64.rpm
[root@master ~]# rpm -ivh /mnt/ruby-*.rpm
4.檢查ruby版本
[root@master ~]# ruby -v
ruby 1.8.7 (2011-06-30 patchlevel 352) [x86_64-linux]
5.安裝facter安裝
[root@master ~]# useradd -s /sbin/nologin puppet
安裝facter源碼包
[root@master ~]# tar zxf /mnt/facter-1.7.1.tar.gz -C /usr/src
[root@master ~]# cd /usr/src/facter-1.7.1/
[root@master facter-1.7.1]# ruby install.rb
6.安裝puppet源碼包
①安裝
[root@master puppet-2.7.21]# tar zxf /mnt/puppet-2.7.21.tar.gz -C /usr/src
[root@master puppet-2.7.21]# cd /usr/src/puppet-2.7.21/
[root@master puppet-2.7.21]# ruby install.rb
②複製配置文件
[root@master puppet-2.7.21]# cp conf/redhat/fileserver.conf /etc/puppet/
[root@master puppet-2.7.21]# cp conf/redhat/puppet.conf /etc/puppet/
[root@master puppet-2.7.21]# cp conf/redhat/server.init /etc/init.d/puppetmaster
③修改文件屬性
[root@master puppet-2.7.21]# chmod +x /etc/init.d/puppetmaster
④建立puppet主目錄
[root@master puppet-2.7.21]# mkdir /etc/puppet/manifests //存放配置文件
[root@master puppet-2.7.21]# mkdir /etc/puppet/modules //存放模塊配置
7.puppet服務證書請求與簽名
master端配置
①關閉防火牆
#service iptables stop //以前已經關閉
②修改配置文件。在【main】標題下添加如下一行:配置服務器模塊路徑
[root@master ~]# vim /etc/puppet/puppet.conf
modulepath = /etc/puppet/modules:/usr/share/puppet/modules
③啓動puppet主程序
[root@master ~]# /etc/init.d/puppetmaster start
Starting puppetmaster: [ OK ]
④查看puppet監聽端口
[root@master ~]# netstat -anpt|grep 8140
tcp 0 0 0.0.0.0:8140 0.0.0.0:* LISTEN 2123/ruby
2、搭建puppetclient1
1.規劃服務器主機名
[root@centos1 ~]# vim /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=client1.test.com
[root@centos1 ~]# hostname client1.test.com
[root@centos1 ~]# bash
2.添加主機名解析
[root@centos1 ~]# vim /etc/hosts
192.168.1.1 master.test.com
192.168.1.2 client1.test.com
192.168.1.3 client2.test.com
3.確保經過域名ping通puppetmaster
[root@client1 ~]# ping master.test.com
PING master.test.com (192.168.1.1) 56(84) bytes of data.
64 bytes from master.test.com (192.168.1.1): icmp_seq=1 ttl=64 time=7.29 ms
3.服務器時間同步
[root@client1 ~]# ntpdate 192.168.1.4
7 May 16:24:22 ntpdate[2532]: adjust time server 192.168.1.4 offset -0.144460 sec
4. 安裝ruby,先安裝compat-readline5(系統盤中),也能夠一併安裝
[root@master ~]# rpm -ivh /mnt/Packages/compat-readline5-5.2-17.1.el6.x86_64.rpm
[root@master ~]# rpm -ivh /mnt/ruby-*.rpm
5檢查ruby版本
[root@master ~]# ruby -v
ruby 1.8.7 (2011-06-30 patchlevel 352) [x86_64-linux]
6.安裝facter
[root@client1 facter-1.7.1]# useradd -s /sbin/nologin puppet
[root@client1 ~]# tar zxf /mnt/facter-1.7.1.tar.gz -C /usr/src
[root@client1 ~]# cd /usr/src/facter-1.7.1/
[root@client1 facter-1.7.1]# ruby install.rb
7.安裝puppet
[root@client1 facter-1.7.1]# tar zxf /mnt/puppet-2.7.21.tar.gz -C /usr/src
[root@client1 facter-1.7.1]# cd /usr/src//puppet-2.7.21/
[root@client1 puppet-2.7.21]# ruby install.rb
8.複製配置文件
[root@client1 puppet-2.7.21]# cp conf/redhat/puppet.conf /etc/puppet
[root@client1 puppet-2.7.21]# cp conf/redhat/client.init /etc/init.d/puppetclient
9.修改文件屬性
[root@client1 puppet-2.7.21]# chmod +x /etc/init.d/puppetclient
10.puppet服務證書與簽名
①關閉防火牆
[root@client1 puppet-2.7.21]# service iptables stop //以前已經關閉
②修改client配置文件(兩臺client配置徹底同樣)
#vim /etc/puppet/puppet.conf
[main]
# The Puppet log directory.
# The default value is '$vardir/log'.
logdir = /var/log/puppet
# Where Puppet PID files are kept.
# The default value is '$vardir/run'.
rundir = /var/run/puppet
server = master.test.com
3、搭建puppetclient2
1.規劃服務器主機名
[root@centos1 ~]# vim /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=client2.test.com
[root@centos1 ~]# hostname client2.test.com
[root@centos1 ~]# bash
2.添加主機名解析
[root@centos1 ~]# vim /etc/hosts
192.168.1.1 master.test.com
192.168.1.2 client1.test.com
192.168.1.3 client2.test.com
3.確保經過域名ping通puppetmaster
[root@client1 ~]# ping master.test.com
PING master.test.com (192.168.1.1) 56(84) bytes of data.
64 bytes from master.test.com (192.168.1.1): icmp_seq=1 ttl=64 time=7.29 ms
3.服務器時間同步
[root@client1 ~]# ntpdate 192.168.1.4
7 May 16:24:22 ntpdate[2532]: adjust time server 192.168.1.4 offset -0.144460 sec
4. 安裝ruby,先安裝compat-readline5(系統盤中),也能夠一併安裝
[root@master ~]# rpm -ivh /mnt/Packages/compat-readline5-5.2-17.1.el6.x86_64.rpm
[root@master ~]# rpm -ivh /mnt/ruby-*.rpm
5檢查ruby版本
[root@master ~]# ruby -v
ruby 1.8.7 (2011-06-30 patchlevel 352) [x86_64-linux]
6.安裝facter
[root@client1 facter-1.7.1]# useradd -s /sbin/nologin puppet
[root@client1 ~]# tar zxf /mnt/facter-1.7.1.tar.gz -C /usr/src
[root@client1 ~]# cd /usr/src/facter-1.7.1/
[root@client1 facter-1.7.1]# ruby install.rb
7.安裝puppet
[root@client1 facter-1.7.1]# tar zxf /mnt/puppet-2.7.21.tar.gz -C /usr/src
[root@client1 facter-1.7.1]# cd /usr/src//puppet-2.7.21/
chmod 0644 /usr/share/man/man8/puppet-node.8
[root@client1 puppet-2.7.21]# ruby install.rb
8.複製配置文件
[root@client1 puppet-2.7.21]# cp conf/redhat/puppet.conf /etc/puppet
[root@client1 puppet-2.7.21]# cp conf/redhat/client.init /etc/init.d/puppetclient
9.修改文件屬性
[root@client1 puppet-2.7.21]# chmod +x /etc/init.d/puppetclient
10.puppet服務證書與簽名
①關閉防火牆
[root@client1 puppet-2.7.21]# service iptables stop //已經關閉
②修改client配置文件(兩臺client配置徹底同樣)
#vim /etc/puppet/puppet.conf
[main]
# The Puppet log directory.
# The default value is '$vardir/log'.
logdir = /var/log/puppet
# Where Puppet PID files are kept.
# The default value is '$vardir/run'.
rundir = /var/run/puppet
server = master.test.com
4、註冊與申請
1.分別在客戶端client1和client2上註冊
[root@client1 puppet-2.7.21]# puppet agent --server=master.test.com --no-daemonize --verbose
[root@client2 puppet-2.7.21]# puppet agent --server=master.test.com --no-daemonize --verbose
這個指令的做用是將puppet agent不用守護進程的模式啓動,好處是能看到與服務器端的通訊及證書籤名的過程,方便debug。 默認puppet每2分鐘向服務器端申請一次簽名直到得到服務器端發來的通過簽名的證書才 start agent 服務
2.在master上查看申請註冊的客戶端
[root@master ~]# puppet cert --list //查看申請註冊的客戶端
"client1.test.com" (99:6A:C3:89:C8:1C:B5:75:24:BA:56:51:43:1E:EB:71)
"client2.test.com" (B8:51:93:3D:5A:77:49:01:B6:1A:B8:EB:57:16:46:8B)
3.在master上將未註冊的客戶端進行註冊
[root@master ~]# puppet cert sign --all
4.能夠經過目錄去查看已經註冊的客戶端
[root@master ~]# ll /var/lib/puppet/ssl/ca/signed/
total 12
-rw-r----- 1 puppet puppet 1915 May 7 17:59 client1.test.com.pem
-rw-r----- 1 puppet puppet 1915 May 7 17:59 client2.test.com.pem
-rw-r----- 1 puppet puppet 1988 May 7 16:17 master.test.com.pem
此時客戶端已經完整了證書的申請和簽名
5、配置實例
1.配置一個測試節點
n 節點信息:/etc/puppet/manifests/nodes
n 模塊信息:/etc/puppet/modules
爲了保護linux的ssh端×××破,批量修改客戶端sshd端口,將端口22改成9922,並實現重啓工做
建立ssh模塊,模塊目錄爲ssh,模塊下面有三個文件:manifests、templates和files
n manifests:此目錄爲存放配置文件的目錄,裏面必須包含一個init.pp的文件,這是該模塊的入口文件
l 導入一個模塊的時候,會從init.pp開始執行。能夠把全部的代碼都寫道init.pp裏面,也能夠分紅多個pp文件,init再去包含其餘文件
l 定義class類名的時候必須是ssh,這樣能實現調用
n files目錄是該模塊的文件發佈目錄,puppet提供一個文件分發機制/etc/ssh/sshd_conf
n templates目錄包含erb模版文件
master端:
①建立須要的必要目錄
#mkdir -p /etc/puppet/modules/ssh/{manifests,templates,files}
#mkdir /etc/puppet/manifests/nodes //節點目錄做用與client
#mkdir /etc/puppet/modules/ssh/files/ssh //控制ssh服務
#chown -R puppet /etc/puppet/modules/ //修改權限
此時/et/puppet/modules/ssh目錄下結構
[root@master ~]# ll /etc/puppet/modules/ssh
②建立模塊配置文件install.pp
[root@master ~]# vim /etc/puppet/modules/ssh/manifests/install.pp
class ssh::install{
package{ "openssh":
ensure => present
}
}
③建立模塊配置文件config.pp
[root@master ~]# vim /etc/puppet/modules/ssh/manifests/config.pp
class ssh::config{
file { "/etc/ssh/sshd_config":
ensure => present,
owner =>"root",
group =>"root",
mode => "0600",
source =>"puppet://$puppetserver/modules/ssh/ssh/sshd_config",
require => Class["ssh::install"],
notify => Class["ssh::service"],
}
}
④建立service.pp
[root@master ~]# vim /etc/puppet/modules/ssh/manifests/service.pp
class ssh::service {
service {"sshd":
ensure => running,
hasstatus =>true,
hasrestart =>true,
enable =>true,
require =>Class["ssh::config"]
}
}
⑤建立模塊主配置文件,將以上配置文件加載進去,輸入如下信息:
[root@master ~]# vim /etc/puppet/modules/ssh/manifests/init.pp
class ssh{
include ssh::install,ssh::config,ssh::service
}
此時/etc/puppet/modules/ssh/manifests下有四個文件
[root@master ~]# ll /etc/puppet/modules/ssh/manifests/
⑥創建服務器ssh統一維護文件
因爲服務器和客戶端的sshd_config文件默認同樣,此時將服務器端/etc/ssh/sshd_config複製到模塊默認路徑
[root@master ~]# cp /etc/ssh/sshd_config /etc/puppet/modules/ssh/files/ssh/
[root@master ~]#
⑦建立測試節點配置文件,並將ssh加載進去
[root@master ~]# vim /etc/puppet/manifests/nodes/ssh.pp
⑧將測試節點載入puppet,即修改site.pp
[root@master ~]# vim /etc/puppet/manifests/site.pp
⑨修改服務器端維護的sshd_config配置文件
[root@master ~]# vim /etc/puppet/modules/ssh/files/ssh/sshd_config
Port 9922
⑩從新啓動puppet
[root@master ~]# /etc/init.d/puppetmaster restart
Stopping puppetmaster: [ OK ]
Starting puppetmaster: [ OK ]
2.客戶端主動拉取 注:這裏使用clinet1測試;client2同理
[root@client1 puppet]# puppet agent -t
①檢查客戶端/etc/ssh/sshd_config配置是否是自動變了
[root@client1 puppet]# vim /etc/ssh/sshd_config
Port 9922
②查看服務器ssh服務是否重啓,端口是否生效
[root@client1 puppet]# netstat -tunlp|grep ssh
3.服務器推送同步
客戶端配置
①修改配置文件
[root@client1 puppet]# vim /etc/puppet/puppet.conf
最後一行添加以下
listen = true //使puppet監聽8139端口
[root@client1 puppet]# vim /etc/puppet/auth.conf
allow * //容許任何服務器推送
②啓動puppet客戶端
[root@client1 puppet]# /etc/init.d/puppetclient start
Starting puppet: [ OK ]
[root@client1 puppet]# netstat -anpt|grep 8139 //自身puppet端口號
③修改/etc/ssh/sshd_config配置文件
[root@client1 puppet]# vim /etc/ssh/sshd_config
Port 22
[root@client1 puppet]#service sshd restart
服務器端配置
④從服務器開始向客戶端推送
[root@master ssh]# puppet kick client1.test.com
⑤校驗結果
[root@client1 puppet]# vim /etc/ssh/sshd_config
Port 9922
[root@client1 puppet]# service sshd restart
[root@client1 puppet]# netstat -anpt|grep 9922