edit-icon download-icon

Custom routing - simple sample

Last Updated: Jan 02, 2018

In this example, an acs/proxy container is deployed, services are exposed by using a Server Load Balancer instance (with the lb label) externally, and an Nginx server is attached at the backend. This example only shows the Nginx homepage, and other functions will be added based on the basic example.

Note: Different services cannot share the same Server Load Balancer. Otherwise, the backend machines of Server Load Balancer will be deleted and the services will become unavailable.

Basic example

The compose template is as follows:

  1. lb:
  2. image: registry.aliyuncs.com/acs/proxy:0.5
  3. ports:
  4. - '80:80'
  5. restart: always
  6. labels:
  7. # Addon allows the proxy image to function as a subscription registry center and dynamically load the service route.
  8. aliyun.custom_addon: "proxy"
  9. # A proxy image container is deployed on each virtual machine (VM).
  10. aliyun.global: "true"
  11. # A Server Load Balancer instance is bound to the frontend.
  12. aliyun.lb.port_80: tcp://proxy_test:80
  13. environment:
  14. # Indicates the range of backend containers that support route loading. "*" indicates the whole cluster. By default, it indicates the services in applications.
  15. ADDITIONAL_SERVICES: "*"
  16. appone:
  17. expose: # For proxied services, use expose or ports to tell proxy containers which port is to be exposed.
  18. - 80/tcp
  19. image: 'nginx:latest'
  20. labels:
  21. # http/https/ws/wss are supported. Use your own domain name instead of the test domain name provided by Container Service.
  22. aliyun.proxy.VIRTUAL_HOST: "http://appone.example.com"
  23. restart: always

After the service is successfully started, the following figure appears.

Screen_Shot_2016_09_10_at_2_28_08_PM

Enable session persistence

  1. lb:
  2. image: registry.aliyuncs.com/acs/proxy:0.5
  3. ports:
  4. - '80:80'
  5. restart: always
  6. labels:
  7. # Addon allows the proxy image to function as a subscription registry center and dynamically load the service route.
  8. aliyun.custom_addon: "proxy"
  9. # A proxy image container is deployed on each VM.
  10. aliyun.global: "true"
  11. # A Server Load Balancer instance is bound to the frontend.
  12. aliyun.lb.port_80: tcp://proxy_test:80
  13. environment:
  14. # Indicates the range of backend containers that support route loading. "*" indicates the whole cluster. By default, it indicates the services in applications.
  15. ADDITIONAL_SERVICES: "*"
  16. appone:
  17. ports:
  18. - 80/tcp
  19. - 443/tcp
  20. image: 'nginx:latest'
  21. labels:
  22. # http/https/ws/wss are supported.
  23. aliyun.proxy.VIRTUAL_HOST: "http://appone.example.com"
  24. # Session persistence is enabled, the cookie method is applied, and the key is CONTAINERID.
  25. aliyun.proxy.COOKIE: "CONTAINERID insert indirect"
  26. restart: always

Customize 503 page

When the VIP address of the Server Load Balancer instance instead of the domain name is entered, the 503 error page is returned as follows.

Screen_Shot_2016_09_10_at_2_31_55_PM

To add messages to the 503 page, add the /errors folder to the VM where the container resides and add the /errors/503.http file with the following content:

  1. HTTP/1.0 503 Service Unavailable
  2. Cache-Control: no-cache
  3. Connection: close
  4. Content-Type: text/html;charset=UTF-8
  5. <html><body><h1>503 Service Unavailable</h1>
  6. <h3>No server is available to handle this request.</h3>
  7. <h3>If this page is returned, a problem occurs during the service access process. Follow these steps for troubleshooting:</h3>
  8. <li>If you are the visitor of this application, contact the application maintainer to solve the problem.</li>
  9. <li>If you are the application maintainer, view the following information.</li>
  10. <li>You are using the simple routing service. The request is sent from Server Load Balancer to the acsrouting application container then to your application container. Follow these steps for troubleshooting.</li>
  11. <li>Log on to the Container Service console. Click "Services" in the left-side navigation pane. Select the corresponding cluster on the "Service List" page. Click the name of the service exposed to the public network. View the "Access Endpoint" of the service, and check whether your access domain name is the same as the domain name configured in the corresponding service.</li>
  12. <li>Locate and troubleshoot the problem according to the <a href="https://www.alibabacloud.com/help/faq-detail/42660.htm?spm=a3c0i.p39867en1.a3.14.idXIm7">Simple routing - service link troubleshooting</a>.</li>
  13. <li>See <a href="https://www.alibabacloud.com/help/faq-detail/42658.htm?spm=a3c0i.p39867en1.a3.12.pyONJ2">Routing FAQs</a>.</li>
  14. <li>If the problem persists, open a ticket and contact the technical staff for help. We will serve you faithfully.</li>
  15. </body></html>

