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