Chef - 在不同节点上触发重启服务

Chef - Trigger reboot service on different node

我有一些应用程序的主节点和客户端节点,我想使用 Chef 进行管理。它们都指向一个包含配置文件的共享文件夹,其中包含有关所有客户端和主服务器的信息。因此,每次安装客户端应用程序(在另一个客户端节点上)并将其主机名添加到该共享文件时,主节点上的应用程序应为 reloaded/rebooted。

关于如何从客户端节点触发主节点上主应用程序重启的任何想法?

停止使用共享文件,这是您体系结构中的单点故障,并且会遇到非常非常多的并发问题。

Chef 有一个功能,它是 search

考虑到这一点,我会做一些事情,在你的 my_app 食谱中加入两个食谱,分别命名为 master.rb 和 client.rb

在client.rb除了安装客户端,还要给节点加个tag。 (或使用角色来定义哪些是客户等)

tag('my_app_client') if !tagged?('my_app_client')

master = search(:node, 'tag:my_app_master') 
slaves = search(:node, 'tag:my_app_client') 

#Tricky line to add current node to the list of slaves, as in first run it won't have been indexed by chef.
slaves[] << node if !slaves.any? { |n| n['hostname'] == node['hostname'] } 

return is master.empty? # to avoid trying to write a file without master

template '/local/path/to/conffile' do
  source 'config.erb' 
  variables({ 
    :master => master
    :slaves => slaves
  })
end

在 master.rb 中,重复搜索和模板化:

tag('my_app_master') if !tagged?('my_app_master')

master = search(:node, 'tag:my_app_master') 
slaves = search(:node, 'tag:my_app_client') 

#Tricky line to add current node as the master, as in first run it won't have been indexed by chef.
master = node if !master.any? { |n| n['hostname'] == node['hostname'] } 

template '/local/path/to/conffile' do
  source 'config.erb' 
  variables({ 
    :master => master
    :slaves => slaves
  })
  notify :restart,"service[my_app_service]", :immediately
end

并且在 config.erb 文件中例如:

master_host: <%= @master['hostname'] %>
<%- @slaves.each_with_index do |s,i|
  slave_host<%= i %>: <%= s['hostname'] %>
<%- end %>

有一些索引延迟需要管理,如果 chef-运行 订单没有仔细计划,每台机器上的文件可能会有点不同步。

如果您定期保持 chef 运行ning,它最多会在 运行 间隔的两倍内收敛您的整个集群。