Running Centralized Service on Elastic Beanstalk - django

I have setup a Django server on AWS Elastic Beanstalk. In that server, I have following services
Application server
Celery Worker
Celery Beat
I have using Docker to deploy my application which means I build my docker image, us that image to run all three services. My Dockerrun.aws.json file is below.
{
"AWSEBDockerrunVersion": 2,
"containerDefinitions": [
{
"command": [
"sh",
"-c",
"./entry_point.sh && gunicorn Project.wsgi:application -w 2 -b :8000 --timeout 120 --graceful-timeout 120 --worker-class gevent"
],
"environment": [
],
"essential": true,
"image": "695189796512.dkr.ecr.us-west-2.amazonaws.com/Project-20181107174734",
"name": "app",
"memory": 500,
"portMappings": [
{
"containerPort": 8000,
"hostPort": 80
}
]
},
{
"command": [
"celery",
"-A",
"Project",
"beat",
"--loglevel=info",
"--uid",
"django"
],
"environment": [
],
"essential": true,
"image": "695189796512.dkr.ecr.us-west-2.amazonaws.com/Project-20181107174734",
"memory": 200,
"name": "celery-beat"
},
{
"command": [
"celery",
"-A",
"Project",
"worker",
"--loglevel=info",
"--uid",
"django"
],
"environment": [
],
"essential": true,
"image": "695189796512.dkr.ecr.us-west-2.amazonaws.com/Project-20181107174734",
"memory": 200,
"name": "celery-worker"
}
],
"family": "",
"volumes": []
}
Problem:
This configuration is working fine. But the problem with this configuration is that on all my nodes, all three services run. When load increases, my server scales up to multiple nodes and all services run on each node. I can have multiple celery worker running on server as my task queue is same (I am using SQS). But I only want one Celery Beat service running on all my nodes because multiple Beat services can cause task added to queue multiple time.
What I wanted to do is to run a centralized service in my server. I want help that how I can achieve this in my current setup.
Also I want to know is there any problem in using multiple instance of Celery Worker on my server?.

Related

Communicate between docker containers in a docker network in AWS Beanstalk