You can modify the error page as per your needs. The compose template is modified as follows:

  1. lb:
  2. image: registry.aliyuncs.com/acs/proxy:0.5
  3. ports:
  4. - '80:80'
  5. restart: always
  6. labels:
  7. # Addon allows the proxy image to function as a subscription registry center and dynamically load the service route.
  8. aliyun.custom_addon: "proxy"
  9. # A proxy image container is deployed on each VM.
  10. aliyun.global: "true"
  11. # A Server Load Balancer instance is bound to the frontend.
  12. aliyun.lb.port_80: tcp://proxy_test:80
  13. environment:
  14. # Indicates the range of backend containers that support route loading. "*" indicates the whole cluster. By default, it indicates the services in applications.
  15. ADDITIONAL_SERVICES: "*"
  16. EXTRA_FRONTEND_SETTINGS_80: "errorfile 503 /usr/local/etc/haproxy/errors/503.http"
  17. volumes:
  18. - /errors/:/usr/local/etc/haproxy/errors/
  19. appone:
  20. ports:
  21. - 80/tcp
  22. - 443/tcp
  23. image: 'nginx:latest'
  24. labels:
  25. # You can specify paths when configuring URLs. In this example, http/https/ws/wss are supported.
  26. aliyun.proxy.VIRTUAL_HOST: "http://appone.example.com"
  27. restart: always

After entering the VIP address of the Server Load Balancer instance, the 503 page is displayed as follows.

6

Support extensive domain names

Modify the configurations as follows to enable the backend of Nginx to support extensive domain names (that is, the Nginx homepage can be accessed by using appone.example.com and *.example.com).

  1. lb:
  2. image: registry.aliyuncs.com/acs/proxy:0.5
  3. ports:
  4. - '80:80'
  5. restart: always
  6. labels:
  7. # Addon allows the proxy image to function as a subscription registry center and dynamically load the service route.
  8. aliyun.custom_addon: "proxy"
  9. # A proxy image container is deployed on each VM.
  10. aliyun.global: "true"
  11. # A Server Load Balancer instance is bound to the frontend.
  12. aliyun.lb.port_80: tcp://proxy_test:80
  13. environment:
  14. # Indicates the range of backend containers that support route loading. "*" indicates the whole cluster. By default, it indicates the services in applications.
  15. ADDITIONAL_SERVICES: "*"
  16. EXTRA_FRONTEND_SETTINGS_80: "errorfile 503 /usr/local/etc/haproxy/errors/503.http"
  17. volumes:
  18. - /errors/:/usr/local/etc/haproxy/errors/
  19. appone:
  20. ports:
  21. - 80/tcp
  22. - 443/tcp
  23. image: 'nginx:latest'
  24. labels:
  25. # You can specify paths when configuring URLs. In this example, http/https/ws/wss are supported.
  26. aliyun.proxy.VIRTUAL_HOST: "http://*.example.com"
  27. restart: always

Bind a host and enter the domain name www.example.com. The Nginx homepage is displayed as follows.

Screen_Shot_2016_09_10_at_3_01_49_PM

Configure default backend

Remove the URL configuration and modify the configurations as follows to enable access to Nginx at the backend by using an IP address.

  1. lb:
  2. image: registry.aliyuncs.com/acs/proxy:0.5
  3. ports:
  4. - '80:80'
  5. restart: always
  6. labels:
  7. # Addon allows the proxy image to function as a subscription registry center and dynamically load the service route.
  8. aliyun.custom_addon: "proxy"
  9. # A proxy image container is deployed on each VM.
  10. aliyun.global: "true"
  11. # A Server Load Balancer instance is bound to the frontend.
  12. aliyun.lb.port_80: tcp://proxy_test:80
  13. environment:
  14. # Indicates the range of backend containers that support route loading. "*" indicates the whole cluster. By default, it indicates the services in applications.
  15. ADDITIONAL_SERVICES: "*"
  16. # Specify the error page when 503 is returned.
  17. EXTRA_FRONTEND_SETTINGS_80: "errorfile 503 /usr/local/etc/haproxy/errors/503.http"
  18. volumes:
  19. # Mount the error page to the container from the host.
  20. - /errors/:/usr/local/etc/haproxy/errors/
  21. appone:
  22. ports:
  23. - 80/tcp
  24. - 443/tcp
  25. image: 'nginx:latest'
  26. labels:
  27. # Indicates that the service must be proxied.
  28. aliyun.proxy.required: "true"
  29. restart: always

