【问题标题】:Docker compose ECS AWS Application Load balance redirectDocker 撰写 ECS AWS 应用程序负载均衡器重定向
【发布时间】:2022-03-03 13:02:08
【问题描述】:

我正在通过我的 docker compose 文件部署我的 docker 容器。

当我最初部署我的应用程序时,它会创建一个负载均衡器,但类型为“网络”。我无法使用这种类型重定向到 https。

我希望将我的负载均衡器创建为“应用程序”类型,然后设置从 http 到 https 的重定向。

我的容器仍将监听 80 端口。

奖励:我想在我的撰写文件中部署并附加我的 SSL 证书,以便在新部署时一切就绪。

如您所见,我尝试了一些方法,但无法正常工作。

谢谢

version: '3.8'
services:

  web:
    container_name: auction_web
    image: <ECR Image>
#    x-aws-pull_credentials: arn:aws:secretsmanager:xxxxxxxxxxxx
    depends_on:
      - redis
    ports:

#      - "80:80" - tried this
#      - "443:443" - tried this
      - target: 80
        x-aws-protocol: http
      - target: 443
        x-aws-protocol: https
#      - published: 80
#        protocol: "http"
#        x-aws-alb-default-actions:
#          - type: redirect
#            host: '<domain>'
#            port: 443
#            protocol: HTTPS
#            status-code: HTTP_301

#      - published: 443
#        protocol: "https"
#        x-aws-acm-certificate: <cert name>
    deploy:
      resources:
        limits:
          cpus: '1'
          memory: 4096M

【问题讨论】:

标签: docker docker-compose amazon-ecs aws-load-balancer


【解决方案1】:

请试试这个:

  1. 使用“80:80”和“443:443”定义端口,仅此而已。

  2. 在 docker-compose 文件的底部添加以下部分:

x-aws-cloudformation:
  Resources:
    Web443Listener:
      Properties:
        Certificates:
          - CertificateArn: "<certificate ARN>"
        Protocol: HTTPS
        Port: 443
    Web80Listener:
      Properties:
        DefaultActions:
          - Type: redirect
            RedirectConfig:
              Port: 443
              Protocol: HTTPS
              StatusCode: HTTP_301

【讨论】:

  • 谢谢你。我做了一些修改,但我可以通过这种方式对 ecs 的撰写进行更多控制,所以我将整个 yml 文件设为原始模板。就最佳实践而言,这是正确的方法吗?还是我应该删除与 docker 相关的所有内容并开始使用 aws cli 进行部署
【解决方案2】:
x-aws-cloudformation:
  Resources:
    App80Listener:
      Properties:
        Port: 80
        Protocol: HTTP
        LoadBalancerArn:
          Ref: LoadBalancer
        DefaultActions:
          - Type: redirect
            RedirectConfig:
              Port: 443
              Protocol: HTTPS
              StatusCode: HTTP_301
      Type: AWS::ElasticLoadBalancingV2::Listener

    App443Listener:
      Properties:
        Port: 443
        Protocol: HTTPS
        LoadBalancerArn:
          Ref: LoadBalancer
        DefaultActions:
          - ForwardConfig:
              TargetGroups:
                - TargetGroupArn:
                    Ref: App8080TargetGroup
            Type: forward
        Certificates:
          - CertificateArn: "<arn for cert>"
      Type: AWS::ElasticLoadBalancingV2::Listener

    App8080TargetGroup:
      Properties:
        Name: 'jenkins-tg'
        Port: 8080
        Protocol: HTTP
        Tags:
          - Key: com.docker.compose.project
            Value: jenkins
        TargetType: ip
        VpcId: vpc-d21afbbb
        HealthCheckPath: '/login'
      Type: AWS::ElasticLoadBalancingV2::TargetGroup

【讨论】:

    猜你喜欢
    • 2019-12-17
    • 2020-02-25
    • 2020-08-21
    • 2018-02-26
    • 2017-03-10
    • 2017-02-11
    • 2017-01-04
    • 1970-01-01
    • 2021-02-01
    相关资源
    最近更新 更多