Prácticas recomendadas
Expose multiple services
Currently, WebCal’s custom services only support exposing a single port. If you need to expose multiple services (such as HTTP services) within containers, you can use a proxy to implement a layer of routing and forwarding—for example, by using Nginx. Here, we provide a lighter-weight alternative. Assuming you have two services in your container, listening on ports 2000 and 3000 respectively, here’s an example using Flask:
# 模拟监听2000端口的服务
from flask import Flask
app = Flask(__name__)
@app.route('/v1/hello')
def hello():
return 'I`m port 2000'
if __name__ == '__main__':
app.run(port=2000)
# 模拟监听3000端口的服务
from flask import Flask
app = Flask(__name__)
@app.route('/v2/hello')
def hello():
return 'I`m port 3000'
if __name__ == '__main__':
app.run(port=3000)
The following starts a route-forwarding service on port 6006. If a route matches /v1/*, it is forwarded to the service listening on port 2000; if it matches /v2/*, it is forwarded to the service listening on port 3000.
First, download the forwarding service program from the instance at wget https://webcal-public.ks3-cn-beijing.ksyuncs.com/tool/api-proxy/proxy_in_instance. Then, in the same directory as this service program file, create a file named config.yaml with the following content:
proxies:
- host_and_port: http://127.0.0.1:2000 # 要代理到的服务地址
route_path: /v1/* # 匹配什么路由就转发到此地址
- host_and_port: http://127.0.0.1:3000 # 可设置多组,转发到不同的host
route_path: /v2/*
Then start the forwarding service:
# 如果没下载转发程序,先下载
wget https://webcal-public.ks3-cn-beijing.ksyuncs.com/tool/api-proxy/proxy_in_instance
# 如果没有添加可执行权限,先添加权限
chmod +x proxy_in_instance
# 启动
./proxy_in_instance
