|
| 1 | +# NGINX Plus Integration with AWS Auto Scaling groups -- nginx-asg-sync |
| 2 | + |
| 3 | +**nginx-asg-sync** allows [NGINX Plus](https://www.nginx.com/products/) to support scaling when load balancing [AWS Auto Scaling groups](http://docs.aws.amazon.com/autoscaling/latest/userguide/WhatIsAutoScaling.html): when the number of instances in an Auto Scaling group changes, nginx-asg-sync adds the new instances to the NGINX Plus configuration and removes the terminated ones. |
| 4 | + |
| 5 | +More details on this solution are available in the blog post [Load Balancing AWS Auto Scaling Groups with NGINX Plus](https://www.nginx.com/blog/load-balancing-aws-auto-scaling-groups-nginx-plus/). |
| 6 | + |
| 7 | +Below you will find instructions on how to use nginx-asg-sync. |
| 8 | + |
| 9 | +## Contents |
| 10 | + |
| 11 | +1. [Supported Operating Systems](#supported-operating-systems) |
| 12 | +1. [Setting up Access to the AWS API](#setting-up-access-to-the-aws-api) |
| 13 | +1. [Installation](#installation) |
| 14 | +1. [Configuration](#configuration) |
| 15 | +1. [Usage](#usage) |
| 16 | +1. [Troubleshooting](#troubleshooting) |
| 17 | +1. [Building a Software Package](#building-a-software-package) |
| 18 | +1. [Support](#support) |
| 19 | + |
| 20 | +## Supported Operating Systems |
| 21 | + |
| 22 | +We provide packages for the following operating systems: |
| 23 | + |
| 24 | +* Ubuntu: 14.04 (Trusty), 16.04 (Xenial) |
| 25 | +* CentOS/RHEL: 7 |
| 26 | +* Amazon Linux |
| 27 | + |
| 28 | +Support for other operating systems can be added. |
| 29 | + |
| 30 | +## Setting up Access to the AWS API |
| 31 | + |
| 32 | +nginx-asg-sync uses the AWS API to get the list of IP addresses of the instances of an Auto Scaling group. To access the AWS API, nginx-asg-sync must have credentials. To provide credentials to nginx-asg-sync: |
| 33 | + |
| 34 | +1. [Create an IAM role](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/iam-roles-for-amazon-ec2.html) and attach the predefined `AmazonEC2ReadOnlyAccess` policy to it. This policy allows read-only access to EC2 APIs. |
| 35 | +1. When you launch the NGINX Plus instance, add this IAM role to the instance. |
| 36 | + |
| 37 | +Alternatively, you can use the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environmental variables to provide credentials to nginx-asg-sync. |
| 38 | + |
| 39 | +## Installation |
| 40 | + |
| 41 | +To install nginx-asg-sync: |
| 42 | + |
| 43 | +1. Download a software package for your OS with the latest version of nginx-asg-sync from the [Releases page](https://github.com/nginxinc/nginx-asg-sync/releases). |
| 44 | +1. Install the package: |
| 45 | + |
| 46 | + For Amazon Linux or CentOS/RHEL, run: `$ sudo rpm -i <package-name>.rpm` |
| 47 | + |
| 48 | + For Ubuntu, run: `$ sudo dpkg -i <package-name>.deb` |
| 49 | + |
| 50 | +## Configuration |
| 51 | + |
| 52 | +As an example, we configure NGINX Plus to load balance two AWS Auto Scaling groups -- backend-group-one and backend-group-two. NGINX Plus routes requests to the appropriate Auto Scaling group based on the request URI: |
| 53 | + |
| 54 | +* Requests for /backend-one go to Backend One group. |
| 55 | +* Requests for /backend-two go to Backend Two group. |
| 56 | + |
| 57 | +### NGINX Plus Configuration |
| 58 | + |
| 59 | +```nginx |
| 60 | +upstream backend-one { |
| 61 | + zone backend-one 64k; |
| 62 | + state /var/lib/nginx/state/backend-one.conf; |
| 63 | +} |
| 64 | +
|
| 65 | +upstream backend-two { |
| 66 | + zone backend-two 64k; |
| 67 | + state /var/lib/nginx/state/backend-two.conf; |
| 68 | +} |
| 69 | +
|
| 70 | +server { |
| 71 | + listen 80; |
| 72 | +
|
| 73 | + status_zone backend; |
| 74 | +
|
| 75 | + location /backend-one { |
| 76 | + proxy_set_header Host $host; |
| 77 | + proxy_pass http://backend-one; |
| 78 | + } |
| 79 | +
|
| 80 | + location /backend-two { |
| 81 | + proxy_set_header Host $host; |
| 82 | + proxy_pass http://backend-two; |
| 83 | + } |
| 84 | +} |
| 85 | +
|
| 86 | +server { |
| 87 | + listen 8080; |
| 88 | +
|
| 89 | + root /usr/share/nginx/html; |
| 90 | +
|
| 91 | + location = / { |
| 92 | + return 302 /status.html; |
| 93 | + } |
| 94 | +
|
| 95 | + location = /status.html { |
| 96 | + } |
| 97 | +
|
| 98 | + location /status { |
| 99 | + access_log off; |
| 100 | + status; |
| 101 | + } |
| 102 | +
|
| 103 | + location /upstream_conf { |
| 104 | + upstream_conf; |
| 105 | + } |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +* We declare two upstream groups – **backend-one** and **backend-two**, which correspond to our Auto Scaling groups. However, we do not add any servers to the upstream groups, because the servers will be added by nginx-aws-sync. The `state` directive names the file where the dynamically configurable list of servers is stored, enabling it to persist across restarts of NGINX Plus. |
| 110 | +* We define a virtual server that listens on port 80. NGINX Plus passes requests for **/backend-one** to the instances of the Backend One group, and requests for **/backend-two** to the instances of the Backend Two group. |
| 111 | +* We define a second virtual server listening on port 8080 and configure the NGINX Plus APIs on it, which are required by nginx-asg-sync: |
| 112 | + * The on-the-fly API is available at **127.0.0.1:8080/upstream_conf** |
| 113 | + * The status API is available at **127.0.0.1:8080/status** |
| 114 | + |
| 115 | +### nginx-asg-sync Configuration |
| 116 | + |
| 117 | +nginx-asg-sync is configured in the file **aws.yaml** in the **/etc/nginx** folder. For our example, we define the following configuration: |
| 118 | + |
| 119 | +```yaml |
| 120 | +region: us-west-2 |
| 121 | +upstream_conf_endpoint: http://127.0.0.1:8080/upstream_conf |
| 122 | +status_endpoint: http://127.0.0.1:8080/status |
| 123 | +sync_interval_in_seconds: 5 |
| 124 | +upstreams: |
| 125 | + - name: backend-one |
| 126 | + autoscaling_group: backend-one-group |
| 127 | + port: 80 |
| 128 | + kind: http |
| 129 | + - name: backend-two |
| 130 | + autoscaling_group: backend-two-group |
| 131 | + port: 80 |
| 132 | + kind: http |
| 133 | +``` |
| 134 | +
|
| 135 | +* The `region` key defines the AWS region where we deploy NGINX Plus and the Auto Scaling groups. |
| 136 | +* The `upstream_conf` and the `status_endpoint` keys define the NGINX Plus API endpoints. |
| 137 | +* The `sync_interval_in_seconds` key defines the synchronization interval: nginx-asg-sync checks for scaling updates every 5 seconds. |
| 138 | +* The `upstreams` key defines the list of upstream groups. For each upstream group we specify: |
| 139 | + * `name` – The name we specified for the upstream block in the NGINX Plus configuration. |
| 140 | + * `autoscaling_group` – The name of the corresponding Auto Scaling group. |
| 141 | + * `port` – The port on which our backend applications are exposed. |
| 142 | + * `protocol` – The protocol of the traffic NGINX Plus load balances to the backend application, here `http`. If the application uses TCP/UDP, specify `stream` instead. |
| 143 | + |
| 144 | +## Usage |
| 145 | + |
| 146 | +nginx-asg-sync runs as a system service and supports the start/stop/restart commands. |
| 147 | + |
| 148 | +For Ubuntu 14.04 and Amazon Linux, run: `$ sudo start|stop|restart nginx-asg-sync` |
| 149 | + |
| 150 | +For Ubuntu 16.04 and CentOS7/RHEL7, run: `$ sudo service nginx-asg-sync start|stop|restart` |
| 151 | + |
| 152 | +## Troubleshooting |
| 153 | + |
| 154 | +If nginx-asg-sync doesn’t work as expected, check its log file available at **/var/log/nginx-aws-sync/nginx-aws-sync.log**. |
| 155 | + |
| 156 | +## Building a Software Package |
| 157 | + |
| 158 | +You can compile nginx-asg-sync and build a software package using the provided Makefile. Before you start building a package, make sure that the following software is installed on your system: |
| 159 | +* make |
| 160 | +* Docker |
| 161 | + |
| 162 | +To build a software package, run: `$ make <os>` |
| 163 | +where `<os>` is the target OS. The following values are allowed: |
| 164 | +* `amazon` for Amazon Linux |
| 165 | +* `centos7` for CentOS7/RHEL7 |
| 166 | +* `ubuntu-trusty` for Ubuntu 14.04 |
| 167 | +* `ubuntu-xenial` for Ubuntu 16.04 |
| 168 | + |
| 169 | +If you run make without any arguments, it will build software packages for all supported OSes. |
| 170 | + |
| 171 | +## Support |
| 172 | + |
| 173 | +Support from the [NGINX Professional Services Team](https://www.nginx.com/services/) is available when using nginx-asg-sync. |
0 commit comments