Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory usage for tasks #299

Merged
merged 9 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion components/gateway/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ ENV DEBIAN_FRONTEND noninteractive
ARG OPENSIPS_VERSION=3.3
ARG OPENSIPS_BUILD=releases

RUN apt-get -y update -qq && apt-get -y install gnupg2 ca-certificates
RUN apt-get -y update -qq && apt-get -y install gnupg2 ca-certificates iproute2
RUN apt-key adv --fetch-keys https://apt.opensips.org/pubkey.gpg
RUN echo "deb https://apt.opensips.org bullseye ${OPENSIPS_VERSION}-${OPENSIPS_BUILD}" >/etc/apt/sources.list.d/opensips.list
RUN apt-get -y update -qq && apt-get -y install opensips opensips-postgres-module opensips-auth-modules netcat jq curl
Expand Down
6 changes: 5 additions & 1 deletion components/gateway/client_gateway/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ if [ "$1" = 'opensips' ]; then
FIFO_NAME="${FIFO_NAME:="/tmp/opensips_fifo"}"
DATABASE_URL="${DATABASE_URL:="postgres://postgres:@localhost:5432/opensips"}"
SIP_PORT="${SIP_PORT:="5060"}"
INTERFACE_NAME="$(ip route list | grep default | grep -E 'dev (\w+)' -o | awk '{print $2}')"

if [ -n "$DATABASE_HOST" ]; then
DATABASE_URL="postgres://$DATABASE_USERNAME:$DATABASE_PASSWORD@$DATABASE_HOST:$DATABASE_PORT/$DATABASE_NAME"
Expand All @@ -16,7 +17,9 @@ if [ "$1" = 'opensips' ]; then
LOCAL_IP="$(hostname -i)"

