ElasticBeanstalk`eb setenv`-应用程序重启

时间:2018-07-02 23:26:17

标签: amazon-web-services elastic-beanstalk

运行eb setenv时,这无法运行.ebextensions中的钩子。在我的.ebextensions中,我删除了默认的nginx配置,并将其替换为新的配置。每次应用程序启动时都必须运行此挂钩,这一点至关重要,这样nginx才不会崩溃。这是我的.ebextensions / proxy.config文件(我也附上了文件)。

当我上传压缩的应用程序时,挂钩会按预期运行。只有通过eb api重新启动应用程序时,文件/etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf才会重新出现

如果我做错了什么,或者这是ElasticBeanstalk的限制,请告诉我。

files:
  /etc/nginx/conf.d/proxy.conf:
    mode: "000644"
    owner: root
    group: root
    content: |
      upstream nodejs {
        server 127.0.0.1:8081;
        keepalive 256;
      }

      server {
        listen 8080;

        if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2})") {
          set $year $1;
          set $month $2;
          set $day $3;
          set $hour $4;
        }

        access_log /var/log/nginx/healthd/application.log.$year-$month-$day-$hour healthd;
        access_log  /var/log/nginx/access.log  main;

        location ~* ^/(api) {
          proxy_pass  http://nodejs;
          proxy_set_header   Connection "";
          proxy_http_version 1.1;
          proxy_set_header        Host            $host;
          proxy_set_header        X-Real-IP       $remote_addr;
          proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        gzip on;
        gzip_min_length 10240;
        gzip_comp_level 4;
        gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;

        location / {
          alias /var/app/current/public/;
          try_files $uri /index.html;
        }
      }

  /opt/elasticbeanstalk/hooks/appdeploy/post/97_kill_default_nginx.sh:
    mode: "000755"
    owner: root
    group: root
    content: |
      #!/usr/bin/env bash
      set -x
      rm -f /tmp/deployment/config/#etc#nginx#conf.d#* /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf
      initctl stop nginx || true
      initctl start nginx

2 个答案:

答案 0 :(得分:0)

我最终重新阅读了Platform Hooks docs,发现appdeployconfigdeploy之间的区别

  

appdeploy-脚本在应用程序部署期间运行。启动新实例以及客户端启动新版本部署时,Elastic Beanstalk会执行应用程序部署。

     

configdeploy-脚本在客户端执行配置更新时运行,该更新会影响实例上的软件配置,例如,通过设置环境属性或启用对Amazon S3的日志轮换。

在阅读了这些描述之后,很明显我需要更改proxy.config文件,以使/opt/elasticbeanstalk/hooks/appdeploy/post/97_kill_default_nginx.sh变成/opt/elasticbeanstalk/hooks/configdeploy/post/97_kill_default_nginx.sh。为安全起见,并确保在启动应用程序的新版本以及更新环境变量时都运行此脚本,我同时复制/粘贴了appdeployconfigdeploy这两个脚本。所以最后的proxy.config看起来像

files:
  /etc/nginx/conf.d/proxy.conf:
    mode: "000644"
    owner: root
    group: root
    content: |
      upstream nodejs {
        server 127.0.0.1:8081;
        keepalive 256;
      }

      server {
        listen 8080;

        if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2})") {
          set $year $1;
          set $month $2;
          set $day $3;
          set $hour $4;
        }

        access_log /var/log/nginx/healthd/application.log.$year-$month-$day-$hour healthd;
        access_log  /var/log/nginx/access.log  main;

        location ~* ^/(api) {
          proxy_pass  http://nodejs;
          proxy_set_header   Connection "";
          proxy_http_version 1.1;
          proxy_set_header        Host            $host;
          proxy_set_header        X-Real-IP       $remote_addr;
          proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        gzip on;
        gzip_min_length 10240;
        gzip_comp_level 4;
        gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;

        location / {
          alias /var/app/current/public/;
          try_files $uri /index.html;
        }
      }

  /opt/elasticbeanstalk/hooks/appdeploy/post/97_kill_default_nginx.sh:
    mode: "000755"
    owner: root
    group: root
    content: |
      #!/usr/bin/env bash
      set -x
      rm -f /tmp/deployment/config/#etc#nginx#conf.d#* /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf
      initctl stop nginx || true
      initctl start nginx

  /opt/elasticbeanstalk/hooks/configdeploy/post/97_kill_default_nginx.sh:
    mode: "000755"
    owner: root
    group: root
    content: |
      #!/usr/bin/env bash
      set -x
      rm -f /tmp/deployment/config/#etc#nginx#conf.d#* /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf
      initctl stop nginx || true
      initctl start nginx

调试此错误(以及与此相关的任何钩子脚本)的一种好方法是将ssh放入由beantalk管理的EC2并创建文件touch /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf。然后在单独的终端窗口中,运行eb setenv(或任何用于重新启动应用程序的命令)。然后在ssh'd窗口中,完成eb setenv命令后,检查文件是否已删除ls -l /etc/nginx/conf.d

答案 1 :(得分:0)

这是一种有趣的方法。我遵循了Amazon在此处发布的示例: AWS example,建议使用容器命令。

container_commands:
removeconfig:
    command: "rm -f /tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf"

但是,当更新压缩文件时(我认为与您的方法相同),配置更新总是崩溃。

我必须遵循此答案中概述的方法才能使其与容器命令一起使用:stackoverflow restart script help

我的postcongif部署钩子现在看起来像(并且我使用了容器命令):

  /opt/elasticbeanstalk/hooks/configdeploy/post/99_kill_default_nginx.sh:
 owner: root
 group: root
 mode: "000755"
 content: |
   #!/bin/bash -xe
   set -x
   echo "starting post config script"
   status=`/sbin/status nginx`
   if [[ -e "/etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf" ]];
    then
      echo "default config file exists - we should delete it"; fi
      rm -f /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf
      echo "restart nginx after deletion"
      if [[ $status = *"start/running"* ]]; then
        echo "stopping nginx..."
        stop nginx
        echo "starting nginx..."
        start nginx
      else
        echo "nginx is not running... starting it..."
        start nginx
      fi
    else
      echo "no default config file exists - no need to delete it"
    fi