使用热模板为 openstack 实例分配一个浮动 ip

Assign a floating ip to an openstack instance using a heat template

这是我的有效模板。我使用该模板执行 'heat stack-create' 命令,它创建了一个堆栈和一个带有 ip 的实例。我可以访问一个界面来管理我的实例。从该界面,我可以创建一个浮动 IP,然后将其分配给我新创建的实例。

heat_template_version: 2013-05-23

description: >
    Docker generic server

parameters:
    image_id: {default: centos-7, description: image, type: string}
    instance_type: {default: ug1.medium, description: instance, type: string}
    key_name: {default: user, description: Name of an existing key pair to use for the instance, type: string}

resources:
    nginx_securitygroup:
      properties:
        GroupDescription: Generic security group for nginx stack
        SecurityGroupIngress:
          - {CidrIp: 10.0.0.0/8, FromPort: '80', IpProtocol: TCP, ToPort: '80'}
  type: AWS::EC2::SecurityGroup


server_securitygroup:
    type: AWS::EC2::SecurityGroup
    properties:
        GroupDescription: Generic security group from docker nginx
        SecurityGroupIngress:
            # This is needed to allow pinging the server
            - {"CidrIp": "10.0.0.0/8", "FromPort": "-1", "ToPort": "-1", "IpProtocol": "ICMP"}

            # Ssh port
            - {"CidrIp": "10.0.0.0/8", "FromPort": "22", "ToPort": "22", "IpProtocol": "TCP"}

            # Open docker ports
            - {"CidrIp": "10.0.0.0/8", "FromPort": "2375", "ToPort": "2375", "IpProtocol": "TCP"}
            - {"CidrIp": "10.0.0.0/8", "FromPort": "2376", "ToPort": "2376", "IpProtocol": "TCP"}

docker_server:
    type: AWS::EC2::Instance
    properties:

        ImageId: { get_param: image_id }
        InstanceType: { get_param: instance_type }
        KeyName: { get_param: key_name }
        SecurityGroups:
            - Ref: server_securitygroup
            - {Ref: nginx_securitygroup}


        UserData: |
            #!/bin/bash -v

            #Do some operations to start the docker container on the instance                

outputs:
    ipaddress_private:
        description: Private ip
        value:
            "Fn::GetAtt":
                - docker_server
                - PrivateIp

我的问题是我不想手动将我创建的浮动 IP 分配给实例,我希望在创建堆栈和实例时自动分配它。我尝试遵循一些文档,例如这个文档:http://blog.oddbit.com/2013/12/06/an-introduction-to-openstack-heat/

但是不行。也许是因为它试图将浮动 IP 分配给另一个现有资源(服务器)。我怎样才能让协会发挥作用?

好的,通过反复试验,我找到了要做什么...

在 'parameters' 部分,添加:

ip_address : {default: x.x.x.x, description : Floating IP address to be associated to the instance, type : string}

在 'resources' 部分:

IPAssoc :
    type : AWS::EC2::EIPAssociation
    properties :
        InstanceId : { get_resource: docker_server }
        EIP : { get_param : ip_address } 

使用 FloatingIPAssociation 资源。示例:

  association:
    type: OS::Nova::FloatingIPAssociation
    properties:
      floating_ip: { get_param: floatingIpId }
      server_id: { get_resource: instance }

与 NeutronFloatingAssociation 的其他示例:

  association:
     type: OS::Neutron::FloatingIPAssociation
     properties:
       floatingip_id: { get_param: floatingIpId }
       port_id: { get_param: port_id }