簡體   English   中英

如何使用Fargate在AWS ECS上配置基於Laravel的應用程序?

[英]How to configure a Laravel based app on AWS ECS using Fargate?

編輯:我的第一個問題是“如何使用Fargate鏈接AWS ECS上的任務定義中的容器?” 但是,從一開始我可能是錯的,所以我改變了我的問題,並保留以下內容:

我正在嘗試通過ECS在AWS上部署一個簡單的基於Laravel的應用程序。 我的服務在本地使用docker-compose-yml文件按預期工作。

但是在AWS上我得到: "nginx: [emerg] host not found in upstream "app" in /etc/nginx/conf.d/default.conf:12"我的Web容器日志"nginx: [emerg] host not found in upstream "app" in /etc/nginx/conf.d/default.conf:12"

這里是我服務的容器:web(nginx),app(Laravel),數據庫(MySQL)和緩存(redis)。

我知道任務描述的所有容器共享相同的命名空間,因此不需要鏈接容器(我們不能使用鏈接屬性與Fargate)。

你能幫我解決一下這個問題嗎? 我瞎了。

這是我工作的本地docker-compose.yml文件:

version: '2'
services:

  #  The Application
  app:
    image: 696759765437.dkr.ecr.us-east-1.amazonaws.com/ali-
maison/tribe-migrate
    volumes:
      - /var/www/storage
    env_file: '.env'
    environment:
      - "DB_HOST=database"
      - "REDIS_HOST=cache"

  # The Web Server
  web:
    image: 696759765437.dkr.ecr.us-east-1.amazonaws.com/ali-maison/laravel-web
    ports:
      - 80:80

  # The Database
  database:
    image: mysql:5.6
    volumes:
      - dbdata:/var/lib/mysql
    environment:
      - "MYSQL_DATABASE=homestead"
      - "MYSQL_USER=homestead"
      - "MYSQL_PASSWORD=secret"
      - "MYSQL_ROOT_PASSWORD=secret"

  # redis
  cache:
    image: redis:3.0-alpine

volumes:
  dbdata:

這是我的web容器Dockerfile:

FROM nginx:1.10

ADD vhost.conf /etc/nginx/conf.d/default.conf
WORKDIR /var/www

我的vhost.conf:

server {
    listen 80;
    index index.php index.html;
    root /var/www/public;

    location / {
        try_files $uri /index.php?$args;
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

從vhost.conf:

fastcgi_pass app:9000;

主機名“app”適用於docker-compose,因為這是您在docker-compose.yml中命名的服務。 docker-compose為它必須管理的容器創建一個新網絡,該網絡使容器能夠通過主機名相互引用而無需進一步配置。

Fargate沒有此功能,因此“app”無法解決。

Fargate目前允許您使用的唯一網絡模式是一種名為awsvpc的特殊AWS模式。 在Fargate中運行的每個任務都有自己的彈性網絡接口(ENI),這意味着每個任務都有自己的私有IP地址。 您還可以將ENI配置為具有公共IP地址,就像在EC2實例上使用ENI一樣。


如何從nginx容器中解析您的app容器

在Fargate中,深入查看應用程序服務器的運行任務定義(“群集”>“任務”選項卡>單擊應用程序服務器的“任務”列中的容器ID)。 網絡部分將擁有私有IP。

在你的conf中用這個ip替換主機名“app”:

fastcgi_pass <private_ip>:9000;

對於可能遇到這種情況的其他人,我遇到了同樣的問題,在aws ecs fargate上連接同一個任務中運行的兩個容器,發現我必須在vhost.conf中用'localhost'替換'app',並且它有效。 (這在另一個答案的一些評論中提到過)所以vhost.conf看起來像:

server {
    listen 80;
    index index.php index.html;
    root /var/www/public;

    location / {
        try_files $uri /index.php?$args;
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass localhost:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

我還確保在應用程序容器上映射並公開了端口9000(可能沒有必要)

#  The Application
app:
  image: 696759765437.dkr.ecr.us-east-1.amazonaws.com/ali-maison/tribe-migrate
  volumes:
    - /var/www/storage
  env_file: '.env'
  environment:
    - "DB_HOST=database"
    - "REDIS_HOST=cache"
  ports:
    - 9000:9000

解決方案是:

  1. 確保兩個容器都在同一個任務中定義,例如nginx-php
  2. 使用localhost:port例如localhost:9000 for php-fpm

第二部分如上所述,但沒有明確說明兩個容器必須處於同一個任務中。

您可以在此處找到更多信息: https//github.com/aws-samples/amazon-ecs-fargate-aspnetcore/blob/master/README.md

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM