Ambassador系列-04-服务配置Mapping

本文详细介绍了Ambassador服务配置的核心——Mapping,包括增加/删除请求和响应Header、基于host和路径的路由、重定向、重写规则及自动重试功能,展示了Ambassador在Kubernetes环境中灵活的流量管理能力。
摘要由CSDN通过智能技术生成

Ambassador设计旨在让Kubernetes服务的开发者可以轻松灵活地配置流量如何路由到该服务,其核心是Mapping资源,支持7层的HTTP,GRPC和Websocket,也可以通过TCPMapping支持4层的TCP连接。Ambassador必须定义一个或多个Mapping才能访问上游服务。

Mapping通过不同的配置选项实现不同的路由规则,下面进行说明。

增加Request Headers

Ambassador可以给上游服务的http请求中添加请求头header。

vi echo-server-mapping.yaml
---
apiVersion: getambassador.io/v1
kind: Mapping
metadata:
  name: echo-server-mapping
spec:
  prefix: /foo
  add_request_headers:
    x-test-proto: "%PROTOCOL%"
    x-test-ip: "%DOWNSTREAM_REMOTE_ADDRESS_WITHOUT_PORT%"
    x-test-static: This is a test header
    x-test-static-2:
      value: This the test header #same as above  x-test-static header
  service: echo:8080

kubectl apply -f echo-server-mapping.yaml

curl -i http://192.168.1.50:38080/foo
HTTP/1.1 200 OK
date: Sat, 07 Dec 2019 07:25:09 GMT
content-type: text/plain
server: envoy
x-envoy-upstream-service-time: 1
lua-scripts-enabled: Processed
transfer-encoding: chunked


Hostname: echo-5599595fd9-2vfnt

Pod Information:
        node name:      k8s-node1
        pod name:       echo-5599595fd9-2vfnt
        pod namespace:  default
        pod IP: 10.244.1.7

Server values:
        server_version=nginx: 1.14.2 - lua: 10015

Request Information:
        client_address=10.244.1.9
        method=GET
        real path=/
        query=
        request_version=1.1
        request_scheme=http
        request_uri=http://192.168.1.50:8080/

Request Headers:
        accept=*/*
        content-length=0
        host=192.168.1.50:38080
        user-agent=curl/7.29.0
        x-envoy-expected-rq-timeout-ms=3000
        x-envoy-internal=true
        x-envoy-original-path=/foo
        x-forwarded-for=10.244.0.0
        x-forwarded-proto=http
        x-request-id=6075a013-5d50-48ec-9f56-4f2d0dca7bdc
        x-test-ip=10.244.0.0
        x-test-proto=HTTP/1.1
        x-test-static=This is a test header
        x-test-static-2=This the test header

Request Body:
        -no body in request-

增加Response Headers

vi echo-server-mapping.yaml
---
apiVersion: getambassador.io/v1
kind: Mapping
metadata:
  name: echo-server-mapping
spec:
  prefix: /foo
  add_response_headers:
    x-test-static: This is a test header
  service: echo:8080

kubectl apply -f echo-server-mapping.yaml

curl -i http://192.168.1.50:38080/foo
HTTP/1.1 200 OK
date: Sat, 07 Dec 2019 07:28:36 GMT
content-type: text/plain
server: envoy
x-envoy-upstream-service-time: 1
x-test-static: This is a test header
lua-scripts-enabled: Processed
transfer-encoding: chunked


Hostname: echo-5599595fd9-2vfnt

Pod Information:
        node name:      k8s-node1
        pod name:       echo-5599595fd9-2vfnt
        pod namespace:  default
        pod IP: 10.244.1.7

Server values:
        server_version=nginx: 1.14.2 - lua: 10015

R
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值