After entering the VIP address of the Server Load Balancer instance, the Nginx homepage is displayed as follows.

Screen_Shot_2016_09_10_at_2_55_59_PM

Select backend based on URL parameter values

You can use different backend proxies based on different URL parameter values.

The following example shows how to access the appone service, that is, the Nginx homepage, by using http://www.example.com?backend=appone and how to access the apptwo service, that is, the hello world homepage, by using http://www.example.com?backend=apptwo. The application template codes are as follows:

  1. lb:
  2. image: registry.aliyuncs.com/acs/proxy:0.5
  3. ports:
  4. - '80:80'
  5. restart: always
  6. labels:
  7. # Addon allows the proxy image to function as a subscription registry center and dynamically load the service route.
  8. aliyun.custom_addon: "proxy"
  9. # A proxy image container is deployed on each VM.
  10. aliyun.global: "true"
  11. # A Server Load Balancer instance is bound to the frontend.
  12. aliyun.lb.port_80: tcp://proxy_test:80
  13. environment:
  14. # Indicates the range of backend containers that support route loading. "*" indicates the whole cluster. By default, it indicates the services in applications.
  15. ADDITIONAL_SERVICES: "*"
  16. # Obtain the value of the "backend" parameter in the URL and modify the HOST header to the backend domain name which needs to be matched.
  17. EXTRA_FRONTEND_SETTINGS_80: " http-request set-header HOST %[urlp(backend)].example.com"
  18. appone:
  19. ports:
  20. - 80/tcp
  21. - 443/tcp
  22. image: 'nginx:latest'
  23. labels:
  24. # You can specify paths when configuring URLs. In this example, http/https/ws/wss are supported.
  25. aliyun.proxy.VIRTUAL_HOST: "http://appone.example.com"
  26. restart: always
  27. apptwo:
  28. ports:
  29. - 80/tcp
  30. image: 'registry.cn-hangzhou.aliyuncs.com/linhuatest/hello-world:latest'
  31. labels:
  32. # You can specify paths when configuring URLs. In this example, http/https/ws/wss are supported.
  33. aliyun.proxy.VIRTUAL_HOST: "http://apptwo.example.com"
  34. restart: always

Bind a host and enter the link http://www.example.com?backend=appone. Then, the Nginx homepage for the appone service is displayed as follows.

Screen_Shot_2016_09_10_at_3_40_04_PM

Bind a host and enter the link http://www.example.com?backend=apptwo. Then, the hello world homepage for the apptwo service is displayed as follows.

Screen_Shot_2016_09_10_at_3_40_20_PM

Record access logs

  1. lb:
  2. image: registry.aliyuncs.com/acs/proxy:0.5
  3. ports:
  4. - '80:80'
  5. restart: always
  6. labels:
  7. # Addon allows the proxy image to function as a subscription registry center and dynamically load the service route.
  8. aliyun.custom_addon: "proxy"
  9. # A proxy image container is deployed on each VM.
  10. aliyun.global: "true"
  11. # A Server Load Balancer instance is bound to the frontend.
  12. aliyun.lb.port_80: tcp://proxy_test:80
  13. environment:
  14. # Indicates the range of backend containers that support route loading. "*" indicates the whole cluster. By default, it indicates the services in applications.
  15. ADDITIONAL_SERVICES: "*"
  16. EXTRA_DEFAULT_SETTINGS: "log rsyslog local0,log global,option httplog"
  17. links:
  18. - rsyslog:rsyslog
  19. rsyslog:
  20. image: registry.cn-hangzhou.aliyuncs.com/linhuatest/rsyslog:latest
  21. appone:
  22. ports:
  23. - 80/tcp
  24. - 443/tcp
  25. image: 'nginx:latest'
  26. labels:
  27. # http/https/ws/wss are supported.
  28. aliyun.proxy.VIRTUAL_HOST: "http://appone.example.com"
  29. restart: always

Logs are printed directly to the standard output of the rsyslog container. The access logs of custom routing can be viewed by using docker logs $rsyslog_container_name.

