在现代IT基础设施中,自动化运维已成为提升效率、降低人为错误的关键手段。而 RockyLinux Puppet配置管理 正是实现这一目标的利器。本教程将从零开始,详细讲解如何在 Rocky Linux 系统上部署和使用 Puppet,即使是 Linux 新手也能轻松上手。
Puppet 是一个开源的配置管理工具,它允许你通过声明式语言定义系统状态(如安装哪些软件包、运行哪些服务、文件权限等),然后自动确保所有受管节点(称为“Agent”)始终符合该状态。这种“期望状态即代码”的方式极大简化了大规模服务器的统一管理。

我们将搭建一个简单的 Puppet 架构:
确保两台机器网络互通,并关闭防火墙或开放相应端口(默认 8140)以简化实验。
首先,在 Puppet Server 主机上启用 Puppet 官方仓库并安装 puppetserver:
# 启用 Puppet 仓库sudo dnf install -y https://yum.puppet.com/puppet-release-el-9.noarch.rpm# 安装 Puppet Serversudo dnf install -y puppetserver安装完成后,编辑配置文件以分配内存(根据你的服务器资源调整):
# 编辑 /etc/sysconfig/puppetserversudo vi /etc/sysconfig/puppetserver# 找到 JAVA_ARGS 行,修改为(例如分配 1GB 内存)JAVA_ARGS="-Xms1g -Xmx1g"启动并设置开机自启:
sudo systemctl start puppetserversudo systemctl enable puppetserver在被控端(Agent)执行以下命令:
# 启用 Puppet 仓库sudo dnf install -y https://yum.puppet.com/puppet-release-el-9.noarch.rpm# 安装 Puppet Agentsudo dnf install -y puppet-agent配置 Agent 指向 Server 的主机名(需能解析):
# 创建配置目录(如果不存在)sudo mkdir -p /etc/puppetlabs/puppet# 编写 puppet.confsudo tee /etc/puppetlabs/puppet/puppet.conf <注意:这里我们使用了默认主机名 puppet。你可以在 Agent 的 /etc/hosts 中添加一行:
192.168.1.10 puppet在 Agent 上首次运行 Puppet Agent,会自动生成证书请求:
sudo /opt/puppetlabs/bin/puppet agent --test此时会提示连接被拒绝(因为 Server 尚未签名)。切换到 Server 端,查看待签名证书:
sudo /opt/puppetlabs/bin/puppetserver ca list你会看到类似 agent.example.com 的请求。签名它:
sudo /opt/puppetlabs/bin/puppetserver ca sign --all再次在 Agent 上运行测试命令,这次应该成功连接并应用空配置:
sudo /opt/puppetlabs/bin/puppet agent --test现在我们来创建一个简单的配置:在所有 Agent 上安装并启动 Apache(httpd)服务。
在 Server 上编辑主清单文件:
sudo vi /etc/puppetlabs/code/environments/production/manifests/site.pp写入以下内容:
node 'agent.example.com' { package { 'httpd': ensure => installed, } service { 'httpd': ensure => running, enable => true, require => Package['httpd'], } file { '/var/www/html/index.html': ensure => file, content => "<h2>Hello from Puppet!</h2>\n", require => Package['httpd'], }}请将 agent.example.com 替换为你实际的 Agent 主机名(可通过 hostname 命令查看)。
保存后,在 Agent 上再次运行:
sudo /opt/puppetlabs/bin/puppet agent --test几秒后,打开浏览器访问 http://192.168.1.11,你应该能看到 “Hello from Puppet!” 页面!这标志着你的 Linux系统配置 已由 Puppet 成功管理。
通过本教程,你已掌握了在 Rocky Linux 上部署 Puppet 的完整流程,包括 Server 与 Agent 的安装、证书认证、以及编写第一个自动化配置。这套 Puppet部署教程 为你打下了坚实基础,后续可深入学习模块化、Hiera 数据分离、报告系统等高级功能,真正实现企业级 自动化运维。
记住,配置即代码(Infrastructure as Code)是 DevOps 的核心理念之一。善用 Puppet,让你的运维工作更高效、更可靠!
本文由主机测评网于2025-12-11发表在主机测评网_免费VPS_免费云服务器_免费独立服务器,如有疑问,请联系我们。
本文链接:https://www.vpshk.cn/2025126289.html