使用 Knife 启动多个 EC2 服务器并从池中分配弹性 IP

使用 Knife 启动多个 EC2 服务器并从池中分配弹性 IP

我想知道如何使用同类型的 Knife 启动多个 EC2 服务器,比如 4 个应用服务器,并从可用弹性 IP 池中为每个服务器分配一个弹性 IP。我已使用 OpsCode 的 ASW cookbook 为 EC2 服务器分配了弹性 IP,使用说明告诉我使用数据包来存储 IP,但它们的说明似乎仅在您启动单个服务器时才有意义。

我的理想用例是创建我的应用服务器食谱,并在配方中 ping Chef 服务器以从池中找出可用的弹性 IP。我不介意手动定义该池,但我似乎无法弄清楚如何从池中删除已占用的 IP,以便只分配可用的 IP。

有任何想法吗?

答案1

以下是我得出的答案:

#
# Cookbook Name:: app-server
# Recipe:: ec2
# 
include_recipe "aws" # http://community.opscode.com/cookbooks/aws

# Assign an Elastic IP to the node on first run
if node[:eip_address].nil?
  # I created two data bags, one for holding AWS credentials,
  # the other for holding on to my IP pool. I pass the names of the 
  # individual bags, in my case, via some environment attributes
  aws = data_bag_item("aws", node[:aws_setup][:credentials])
  eip = data_bag_item("elastic-ips", node[:aws_setup][:eip])

  ip_pool = eip["eip_pool"]

  # Search for servers, and remove used IPs from available list
  search(:node, "chef_environment:#{node.chef_environment} AND role:YOUR_SERVER_ROLE") do |matching_node|
    ip_pool.delete_if {|ip| ip == matching_node[:eip_address]}
  end

  # Attach IP (unless there are no more EIPs in the pool)
  if ip_pool.length <= 0
    Chef::Log.fatal("There are no more EIPs available. Please allocate more and add to the elastics-aps/#{node[:aws_setup][:eip]} data_bag")
  else
    aws_elastic_ip "eip-assignment" do
      aws_access_key aws['aws_access_key']
      aws_secret_access_key aws['aws_secret_access_key']
      ip ip_pool[0]
      action :associate
    end
    # Assign this new IP to the current node, and save
    node.set[:eip_address] = ip_pool[0]
    node.set[:ec2][:public_ipv4] = node[:eip_address]
    node.save
  end
end

相关内容