Server Load Balancer between services

The following template creates a Server Load Balancer service lb and an application service appone to provide services externally with the domain name appone.example.com.

  1. lb:
  2. image: registry.aliyuncs.com/acs/proxy:0.5
  3. hostname: proxy # Specify the domain name of the service as proxy, which is resolved to all containers with this image deployed.
  4. ports:
  5. - '80:80'
  6. restart: always
  7. labels:
  8. # Addon allows the proxy image to function as a subscription registry center and dynamically load the service route.
  9. aliyun.custom_addon: "proxy"
  10. # A proxy image container is deployed on each VM.
  11. aliyun.global: "true"
  12. # A Server Load Balancer instance is bound to the frontend.
  13. aliyun.lb.port_80: tcp://proxy_test:80
  14. environment:
  15. # Indicates the range of backend containers that support route loading. "*" indicates the whole cluster. By default, it indicates the services in applications.
  16. ADDITIONAL_SERVICES: "*"
  17. appone:
  18. ports:
  19. - 80/tcp
  20. - 443/tcp
  21. image: 'nginx:latest'
  22. labels:
  23. # http/https/ws/wss are supported.
  24. aliyun.proxy.VIRTUAL_HOST: "http://appone.example.com"
  25. restart: always

The following template is used as a client to access the appone application service, but the access path is used to request access to the Server Load Balancer service lb and then provide a reverse proxy for the appone application service.

  1. restclient: # Simulate rest service consumers.
  2. image: registry.aliyuncs.com/acs-sample/alpine:3.3
  3. command: "sh -c 'apk update; apk add curl; while true; do curl --head http://appone.example.com; sleep 1; done'" # Access the rest service and test Server Load Balancer.
  4. tty: true
  5. external_links:
  6. - "proxy:appone.example.com" # Specify the domain name of the link service and the alias of the domain name.

In the containers of the restclient service, the appone.example.com domain name is resolved to the IP addresses of all containers of the Server Load Balancer service lb.

  1. / # drill appone.example.com
  2. ;; ->>HEADER<<- opcode: QUERY, rcode: NOERROR, id: 60917
  3. ;; flags: qr rd ra ; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 0
  4. ;; QUESTION SECTION:
  5. ;; appone.example.com. IN A
  6. ;; ANSWER SECTION:
  7. appone.example.com. 600 IN A 172.18.3.4
  8. appone.example.com. 600 IN A 172.18.2.5
  9. appone.example.com. 600 IN A 172.18.1.5
  10. ;; AUTHORITY SECTION:
  11. ;; ADDITIONAL SECTION:
  12. ;; Query time: 0 msec
  13. ;; SERVER: 127.0.0.11
  14. ;; WHEN: Mon Sep 26 07:09:40 2016
  15. ;; MSG SIZE rcvd: 138

Configure monitoring page

  1. lb:
  2. image: registry.aliyuncs.com/acs/proxy:0.5
  3. ports:
  4. - '80:80'
  5. - '127.0.0.1:1935:1935' # The port that monitoring page exposes to the public network. Configure the port with due care because of the potential security risk.
  6. restart: always
  7. labels:
  8. aliyun.custom_addon: "proxy"
  9. aliyun.global: "true"
  10. aliyun.lb.port_80: tcp://proxy_test:80
  11. environment:
  12. ADDITIONAL_SERVICES: "*"
  13. STATS_AUTH: "admin:admin" # The logon account and password used for monitoring, which are customizable.
  14. STATS_PORT: "1935" # The port used for monitoring, which is customizable.
  15. appone:
  16. expose:
  17. - 80/tcp
  18. image: 'nginx:latest'
  19. labels:
  20. aliyun.proxy.VIRTUAL_HOST: "http://appone.example.com"
  21. restart: always

Log on to each machine where the custom routing image resides (each machine can receive the request, no matter the application container is on which machine) and request the acs/proxy health check page.

Note: Configure the correct username and password according to the environment variable STATS_AUTH of the application template.

  1. root@c68a460635b8c405e83c052b7c2057c7b-node2:~# curl -Ss -u admin:admin 'http://127.0.0.1:1935/' &> test.html

Copy the page test.html to a machine with browsers and open the local file test.html with the browser. View the stats monitoring statistics page. Green indicates the network from container acs/proxy to backend containers is connected and the container acs/proxy is working normally. Other colors indicate an exception.

Thank you! We've received your feedback.