溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

DevOps 系列工具之 Puppet 安裝與基礎(chǔ)配置

發(fā)布時(shí)間:2020-03-04 18:52:22 來源:網(wǎng)絡(luò) 閱讀:6807 作者:魏文弟 欄目:系統(tǒng)運(yùn)維

Puppet 簡(jiǎn)介:

Puppet是基于Ruby語言(早期的版本,4及之后的Server端采用Clojure語言)開發(fā)的、可以管理Unix(包括OS X)、Linux和Microsoft Windows平臺(tái)的配置管理工具。同時(shí)面向研發(fā)及運(yùn)維人員,是實(shí)現(xiàn) DevOps 的重要工具之一。

應(yīng)用場(chǎng)景:

統(tǒng)一配置管理軟件
統(tǒng)一配置系統(tǒng)優(yōu)化參數(shù)
定期檢測(cè)服務(wù)是否運(yùn)行
根據(jù)機(jī)器硬件環(huán)境自動(dòng)為相應(yīng)主機(jī)上的軟件配置合理的屬性

Puppet的操作模型:

部署層

Puppet 通常運(yùn)行在C/S模式下,服務(wù)器端稱為 Puppet master,客戶端稱為 Agent,客戶端主機(jī)稱為 Node。Agent 通過標(biāo)準(zhǔn)的 SSL 加密認(rèn)證的方式與 Master 建立連接,獲取本機(jī)需要的配置信息。
在 Agent 未取得配置信息、或者已經(jīng)達(dá)到配置狀態(tài)時(shí),Puppet 不會(huì)對(duì)系統(tǒng)進(jìn)行改動(dòng),它只有在被要求的時(shí)候才修改系統(tǒng),這是 Puppet 的一個(gè)關(guān)鍵特征,稱為冪等性(Idempotency),這個(gè)修改過程稱為一次配置運(yùn)行(Configuration run)。
Agent 通常以守護(hù)進(jìn)程的方式運(yùn)行,默認(rèn)每30分鐘與 Master 進(jìn)行一次交互,以確認(rèn)配置項(xiàng)是否發(fā)生了變化,這個(gè)時(shí)間間隔可以根據(jù)自己的需求靈活調(diào)整。也可以以 cron 的方式運(yùn)行或者手工觸發(fā) Agent。

配置語言與資源抽象層

大多數(shù)腳本語言(如 Shell Perl)是命令式或者過程式的,即“如何”達(dá)到想要的狀態(tài),而 Puppet 語言是描述式的,只需要描述最終狀態(tài)是什么,不用關(guān)心是如何達(dá)到。例如安裝 vim 軟件包,手工安裝時(shí)需要以下步驟:
1.連接到需要安裝軟件包的主機(jī)
2.檢查 vim 是否安裝
3.沒有安裝,需要根據(jù)主機(jī)的操作系統(tǒng)選擇合適的命令安裝,如 CentOS 使用 yum 命令,Ubuntu 使用 apt-get 命令
4.安裝結(jié)束根據(jù)返回的結(jié)果確認(rèn)是否安裝成功。

  使用 Puppet 安裝vim,只需要定義一個(gè)資源,并且定義資源要達(dá)到的狀態(tài)即可
  package { ‘vim’:
    ensure => present,
  }

事物層

事物層是 Puppet 的工作引擎,一個(gè) Puppet 事物包含配置一臺(tái) Agent 主機(jī)的完整過程,包括如下步驟:

  1. 解析并編譯配置
  2. 將編譯的后的配置發(fā)送到 Agent 上
  3. 在 Agent 上應(yīng)用編譯后的配置
  4. 將運(yùn)行結(jié)果上報(bào)給 Master

工作流程

  1. Agent 訪問 Master 建立訪問信任關(guān)系,包括 Master 對(duì) Agent 證書授權(quán)簽名,并允許 Agent 訪問 Master 資源
  2. 建立信任關(guān)系后,Master 調(diào)用 Agent 的 Facter,探測(cè)出 Agent 主機(jī)的一些機(jī)器變量,如操作系統(tǒng)、主機(jī)名、IP地址、CPU等信息。Agent 將這些信息通過 SSL 加密傳輸?shù)?Master,Master 以變量的形式獲取這些信息并使用
    1. Master 接收到 Agent 的請(qǐng)求,將它們發(fā)送到本機(jī)的 manifests 或 ENC(外部節(jié)點(diǎn)分類器),然后進(jìn)行配置信息查詢
  3. 根據(jù) Agent 的 HOSTNAME 匹配到相應(yīng)的Node,進(jìn)行語法檢查并生成相應(yīng)的 Catalog
  4. Agent 接收到 Catalog 后,在本機(jī)應(yīng)用,并根據(jù) Catalog 中的信息判斷是否有 File 文件要從 Master 端獲取,有則向 Master Fileserver 發(fā)起請(qǐng)求獲取文件
  5. 將執(zhí)行后的結(jié)果以報(bào)告的形式上報(bào) Master
  6. 整個(gè)事務(wù)完成

    安裝前需要考慮的地方:

    版本選擇:Puppet 最好的版本通常是最新的發(fā)布版
    運(yùn)行模式:服務(wù)器端-客戶端模式和獨(dú)立運(yùn)行模式
    DNS:Agent 每次連接 Master 時(shí),都會(huì)使用域名
    防火墻 :Puppet master 監(jiān)聽在 8140 端口,如果開啟了防火墻必須放行 8140 端口,Iptables 配置如下:
    iptables -A INPUT -p tcp -m state --state NEW --dport 8140 -j ACCEPT
    iptables -A INPUT -p tcp -m state --state NEW -s 172.16.0.0/16 --sport 8140 -j ACCEPT
    NTP 時(shí)鐘同步
    Puppetmaster:不能運(yùn)行在Windows主機(jī)上
    混合版本部署:服務(wù)端的版本必須比客戶端高,4.x及以上不支持3.x及以下
    硬件要求:2~4核CPU,4GB以上內(nèi)存,大約可管理1000個(gè)節(jié)點(diǎn)

