简体   繁体   English

具有弹性 IP 的 EC2 自动缩放

[英]EC2 auto scaling with elastic IP

I have deployed an auto scaling EC2 and has associated an Elastic IP address with it.我已经部署了一个自动缩放 EC2 并关联了一个弹性 IP 地址。 I'm not using a load balancer, because the total number of users doesn't exceed 20. Therefore, my current settings are to have 1 minimum and 1 maximum servers.我没有使用负载均衡器,因为用户总数不超过 20。因此,我当前的设置是最少有 1个服务器,最多有 1 个服务器。

If the EC2 server fails, another one is created instead, which is what i'm trying to do.如果 EC2 服务器出现故障,则会创建另一个服务器,这就是我正在尝试做的。 However, the elastic IP is not automatically remapped to the newly created server.但是,弹性 IP 不会自动重新映射到新创建的服务器。

How can i assign the elastic IP automatically to the newly created EC2 instance?如何将弹性 IP 自动分配给新创建的 EC2 实例? Is there a workaround this issue?这个问题有解决方法吗?

UPDATE:更新:

I've added the following to User Data, but the new EC2 is created without a public ip still.我已将以下内容添加到用户数据中,但创建的新 EC2 仍然没有公共 ip

#!/bin/bash
INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
aws ec2 associate-address --instance-id $INSTANCE_ID --allocation-id=eipalloc-**.***.***.***

Without an ELB to manage your Elastic IPs, you'll need to use the User Data field on your EC2 instance to call the aws ec2 associate-address API endpoint upon instance creation:如果没有 ELB 来管理您的弹性 IP,您将需要使用 EC2 实例上的用户数据字段在实例创建时调用aws ec2 associate-address API 端点:

aws ec2 associate-address --instance-id <instance id> --allocation-id <eip-alloc-id>

The EIP allocation ID can be found using the AWS Console.可以使用 AWS 控制台找到 EIP 分配 ID。 You can obtain the Instance ID by making this call in the User Data:您可以通过在用户数据中进行此调用来获取实例 ID:

INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)

With EC2 & Auto scaling, You need using user data in EC2 to Auto Attach Elastic IP to EC2 Instance For Auto scaling使用 EC2 和 Auto scaling,您需要使用 EC2 中的user data将弹性 IP 自动附加到 EC2 实例以进行 Auto scaling

#!/bin/bash
aws configure set aws_access_key_id "XYZ..."
aws configure set aws_secret_access_key "ABC..."
aws configure set region "ap-..."
aws ec2 associate-address --instance-id "$(curl -X GET "http://169.254.169.254/latest/meta-data/instance-id")" --public-ip your_elastic_IP

Note: you should create new user & IAM have only permission associate-address to create/get aws key注意:您应该创建新用户 & IAM 只有权限 associate-address 来创建/获取 aws 密钥

Hope it be help you:)希望对你有帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM