Come posso utilizzare Amazon ECS Service Discovery con CloudFormation?

5 minuti di lettura
0

Desidero utilizzare Amazon Elastic Container Service (Amazon ECS) service discovery con AWS CloudFormation.

Breve descrizione

La console Amazon ECS consente di creare risorse di rilevamento dei servizi durante la creazione del servizio. Tuttavia, il tipo di risorsa AWS::ECS::Service per CloudFormation non consente di creare risorse di rilevamento dei servizi durante la creazione del servizio.

Nota: la console Amazon ECS utilizza le API di integrazione CreatePrivateDNSnamespace, CreateService (AWS Cloud Map) e CreateService (Amazon ECS). In CloudFormation, puoi utilizzare i tipi di risorse AWS::ServiceDiscovery::PrivateDNSnamespace, AWS: :ServiceDiscovery::Service e AWS::ECS::Service per implementare un'integrazione in un singolo modello CloudFormation.

Risoluzione

  1. Nella sezione Risorse del tuo modello CloudFormation, crea un namespace privato per l'individuazione dei servizi, ad esempio example.com. Crea il namespace in un Amazon Virtual Private Cloud (Amazon VPC) esistente. Guarda i seguenti esempi JSON e YAML.

JSON:

{
  "PrivateNamespace": {
    "Type": "AWS::ServiceDiscovery::PrivateDnsNamespace",
    "Properties": {
      "Name": "example.com",
      "Vpc": "vpc-xxxxxxx"
    }
  }
}

YAML:

PrivateNamespace:
    Type: AWS::ServiceDiscovery::PrivateDnsNamespace
    Properties:
        Name: example.com
        Vpc: vpc-xxxxxxx

Nota: sostituisci **example.com ** con un nome per il tuo namespace. Sostituisci vpc-xxxxxxx con l'ID di un VPC del tuo account.

  1. Crea un servizio di rilevamento dei servizi utilizzando l'ID dello spazio dei nomi creato nel passaggio 1.

Puoi ottenere il valore dell'ID del namespace utilizzando la funzione intrinseca Ref nel tuo modello CloudFormation. Guarda i seguenti esempi JSON e YAML.

JSON:

{
  "DiscoveryService": {
    "Type": "AWS::ServiceDiscovery::Service",
    "Properties": {
      "Description": "Discovery Service for the Demo Application",
      "DnsConfig": {
        "RoutingPolicy": "MULTIVALUE",
        "DnsRecords": [
          {
            "TTL": 60,
            "Type": "A"
          },
          {
            "TTL": 60,
            "Type": "SRV"
          }
        ]
      },
      "HealthCheckCustomConfig": {
        "FailureThreshold": 1
      },
      "Name": "example-service-name",
      "NamespaceId": {
        "Ref": "PrivateNamespace"
      }
    }
  }
}

YAML:

DiscoveryService:
  Type: AWS::ServiceDiscovery::Service
  Properties:
    Description: Discovery Service for the Demo Application
    DnsConfig:
      RoutingPolicy: MULTIVALUE
      DnsRecords:
        - TTL: 60
          Type: A
        - TTL: 60
          Type: SRV
    HealthCheckCustomConfig:
      FailureThreshold: 1
    Name: example-service-name
    NamespaceId: !Ref PrivateNamespace

Nota: sostituisci example-service-name con il nome del tuo servizio.

La proprietà DNSConfig specifica i record DNS che vengono creati automaticamente in Amazon Route 53. Questi record DNS vengono creati quando l'attività Amazon ECS viene registrata con il servizio di ricerca del servizio. Sono supportati i record DNS di tipo A e SRV. Per ulteriori informazioni, consulta Configurazione del servizio per l'utilizzo del rilevamento servizi.

Suggerimento: è consigliabile utilizzare la proprietà HealthCheckCustomConfig per consentire ad Amazon ECS di segnalare lo stato delle attività del rilevamento servizi. Amazon ECS utilizza le informazioni provenienti dai controlli dei container, dai controlli dell'integrità e dallo stato delle attività per aggiornare l'integrità del rilevamento servizi con AWS Cloud Map.

  1. Crea un servizio Amazon ECS che si integri con il namespace e il servizio di rilevamento servizi che hai creato nei passaggi precedenti. Usa la proprietà ServiceRegistries nella risorsa AWS:ECS::Service per specificare la tua integrazione. Guarda i seguenti esempi JSON e YAML.

