0. 安裝環境
客戶端IP | puppet_client.example.net(192.168.1.10) |
服務端IP | puppet_server.example.net(192.168.1.11) |
OS版本 | CentOS release 6.6 x86_64 |
puppet版本 | 3.7.5 |
1.預安裝配置
需要在服務端和客戶端進行一些必要的預安裝配置,因此本節下面的命令需要在客戶端和服務端均要執行。
(1) yum install ruby #安裝ruby
(2) 修改/etc/hosts,寫入兩行:
192.168.1.10 puppet_client.example.net 192.168.1.11 puppet_server.example.net |
(3) rpm -ivh http://yum.puppetlabs.com/puppetlabs-release-el-6.noarch.rpm #安裝puppet倉庫
2. 安裝服務端
登錄puppet_server.example.net,執行如下命令:
[root@puppet_server.example.net ~]# yum install puppet-server puppet –y #安裝puppet服務端和客戶端
[root@puppet_server.example.net ~]# puppet resource package puppet-server ensure=latest #更新puppet服務端
[root@puppet_client.example.net ~]# mv /etc/puppet/puppet.conf /etc/puppet/puppet.conf_bak #備份原配置文件
[root@puppet_client.example.net ~]# vim /etc/puppet/puppet.conf # 重新寫入文件,文件內容如下:
[main] |
[root@puppet_server.example.net ~]# chkconfig puppetmaster on #設置puppetmaster服務開啓啓動
[root@puppet_server.example.net ~]# service puppetmaster start #啓動puppetmaster
3. 安裝客戶端
登錄puppet_client.example.net,執行如下命令:
[root@puppet_client.example.net ~]# yum install puppet –y #安裝puppet客戶端
[root@puppet_client.example.net ~]# puppet resource package puppet ensure=latest #更新puppet
[root@puppet_client.example.net ~]# mv /etc/puppet/puppet.conf /etc/puppet/puppet.conf_bak #備份原配置文件
[root@puppet_client.example.net ~]# vim /etc/puppet/puppet.conf # 重新寫入文件,文件內容如下:
[main] |
[root@puppet_client.example.net ~]# chkconfig puppet on #設置puppet服務開啓啓動/etc/puppet/puppet.conf
[root@puppet_client.example.net ~]# service puppet start #啓動puppet
4. 客戶端與服務端進行認證
客戶端通過調試模式啓動節點向puppetmaster端發起認證:
[root@puppet_client.example.net ~]# puppet agent –t
Info: Caching certificate for puppet_client.example.net
Info: Caching certificate for puppet_client.example.net
Info: Caching catalog for puppet_client.example.net
Info: Applying configuration version '1430120791'
Notice: Finished catalog run in 0.02 seconds
服務端確定認證:
[root@puppet_server.example.net ~]# puppet cert --list -all #查看所有證書,未認證的證書前面沒有+號
[root@puppet_server.example.net ~]# puppet cert --sign -all #對所有證書進行認證
或者只對指定的證書進行認證:
[root@puppet_server.example.net ~]# puppet cert --sign “puppet_client.example.net” #對指定的證書進行認證
[root@puppet_server.example.net ~]# puppet cert --list -all #再次查看所有證書,證書前面已經有+號,表示已經認證通過
5. 客戶端進行操作的一個簡單例子
現在舉一個最簡單的例子說明一下如何使用。
假如我們需要在客戶端上安裝lsof命令,常規情況下我們需要運行yum install lsof -y命令,這只是RHEL、CentOS系統上的命令,而在Debian、Ubuntu等系統上則是用apt-get命令。如果需要安裝lsof命令的系統比較多的話,就需要編寫腳本判斷系統版本進行安裝了。但是在puppet中,可以通過以下方式簡單的實現上述需求。
首先,在puppet服務端創建一個/etc/puppet/manifests/site.pp文件,內容如下:
[root@puppet_server.example.net ~]# vim /etc/puppet/manifests/site.pp
package{ 'lsof': |
然後,在client端執行puppet agent -t命令:
[root@puppet_client.example.net ~]# puppet agent -t
Info: Caching catalog for puppet_client.example.net
Info: Applying configuration version '1430127711'
Notice: /Stage[main]/Main/Package[lsof]/ensure: created
Notice: Finished catalog run in 6.90 seconds
輸出已經表明lsof包已經安裝成功。查看一下:
[root@puppet_client.example.net ~]# rpm -q lsof
lsof-4.82-4.el6.x86_64
說明:puppet agent -t命令是在客戶端手動運行agent程序。puppet客戶端本身如果啓動了puppet服務的話,puppet是可以自動間隔一段時間後去運行agent的,默認時間間隔爲1800s。可以修改該時間間隔參數,修改客戶端agent節點中的/etc/puppet/puppet.conf文件,修改[agent]下面的:runinterval = 100,單位爲秒,然後重啓puppet:service puppet restart。