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 build your own routing link, activate a new intranet or Internet Server Load Balancer instance routing to the virtual machine (VM) port by using the Alibaba Cloud extension label lb, and configure the mapping between host and container to route requests.

Scenarios:

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

Expose TCP or UDP services

Currently, to expose TCP services, configure a Server Load Balancer instance or a public IP address, and configure the port mapping between host and container by using the Alibaba Cloud extension label lb.

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 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 and create a Server Load Balancer instance used for routing.

    Select Internet as the Instance type in this example. Select Internet or Intranet as the Instance type 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 or the instance ID 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. Create a listening port.

    Click Manage at the right of the instance. Click Listeners in the left-side navigation pane. Click Add Listener in the upper-right corner and then set the configurations. Select TCP as the frontend protocol and configure the port mapping as 6379:6379.

    1

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

    For how to create an application, see Create an application.

    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

  5. Select the redis image and set the Port Mapping.

    Note: The Redis image only enables port 6379 on the container. To route the created Server Load Balancer instance to this container port, you must specify the port mapping between host and container of the Redis image.

    In Port Mapping, specify Host Port as 6379, which is the backend host port bound to the Server Load Balancer instance, and select TCP as the Protocol.

    1

  6. To configure custom Server Load Balancer, let the Redis service know the information of the used Server Load Balancer instance by adding a label to the Redis service or configuring the Load Balancer.

    • Add a label to the service. In this example, the label is aliyun.lb.port_6379: tcp://slb_redis_app:6379.

      1

      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.

      For more information, see Alibaba Cloud extension label lb.

    • Click the plus icon at the right of Load Balancer and set the Server Load Balancer instance information.

      This setting corresponds to the label aliyun.lb.port_6379: tcp://slb_redis_app:6379.

      1

    In this example, set the destination container port as 6379, reference the Server Load Balancer instance name slb_redis_app, set the listening port protocol as TCP, which corresponds to the protocol configured in the port mapping between host and container, and set the frontend port of the Server Load Balancer instance as 6379.

    Note: In this example, set 6379 as the frontend port and backend port (namely, the host port) of the Server Load Balancer instance and the container port, you can set a different frontend port and host port as per your needs.

  7. Click Create to create the Redis application.

    During the creation process, the slb_redis_app Server Load Balancer instance is automatically bound to the backend host deployed with the Redis image.

  8. 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.

      1

  9. You can view the IP address of the Server Load Balancer instance on the Instances page of the Server Load Balancer console, and use the command line tool telnet $Server_Load_Balancer_IP_address 6379 to check port accessibility.

  10. 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=6379)
    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.