puppet的多環境實現
前提:確保所有節點的epel源可用,因為安裝nginx需要epel源。
步驟:
1、Master端操作:
①創建如下目錄結構
tree /puppet/environment/
/puppet/environment/
├── dev
│ ├── manifests
│ └── modules
├── prod
│ ├── manifests
│ └── modules
└── test
├── manifests
└── modules
mkdir -pv /puppet/environment/{prod,test,dev}/{modules,manifests}
②在puppet.conf的master段添加environmentpath:指向多環境配置的目錄
puppet config --section=master set environmentpath /puppet/environments
如果不想這樣指定,可以在/etc/puppet/下建一個軟鏈接environments指向該路徑,就不需要改配置文件了
③定義模塊,準備相關文件
cd /puppet/environment/prod/
mkdir modules/nginx/{manifests,files -pv}
cp /etc/nginx/nginx.conf modules/nginx/files/
vim modules/nginx/files/nginx.conf
配置進程數為worker_processes auto
④編輯清單文件,定義類:
vim prod/modules/nginx/manifests/init.pp
class nginx {
package{'nginx':
ensure => latest,
} ->
file{'nginx':
ensure => file,
source => 'puppet:///modules/nginx/nginx.conf',
path => '/etc/nginx/nginx.conf',
owner => 'root',
group => 'root',
mode => '0644',
} ~>
service{'nginx':
ensure => running,
enable => true,
hasrestart => true,
hasstatus => true,
restart => 'usr/sbin/nginx -s reload'
}
}
⑤編輯站點清單文件:
vim prod/manifests/site.pp
node 'server1.tingjie.com'{
include nginx
}
⑥配置testing環境和development環境的文件
cp -r modules/nginx/ /puppet/environment/test/modules/
cp -r modules/nginx/ /puppet/environment/dev/modules/
cp manifests/site.pp /puppet/environment/test/manifests/
cp manifests/site.pp /puppet/environment/dev/manifests/
vim test/modules/nginx/files/nginx.conf
worker_processes 2;
vim dev/modules/nginx/files/nginx.conf
worker_processes 1;
puppet parser validate test/modules/nginx/ 可以檢查語法錯誤
最終的目錄結構如下:
tree /puppet/environment/
/puppet/environment/
├── dev
│ ├── manifests
│ │ └── site.pp
│ └── modules
│ └── nginx
│ ├── files
│ │ └── nginx.conf
│ └── manifests
│ └── init.pp
├── prod
│ ├── manifests
│ │ └── site.pp
│ └── modules
│ └── nginx
│ ├── files
│ │ └── nginx.conf
│ └── manifests
│ └── init.pp
└── test
├── manifests
│ └── site.pp
└── modules
└── nginx
├── files
│ └── nginx.conf
└── manifests
└── init.pp
⑦重啟服務:systemctl restart puppetmaster.service
2、server1端測試:
systemctl stop puppetagent.service
使用下面的命令可以看過程,當然也可以直接重啟服務
puppet agent -v --noop --environment=prod --no-daemonize
puppet agent -v --noop --environment=test --no-daemonize
puppet agent -v --noop --environment=dev --no-daemonize
如果想固定一臺機器使用的環境為prod,那麽
vim /etc/puppet/puppet.conf
environment = prod
重啟服務即可
puppet的多環境實現