Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux綜合 >> 學習 Linux

Puppet 快速入門:安裝、配置解析與第一個清單

Puppet 是老牌配置管理工具,用聲明式語法描述「機器應該長什麼樣」,然後由它想方設法把狀態對齊。下面從安裝到寫出第一個可執行的清單。

一、找到對應的軟件源

官方倉庫說明:
https://docs.puppet.com/guides/puppetlabs_package_repositories.html
各發行版對應的 YUM 源:
https://yum.puppetlabs.com/

二、安裝(以 CentOS 7.1 為例)

wget https://yum.puppetlabs.com/puppetlabs-release-el-7.noarch.rpm
rpm -ivh puppetlabs-release-el-7.noarch.rpm
yum install puppet -y
puppet --version        # 3.8.7

三、配置文件都在 /etc/puppet

auth.conf  modules  puppet.conf

puppet.conf 是 master 守護進程的主配置,定義運行環境與加載的模塊,進程啟動前會先按它做預檢,預檢不通過就不會起來。auth.conf 定義 agent 訪問 master 各目錄的權限,沒有這層控制,agent 理論上能讀到 master 上的所有資源。

四、用 puppet resource 生成模板

不確定語法怎麼寫時,先讓 Puppet 把當前狀態導出來改:

[root@master1 ~]# puppet resource package nginx
package { 'nginx':
    ensure => 'absent',
}
[root@master1 ~]# puppet resource service nginx
service { 'nginx':
    ensure => 'stopped',
    enable => 'false',
}

五、寫一個安裝並啟動 nginx 的清單

把模板改成期望狀態:

package { 'nginx':
    ensure => 'present',
}
service { 'nginx':
    ensure => 'running',
}

本地套用:

# puppet apply nginx.pp
Notice: Compiled catalog for master1.localdomain in environment production in 0.56 seconds
Notice: /Stage[main]/Main/Package[nginx]/ensure: created
Notice: /Stage[main]/Main/Service[nginx]/ensure: ensure changed 'stopped' to 'running'
Notice: Finished catalog run in 5.76 seconds

驗證結果:

[root@master1 ~]# rpm -qa | grep nginx
nginx-1.6.3-9.el7.x86_64
nginx-filesystem-1.6.3-9.el7.noarch
[root@master1 ~]# ps -ef | grep nginx
root  3423  1  0 13:16 ?  00:00:00 nginx: master process /usr/sbin/nginx
nginx 3424 3423 0 13:16 ?  00:00:00 nginx: worker process

接下來把清單放進 modules 目錄、配置 puppet.conf 的 server 地址,就能從單機 apply 過渡到 master-agent 的集中管理。Puppet 的關鍵心態轉換在於:不要寫「怎麼做」,而是描述「最終應該是什麼樣」。

Copyright © Linux教程網 All Rights Reserved