Puppet 简明教程
Puppet - Environment
在软件开发和交付模型中,有不同类型的测试环境,用于测试特定产品或服务。作为标准实践,主要有三种类型的环境,如开发、测试和生产,其中每一种都有自己的配置集。
In software development and delivery model, there are different kind of testing environments which are used for testing a particular product or a service. As a standard practice, there are mainly three kind of environments as development, testing and production, wherein each of them have their own set configuration.
Puppet 支持管理与 Ruby on Rails 相同的多个环境。创建这些环境的关键因素是为管理不同级别的 SLA 协议提供一种轻松的机制。在某些情况下,机器始终需要保持启用状态,且没有任何容忍度和旧软件的使用。其他环境则为最新状态,并用于测试目的。它们用于更重要机器的升级。
Puppet supports the management of multiple environment along the same line as Ruby on Rails. The key factor behind the creation of these environments is providing an easy mechanism for managing at different levels of SLA agreement. In some cases, the machine always needs to be up without any tolerance and use of old software. Wherein other environments are up-to-date and are used for testing purposes. They are used for upgrades for more important machines.
Puppet 建议坚持使用标准的生产、测试和开发环境配置,但是,它甚至还为用户提供了根据要求创建自定义环境的杠杆。
Puppet recommends to stick with the standard production, testing, and development environment configuration, however, here it even provides the user with a leverage of creating custom environments as per requirement.
Environment Goal
按环境拆分设置的主要目标是:Puppet 可以为模块和清单使用不同的源。然后可以在测试环境中测试配置中的更改,而不会影响生产节点。这些环境还可用于在不同网络源上部署基础设施。
The main goal of setup split by an environment, is that Puppet can have different sources for modules and manifests. One can then test the changes in configuration in the testing environment without impacting the production nodes. These environments can also be used to deploy infrastructure on different sources of network.
Using the Environment on Puppet Master
环境的重点是测试需要发送给客户端的文件清单、模块和模板。因此,必须配置 Puppet 以提供针对这些信息的特定于环境的源。
The point of an environment is to test which manifest, module, template of the file needs to be send to the client. Thus, Puppet must be configured to provide environment-specific source for these information.
Puppet 环境的实现方式非常简单,只需将 pre-environment 部分添加到服务器的 puppet.conf,并为每个环境选择不同的配置源即可实现。然后将优先使用这些 pre-environment 部分,而不是主部分。
Puppet environments are implemented simply by adding the pre-environment sections to the server’s puppet.conf and choosing different configuration source for each environment. These pre-environment sections are then used in preference to the main section.
[main]
manifest = /usr/testing/puppet/site.pp
modulepath = /usr/testing/puppet/modules
[development]
manifest = /usr/testing/puppet/development/site.pp
modulepath = /usr/testing/puppet/development/modules
在上述代码中,开发环境中的任何客户端都将使用 /usr/share/puppet/development 目录中位于 site.pp 清单文件,而 Puppet 将在 /usr/share/puppet/development/modules directory 中搜索任何模块。
In the above code, any client in the development environment will use the site.pp manifest file located in the directory /usr/share/puppet/development and Puppet will search for any module in /usr/share/puppet/development/modules directory.
使用或不使用任何环境运行 Puppet 都将默认使用 site.pp 文件和在主配置部分中的清单和 modulepath 值中指定目录。
Running Puppet with or without any environment would default to site.pp file and the directory specified in the manifest and modulepath values in the main configuration section.
实际上只有几个配置才适合在环境前进行配置,而且这些参数都围绕指定使用哪些文件来编译客户端的配置。
There are only few configurations which actually makes sense to be configured preenvironment, and all of those parameters revolve around specifying what files to use to compile a client’s configuration.
以下是这些参数。
Following are the parameters.
-
Modulepath − In Puppet, as a basic standard mode it’s best to have a standard module directory that all environment share and then a pre-environment directory where the custom module can be stored. Module path is the location where Puppet looks for all the environment related configuration files.
-
Templatedir − Template directory is the location where all the versions of related templates are saved. The module should be preferred to these settings, however it allows one to have different versions of a given template in each environment.
-
Manifest − This defines which configuration to use as entrypoint script.
对于多个模块,Puppets 帮助获取配置模块性。如果你主要依赖模块,你可以在 Puppet 中使用多个环境,效果会非常好。通过对模块中的更改进行封装,更容易将更改迁移到环境。文件服务器使用特定于环境的模块路径;如果你从模块提供文件服务,而不是独立的装载目录,这个环境将能够获取特定于环境的文件,最后,当前环境也会在清单文件中 $environment 变量中显示。
With multiple modules, Puppets help in getting the modularity for configurations. One can use multiple environments in Puppet which works much better if one relies largely on modules. It is easier to migrate changes to environments by encapsulating changes in the module. File server uses an environment specific module path; if one does file serving from modules, instead of separate mounted directories, this environment will be able to get environment-specific files and finally the current environment will also be available in $environment variable within the manifest file.
Setting the Clients Environment
与环境配置相关的所有配置都将在 puppet.conf 文件中完成。若要指定 Puppet 客户端应该使用哪个环境,你可以在客户端的 puppet.conf 文件中为 environment 配置变量指定一个值。
All the configurations related to environment configuration are done on puppet.conf file. To specify which environment the Puppet client should use, one can specify a value for the environment configuration variable in client’s puppet.conf file.
[puppetd]
environment = Testing
配置文件中的以上定义定义了配置文件所在的哪个环境,在本例中为 testing。
The above definition in configuration file defines which environment the configuration file is in our case it is testing.
你还可以使用 - 在命令行中指定此内容:
One can also specify this on the command line using −
#puppetd -–environment = testing
此外,Puppet 还支持在环境配置中使用动态值。开发人员可以创建自定义事实,而不是定义静态值,这些事实基于其他客户端属性或外部数据源创建客户端环境。这样做的方式是使用自定义工具。这些工具能够指定节点的环境,并且通常更擅长指定节点信息。
Alternatively, Puppet also supports the use of dynamic values in environment configuration. Rather than defining the static values, the developer has a leverage to create custom facts that creates client environment based upon some other client attributes or an external data source. The preferred way of doing it is using a custom tool. These tools are capable of specifying a node’s environment and are generally much better at specifying node information.
Puppet Search Path
Puppet 使用一个简单的搜索路径来确定需要在目标机器上应用哪些配置。同样,当 Puppet 尝试选取需要应用的适当值时,Puppet 中的搜索路径非常有用。Puppet 搜索需要应用的值时有如下多个位置。
Puppet uses a simple search path to determine which configuration needs to be applied on the target machine. In the same way, search path in Puppet is very useful when it is trying to pick up appropriate values which needs to be applied. There are multiple locations as listed below where Puppet searches for the values which needs to be applied.
-
Value specified in the command line
-
Values specified in an environment-specific section
-
Values specified in an executable-specific section
-
Values specified in the main section