萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 伺服器教學

CentOS 7 部署 Puppet + Foreman 自動化運維平台完整實戰

架構規劃

角色主機名IP功能
Puppet Master / Foremanpuppet.domain.com192.168.6.171配置管理、Web UI、ENC、報告收集
Puppet Agentagent1.domain.com192.168.6.173受管節點

前置準備 (所有節點)

# 1. 關閉 SELinux 與防火牆 (測試環境)
setenforce 0
sed -i 's/^SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
systemctl stop firewalld && systemctl disable firewalld
# 或開放埠:firewall-cmd --permanent --add-port=8140/tcp --add-port=443/tcp --add-port=80/tcp

# 2. 設定主機名與 /etc/hosts
hostnamectl set-hostname puppet.domain.com   # master
hostnamectl set-hostname agent1.domain.com   # agent

cat > /etc/hosts <<'EOF'
192.168.6.171 puppet.domain.com puppet
192.168.6.173 agent1.domain.com agent1
EOF

# 3. 配置 USTC yum 源 (加速)
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.ustc.edu.cn/repo/CentOS-Base.repo
yum clean all && yum makecache

安裝 Puppet 官方倉庫

rpm -Uvh https://yum.puppet.com/puppet7-release-el-7.noarch.rpm
# Puppet 7 支援 CentOS 7/8/9, Rocky, AlmaLinux

安裝 Ruby 環境

yum install -y ruby ruby-devel ruby-libs ruby-shadow
ruby -v
# ruby 2.7.x 或 3.x

Master 節點安裝與配置

1. 安裝 Puppet Server

yum install -y puppetserver

2. 記憶體調整 (預設 2G,小記憶體機器必改)

vi /etc/sysconfig/puppetserver
# JAVA_ARGS="-Xms512m -Xmx512m -Djruby.logger.class=com.puppetlabs.jruby_utils.jruby.Slf4jLogger"
# 視記憶體調整,最小 512m

3. Puppet 設定檔 /etc/puppetlabs/puppet/puppet.conf

[main]
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
ssldir = /etc/puppetlabs/puppet/ssl

[master]
dns_alt_names = puppet,puppet.domain.com
reports = puppetdb,foreman
storeconfigs = true
storeconfigs_backend = puppetdb
environment_timeout = unlimited
strict_variables = true

4. 啟動 Puppet Server

systemctl enable --now puppetserver
# 查看日誌
journalctl -u puppetserver -f

Foreman 安裝 (整合 Puppet 管理介面)

# 1. 安裝 Foreman 安裝程式
yum install -y foreman-installer

# 2. 執行安裝 (整合 Puppet、PuppetDB、Apache、PostgreSQL)
foreman-installer \
  --foreman-admin-password admin123 \
  --foreman-db-type postgresql \
  --puppet-server=true \
  --puppet-server-ca=true \
  --puppet-server-foreman-url=https://puppet.domain.com \
  --enable-foreman-plugin-ansible \
  --enable-foreman-plugin-remote-execution \
  --enable-foreman-plugin-tasks

# 3. 驗證
# 瀏覽器開啟 https://puppet.domain.com
# 帳號: admin, 密碼: admin123

Agent 節點安裝與配置

# 1. 安裝 Puppet Agent
yum install -y puppet-agent

# 2. 配置 /etc/puppetlabs/puppet/puppet.conf
cat > /etc/puppetlabs/puppet/puppet.conf <<'EOF'
[main]
server = puppet.domain.com
environment = production
runinterval = 30m
certname = agent1.domain.com

[agent]
report = true
pluginsync = true
splay = true
splaylimit = 300
EOF

# 3. 首次執行申請憑證
/opt/puppetlabs/bin/puppet agent -t --waitforcert 60
# 輸出: Info: Creating a new SSL certificate request for agent1.domain.com
#       Info: Certificate Request fingerprint (SHA256): ...
#       Notice: /File[/opt/puppetlabs/puppet/cache/facts.d] ... (等待 Master 簽署)

Master 簽署 Agent 憑證

# 查看待簽署憑證
/opt/puppetlabs/bin/puppetserver ca list

# 簽署特定節點
/opt/puppetlabs/bin/puppetserver ca sign --certname agent1.domain.com

# 或全部簽署 (測試環境)
/opt/puppetlabs/bin/puppetserver ca sign --all

# 驗證
/opt/puppetlabs/bin/puppetserver ca list --all

Agent 套用配置驗證

# Agent 再次執行,應成功套用配置
/opt/puppetlabs/bin/puppet agent -t
# 輸出: Notice: Applied catalog in XX.XX seconds

Foreman Web UI 基本操作

  • Hosts → All Hosts:查看所有受管節點狀態、最後報告、事實。
  • Configure → Classes / Config Groups:管理 Puppet 模組/類別、建立配置群組。
  • Monitor → Reports:查看每次執行詳細報告 (資源變更、錯誤、時間)。
  • Infrastructure → Smart Proxies:管理 DHCP、DNS、TFTP、Puppet CA 等代理服務。
  • Administer → Users / Roles:RBAC 權限控制。

常見問題排查

問題解決
Agent 證書驗證失敗 (hostname mismatch)確保 certname 與 /etc/hosts、DNS 一致;Master dns_alt_names 包含所有別名
Puppet Server 啟動慢/記憶體不足調整 JAVA_ARGS Xms/Xmx;關閉不需功能 (如 puppetdb 若未用)
Foreman 安裝失敗檢查 /var/log/foreman-installer/ 日誌;確保 PostgreSQL、Apache 正常
Agent 執行卡住puppet agent -t --debug 查看卡在哪個資源;常見於 exec 無 timeout、檔案來源不可達
環境隔離 (Production/Staging/Development)Master environment.conf、environment_timeout;Agent 指定 environment;Git workflow 部署 code

生產環境建議

  • 啟用 PuppetDB 儲存事實、目錄、報告,支援 Foreman 查詢 API。
  • 使用 r10k / Code Manager 透過 Git 管理控制碼部署 (環境分支)。
  • 配置 Hiera 分離資料與程式碼,支援層級查找。
  • 監控 Puppet Server 指標 (JRuby 池、編譯佇列、HTTP 延遲) → Prometheus + Grafana。
  • 定期備份 /etc/puppetlabs、/opt/puppetlabs/server/data、PostgreSQL 資料庫。
copyright © 萬盛學電腦網 all rights reserved