Setting up a new instance - tooltwist/documentation GitHub Wiki

We follow the instructions provided by Amazon.

http://docs.aws.amazon.com/AmazonECS/latest/developerguide/launch_container_instance.html

Important!!! - for step 10, click on Advanced Details and enter the User Data as described below.

2016-09-23_11-07-01

At the time the EC2 instance starts this User Data script is run. In our case we will use this script to:

  1. Copy ecs.config into it's standard location on the instance, where it can be read the the ECS agent.
  2. Mount the S3 bucket so it can be used to provide volumes for the Docker containers.

User Data

If you are adding an instance to an existing cluster you can copy the User Data from an existing instance. If not then modify the following:

#!/bin/bash

AWS_IDENTITY=AKIAxxxxxxxxxxxxxxxx
AWS_CREDENTIAL=xxxxxxxxxxxxxxxxxxxxxxxxxxxx+xxxxxxxxxxx
S3_BUCKET_NAME=ecs-xxxx-dev


# Install ECS config from S3 bucket
yum install -y aws-cli
aws s3 cp s3://${S3_BUCKET_NAME}/ecs.config /etc/ecs/ecs.config

# Install s3fs
if [ ! -e /usr/local/bin/s3fs ] ; then
    yum -y install automake fuse fuse-devel gcc-c++ git libcurl-devel libxml2-devel make openssl-devel
    git clone https://github.com/s3fs-fuse/s3fs-fuse.git
    cd s3fs-fuse
    ./autogen.sh
    ./configure
    make
    sudo make install
fi

# Set up credentials to access the S3 bucket
echo ${AWS_IDENTITY}:${AWS_CREDENTIAL}  > /etc/passwd-s3fs
chmod 600 /etc/passwd-s3fs

# Mount the S3 bucket
rm -rf /Configs-s3; mkdir /Configs-s3
/usr/local/bin/s3fs ${S3_BUCKET_NAME} /Configs-s3

# Periodically sync from s3 to the volumes
rm -rf /Configs; mkdir /Configs
(while true ; do rsync -a /Configs-s3/ /Configs/ ; date > /tmp/sync-configs-timestamp ; sleep 120 ; done) &

# Create a marker file, so we know it ran
date >> /tmp/user-data-was-run

Once the instance has started the Instances page will be show. Set the instance name immediately.

2016-09-23_12-21-59

Initial Login

Take the Public IP from the EC2 instance page and login using

ssh -i ~/.ssh/phil-singapore [email protected]
  1. The S3 bucket should be mounted at /Configs. If it is not, then try running the commands from the User Data manually.

  2. Check the server is up to date

     sudo yum install -y
    
  3. You will need to update the ulimit for the server (the maximum number of files open), as this limitation passes through to Docker containers and may prevent some applications from running. Edit /etc/sysconfig/docker and change the following line as shown below:

     # The max number of open files for the daemon itself, and all
     # running containers.  The default value of 1048576 mirrors the value
     # used by the systemd service unit.
     DAEMON_MAXFILES=1048576
     
     # Additional startup options for the Docker daemon, for example:
     # OPTIONS="--ip-forward=true --iptables=true"
     # By default we limit the number of open files per container
     OPTIONS="--default-ulimit nofile=1024000:1024000"
    

Restart Docker to pick up the new value, and confirm it with these commands:

    $ sudo service docker restart
    Stopping docker:                                           [  OK  ]
    Starting docker:	                                   [  OK  ]
    
    $ docker start ecs-agent    

    $ docker run busybox sh -c "ulimit -n"
    1024000   <-- This value should be updated

Defining User Data for an existing instance

This may or may not be possible, as an instance must be stopped in order to re-define the user data, and some instances are set up so that they are terminated (deleted) as soon as the instance stops.

If it is possible in your configuration, this is where to do it...

2016-09-22_00-44-46

References

https://github.com/tooltwist/documentation/wiki/Setting-up-a-new-instance#defining-user-data

http://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-agent-config.html

http://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-agent-config.html#ecs-config-s3

http://docs.aws.amazon.com/AmazonECS/latest/developerguide/launch_container_instance.html

https://www.jacksoncage.se/posts/2013/04/08/Sync-files-to-S3-on-Mac-OS-X/

https://github.com/s3fs-fuse/s3fs-fuse