if [ -n "$ECS_CONTAINER_METADATA_FILE" ]; then
SIP_ADVERTISED_IP="${SIP_ADVERTISED_IP:="$(curl -s http://169.254.169.254/latest/meta-data/public-ipv4)"}"
TOKEN=$(curl -s -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600")
AWS_PUBLIC_IP=$(curl -s -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/public-ipv4)
SIP_ADVERTISED_IP="${SIP_ADVERTISED_IP:="$AWS_PUBLIC_IP"}"
else
SIP_ADVERTISED_IP="${SIP_ADVERTISED_IP:="$(hostname -i)"}"
fi
Expand All @@ -26,6 +29,7 @@ if [ "$1" = 'opensips' ]; then
sed -i "s|SIP_PORT|$SIP_PORT|g" /etc/opensips/opensips.cfg
sed -i "s|SIP_ADVERTISED_IP|$SIP_ADVERTISED_IP|g" /etc/opensips/opensips.cfg
sed -i "s|LOCAL_IP|$LOCAL_IP|g" /etc/opensips/opensips.cfg
sed -i "s|INTERFACE_NAME|$INTERFACE_NAME|g" /etc/opensips/opensips.cfg

exec "$OPENSIPS_CONTAINER_BINARY" -FE
fi
Expand Down
4 changes: 2 additions & 2 deletions components/gateway/client_gateway/opensips.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ udp_workers=4
lookup failures (default disabled) */
#dns_try_ipv6=yes

socket=udp:eth0:SIP_PORT
socket=tcp:eth0:SIP_PORT # TCP Health Check
socket=udp:INTERFACE_NAME:SIP_PORT
socket=tcp:INTERFACE_NAME:SIP_PORT # TCP Health Check

####### Modules Section ########

Expand Down
2 changes: 2 additions & 0 deletions components/gateway/public_gateway/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ if [ "$1" = 'opensips' ]; then
DATABASE_URL="${DATABASE_URL:="postgres://postgres:@localhost:5432/opensips"}"
SIP_ADVERTISED_IP="${SIP_ADVERTISED_IP:="$(hostname -i)"}"
LOCAL_IP="$(hostname -i)"
INTERFACE_NAME="$(ip route list | grep default | grep -E 'dev (\w+)' -o | awk '{print $2}')"

if [ -n "$DATABASE_HOST" ]; then
DATABASE_URL="postgres://$DATABASE_USERNAME:$DATABASE_PASSWORD@$DATABASE_HOST:$DATABASE_PORT/$DATABASE_NAME"
Expand All @@ -22,6 +23,7 @@ if [ "$1" = 'opensips' ]; then
sed -i "s|SIP_ALTERNATIVE_PORT|$SIP_ALTERNATIVE_PORT|g" /etc/opensips/opensips.cfg
sed -i "s|SIP_ADVERTISED_IP|$SIP_ADVERTISED_IP|g" /etc/opensips/opensips.cfg
sed -i "s|LOCAL_IP|$LOCAL_IP|g" /etc/opensips/opensips.cfg
sed -i "s|INTERFACE_NAME|$INTERFACE_NAME|g" /etc/opensips/opensips.cfg

exec "$OPENSIPS_CONTAINER_BINARY" -FE
fi
Expand Down
6 changes: 3 additions & 3 deletions components/gateway/public_gateway/opensips.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ udp_workers=4
lookup failures (default disabled) */
#dns_try_ipv6=yes

socket=udp:eth0:SIP_PORT as SIP_ADVERTISED_IP
socket=udp:eth0:SIP_ALTERNATIVE_PORT as SIP_ADVERTISED_IP
socket=tcp:eth0:SIP_PORT # TCP Health Check
socket=udp:INTERFACE_NAME:SIP_PORT as SIP_ADVERTISED_IP
socket=udp:INTERFACE_NAME:SIP_ALTERNATIVE_PORT as SIP_ADVERTISED_IP
socket=tcp:INTERFACE_NAME:SIP_PORT # TCP Health Check

####### Modules Section ########

Expand Down
4 changes: 3 additions & 1 deletion components/media_proxy/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ if [ "$1" = 'rtpengine' ]; then
LOCAL_IP="$(hostname -i)"

if [ -n "$ECS_CONTAINER_METADATA_FILE" ]; then
ADVERTISED_IP="${ADVERTISED_IP:="$(curl -s http://169.254.169.254/latest/meta-data/public-ipv4)"}"
TOKEN=$(curl -s -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600")
AWS_PUBLIC_IP=$(curl -s -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/public-ipv4)
ADVERTISED_IP="${ADVERTISED_IP:="$AWS_PUBLIC_IP"}"
else
ADVERTISED_IP="${ADVERTISED_IP:="$(hostname -i)"}"
fi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ systemctl start amazon-ssm-agent
# ECS config
cat <<'EOF' >> /etc/ecs/ecs.config
ECS_CLUSTER=${cluster_name}
ECS_RESERVED_MEMORY=128
ECS_RESERVED_MEMORY=256
ECS_ENABLE_CONTAINER_METADATA=true
EOF
2 changes: 1 addition & 1 deletion infrastructure/modules/somleng_switch/client_gateway.tf
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ resource "aws_ecs_task_definition" "client_gateway" {
requires_compatibilities = ["EC2"]
execution_role_arn = aws_iam_role.client_gateway_task_execution_role.arn
container_definitions = data.template_file.client_gateway.rendered
memory = module.client_gateway_container_instances.ec2_instance_type.memory_size - 256
memory = module.client_gateway_container_instances.ec2_instance_type.memory_size - 512

volume {
name = "opensips"
Expand Down
2 changes: 1 addition & 1 deletion infrastructure/modules/somleng_switch/media_proxy.tf
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ resource "aws_ecs_task_definition" "media_proxy" {
requires_compatibilities = ["EC2"]
execution_role_arn = aws_iam_role.media_proxy_task_execution_role.arn
container_definitions = data.template_file.media_proxy.rendered
memory = module.media_proxy_container_instances.ec2_instance_type.memory_size - 256
memory = module.media_proxy_container_instances.ec2_instance_type.memory_size - 512
}

resource "aws_ecs_service" "media_proxy" {
Expand Down
2 changes: 1 addition & 1 deletion infrastructure/modules/somleng_switch/public_gateway.tf
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ resource "aws_ecs_task_definition" "public_gateway" {
task_role_arn = aws_iam_role.public_gateway_task_role.arn
execution_role_arn = aws_iam_role.public_gateway_task_execution_role.arn
container_definitions = data.template_file.public_gateway.rendered
memory = module.public_gateway_container_instances.ec2_instance_type.memory_size - 256
memory = module.public_gateway_container_instances.ec2_instance_type.memory_size - 512

volume {
name = "opensips"
Expand Down
2 changes: 1 addition & 1 deletion infrastructure/modules/somleng_switch/switch.tf
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ resource "aws_ecs_task_definition" "switch" {
container_definitions = data.template_file.switch.rendered
task_role_arn = aws_iam_role.ecs_task_role.arn
execution_role_arn = aws_iam_role.task_execution_role.arn
memory = module.switch_container_instances.ec2_instance_type.memory_size - 256
memory = module.switch_container_instances.ec2_instance_type.memory_size - 512

volume {
name = local.efs_volume_name
Expand Down
6 changes: 4 additions & 2 deletions infrastructure/modules/somleng_switch/templates/assign_eip.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip
unzip awscliv2.zip
./aws/install

AWS_REGION="$(curl http://169.254.169.254/latest/meta-data/placement/region)"
INSTANCE_ID="$(curl http://169.254.169.254/latest/meta-data/instance-id)"
TOKEN=$(curl -s -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600")

AWS_REGION="$(curl -s -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/placement/region)"
INSTANCE_ID="$(curl -s -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/instance-id)"

# Get first unallocated EIP with a matching tag sorted by the Priority tag
ALLOCATION_ID="$(aws ec2 describe-addresses --filters "Name=tag-key,Values=${eip_tag}" --output text --query 'Addresses[?AssociationId==null].[AllocationId,Tags[?Key==`Priority`].Value|[0]]|sort_by(@, &[1])|[0][0]' --region $AWS_REGION)"
Expand Down
Loading