puppet-mongodb 模块
MongoDB 是一个基于分布式文件存储的数据库,旨在为 Web 应用提供可扩展的高性能数据存储解决方案。 puppetlabs-mongodb
模块是由 Puppet 公司维护的官方项目,用于管理 MongoDB 服务,包括:
- 配置 mongodb server(包括不同模式)
- 配置 mongodb client
- 配置 mongos
- 管理安装源
puppet-mongodb
项目地址: https://github.com/puppetlabs/puppetlabs-mongodb
1.先睹为快
不想看下面大段的代码解析,已经跃跃欲试了?
OK,我们开始吧!
打开虚拟机终端并输入以下命令:
$ puppet apply -e "include mongodb::server,mongodb::client" -v
在看到赏心悦目的绿字后,Puppet 已经完成了 MongoDB 服务的安装,配置和启动,输入 mongo 就可以进入命令行界面了。
2.代码讲解
2.1 class mongodb
class mongodb
的代码比较简单,声明了 class mongodb::server
。
2.2 class mongodb::server
2.2.1 类包含和链式箭头
在该类中,有一段复杂的代码:
if ($ensure == 'present' or $ensure == true) {
if $restart {
anchor { 'mongodb::server::start': }
-> class { 'mongodb::server::install': }
# If $restart is true, notify the service on config changes (~>)
-> class { 'mongodb::server::config': }
~> class { 'mongodb::server::service': }
-> anchor { 'mongodb::server::end': }
} else {
anchor { 'mongodb::server::start': }
-> class { 'mongodb::server::install': }
# If $restart is false, config changes won't restart the service (->)
-> class { 'mongodb::server::config': }
-> class { 'mongodb::server::service': }
-> anchor { 'mongodb::server::end': }
}
} else {
anchor { 'mongodb::server::start': }
-> class { '::mongodb::server::service': }
-> class { '::mongodb::server::config': }
-> class { '::mongodb::server::install': }
-> anchor { 'mongodb::server::end': }
}
在 Puppet 中,非常特别的一点是:资源和类的执行顺序并不是由上到下的。其背后是由于 Puppet 本身的设计机制产生了这一结果,在此我们不做展开。
而链式箭头(chain arrow) 用于指定资源的执行顺序,一共有两种类型的运算符:
- 序列箭头
->
,左侧资源的执行顺序优于右侧 - 通告箭头
~>
,左侧资源执行后,右侧资源将会刷新
在 mongodb::server
中,若$restart 为 true,则执行以下代码段:
anchor { 'mongodb::server::start': }
-> class { 'mongodb::server::install': }
-> class { 'mongodb::server::config': }
~> class { 'mongodb::server::service': }
-> anchor { 'mongodb::server::end': }
表示 MongoDB 配置文件的变化( mongodb::server::config
) 将触发 MongoDB Server 端服务的重启( mongodb::server::service
)。
若$restart 为 false,则执行以下代码段:
anchor { 'mongodb::server::start': }
-> class { 'mongodb::server::install': }
-> class { 'mongodb::server::config': }
-> class { 'mongodb::server::service': }
-> anchor { 'mongodb::server::end': }
}
表示 MongoDB 配置文件的变更在管理 MongoDB Server 端服务之前。
其次,在 Puppet 中并无法通过 include 加上链式箭头声明的方式来指定类的执行顺序,或者说在类中无法通过 include 的方式包含(contain) 一个类。
由于 include 和 contain 翻译成中文都可以理解为包含或者含有,从字面理解来看比较晦涩,我们通过举例说明。
class first {
notify { 'foo': }
}
class second {
notify { 'bar': }
}
class classa {
include first
}
class classb {
include second
}
Class['classa'] -> Class['classb']
include classa
include classb
无论将两个类之间的执行顺序如何改变,其输出结果可能是"foo bar"也可能是"bar foo"。
而 anchor 是解决这个问题的方法之一,其格式通常如下:
anchor{'start':} -> class{'new_class':} -> anchor{'end':}
通过这种方式使得 new_class 类被包含,从而可以指定类的依赖顺序。例如:
anchor { 'mongodb::server::start': }
-> class { '::mongodb::server::service': }
-> class { '::mongodb::server::config': }
-> class { '::mongodb::server::install': }
-> anchor { 'mongodb::server::end': }
}
其执行顺序是:
- mongodb::server::service
- mongodb::server::config
- mongodb::server::install
在 Puppet 3.4.0 之前,使用 anchor
资源类型是解决类包含类的唯一方法。
在 Puppet 3.4.0 之后,新增了函数 contain 的方法来解决这个问题。但在使用 contain 声明多个 class 时,无法和 anchor 一样同时配合链式箭头使用,而需要单独声明。如:
class a {
notify { 'a':}
}
class b {
notify { 'b':}
}
class include_class {
contain a
contain b
Class['a']->Class['b']
}
2.2.2
MongoDB 分为三种模式:StandAlone,Replication 和 Sharding。
StandAlone 是标准单机环境,Replication 是主从结构,一个 Primary,多个 Secondary,Sharding,share nothing 的结构,每台机器只存一部分数据。mongod 服务器存数据,mongos 服务器负责路由读写请求,元数据存在 config 数据库中。
创建 MongoDB server 时可以设置为 config server 或者 shard server,对应的参数为 configsvr 或 shardsvr,但是只能选择其一。
同时,在 mongodb::server 中可以通过 replset 参数来配置副本集的名称,通过 replset_config 或 replset_members 指定副本集中的成员,当然 replset_members 也是要转换为 replset_config 的。
$replset_config_REAL = {
"${replset}" => {
'ensure' => 'present',
'members' => $replset_members
}
}
2.3 class mongodb::client
mongodb::client
用于安装 MongoDB 客户端,声明了 mongodb::client::install,其代码结构和 mongodb::server 相似。
2.4 class mongodb::db
class mongodb::db
用于创建 MongoDB 数据库,创建数据库时可以传入密码或者是一个 hash 的密码,调用方式如下:
mongodb::db { 'testdb':
user => 'user1',
# password_hash 是'user1:mongo:pass1'的 md5 值
password_hash => 'a15fbfca5e3a758be80ceaf42458bcd8',
}
2.5 class mongodb::mongos
mongodb::mongos 用于配置 Mongo Shard 进程,其代码结构和 mongodb::server 相似,通过声明 install、config、service 三个 class 来配置 mongos,在这就不再赘述。
2.6 class mongodb::repo
class mongodb::repo
用于配置安装源,也支持通过 repo_location 参数自己配置安装源。
3.扩展阅读
- Containment https://docs.puppet.com/puppet/4.10/lang_containment.html
- What is Class containment https://puppet.com/blog/class-containment-puppet
- Relationships and orderings https://docs.puppet.com/puppet/5.0/lang_relationships.html#syntax-chaining-arrows
4.动手练习
- 配置一个 mongo 集群,使用 Replication 模式
- 配置一个 mongo 集群,使用 sharding 模式
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论