安裝(來源于官網(wǎng))

Yum-based systems
To enable the Puppet 5 Platform repository:
1 Choose the package based on your operating system and version.
The packages are located in the puppet5 directory of the yum.puppet.com repository and named using the following convention: <PLATFORM_NAME>-release-<OS ABBREVIATION>-<OS VERSION>.noarch.rpm For instance, the package for Puppet 5 Platform on Red Hat Enterprise Linux 7 (RHEL 7) is puppet5-release-el-7.noarch.rpm.
2 Use the rpm tool as root with the upgrade (-U) flag, and optionally the verbose (-v), and hash (-h) flags:

Enterprise Linux 7
sudo rpm -Uvh https://yum.puppet.com/puppet5/puppet5-release-el-7.noarch.rpm

Enterprise Linux 6
sudo rpm -Uvh https://yum.puppet.com/puppet5/puppet5-release-el-6.noarch.rpm

Enterprise Linux 5
wget https://yum.puppet.com/puppet5/puppet5-release-el-5.noarch.rpm
sudo rpm -Uvh puppet5-release-el-5.noarch.rpm

Note: For recent versions of Puppet, we no longer ship Puppet master components for RHEL 5. However, we continue to ship new versions of the puppet-agent package for RHEL 5 agents.

Fedora 26
sudo rpm -Uvh https://yum.puppet.com/puppet5/puppet5-release-fedora-26.noarch.rpm
Fedora 25
sudo rpm -Uvh https://yum.puppet.com/puppet5/puppet5-release-fedora-25.noarch.rpm

SuSE Enterprise Linux 12
sudo rpm -Uvh https://yum.puppet.com/puppet5/puppet5-release-sles-12.noarch.rpm
SuSE Enterprise Linux 11
sudo rpm -Uvh https://yum.puppet.com/puppet5/puppet5-release-sles-11.noarch.rpm

Apt-based systems
To enable the Puppet 5 Platform repository:
1 Choose the package based on your operating system and version. The packages are located in the apt.puppet.com repository and named using the convention <PLATFORM_VERSION>-release-<VERSION CODE NAME>.debFor instance, the release package for Puppet Platform on Debian 7 “Wheezy” is puppet5-release-wheezy.deb. For Ubuntu releases, the code name is the adjective, not the animal.
2 Download the release package and install it as root using the dpkg tool and the install flag (-i):wget https://apt.puppetlabs.com/puppet5-release-wheezy.deb
3 sudo dpkg -i puppet5-release-wheezy.deb
4 Run apt-get update after installing the release package to update the apt package lists.

Ubuntu 16.04 Xenial Xerus
wget https://apt.puppetlabs.com/puppet5-release-xenial.deb
sudo dpkg -i puppet5-release-xenial.deb
sudo apt update

Ubuntu 14.04 Trusty Tahr
wget https://apt.puppetlabs.com/puppet5-release-trusty.deb
sudo dpkg -i puppet5-release-trusty.deb
sudo apt-get update

Debian 9 Stretch
wget https://apt.puppetlabs.com/puppet5-release-stretch.deb
sudo dpkg -i puppet5-release-stretch.deb
sudo apt-get update

Debian 8 Jessie
wget https://apt.puppetlabs.com/puppet5-release-jessie.deb
sudo dpkg -i puppet5-release-jessie.deb
sudo apt-get update

Debian 7 Wheezy
wget https://apt.puppetlabs.com/puppet5-release-wheezy.deb
sudo dpkg -i puppet5-release-wheezy.deb
sudo apt-get update

[root@master1 yum.repos.d]# cat /etc/redhat-release 
CentOS Linux release 7.4.1708 (Core) 
[root@master1 yum.repos.d]# sudo rpm -Uvh https://yum.puppet.com/puppet5/puppet5-release-el-7.noarch.rpm
[root@master1 yum.repos.d]# yum list |grep puppet
puppet5-release.noarch                      5.0.0-1.el7                installed
bolt.x86_64                                 0.17.2-1.el7               puppet5  
pdk.x86_64                                  1.4.1.1-1.el7              puppet5  
puppet-agent.x86_64                         5.4.0-1.el7                puppet5  
puppet-client-tools.x86_64                  1.2.2-1.el7                puppet5  
puppet-release.noarch                       1.0.0-1.el7                puppet5  
puppetdb.noarch                             5.2.0-1.el7                puppet5  
puppetdb-termini.noarch                     5.2.0-1.el7                puppet5  
puppetserver.noarch                         5.2.0-1.el7                puppet5  
razor-server.noarch                         1.7.1-1.el7                puppet5

