GithubHelp home page GithubHelp logo

eb-healthd-middleware's Introduction

eb-healthd-middleware

When running Elastic Beanstalk in multi-container Docker mode the very useful enhanced health reporting does not work out of the box. There are several steps to configure it correctly and unfortunately they are not located in one document. Specifically, it requires one's application to log information about requests in a particular format and location.

This repo will detail those steps and provide Node.js middleware for Restify and Express to aid in logging appropriately.

Install

This module will only work with versions of Node 4 and above:

npm install eb-healthd-middleware --save

Setup

The first step is to create an .ebextensions folder that is uploaded in the same zip archive as your Dockerrun.aws.json file. Place the following yaml file (named 01-nginx-healthd.config) in .ebextensions ensuring the placeholder is replaced with the name of the container to be monitored. This informs the health monitoring agent as to where to find the logs on the host and also sets up a Cron job to periodically delete rotated logs.

files:
    "/etc/cron.hourly/cron.logcleanup.elasticbeanstalk.healthd.nginx.conf":
        mode: "000755"
        owner: root
        group: root
        content: |
            #!/bin/sh
            find /var/log/containers/<YOURCONTAINERNAME>/healthd -type f | grep -v application.log.`date -u +"%Y-%m-%d-%H"` | xargs rm -f
    "/home/ec2-user/setup-healthd-appstat.sh":
        mode: "000755"
        owner: root
        group: root
        content: |
            #!/bin/sh
            set -e
            mkdir -p /var/log/containers/<YOURCONTAINERNAME>/healthd
            chmod 777 /var/log/containers/<YOURCONTAINERNAME>/healthd
            if [ -d "/etc/healthd" ]
            then
                echo "appstat_log_path: /var/log/containers/<YOURCONTAINERNAME>/healthd/application.log" >> /etc/healthd/config.yaml
                echo "appstat_unit: sec" >> /etc/healthd/config.yaml
                echo "appstat_timestamp_on: completion" >> /etc/healthd/config.yaml
                initctl start healthd || initctl restart healthd
            fi

container_commands:
    01-healthd-configure:
        command: /home/ec2-user/setup-healthd-appstat.sh 

Once the host's logging location has been created we create mount points for the Docker container. The default logging location is usually /var/log/nginx but is configurable. One must edit the Dockerrun.aws.json to do so (note that the awseb-logs source volume is a preconfigured Elastic Beanstalk volume):

{
  "AWSEBDockerrunVersion": "2",
  "containerDefinitions": [{
    "name": "<YOURCONTAINERNAME>",
    "image": "mydocker/image:latest",
    "essential": "true",
    "memory": 1700,
    "portMappings": [{
      "hostPort": "80",
      "containerPort": "8080"
    }],
    "mountPoints": [
      {
        "sourceVolume": "awseb-logs-<YOURCONTAINERNAME>",
        "containerPath": "<LOGLOCATION>"
      }
    ]
  }]
}

Finally, one must log the requests as they come into the server. To do so we assign some simple middleware to log these in a file of the format /my/log/dir/application.log.yyyy-MM-dd-hh. Ensure that the log directory specified has been created in the Dockerfile.

For example, using Restify:

const restify = require('restify');
const ebHealth = require('eb-healthd-middleware');

const server = restify.createServer({
    name: 'myApi'
});
    
server.use(ebHealth({directory: '/my/log/dir'}));

Or with Express:

const express = require('express');
const ebHealth = require('eb-healthd-middleware');

const app = express();

// passing an empty options object will result in a default logging 
// directory of /var/log/nginx/healthd being used
app.use(ebHealth({}));

eb-healthd-middleware's People

Contributors

svozza avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

mleiseca

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.