使用virtualbox 安裝兩臺虛擬機來搭建Puppet服務端和客戶端的測試環境。
系統版本: CentOS 7.6 64位
內核版本: 3.10.0-957
puppetserver版本: 5.3.10-1.el7
puppet-agent版本: 5.5.17-1.el7
機器名/IP地址:
服務端: pp-master / 192.168.31.123
客戶端: pp-agent / 192.168.31.124
【準備】
關閉防火牆和SELINUX
將2臺主機的IP和主機名添加到/etc/hosts裏,確保互相可以ping同對方的主機名
【安裝】
分別在pp-master,pp-agent下載並安裝puppet repo
rpm -ivh https://yum.puppetlabs.com/puppet5/puppet5-release-el-7.noarch.rpm
生成puppet repo的文件路徑 /etc/yum.repos.d/puppet5.repo
在pp-master上面安裝puppetserver和puppet
yum install puppetserver puppet -y
在pp-agent上面安裝puppet
yum install puppet -y
【配置】
puppet的配置文件 /etc/puppetlabs/puppet/puppet.conf
服務器端puppet.conf
默認配置如下:
[master]
vardir = /opt/puppetlabs/server/data/puppetserver
logdir = /var/log/puppetlabs/puppetserver
rundir = /var/run/puppetlabs/puppetserver
pidfile = /var/run/puppetlabs/puppetserver/puppetserver.pid
codedir = /etc/puppetlabs/code
將如下main配置添加進服務器端puppet.conf
[main]
certname = pp-master
server = pp-master
environment = production
runinterval = 10m
strict_variables = true
certname(證書名)和server(服務器名)都設置爲 pp-master
environment(環境)默認爲production(生產環境)
runinterval(運行間隔時間)設置爲10分鐘
strict_variables(強制變量)設定爲true
將如下main配置添加進客戶端puppet.conf
[main]
certname = pp-agent
server = pp-master
environment = production
runinterval = 10m
證書名爲本機hostname: pp-agent
服務器端爲pp-master
環境默認爲production
運行間隔時間爲10分鐘
編輯hiera配置 /etc/puppetlabs/puppet/hiera.yaml
---
:backends:
- yaml
:yaml:
:datadir: "/etc/puppetlabs/code/environments/%{environment}/hieradata"
:hierarchy:
- "hosts/%{::trusted.certname}"
- common
:yaml:
# datadir is empty here, so hiera uses its defaults:
# - /etc/puppetlabs/code/environments/%{environment}/hieradata on *nix
# - %CommonAppData%\PuppetLabs\code\environments\%{environment}\hieradata on Windows
# When specifying a datadir, make sure the directory exists.
:datadir:
客戶端host配置存放在路徑/etc/puppetlabs/code/environments/production/hieradata/hosts/pp-agent
配置內容:
---
classes:
- helloworld
首先定義一個叫helloworld的模塊用於測試
模塊目錄:
/etc/puppetlabs/code/environments/production/modules/helloworld
目錄下有3個目錄:
helloworld/
├── files
│ └── hw.txt
├── manifests
│ └── init.pp
└── templates
files和templates目錄下存放模板文件,該模板文件爲hw.txt
manifests的init.pp文件用於定義模塊需要哪些資源和操作
init.pp
class helloworld {
file { '/tmp/hw.txt':
ensure => 'file',
source => 'puppet:///modules/helloworld/hw.txt',
mode => '0644',
owner => 'root',
group => 'root',
}
}
該模塊定義了一個helloworld的類,資源爲file,其內容爲"Hello world!".
'/tmp/hw.txt' 爲客戶端生成的文件路徑和名稱
ensure 定義該類型爲文件,其他還有link, directory, 或者可以定義爲present和absent表示該文件存在或不存在
mode爲文件權限644
owner文件所有者爲root
group文件組爲root
【服務】
啓動服務器端服務
systemctl start puppetserver
systemctl start puppet
啓動客戶端服務systemctl start puppet
客戶端執行puppet agent -t 用戶拉取配置
服務器端需要對證書籤名,puppet cert sign --all
【測試】
客戶端執行puppet agent -t 命令後,可以看到文件已經生成
Notice: /Stage[main]/Helloworld/File[/tmp/hw.txt]/ensure: defined content as '{md5}59ca0efa9f5633cb0371bbc0355478d8'
Notice: Applied catalog in 0.60 seconds
至此,一個簡單的Puppet CS環境搭建完成。