版本:v1.3

给应用配置访问网关

本文介绍多种应用访问策略的设置方法,你可以根据基础设施条件选择应用合适访问方式。

我们以给一个 Web Service 组件配置网关,来进行示例讲解。这个组件从 oamdev/hello-world 镜像中拉取过来,设置网关后,对外通过 testsvc.example.com 加上端口 8000 提供访问。

为了便于你快速学习,请直接复制下面的 Shell 执行,会部署到集群中:

  1. cat <<EOF | vela up -f -
  2. # YAML 文件开始
  3. apiVersion: core.oam.dev/v1beta1
  4. kind: Application
  5. metadata:
  6. name: gateway-app
  7. spec:
  8. components:
  9. - name: express-server
  10. type: webservice
  11. properties:
  12. image: oamdev/hello-world
  13. port: 8000
  14. traits:
  15. - type: gateway
  16. properties:
  17. domain: testsvc.example.com
  18. http:
  19. "/": 8000
  20. # YAML 文件结束
  21. EOF

你也可以自行将 YAML 文件保存为 gateway-app.yaml,使用 vela up -f gateway-app.yaml 命令进行部署。

当我们通过 vela ls 看到应用的 status 为 running 并且服务为 healthy,表示应用部署计划完全生效。同时它的 TRAITS 类型也正确显示为 gateway。

  1. $ vela ls
  2. APP COMPONENT TYPE TRAITS PHASE HEALTHY STATUS CREATED-TIME
  3. gateway-app express-server webservice gateway running healthy 2021-08-28 21:49:44 +0800 CST

如果 status 显示为 rendering,则表示仍在渲染中,或者 HEALTHY 一直 false,则你需要使用 vela status gateway-app 查看报错信息进行对应的处理。

查看返回的信息:

  1. $ vela status gateway-app
  2. About:
  3. Name: gateway-app
  4. Namespace: default
  5. Created at: 2022-01-12 17:34:25 +0800 CST
  6. Status: running
  7. Workflow:
  8. mode: DAG
  9. finished: true
  10. Suspend: false
  11. Terminated: false
  12. Steps
  13. - id:n5u4dsa1t4
  14. name:express-server3
  15. type:apply-component
  16. phase:succeeded
  17. message:
  18. Services:
  19. - Name: express-server3 Env:
  20. Type: webservice
  21. healthy Ready:1/1
  22. Traits:
  23. - gateway: Visiting URL: testsvc.example.com, IP: 1.5.1.1

最后通过 vela port-forward gateway-app 转发到本地处理请求:

  1. vela port-forward gateway-app
  2. Forwarding from 127.0.0.1:8000 -> 8000
  3. Forwarding from [::1]:8000 -> 8000
  4. Forward successfully! Opening browser ...
  5. Handling connection for 8000

访问服务:

  1. curl -H "Host:testsvc.example.com" http://127.0.0.1:8000/
  2. Hello World
  3. ## .
  4. ## ## ## ==
  5. ## ## ## ## ## ===
  6. /""""""""""""""""\___/ ===
  7. ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ / ===- ~~~
  8. \______ o _,/
  9. \ \ _,'
  10. `'--.._\..--''

Last updated on 2022年11月1日 by Tianxin Dong