I'm using AWS Beanstalk to deploy my project in 'Multi-container Docker running on 64bit Amazon Linux'
Here's my Dockerrun.aws.json as per documentation
{
"AWSEBDockerrunVersion": 2,
"containerDefinitions": [
{
"name": "child",
"image": "nithinsgowda/child",
"essential": true,
"memory": 256,
"portMappings": [
{
"hostPort": 9000,
"containerPort": 9000
}
],
"links": [
"master"
]
},
{
"name": "master",
"image": "nithinsgowda/master",
"essential": true,
"memory": 512,
"portMappings": [
{
"hostPort": 80,
"containerPort": 8080
}
],
"links": [
"child"
]
}
]
}
I can access my master container at port 80 from the public internet
Inside my master container I have an API call to be made to the child container
I have tried the below options :
None of them worked
fetch('http://child/api')
fetch('http://child:9000/api')
fetch('http://15.14.13.12:9000/api') //My public DNS for the beanstalk application (Example)
If it was in a local docker-compose environment 'http://child/api' works perfectly fine. But this doesn't work on Beanstalk.
How do I communicate with the child container from my master container ?
I have even tried bindIP attribute and assigned a local IP and tried accessing it with the local IP, it still doesn't work
When looked into the server logs, docker ps was executed by the environment and both containers were up and running, port mappings were also displayed correct.
Here's what you need to specify in Dockerrun.aws.json
"containerDefinitions": [
{
"name": "child",
"image": "nithinsgowda/child",
"essential": true,
"memory": 256,
"portMappings": [
{
"hostPort": 9000,
"containerPort": 9000
}
],
"links": [
"master"
],
"environment": [
{
"name": "Container",
"value": "child"
}
]
},
The environment variable named Container will be the name given to your container inside the network.
"environment": [
{
"name": "Container",
"value": "child" //Any custom name accepted
}
]
And hence after specifying the environment variable, I can now access the child container as fetch('http://child:9000/api')
Here's AWS official documentation link specifying the above content https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_docker_v2config.html#create_deploy_docker_v2config_dockerrun

Running multiple containers in single fargate task for clair container scanning

We are running clair and clair-db containers in the same fargate task. Below is a snippet of our task definition.
{
"family": "clair",
"networkMode": "awsvpc",
"containerDefinitions": [
{
"name": "db",
"image": "<REPO_URL>/clairdb:v1.0",
"essential": true,
"command": [
"sh",
"-c",
"echo clair db runs"
],
"portMappings": [
{
"containerPort": 5432,
"hostPort": 5432,
"protocol": "tcp"
}
],
},
{
"name": "clair",
"image": "<REPO_URL>/clair:v1.0",
"essential": true,
"command": [
"sh",
"-c",
"echo clair runs"
],
"portMappings": [
{
"containerPort": 6060,
"hostPort": 6060,
"protocol": "tcp"
}
],
As per the AWS fargate docs, localhost can be used to communicate between these two containers of a single task in awsvpc mode. We have given the below option in Clair config.yaml
clair:
database:
type: pgsql
options:
source: host=localhost port=5432 user=postgres password=xxxx sslmode=disable statement_timeout=60000
So as per this, clair should ideally be able to link to the clair-db container running on localhost:5432 on the same network. Clair-db container is running fine in fargate, but clair container is failing with the below logs:
{"Event":"pgsql: could not open database: dial tcp 127.0.0.1:5432: connect: connection refused","Level":"fatal","Location":"main.go:97","Time":"2021-03-23 13:26:38.737437"}
In docker terms, this is how we link these two conatainers:
docker run -p 5432:5432 -d --name db arminc/clair-db:2017-05-05
docker run -p 6060:6060 --link db:postgres -d --name clair arminc/clair-local-scan:v2.0.0-rc.0
Are we missing anything here? Any idea why connection to localhost isn't working in fargate containers for clair?

Elastic Beanstalk Multicontainer Docker environment: no entries in etc/hosts for lined containers

I have an environment with a few containers. Some of them are linked. When I run the environment with "docker-compose up -d", it creates entries in etc/hosts for linked containers. When I run it with "eb local run", no entries are created. Why is that?
My Dockerrun.aws.json
{
"AWSEBDockerrunVersion": 2,
"containerDefinitions": [
{
"name": "api",
"image": "php7",
"essential": true,
"memory": 128,
"portMappings": [
{
"hostPort": 8080,
"containerPort": 80
}
],
"mountPoints": [
{
"sourceVolume": "api",
"containerPath": "/var/www/html/"
}
]
},
{
"name": "nodeapi",
"image": "nodejs",
"essential": true,
"memory": 256,
"portMappings": [
{
"hostPort": 5000,
"containerPort": 5000
}
],
"mountPoints": [
{
"sourceVolume": "nodeapi",
"containerPath": "/var/www/app/"
}
],
"Logging": "/var/eb_log"
},
{
"name": "proxy",
"image": "nginx",
"essential": true,
"memory": 128,
"links": [
"api",
"nodeapi"
],
"portMappings": [
{
"hostPort": 8443,
"containerPort": 80
}
]
}
]
}
This generates docker-compose.yml:
api:
image: php7
ports:
- 8080:80
nodeapi:
image: nodejs
ports:
- 5000:5000
proxy:
image: nginx
links:
- api:api
- nodeapi:nodeapi
ports:
- 8443:80
Docker switched to DNS based lookups a while back instead of adding entries to /etc/hosts. Linking is also discouraged in favor of using a common network for the containers.
Ok, this is was a local issue. I upgraded Docker and EB cli to the latest versions and this solved the issue. I'm not sure why EB cli failed to add aliases to etc/hosts previously, but after upgrade it does. Now I get same results either by using "docker-compose up" or "eb local run". All linked container are linked now and work as expected.

command not working in dockeraws.json in elastic beanstalk AWSEBDockerrunVersion:2

Hi I have a small docker container that I want to ship to elasticbeanstalk
So this is my configuration:
my Dockerfile that am using to build the image:
FROM mwaaas/sms_packages:0.0.2
WORKDIR /root
# create a folder code where we will put our code
RUN mkdir code
# add code to the folder
ADD . code
WORKDIR /root/code
CMD gunicorn --bind=0.0.0.0:3000 --env DJANGO_SETTINGS_MODULE=sms_platform.settings sms_platform.wsgi
EXPOSE 3000
and the Dockerrunaws.json is configured this way
{
"AWSEBDockerrunVersion": "2",
"containerDefinitions": [
{
"name": "sms_platform_app",
"image": "mwaaas/sms_platform:{{version}}",
"essential": true,
"memory": 128,
"portMappings": [
{
"hostPort": 80,
"containerPort": 3000
}
]
}
]
}
If i check at the logs:
ddef82a9bc59 mwaaas/sms_platform:staging_ "/bin/sh -c 'gunicor
For some reasons I want gunicorn command to be in the Dockerrunaws.json
so I removed the CMD command in the docker file and
fixed it in the Dockerrunaws.json file
{
"AWSEBDockerrunVersion": "2",
"containerDefinitions": [
{
"name": "sms_platform_app",
"image": "mwaaas/sms_platform:production_32b7",
"essential": true,
"memory": 128,
"portMappings": [
{
"hostPort": 80,
"containerPort": 3000
}
],
"command": "gunicorn --bind=0.0.0.0:3000 --env DJANGO_SETTINGS_MODULE=sms_platform.settings sms_platform.wsgi"
}
]
}
Some how elastic beanstalk is not running the command.
when I check at the logs I see:
67798ba99a94 mwaaas/sms_platform:staging_b68d "/sbin/my_init"
am expecting the command to be gunicorn instead its /sbin/myinit

AWS Elastic Beanstalk Docker environment variables

How to pass environment variables to Docker containers running in AWS Elastic Beanstalk multiple docker container configuration(different for different containers)?
Use the environment key in your container descriptions.
{
"containerDefinitions": [
{
"name": "myContainer",
"image": "something",
"environment": [
{
"name": "MY_DB_PASSWORD",
"value": "password"
}
],
"portMappings": [
{
"containerPort": 80,
"hostPort": 80
}
],
"memory": 500,
"cpu": 10
}]
}
For more information, see: http://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html