JSON:

{
  "EcsService": {
    "Type": "AWS::ECS::Service",
    "Properties": {
      "Cluster": "awsExampleCluster",
      "DesiredCount": 4,
      "LaunchType": "FARGATE",
      "NetworkConfiguration": {
        "AwsvpcConfiguration": {
          "SecurityGroups": [
            "sg-xxxxxxx"
          ],
          "Subnets": [
            "subnet-xxxxxxx"
          ]
        }
      },
      "ServiceName": "awsExampleService",
      "TaskDefinition": "awsExampleTaskDefinition",
      "ServiceRegistries": [
        {
          "RegistryArn": {
            "Fn::GetAtt": [
              "DiscoveryService",
              "Arn"
            ]
          },
          "Port": 80
        }
      ]
    }
  }
}

YAML:

EcsService:
  Type: AWS::ECS::Service
  Properties:
    Cluster: awsExampleCluster
    DesiredCount: 4
    LaunchType: FARGATE
    NetworkConfiguration:
      AwsvpcConfiguration:
        SecurityGroups:
          - sg-xxxxxxx
        Subnets:
          - subnet-xxxxxxx
    ServiceName: awsExampleService
    TaskDefinition: awsExampleTaskDefinition
    ServiceRegistries:
      - RegistryArn: !GetAtt DiscoveryService.Arn
        Port: 80

Nota: sostituisci awsExampleCluster con il tuo cluster. Sostituisci awsExampleService con il tuo servizio. Sostituisci awsExampleTaskDefinition con la tua definizione di attività. Sostituisci sg-xxxxxxx con l'ID del tuo gruppo di sicurezza. Sostituisci subnet-xxxxxxx con il tuo ID di sottorete.

Per RegistryArn, utilizza l'Amazon Resource Name (ARN) del servizio di ricerca del servizio che desideri integrare con il tuo servizio Amazon ECS. Usa la funzione intrinseca Fn::GetAtt per ottenere questo valore dal tuo modello CloudFormation.

Se usi la modalità awsvpc, imposta il valore su Port o su una combinazione di ContainerPort e ContainerName (come specificato nella definizione dell'attività). Se utilizzi la modalità host o bridge, imposta il valore su ContainerPort o ContainerName (come specificato nella definizione dell'attività).

  1. Apri la console CloudFormation, quindi scegli Crea stack per distribuire le risorse in base al modello aggiornato.

  2. Per verificare che le tue attività possano essere risolte dal tuo VPC, esegui i seguenti comandi:

$ dig awsExampleService.awsExampleNamespace. +short
$ dig srv awsExampleService.awsExampleNamespace. +short
$ curl awsExampleService.awsExampleNamespace. -I

Nota: il valore di awsExampleService è il nome della tua risorsa AWS::ECS::Service. Il valore di awsExampleNamespace è il nome della tua risorsa AWS::ServiceDiscovery::PrivateDNSnamespace.

I comandi restituiscono un output simile al seguente:

Per $ digita awsExampleService.awsExampleNamespace. +short:

172.31.182.0
172.31.160.124
172.31.137.81
172.31.149.244

Per $ digita srv awsExampleService.awsExampleNamespace. +short:

1 1 80 ffe95d27ea8d4f7aba0dfed87297fc5a.awsExampleService.awsExampleNamespace.
1 1 80 44a17fa781974a93bb563bc1826a8697.awsExampleService.awsExampleNamespace.
1 1 80 d640ecb3d283421bb2d1318caf4b0d66.awsExampleService.awsExampleNamespace.
1 1 80 65aff6fff33144b2ad79d283ab52cfe9.awsExampleService.awsExampleNamespace.

Per $ curl awsExampleService.awsExampleNamespace. -I:

HTTP/1.1 200 OK
Server: nginx/1.15.12
Date: Wed, 15 May 2019 02:25:19 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 16 Apr 2019 13:08:19 GMT
Connection: keep-alive
ETag: "5cb5d3c3-264"
Accept-Ranges: bytes

AWS UFFICIALE
AWS UFFICIALEAggiornata 2 anni fa