Puppet: relationship between resources on differen

2019-05-02 00:20发布

问题:

I know that we can specify relationship between resources, which determines the deployment order. But is it possible to create relationship between resources on different nodes in Puppet?

For example, I have apache web server in node A and mysql server in node B. I want to start mysql first before starting apache web server. How could I express this in Puppet language?


I have tried the following codes:

node ‘host1’ {
  @@service { ‘mysql’:
    ensure => running,
    tag => ‘host1-mysql’,
  }
}
node ‘host2’ {
  service { ‘apache2’:
    ensure => running,
  }
  Service<<| tag == ‘host1-mysql’ |>> -> Service[‘apache2’]
}

But it didn't work - produced a compile error. Any other solution?

回答1:

In a distributed "puppet" setup, the apply order isn't guaranteed.

Puppet don't do orchestration across multiple nodes. At best, your changes will get applied multiple times on the machines and finally will converge to the desired state.

Dependencies only work in a same node. You can actually get values of resources exported by others nodes (for eg to configure the firewall of your db to allow the web server to do sql) Or cheat with hiera to know who have the "db" and "app" roles.

For orchestration see tools like mcollective, capistrano, ansible,...



回答2:

Whenever a resource depends on another resource, use the before or require metaparameter or chain the resources with ->. Whenever a resource needs to refresh when another resource changes, use the notify or subscribe metaparameter or chain the resources with ~>. Some resources will autorequire other resources if they see them, which can save you some effort.

Refer to link for more precision.

In init.pp where you declare/instantiate these classes, replace the include statements with parameterized class syntax:

class {"taskname":} -> class {"taskname2":}
This will ensure taskname is invoked before taskname2.

Check link



回答3:

A bit of googling for puppet node dependency turns up some links about exported resources, which seems like the way to go.

According to http://docs.puppetlabs.com/puppet/latest/reference/lang_exported.html

Exported resources allow nodes to share information with each other.

Basically you export a resource in the node which it belongs to, and collect it in the node that requires it and use dependency arrows as usual.

MCollective also appears to be an alternative, but seems to be a whole new framework.



回答4:

You can't do that just using Puppet. Conceptually, with Puppet you generate a static description of a node configuration, and then you apply it. The exported resources system is convenient, but doesn't fundamentally changes the way Puppet works (you can convince yourself by noticing you could just hardcode the resources, and this would have the exact same effect).

For this reason, you need another kind of orchestration system, and you should not set enable => running on your services.

Unfortunately, I do not have great recommendations :

  • I only used heartbeat, which in my experience was hard to configure, unreliable, and very limited (only a handful of nodes are supported).
  • Ubuntu has a juju tool that does exactly what you want and seems easy to use, but I have no experience with it.
  • There is probably something equivalent in your distribution of choice.