我怎样才能设置一个弹性魔豆泊坞窗容器中的每个实例ENV变量? [英] How can I set a per-instance env variable for an elastic beanstalk docker container?

查看:222
本文介绍了我怎样才能设置一个弹性魔豆泊坞窗容器中的每个实例ENV变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的弹性单容器魔豆泊坞窗应用程序负载的多个EC2实例平衡运行。

Our elastic single-container beanstalk docker application runs as load balanced with multiple ec2 instances.

我想通过它作为一个环境变量的泊坞窗容器中运行的机器的EC2实例ID。 (我想避免这样做AWS具体的东西的容器内)。

I want to pass the ec2 instance id of the machine it's running on as an environment variable to the docker container. (I want to avoid doing AWS specific stuff inside the container).

我想我需要把一些东西在.ebextension配置文件,在那里我做了卷曲获取实例数据,然后将其设置成将传递到泊坞窗容器中的环境变量中。

I figure I need to put something in an .ebextension config file, where I do a curl to get the instance data and then set it into an environment variable that will be passed to the docker container.

这样的事情(不工作;它不会导致EB错误,但的环境变量是不可用的容器内):

Something like this (which doesn't work; it doesn't cause EB errors, but the env var is not available inside the container):

container_commands:
  set_instance_id:
    command: export EC2_INSTANCE_ID=`curl -s http://169.254.169.254/latest/meta-data/instance-id`

在理想情况下,我想,以避免黑客的EB运行脚本,因为他们没有证件,似乎更改,恕不另行通知。

Ideally, I'd like to avoid hacking the EB run scripts, because they're undocumented and seem to change without notice.

推荐答案

不幸的是,在这一刻,您不可以做,你想不弄乱你的应用程序与AWS具体的东西或修改的东西EB脚本/部署过程。

Unfortunately, at this moment, you can not do the thing that you want without cluttering your app with AWS specific stuff or modifying the EB script/deployment process.

选项#1:检查AWS元数据从你的应用程序

您可以直接卷曲的AWS元数据(的 http://169.254.169.254/latest/meta-data/instance-id 直接)从泊坞容器。

You can directly curl the AWS Metadata (http://169.254.169.254/latest/meta-data/instance-id) directly from your Docker container.

运行<

这是你的应用程序,如果没有环境名称 EC2_INSTANCE_ID (或任何你想要的),只要调用AWS元数据服务来获得实例ID。仅供参考,169.254.0.0/16是链路本地地址。您也可以检测到您的应用程序在AWS与否

From your app, if there is no environment name EC2_INSTANCE_ID (or anything you want), just call the AWS metadata service to get the instance id. FYI, 169.254.0.0/16 is a link-local address. You can also detect your app is in AWS or not.

选项#2:注入Dockerfile与环境变量

Dockerfile可以使用 ENV 关键词包含环境变量。我们可以使用 ENV 到Dockerfile注入了新的环境变量。环境变量注入您的Dockerfile必须完成后,你的应用程序中提取并泊坞窗图像前建成。

Dockerfile can contain environment variable by using ENV keyword. We can inject a new environment variable using ENV into your Dockerfile. The environment variable injection your Dockerfile must be done after your app is extracted and before the Docker images is built.

注入Dockerfile可以通过添加pre-应用程序部署挂钩来完成。只要创建一个内部的新文件中的 appdeploy / pre 使用 .ebextensions

Injecting Dockerfile can be done by adding a pre-app-deployment hook. Just create a new file inside the appdeploy/pre using .ebextensions:

files:
  "/opt/elasticbeanstalk/hooks/appdeploy/pre/02injectdockerfile.sh":
    mode: "000755"
    content: |
      . /opt/elasticbeanstalk/hooks/common.sh
      EB_CONFIG_APP_CURRENT=$(/opt/elasticbeanstalk/bin/get-config container -k app_deploy_dir)
      cd $EB_CONFIG_APP_CURRENT
      echo "ENV EC2_INSTANCE_ID \"`curl -s http://169.254.169.254/latest/meta-data/instance-id`\"" >> Dockerfile

为什么一定要在pre-appdeploy做些什么呢?或者,我们可以只使用 container_commands

container_commands 将要执行的应用程序部署为<一之前href="http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html#customize-containers-format-container_commands"相对=nofollow>文档说。它看起来像有希望的,但我们不能使用它。该 container_commands 将要执行的Dockerfile建成后(泊坞窗构建)。要在Dockerfile使用环境变量中,我们需要注入的 ENV 运行之前泊坞窗构建

The container_commands will be executed just before the app is deployed as the documentation said that. It looks like promising, but we can't use it. The container_commands will be executed after the Dockerfile is built (docker build). To use environment variable in Dockerfile, we need to inject the ENV before run docker build.

看看到弹性魔豆:发动机罩下的。这里是appdeploy钩的文件结构:

Take a look into Elastic Beanstalk: Under the Hood. Here is the file structure of appdeploy hook:

[ec2-user@ip-172-31-62-137 ~]$ tree /opt/elasticbeanstalk/hooks/appdeploy/
/opt/elasticbeanstalk/hooks/appdeploy/
├── enact
│   ├── 00run.sh
│   └── 01flip.sh
├── post
│   └── 01_monitor_pids.sh
└── pre
    ├── 00clean_dir.sh
    ├── 01unzip.sh
    ├── 02docker_db_check.sh
    └── 03build.sh

该应用程序文件提取 pre / 01unzip.sh 泊坞窗构建在执行 pre / 03build.sh 。因此,我们需要添加一个新的脚本注入 ENV 与脚本文件名的顺序是在 01unzip.sh 和前 03build.sh 。正如你所说,这是无证的,可能被改变。但是,我认为,不应该,如果你使用相同的弹性魔豆平台版本改变。您需要验证该黑客可以在未来的平台版本上运行在升级生产环境之前。

The app file is extracted in pre/01unzip.sh and docker build is executed in pre/03build.sh. So, we need add a new script to inject the ENV with the script file name order is after 01unzip.sh and before 03build.sh. As you said, this is undocumented and might be changed. But, I thought, it should not be changed if you use same Elastic Beanstalk platform version. You need to verify this "hack" can be run in the next platform version before you upgrade the production environment.

其实,有没有一些其他的选择来设置实例ID为环境变量。如:修改泊坞窗运行颁布/ 00run.sh 。我也不preFER修改EB脚本。

Actually, there any some other options to set instance id as environment variable. Such as: modifying the docker run line in enact/00run.sh. I also don't prefer to modify the EB script.

这篇关于我怎样才能设置一个弹性魔豆泊坞窗容器中的每个实例ENV变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