edit-icon download-icon

Server Load Balancer routing

Last Updated: Mar 16, 2018

Expose HTTP or HTTPS services

We recommend that you use simple routing service (namely, routing) to expose HTTP or HTTPS services. To expose the HTTP or HTTPS services by using Server Load Balancer directly, purchase a new intranet or Internet Server Load Balancer instance and configure the routing by using the Alibaba Cloud extension label lb.

Scenarios:

In Layer-7 protocol Server Load Balancer, a route is customized for each service. Services of non-container clusters access the services of containers in container clusters when a traditional architecture is migrated to a container architecture.

Expose TCP or UDP services

Swarm mode clusters support automatically configuring the Server Load Balancer instances and one Server Load Balancer instance can be bound to multiple services. To expose TCP/UDP services, the Server Load Balancer instance is automatically bound to the port 9080 of the cluster machine. You can also configure the port mapping between the host and container by using the Port Mapping and Container Label fields.

Note: When you want to use Server Load Balancing routing, you need to buy a new Server Load Balancer instance. Multiple services cannot share the same Server Load Balancer instance, and the default Server Load Balancer instance of the cluster cannot be used for Server Load Balancer routing.

Scenarios:

In Layer-4 protocol Server Load Balancer, a route is customized for each service. Services of non-container clusters access the services of containers in container clusters when a traditional architecture is migrated to a container architecture.

Example:

Expose the Redis service in a container cluster to the Python application outside the container cluster by customizing a Server Load Balancer instance.

  1. In the Server Load Balancer console, click Create Server Load Balancer in the upper-right corner to purchase a Server Load Balancer instance used for routing. Select Internet as the Instance type and By traffic as the Bandwidth in this example. You can configure the settings based on your needs.

    Note: Server Load Balancer does not support cross-region deployment. Therefore, select the same region as the used Container Service cluster.

    1

  2. Return to the Server Load Balancer console and name the created Server Load Balancer instance as slb_redis_app. Container Service can use this name to reference the Server Load Balancer instance.

    Click Instances in the left-side navigation pane. Select the region in which the Server Load Balancer instance resides. Edit the instance name and then click Confirm.

    1

  3. Log on to the Container Service console. Create an application named redis-demo in an existing swarm mode cluster and then click Create with Image.

    For how to create an application, see Create an application by using an image.

    Note: Server Load Balancer does not support cross-region deployment. Therefore, the used Container Service cluster must be in the same region as the Server Load Balancer instance created in the preceding steps.

    1

  4. Enter the Redis image name (domainname/namespace/imagename:tag) and configure the Port Mapping.

    The Redis image only enables the port 80 on the container, and the host port is not specified. To specify the mapping rules of the Server Load Balancer instance, set the listening rules in the Server Load Balancer console according to the security group rules.

    Note: Container Service provides some default security group rules. The Server Load Balancer instance will route to this container port automatically. The reserved port mapping of Server Load Balancer instance frontend > backend Elastic Compute Service (ECS) instance is 80:9080.

    1

  5. You can bind the Server Load Balancer instance by using Container Label.

    In this example, the label is aliyun.lb.port_80: tcp://slb_redis_app:80.

    3

    The label format is as follows (the variable with $ is a placeholder).

    1. aliyun.lb.port_$container_port:$scheme://$[slb_name|slb_id]:$front_port
    • $container_port indicates the port to be exposed by the container.
    • $scheme indicates the protocol supported by the listening port of the Server Load Balancer instance, whose value might be tcp, http, https, or udp.
    • $[slb_name|slb_id] indicates the name or ID of the Server Load Balancer instance.
    • $front_port indicates the frontend port to be exposed by the Server Load Balancer instance.
  6. Click Create and Deploy to create the Redis application.

    During the application creation process, the slb_redis_app Server Load Balancer instance is automatically bound to the backend host deployed with the Redis image. You can access the Redis application by using the Server Load Balancer access address.

    1

  7. When the Redis application is ready, log on to the Server Load Balancer console to view the status of the slb_redis_app Server Load Balancer instance.

    1. Click Instances in the left-side navigation pane.

    2. Select the region in which the Server Load Balancer instance resides.

    3. Click Manage at the right of the instance.

    4. Click Servers > Backend Servers in the left-side navigation pane.

      The health status shows that the Server Load Balancer instance is correctly bound to the Redis backend and the backend servers where the services in the same cluster reside are all bound to this Server Load Balancer instance.

      1

  8. You can configure the listening rules for the Server Load Balancer instance in the Server Load Balancer console. In this example, configure the port of the frontend protocol as the reserved port 80. View the IP address of the Server Load Balancer instance on the Instances page and use the command line tool telnet $Server_Load_Balancer_IP_address 80 to check port accessibility.

  9. To test the preceding configurations, start a simple Python application locally to access Redis in the container cluster by using the slb_redis_app Server Load Balancer instance.

    Note: The Redis host address is the same as the IP address of the Server Load Balancer instance.

    app.py

    1. from flask import Flask
    2. from redis import Redis
    3. app = Flask(__name__)
    4. redis = Redis(host='$Server_Load_Balancer_IP_address', port=80) #Note
    5. @app.route('/')
    6. def hello():
    7. redis.incr('hits')
    8. return 'Hello World! I have been seen %s times.' % redis.get('hits')
    9. if __name__ == "__main__":
    10. app.run(host="0.0.0.0", debug=True)

    requirements.txt

    1. flask
    2. redis

    shell

    1. $ pip install -r requirements.txt
    2. $ python app.py
    3. Running on http://0.0.0.0:5000/ ## Press CTRL+C to exit
    4. Restarting with stat
    5. Debugger is active!
    6. Debugger pin code: 243-626-653

    The access result is as follows.

Thank you! We've received your feedback.