安裝Puppet Master

[root@master1 yum.repos.d]# sudo yum install -y puppetserver
[root@master1 puppetlabs]# pwd
/opt/puppetlabs
[root@master1 puppetlabs]# bin/puppet --version
5.4.0
[root@master1 puppetlabs]# server/bin/puppetserver --version
puppetserver version: 5.2.0

yum 安裝 Master 時(shí),也會(huì)安裝 Agent 包,4.x及以上的安裝路徑為 /opt/puppetlabs/ 目錄,配置文件路徑為 /etc/puppetlabs/ 目錄

安裝 Puppet Agent

[root@master1 yum.repos.d]# sudo yum install -y puppet-agent

修改配置文件

Puppet5.x 的配置文件路徑為 /etc/puppetlabs/puppet/ 目錄下
puppet.conf 配置文件簡(jiǎn)介:
[main] 用于 Puppet 全局配置
[master] 用于 Puppet 的 Master 配置
[agent] 用于 Puppet 的 Agent 配置

[main]
server = master1.tongwen.life #指定 Puppet 服務(wù)端地址
autoflush = false #是否實(shí)時(shí)刷新日志到磁盤
logdir = /var/log/puppet #日志目錄
rundir = /var/run/puppet #進(jìn)程pid文件存放目錄

[master]
reportdir = /var/lib/puppet/reports #報(bào)告存放目錄
autosign = true #自動(dòng)授權(quán)簽名配置文件
autosign = /etc/puppet/autosign.conf
bindaddress = 0.0.0.0 #puppetserver 服務(wù)監(jiān)聽地址
masterport = 8140 #puppetserver 服務(wù)監(jiān)聽端口
evaltrace = true #定義為true,可以看到執(zhí)行的過程與變化

[agent]
certname = www1.tongwen.life #客戶端的主機(jī)名
daemonize = true #是否后臺(tái)運(yùn)行,true表示是
allow_duplicate_certs = true #是否允許證書自動(dòng)覆蓋,默認(rèn)不允許,有效期5年
report = true #是否上傳客戶端對(duì)資源的執(zhí)行結(jié)果
reports = store, http #上傳的方式
report_server = master1.tongwen.life #store 上傳地址
report_port = 8140
reporturl = http://localhost:3000/reports/upload
runinterval = 20m #客戶端執(zhí)行間隔,默認(rèn)30m
splay = true #是否在執(zhí)行時(shí)間上加一個(gè)隨機(jī)時(shí)間,0到最大隨機(jī)時(shí)間之間的整數(shù)
splaylimit = 10m #隨機(jī)時(shí)間的最大長度
configtimeout = 2m #客戶端獲取配置超時(shí)時(shí)間
color = ansi #日志記錄是否加顏色
ignorecache = true #是否忽略本地緩存

啟動(dòng)服務(wù)

如果 Master 主機(jī)的內(nèi)存配置小于2GB,需要修改Java初始內(nèi)存

vi /etc/sysconfig/puppetserver
JAVA_ARGS="-Xms2g -Xmx2g
systemctl start puppetserver #啟動(dòng)server端
systemctl start puppet #啟動(dòng)agent

簽批證書

[root@master1 puppetlabs]# puppet cert list
"node1.tongwen.life" (SHA256) E9:62:D5:7A:AD:1F:1D:DD:8F:0F:36:16:50:0C:

[root@master1 puppetlabs]# puppet cert sign node1.tongwen.life
Signing Certificate Request for:
"node1.tongwen.life" (SHA256)  E9:62:D5:7A:AD:1F:1D:DD:8F:0F:36:16:50:0C:11:D6:02:39:7B:CB:8C:87:C9:25:E0:F7:A2:D7:D9:55:3B:37
Notice: Signed certificate request for node1.tongwen.life
Notice: Removing file Puppet::SSL::CertificateRequest node1.tongwen.life at '/etc/puppetlabs/puppet/ssl/ca/requests/node1.tongwen.life.pem'
[root@master1 puppetlabs]# puppet cert sign --all  #簽批所有

在agent上執(zhí)行 puppet agent --test 驗(yàn)證證書的認(rèn)證結(jié)果,如果有報(bào)錯(cuò),請(qǐng)檢查時(shí)間是否同步。

[root@master1 puppetlabs]# ntpdate cn.ntp.org.cn

創(chuàng)建配置項(xiàng)

[root@master1 manifests]# pwd
/etc/puppetlabs/code/environments/production/manifests
vi site.pp
node 'node1.tongwen.life' {
    package { 'vim':
     ensure => present,
  }
}
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI