简体   繁体   English

当使用自动缩放创建ec2时,我们可以在云形成模板中为EC2添加CNAME吗?

[英]Can we add a CNAME for an EC2 in Cloud Formation Template when ec2s are created using auto scaling?

I am working with a kafka cluster on AWS, I would like to give each broker a CNAME so I can reference that rather than the IP address. 我正在AWS上使用kafka集群,我想给每个代理一个CNAME,这样我就可以引用它而不是IP地址。

I know usually with an EC2 we can do this in the cloud formation template using something like this.. 我知道通常使用EC2我们可以使用类似的东西在云形成模板中执行此操作。

{
"Parameters": {
    "TestCname":{
        "Description": "The IpAddress for the MyInstance Ec2",
        "Type": "String",
        "Default": "domainName.com"            
    }
},
"Resources": {
    "MyInstanceCNAME": {
        "Type": "Custom::ResourceDNS",
          "Version": "1.0",
          "Properties": {                                            
            "ServiceToken": { "Fn::Sub": "arn:aws:sns:${AWS::Region}:<Account_Num>:custom-resource" },
            "Resource": "CNAME",
            "CNAME": { "Ref": "TestCname" },
            "IpAddress" : { "Fn::GetAtt" : [ "MyInstance", "PrivateIp" ]}
        }
    },
    "MyInstance": {
        "Type": "AWS::EC2::Instance",
        "Properties": {
            "AvailabilityZone": "us-east-1a",
            "ImageId": "ami-a4c7edb2",
            "InstanceType": "t2.micro"}
    }
}

The problem is that with confluent quickstart script for AWS 问题是AWS的汇总快速入门脚本

https://github.com/aws-quickstart/quickstart-confluent-kafka/blob/master/templates/confluent-kafka.template https://github.com/aws-quickstart/quickstart-confluent-kafka/blob/master/templates/confluent-kafka.template

I initially thought the creation of the ec2s must be in the referenced templates eg. 我最初认为ec2s的创建必须在引用的模板中,例如。

Fn::Sub": "https://${QSS3BucketName}.s3.amazonaws.com/${QSS3KeyPrefix}templates/nodegroup.template

But I couldn't see the instantation in that template either and have just discovered through a guy on my team that the ec2s are being created through auto scaling. 但我无法看到该模板中的瞬间,并且刚刚通过我团队中的一个人发现ec2是通过自动缩放创建的。

Can we add a CNAME and how is the question? 我们可以添加CNAME吗?问题是什么? Thanks 谢谢

EC2 Autoscaling helps you setup scaling for your EC2 quickly. EC2 Autoscaling可帮助您快速设置EC2的缩放比例。 The EC2s controlled by this can be terminated or started depending on demand or schedules. 由此控制的EC2可以根据需求或时间表终止或启动。 Because of this, it does not make sense to reference to any particular EC2. 因此,参考任何特定的EC2都没有意义。

I would like to suggest two alternatives: 我想建议两种选择:

  1. Using an AWS Elastic Load Balancer per group of EC2, each load balancer comes with its own domain but you can assign a custom one if you want. 每组EC2使用AWS Elastic Load Balancer,每个负载均衡器都有自己的域,但您可以根据需要分配自定义域。 Reference the ELB domain afterwords. 在后面引用ELB域。
  2. Using an AWS Elastic Beanstalk per group of EC2, this will manage whats underneath for you including the AutoScaling group, Load balancer, EC2, etc. Again, you will have a domain out of the box and you can set your own custom domain. 使用每组EC2的AWS Elastic Beanstalk,这将为您管理下面的内容,包括AutoScaling组,负载均衡器,EC2等。再次,您将拥有一个开箱即用的域,您可以设置自己的自定义域。 Reference the ELB domain afterwords. 在后面引用ELB域。

Here is a small cloudformation snippet of how I assign a custom domain name to my ELB: 这是一个关于如何为ELB分配自定义域名的小型cloudformation摘录:

Resources:
  MyAppEnvironment:
    Type: AWS::ElasticBeanstalk::Environment
    Properties:
      # this one also becomes a part of the domain name
      EnvironmentName: !Join ['', [!Ref MyApp, '-env']]
      ApplicationName: !Ref MyApp
      TemplateName: !Ref ConfigurationTemplate
      VersionLabel: !Ref ApplicationVersion
  DNS:
    Type: AWS::Route53::RecordSetGroup
    DependsOn: MyAppEnvironment
    Properties:
      HostedZoneName: your.hosted.zone
      RecordSets:
      - Name: !Join ['.', [!Ref appName, !Ref domain]]
        Type: CNAME
        TTL: 900
        ResourceRecords: [!GetAtt [MyAppEnvironment, EndpointURL]]

Other elastic beanstalk snippets can be found here . 其他弹性豆茎片段可以在这里找到。

Because the instances are being created with an ASG they're unavailable as CloudFormation resources. 由于实例是使用ASG创建的,因此它们不可用作CloudFormation资源。

To overcome this you could: 要克服这个问题,您可以:

  • create a script to retrieve all the public IP addresses of the instances associated with the ASG and setup Route53 CNAMEs in your hosted 创建一个脚本以检索与ASG关联的实例的所有公共IP地址,并在托管中设置Route53 CNAME
  • alternatively if you want to keep using Custom::ResourceDNS , you could again retrieve the instance IP addresses and then pass them in to your CloudFormation stack as another parameter. 或者,如果您想继续使用Custom::ResourceDNS ,您可以再次检索实例IP地址,然后将它们作为另一个参数传递到您的CloudFormation堆栈。

Not sure how many brokers you need to spin up (I am not kafka-knowledgeable), but if it is a small set of brokers.....you can block a set of ENIs and attach them using the user-data script based on a pool of available ENIs. 不确定你需要多少经纪人(我不是kafka知识渊博),但如果它是一小部分经纪人......你可以阻止一组ENI并使用基于用户数据的脚本附加它们在可用的ENI池上。 You can define a CNAME for the ENIs. 您可以为ENI定义CNAME。 Only limitation or risk is that you cannot enable the 'Delete on Termination' flag...else your ENI gets deleted too. 唯一的限制或风险是您无法启用“终止时删除”标记...否则您的ENI也会被删除。

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

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