

# Amazon ECS 클러스터의 NGINX Plus 워크로드 샘플
<a name="ContainerInsights-Prometheus-Setup-nginx-plus-ecs"></a>

NGINX Plus는 NGINX의 상용 버전입니다. 사용하려면 라이선스가 있어야 합니다. 자세한 내용은 [NGINX Plus](https://www.nginx.com/products/nginx/)를 참조하세요.

NGINX Prometheus Exporter는 NGINX 데이터를 Prometheus 지표로 스크레이프하고 노출할 수 있습니다. 이 예에서는 Amazon ECS의 NGINX Plus 역방향 프록시 서비스와 함께 Exporter를 사용합니다.

NGINX Prometheus Exporter에 대한 자세한 내용은 Github의 [nginx-prometheus-exporter](https://github.com/nginxinc/nginx-prometheus-exporter)를 참조하세요. NGINX 역방향 프록시에 대한 자세한 내용은 Github의 [ecs-nginx-reverse-proxy](https://github.com/awslabs/ecs-nginx-reverse-proxy)를 참조하세요.

Prometheus가 지원되는 CloudWatch 에이전트는 Amazon ECS 클러스터의 서비스 검색 구성을 기반으로 NGINX Plus Prometheus 지표를 스크레이프합니다. 지표를 다른 포트 또는 경로에 노출하도록 NGINX Prometheus Exporter를 구성할 수 있습니다. 포트 또는 경로를 변경하는 경우 CloudWatch 에이전트 구성 파일의 `ecs_service_discovery` 섹션을 업데이트하세요.

## Amazon ECS 클러스터의 NGINX Plus 역방향 프록시 샘플 워크로드 설치
<a name="ContainerInsights-Prometheus-nginx-plus-ecs-setup"></a>

다음 단계에 따라 NGINX 역방향 프록시 샘플 워크로드를 설치합니다.

### Docker 이미지 생성
<a name="ContainerInsights-Prometheus-nginx-plus-ecs-setup-docker"></a>

**NGINX Plus 역방향 프록시 샘플 워크로드의 Docker 이미지를 생성하려면**

1. NGINX 역방향 프록시 리포지토리에서 다음 폴더를 다운로드합니다. [ https://github.com/awslabs/ecs-nginx-reverse-proxy/tree/master/reverse-proxy/](https://github.com/awslabs/ecs-nginx-reverse-proxy/tree/master/reverse-proxy/)

1. `app` 디렉터리를 찾아 해당 디렉터리에서 이미지를 구축합니다.

   ```
   docker build -t web-server-app ./path-to-app-directory
   ```

1. NGINX Plus용 사용자 지정 이미지를 구축합니다. NGINX Plus용 이미지를 구축하기 전에 먼저, `nginx-repo.key`라는 키와 라이선스가 있는 NGINX Plus의 SSL 인증서인 `nginx-repo.crt`를 가져와야 합니다. 디렉터리를 만들어서 그 안에 `nginx-repo.key` 및 `nginx-repo.crt` 파일을 저장합니다.

   방금 만든 디렉터리에서 다음과 같은 두 파일을 생성합니다.
   + 다음 내용이 포함된 샘플 Dockerfile: 이 Docker 파일은 [https://docs.nginx.com/nginx/admin-guide/installing-nginx/installing-nginx-docker/\$1docker\$1plus\$1image](https://docs.nginx.com/nginx/admin-guide/installing-nginx/installing-nginx-docker/#docker_plus_image)에서 제공한 샘플 파일에서 채택한 것입니다. 중요한 변경 사항은 다음 단계에서 생성될 `nginx.conf`라는 별도의 파일을 로드한다는 것입니다.

     ```
     FROM debian:buster-slim
     
     LABEL maintainer="NGINX Docker Maintainers <docker-maint@nginx.com>“
     
     # Define NGINX versions for NGINX Plus and NGINX Plus modules
     # Uncomment this block and the versioned nginxPackages block in the main RUN
     # instruction to install a specific release
     # ENV NGINX_VERSION 21
     # ENV NJS_VERSION 0.3.9
     # ENV PKG_RELEASE 1~buster
     
     # Download certificate and key from the customer portal (https://cs.nginx.com (https://cs.nginx.com/))
     # and copy to the build context
     COPY nginx-repo.crt /etc/ssl/nginx/
     COPY nginx-repo.key /etc/ssl/nginx/
     # COPY nginx.conf /etc/ssl/nginx/nginx.conf
     
     RUN set -x \
     # Create nginx user/group first, to be consistent throughout Docker variants
     && addgroup --system --gid 101 nginx \
     && adduser --system --disabled-login --ingroup nginx --no-create-home --home /nonexistent --gecos "nginx user" --shell /bin/false --uid 101 nginx \
     && apt-get update \
     && apt-get install --no-install-recommends --no-install-suggests -y ca-certificates gnupg1 \
     && \
     NGINX_GPGKEY=573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62; \
     found=''; \
     for server in \
     ha.pool.sks-keyservers.net (http://ha.pool.sks-keyservers.net/) \
     hkp://keyserver.ubuntu.com:80 \
     hkp://p80.pool.sks-keyservers.net:80 \
     pgp.mit.edu (http://pgp.mit.edu/) \
     ; do \
     echo "Fetching GPG key $NGINX_GPGKEY from $server"; \
     apt-key adv --keyserver "$server" --keyserver-options timeout=10 --recv-keys "$NGINX_GPGKEY" && found=yes && break; \
     done; \
     test -z "$found" && echo >&2 "error: failed to fetch GPG key $NGINX_GPGKEY" && exit 1; \
     apt-get remove --purge --auto-remove -y gnupg1 && rm -rf /var/lib/apt/lists/* \
     # Install the latest release of NGINX Plus and/or NGINX Plus modules
     # Uncomment individual modules if necessary
     # Use versioned packages over defaults to specify a release
     && nginxPackages=" \
     nginx-plus \
     # nginx-plus=${NGINX_VERSION}-${PKG_RELEASE} \
     # nginx-plus-module-xslt \
     # nginx-plus-module-xslt=${NGINX_VERSION}-${PKG_RELEASE} \
     # nginx-plus-module-geoip \
     # nginx-plus-module-geoip=${NGINX_VERSION}-${PKG_RELEASE} \
     # nginx-plus-module-image-filter \
     # nginx-plus-module-image-filter=${NGINX_VERSION}-${PKG_RELEASE} \
     # nginx-plus-module-perl \
     # nginx-plus-module-perl=${NGINX_VERSION}-${PKG_RELEASE} \
     # nginx-plus-module-njs \
     # nginx-plus-module-njs=${NGINX_VERSION}+${NJS_VERSION}-${PKG_RELEASE} \
     " \
     && echo "Acquire::https::plus-pkgs.nginx.com::Verify-Peer \"true\";" >> /etc/apt/apt.conf.d/90nginx \
     && echo "Acquire::https::plus-pkgs.nginx.com::Verify-Host \"true\";" >> /etc/apt/apt.conf.d/90nginx \
     && echo "Acquire::https::plus-pkgs.nginx.com::SslCert \"/etc/ssl/nginx/nginx-repo.crt\";" >> /etc/apt/apt.conf.d/90nginx \
     && echo "Acquire::https::plus-pkgs.nginx.com::SslKey \"/etc/ssl/nginx/nginx-repo.key\";" >> /etc/apt/apt.conf.d/90nginx \
     && printf "deb https://plus-pkgs.nginx.com/debian buster nginx-plus\n" > /etc/apt/sources.list.d/nginx-plus.list \
     && apt-get update \
     && apt-get install --no-install-recommends --no-install-suggests -y \
     $nginxPackages \
     gettext-base \
     curl \
     && apt-get remove --purge --auto-remove -y && rm -rf /var/lib/apt/lists/* /etc/apt/sources.list.d/nginx-plus.list \
     && rm -rf /etc/apt/apt.conf.d/90nginx /etc/ssl/nginx
     
     # Forward request logs to Docker log collector
     RUN ln -sf /dev/stdout /var/log/nginx/access.log \
     && ln -sf /dev/stderr /var/log/nginx/error.log
     
     COPY nginx.conf /etc/nginx/nginx.conf
     
     EXPOSE 80
     
     STOPSIGNAL SIGTERM
     
     CMD ["nginx", "-g", "daemon off;"]
     ```
   + [https://github.com/awslabs/ecs-nginx-reverse-proxy/tree/master/reverse-proxy/nginx](https://github.com/awslabs/ecs-nginx-reverse-proxy/tree/master/reverse-proxy/nginx)에서 수정한 `nginx.conf` 파일

     ```
     events {
       worker_connections 768;
     }
     
     http {
       # Nginx will handle gzip compression of responses from the app server
       gzip on;
       gzip_proxied any;
       gzip_types text/plain application/json;
       gzip_min_length 1000;
     
       upstream backend {
         zone name 10m;
         server app:3000    weight=2;
         server app2:3000    weight=1;
       }
     
       server{
         listen 8080;
         location /api {
           api write=on;
         }
       }
     
       match server_ok {
         status 100-599;
       }
     
       server {
         listen 80;
         status_zone zone;
         # Nginx will reject anything not matching /api
         location /api {
           # Reject requests with unsupported HTTP method
           if ($request_method !~ ^(GET|POST|HEAD|OPTIONS|PUT|DELETE)$) {
             return 405;
           }
     
           # Only requests matching the whitelist expectations will
           # get sent to the application server
           proxy_pass http://backend;
           health_check uri=/lorem-ipsum match=server_ok;
           proxy_http_version 1.1;
           proxy_set_header Upgrade $http_upgrade;
           proxy_set_header Connection 'upgrade';
           proxy_set_header Host $host;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_cache_bypass $http_upgrade;
         }
       }
     }
     ```

1. 새 디렉터리의 파일에서 이미지를 구축합니다.

   ```
   docker build -t nginx-plus-reverse-proxy ./path-to-your-directory
   ```

1. 나중에 사용할 수 있도록 새 이미지를 이미지 리포지토리에 업로드합니다.

### Amazon ECS에서 NGINX Plus 및 웹 서버 앱을 실행하는 태스크 정의 생성
<a name="ContainerInsights-Prometheus-nginx-plus-ecs-setup-task"></a>

다음으로, 태스크 정의를 설정합니다.

이 태스크 정의를 사용하면 NGINX Plus Prometheus 지표를 수집하고 내보낼 수 있습니다. NGINX 컨테이너는 앱의 입력을 추적하고, `nginx.conf`에 설정된 대로 해당 데이터를 포트 8080에 노출합니다. NGINX Prometheus Exporter 컨테이너는 이러한 지표를 스크레이프하며 CloudWatch에서 사용할 수 있도록 포트 9113에 게시합니다.

**NGINX 샘플 Amazon ECS 워크로드의 태스크 정의를 설정하려면**

1. 다음 내용이 포함된 태스크 정의 JSON 파일을 생성합니다. *your-customized-nginx-plus-image*를 사용자 지정 NGINX Plus 이미지의 이미지 URI로 바꾸고 *your-web-server-app-image*를 웹 서버 앱 이미지의 이미지 URI로 바꿉니다.

   ```
   {
     "containerDefinitions": [
       {
         "name": "nginx",
         "image": "your-customized-nginx-plus-image",
         "memory": 256,
         "cpu": 256,
         "essential": true,
         "portMappings": [
           {
             "containerPort": 80,
             "protocol": "tcp"
           }
         ],
         "links": [
           "app",
           "app2"
         ]
       },
       {
         "name": "app",
         "image": "your-web-server-app-image",
         "memory": 256,
         "cpu": 128,
         "essential": true
       },
       {
         "name": "app2",
         "image": "your-web-server-app-image",
         "memory": 256,
         "cpu": 128,
         "essential": true
       },
       {
         "name": "nginx-prometheus-exporter",
         "image": "docker.io/nginx/nginx-prometheus-exporter:0.8.0",
         "memory": 256,
         "cpu": 256,
         "essential": true,
         "command": [
           "-nginx.plus",
           "-nginx.scrape-uri",
            "http://nginx:8080/api"
       ],
       "links":[
         "nginx"
       ],
         "portMappings":[
           {
             "containerPort": 9113,
             "protocol": "tcp"
           }
         ]
       }
     ],
     "networkMode": "bridge",
     "placementConstraints": [],
     "family": "nginx-plus-sample-stack"
   }
   ```

1. 태스크 정의를 등록합니다.

   ```
   aws ecs register-task-definition --cli-input-json file://path-to-your-task-definition-json
   ```

1. 다음 명령을 입력하여 태스크를 실행할 서비스를 생성합니다.

   ```
   aws ecs create-service \
    --cluster your-cluster-name \
    --service-name nginx-plus-service \
    --task-definition nginx-plus-sample-stack:1 \
    --desired-count 1
   ```

   서비스 이름을 변경해서는 안 됩니다. 태스크를 시작한 서비스의 이름 패턴을 사용해 태스크를 검색하는 구성을 사용하여 CloudWatch 에이전트 서비스를 실행합니다. 예를 들어 CloudWatch 에이전트가 이 명령으로 시작된 태스크를 찾도록 하려면 `sd_service_name_pattern` 값을 `^nginx-plus-service$`로 지정할 수 있습니다. 다음 단원에서 더 자세히 설명합니다.

### NGINX Plus Prometheus 지표를 스크레이프하도록 CloudWatch 에이전트 구성
<a name="ContainerInsights-Prometheus-nginx-plus-ecs-setup-agent"></a>

마지막 단계는 NGINX 지표를 스크레이프하도록 CloudWatch 에이전트를 구성하는 것입니다. 이 예에서 CloudWatch 에이전트는 서비스 이름 패턴과 Exporter가 NGINX에 대한 Prometheus 지표를 노출하는 포트 9113을 통해 태스크를 검색합니다. 태스크를 검색하고 지표를 사용할 수 있게 되면 CloudWatch 에이전트는 수집된 지표를 로그 스트림 **nginx-prometheus-exporter**에 게시하기 시작합니다.

**NGINX 지표를 스크레이프하도록 CloudWatch 에이전트를 구성하려면**

1. 다음 명령을 입력하여 필요한 YAML 파일의 최신 버전을 다운로드합니다.

   ```
   curl -O https://raw.githubusercontent.com/aws-samples/amazon-cloudwatch-container-insights/latest/ecs-task-definition-templates/deployment-mode/replica-service/cwagent-prometheus/cloudformation-quickstart/cwagent-ecs-prometheus-metric-for-bridge-host.yaml
   ```

1. 텍스트 편집기를 사용하여 파일을 열고 `resource:CWAgentConfigSSMParameter` 섹션에서 `value` 키의 전체 CloudWatch 에이전트 구성을 찾습니다. 그런 다음, `ecs_service_discovery` 섹션에서 다음 `service_name_list_for_tasks` 섹션을 추가합니다.

   ```
   "service_name_list_for_tasks": [
     {
       "sd_job_name": "nginx-plus-prometheus-exporter",
       "sd_metrics_path": "/metrics",
       "sd_metrics_ports": "9113",
       "sd_service_name_pattern": "^nginx-plus.*"
      }
   ],
   ```

1. 동일한 파일의 `metric_declaration` 섹션에 다음 섹션을 추가하여 NGINX Plus 지표를 허용합니다. 이때 기존의 들여쓰기 패턴을 따라야 합니다.

   ```
   {
     "source_labels": ["job"],
     "label_matcher": "^nginx-plus.*",
     "dimensions": [["ClusterName", "TaskDefinitionFamily", "ServiceName"]],
     "metric_selectors": [
       "^nginxplus_connections_accepted$",
       "^nginxplus_connections_active$",
       "^nginxplus_connections_dropped$",
       "^nginxplus_connections_idle$",
       "^nginxplus_http_requests_total$",
       "^nginxplus_ssl_handshakes$",
       "^nginxplus_ssl_handshakes_failed$",
       "^nginxplus_up$",
       "^nginxplus_upstream_server_health_checks_fails$"
     ]
   },
   {
     "source_labels": ["job"],
     "label_matcher": "^nginx-plus.*",
     "dimensions": [["ClusterName", "TaskDefinitionFamily", "ServiceName", "upstream"]],
     "metric_selectors": [
       "^nginxplus_upstream_server_response_time$"
     ]
   },
   {
     "source_labels": ["job"],
     "label_matcher": "^nginx-plus.*",
     "dimensions": [["ClusterName", "TaskDefinitionFamily", "ServiceName", "code"]],
     "metric_selectors": [
       "^nginxplus_upstream_server_responses$",
       "^nginxplus_server_zone_responses$"
     ]
   },
   ```

1. 이 클러스터에 CloudWatch 에이전트를 아직 배포하지 않은 경우 8단계로 건너뜁니다.

   AWS CloudFormation을 사용하여 Amazon ECS 클러스터에 CloudWatch 에이전트를 이미 배포한 경우 다음 명령을 입력하여 변경 세트를 생성할 수 있습니다.

   ```
   ECS_CLUSTER_NAME=your_cluster_name
   AWS_REGION=your_aws_region
   ECS_NETWORK_MODE=bridge
   CREATE_IAM_ROLES=True
   ECS_TASK_ROLE_NAME=your_selected_ecs_task_role_name
   ECS_EXECUTION_ROLE_NAME=your_selected_ecs_execution_role_name
   
   aws cloudformation create-change-set --stack-name CWAgent-Prometheus-ECS-${ECS_CLUSTER_NAME}-EC2-${ECS_NETWORK_MODE} \
       --template-body file://cwagent-ecs-prometheus-metric-for-bridge-host.yaml \
       --parameters ParameterKey=ECSClusterName,ParameterValue=$ECS_CLUSTER_NAME \
                    ParameterKey=CreateIAMRoles,ParameterValue=$CREATE_IAM_ROLES \
                    ParameterKey=ECSNetworkMode,ParameterValue=$ECS_NETWORK_MODE \
                    ParameterKey=TaskRoleName,ParameterValue=$ECS_TASK_ROLE_NAME \
                    ParameterKey=ExecutionRoleName,ParameterValue=$ECS_EXECUTION_ROLE_NAME \
       --capabilities CAPABILITY_NAMED_IAM \
       --region $AWS_REGION \
       --change-set-name nginx-plus-scraping-support
   ```

1. CloudFormation 콘솔([https://console.aws.amazon.com/cloudformation](https://console.aws.amazon.com/cloudformation/))을 엽니다.

1. 새로 생성한 변경 세트인 **nginx-plus-scraping-support**를 검토합니다. **CWAgentConfigSSMParameter** 리소스에 적용된 변경 사항 하나가 표시되어야 합니다. 변경 세트를 실행하고 다음 명령을 입력하여 CloudWatch 에이전트 태스크를 다시 시작합니다.

   ```
   aws ecs update-service --cluster $ECS_CLUSTER_NAME \
   --desired-count 0 \
   --service cwagent-prometheus-replica-service-EC2-$ECS_NETWORK_MODE \
   --region $AWS_REGION
   ```

1. 10초 정도 기다린 후 다음 명령을 입력합니다.

   ```
   aws ecs update-service --cluster $ECS_CLUSTER_NAME \
   --desired-count 1 \
   --service cwagent-prometheus-replica-service-EC2-$ECS_NETWORK_MODE \
   --region $AWS_REGION
   ```

1. 클러스터에 처음으로 Prometheus 지표 수집이 포함된 CloudWatch 에이전트를 설치하는 경우 다음 명령을 입력합니다.

   ```
   ECS_CLUSTER_NAME=your_cluster_name
   AWS_REGION=your_aws_region
   ECS_NETWORK_MODE=bridge
   CREATE_IAM_ROLES=True
   ECS_TASK_ROLE_NAME=your_selected_ecs_task_role_name
   ECS_EXECUTION_ROLE_NAME=your_selected_ecs_execution_role_name
   
   aws cloudformation create-stack --stack-name CWAgent-Prometheus-ECS-${ECS_CLUSTER_NAME}-EC2-${ECS_NETWORK_MODE} \
       --template-body file://cwagent-ecs-prometheus-metric-for-bridge-host.yaml \
       --parameters ParameterKey=ECSClusterName,ParameterValue=$ECS_CLUSTER_NAME \
                    ParameterKey=CreateIAMRoles,ParameterValue=$CREATE_IAM_ROLES \
                    ParameterKey=ECSNetworkMode,ParameterValue=$ECS_NETWORK_MODE \
                    ParameterKey=TaskRoleName,ParameterValue=$ECS_TASK_ROLE_NAME \
                    ParameterKey=ExecutionRoleName,ParameterValue=$ECS_EXECUTION_ROLE_NAME \
       --capabilities CAPABILITY_NAMED_IAM \
       --region $AWS_REGION
   ```

## NGINX Plus 지표 및 로그 보기
<a name="ContainerInsights-Prometheus-Setup-nginx-plus-view"></a>

이제 수집 중인 NGINX Plus 지표를 볼 수 있습니다.

**샘플 NGINX 워크로드에 대한 지표를 보려면**

1. [https://console.aws.amazon.com/cloudwatch/](https://console.aws.amazon.com/cloudwatch/)에서 CloudWatch 콘솔을 엽니다.

1. 클러스터가 실행되고 있는 리전에서 왼쪽 탐색 창의 [**지표(Metrics)**]를 선택합니다. **ContainerInsights/Prometheus** 네임스페이스를 찾아 지표를 확인합니다.

1. CloudWatch Logs 이벤트를 보려면 탐색 창에서 [**로그 그룹(Log groups)**]을 선택합니다. 이벤트는 로그 그룹 **/aws/containerinsights/*your\$1cluster\$1name*/prometheus**의 로그 스트림 *nginx-plus-prometheus-exporter*에 있습니다.