【问题标题】:AWS ECS using docker and ngnix, how to get my nginx config into the container?AWS ECS 使用 docker 和 nginx,如何将我的 nginx 配置放入容器中?
【发布时间】:2018-02-18 08:27:53
【问题描述】:

我正在尝试使用 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;
}

【问题讨论】:

标签: django amazon-web-services nginx


【解决方案1】:

据我所知(在撰写本文时),除了使用环境变量之外,没有“直接”的方法可以将配置注入 ECS 中的容器。

尽管如此,您可以执行以下操作:

  1. 使用 AWS CLI 从 S3 获取 nginx 配置。

  2. etcdConsul 等分布式键值存储部署到您的ECS 集群,然后从中存储和检索您需要的所有配置。这些工具通常用于共享配置和服务发现。如果您打算将 ECS 用于环境中的所有内容,这可能是一个好主意。关于 nginx,例如,您可以设置 nginx + consul + registrator + consul 模板,只需在 Consul 中更新 nginx 配置,即可自动重新加载容器内的 nginx 配置。 Here 就是一个例子。


好吧,只是说...我希望有一天 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

就是这样!

【讨论】:

    猜你喜欢
    • 2015-12-31
    • 2017-05-30
    • 2018-08-30
    • 1970-01-01
    • 1970-01-01
    • 2020-08-02
    • 2021-09-30
    • 1970-01-01
    • 2015-07-20
    相关资源
    最近更新 更多