ambassador 学习七 Mapping说明

mapping 通过rest 资源与k8s 的service进行关联,ambassador 必须有一个或者多个提供访问servide 的mapping定义

mapping 可以包含的配置

  • rewrite rule
修改URL 对于k8s service 的访问
  • weight
指定流量路由的权重
  • host
指定请求的host header

mapping 的处理顺序

ambassador 对于mappings 会进行排序,较多约束的会优先于较低的约束,请求
前缀的长度,请求的方法,以及约束的header 都会有影响,如果有必须可以使用
precedence 进行修改,但是通常来说没有必要,除非使用了regex_headers 以及
host_regex的mapping 特性,如果有相关的问题,查看统计分析界面会比较好

可选的降级处理

可以使用catch-all 进行默认处理,比如404 
参考
---
apiVersion: ambassador/v0
kind: Mapping
name: catch-all
prefix: /
service: https://www.getambassador.io

定义mapping

参考
最简单的例子
---
apiVersion: ambassador/v0
kind:  Mapping
name:  qotm_mapping
prefix: /qotm/
service: http://qotm
一个cqrs 的例子
---
apiVersion: ambassador/v0
kind: Mapping
name: cqrs_get_mapping
prefix: /cqrs/
method: GET
service: getcqrs
---
apiVersion: ambassador/v0
kind: Mapping
name: cqrs_put_mapping
prefix: /cqrs/
method: PUT
service: putcqrs
更多配置参考:https://www.getambassador.io/reference/mappings.html 

几个配置的使用

  • host_rewrite
主要是为了解决一些微服务框架基于主机头进行路由
参考:
---
apiVersion: ambassador/v0
kind: Mapping
name: httpbin_mapping
prefix: /httpbin/
service: httpbin.org:80
host_rewrite: httpbin.org
  • host && host-regex
基于http header HOST 的mapping
参考
---
apiVersion: ambassador/v0
kind:  Mapping
name:  qotm_mapping
prefix: /qotm/
service: qotm1
---
apiVersion: ambassador/v0
kind:  Mapping
name:  qotm_mapping
prefix: /qotm/
host: qotm.datawire.io
service: qotm2
---
apiVersion: ambassador/v0
kind:  Mapping
name:  qotm_mapping
prefix: /qotm/
host: "^qotm[2-9]\\.datawire\\.io$"
host_regex: true
service: qotm3
  • headers
对于包含特定header 的请求进行mapping
参考
---
apiVersion: ambassador/v0
kind:  Mapping
name:  qotm_mapping
prefix: /qotm/
headers:
  x-qotm-mode: canary
  x-random-header: datawire
service: qotm
  • cors
进行跨域的处理(基于cors)
参考
apiVersion: ambassador/v0
kind:  Mapping
name:  cors_mapping
prefix: /cors/
service: cors-example
cors:
  origins: http://foo.example,http://bar.example
  methods: POST, GET, OPTIONS
  headers: Content-Type
  credentials: true
  exposed_headers: X-Custom-Header
  max_age: "86400"

参考资料

https://www.getambassador.io/reference/mappings

posted on 2018-07-06 16:22  荣锋亮  阅读(596)  评论(0编辑  收藏  举报

导航