簡體   English   中英

AWS ECS使用docker和ngnix,如何將我的nginx配置放入容器?

[英]AWS ECS using docker and ngnix, how to get my nginx config into the container?

我正在嘗試使用Nginx,unicorn和Django在AWS中設置ECS集群。

我已將docker-compose.yml轉換為ECS任務,但我想知道如何將我的nginx配置放入容器中?

這是我在json的任務

我已經為文件創建了一些掛載點,但我不確定如何在那里獲取配置。 當我從本地服務器運行docker時,nginx配置文件對於compose文件是本地的,顯然在aws中它不是?

如何通過ecs將nginx配置納入包含?

{
    "containerDefinitions": [
        {
            "volumesFrom": null,
            "memory": 300,
            "extraHosts": null,
            "dnsServers": null,
            "disableNetworking": null,
            "dnsSearchDomains": null,
            "portMappings": [
                {
                    "containerPort": 8000,
                    "protocol": "tcp"
                }
            ],
            "hostname": null,
            "essential": true,
            "entryPoint": null,
            "mountPoints": [
                {
                    "containerPath": "/static",
                    "sourceVolume": "_Static",
                    "readOnly": null
                }
            ],
            "name": "it-app",
            "ulimits": null,
            "dockerSecurityOptions": null,
            "environment": null,
            "links": null,
            "workingDirectory": "/itapp",
            "readonlyRootFilesystem": null,
            "image": "*****.amazonaws.com/itapp",
            "command": [
                "bash",
                "-c",
                "",
                "python manage.py collectstatic --noinput && python manage.py makemigrations && python manage.py migrate && gunicorn itapp.wsgi -b 0.0.0.0:8000"
            ],
            "user": null,
            "dockerLabels": null,
            "logConfiguration": null,
            "cpu": 0,
            "privileged": null
        },
        {
            "volumesFrom": null,
            "memory": 300,
            "extraHosts": null,
            "dnsServers": null,
            "disableNetworking": null,
            "dnsSearchDomains": null,
            "portMappings": [
                {
                    "hostPort": 80,
                    "containerPort": 8000,
                    "protocol": "tcp"
                }
            ],
            "hostname": null,
            "essential": true,
            "entryPoint": null,
            "mountPoints": [
                {
                    "containerPath": "/etc/nginx/conf.d",
                    "sourceVolume": "_ConfigNginx",
                    "readOnly": null
                },
                {
                    "containerPath": "/static",
                    "sourceVolume": "_Static",
                    "readOnly": null
                }
            ],
            "name": "nginx",
            "ulimits": null,
            "dockerSecurityOptions": null,
            "environment": null,
            "links": [
                "it-app"
            ],
            "workingDirectory": null,
            "readonlyRootFilesystem": null,
            "image": "nginx:latest",
            "command": null,
            "user": null,
            "dockerLabels": null,
            "logConfiguration": null,
            "cpu": 0,
            "privileged": null
        }
    ],
    "placementConstraints": [],
    "volumes": [
        {
            "host": {
                "sourcePath": "./config/nginx"
            },
            "name": "_ConfigNginx"
        },
        {
            "host": {
                "sourcePath": "./static"
            },
            "name": "_Static"
        }
    ],
    "family": "it-app-task",
    "networkMode": "bridge"
}

ngnix配置

upstream web {  
  ip_hash;
  server web:8000;
}
server {
    location /static/ {    
        autoindex on;    
        alias /static/; 
    }
    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_pass http://web/;
    }
    listen 8000;
    server_name localhost;
}

據我所知(截至本文撰寫時),除了使用環境變量之外,沒有“直接”方式將配置注入ECS中的容器。

盡管如此,這里有一些你可以做的事情:

  1. 使用AWS CLI從S3獲取nginx配置。

  2. 將分布式鍵值存儲(如etcdConsul)部署到ECS群集,然后存儲並檢索所需的所有配置。 這些工具通常用於共享配置和服務發現。 如果您打算將ECS用於您環境中的所有內容,這可能是一個好主意。 例如,關於nginx,您可以設置nginx + consul + registrator + consul模板,通過更新Consul中的nginx配置,自動在容器內重新加載nginx配置。 是一個例子。


好吧,只是說...我希望有一天AWS可以在Kubernetes上提供類似ConfigMapSecrets的東西。 在Kubernetes,這將是如此簡單:

  1. 將nginx配置添加到Kubernetes集群: kubectl create configmap nginx-configmap --from-file=nginx.conf

  2. 在您的Pod定義中定義(如“ECS任務定義”),您希望Kubernetes將配置注入您的容器:

volumeMounts: - name: nginx-config-volume mountPath: /etc/nginx ... volumes: - name: nginx-config-volume configMap: name: nginx-configmap

就是這樣!

暫無
暫無

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

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