【问题标题】:Chef - Trigger reboot service on different nodeChef - 在不同节点上触发重启服务
【发布时间】:2015-12-15 19:11:38
【问题描述】:

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

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

【问题讨论】:

    标签: chef-infra chef-recipe


    【解决方案1】:

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

    Chef 有一个功能,它是search

    考虑到这一点,我会在您的 my_app 食谱中添加两个食谱,分别命名为 master.rb 和 client.rb

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

    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 定期运行,它将在最大运行间隔的两倍内收敛您的整个集群。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-09
      • 1970-01-01
      • 2017-09-11
      相关资源
      最近更新 更多