Z.S.K.'s Records

Kong学习(strip_path使用)

kong中的strip_path用于是否将请求中的url中path前缀进行剥离,在kong controller的不同版本中, 使用方法不同

在1.x的版本中, strip_path需要使用kongingress进行指定

而在2.x中, 直接在ingress中指定annotataion即可

这里主要以2.x的新版本来实践一番

strip_path

部署echoserver服务

1
2
3
4
5
$ kubectl create namespace echo
namespace/echo created
$ kubectl apply -n echo -f https://bit.ly/echo-service
service/echo created
deployment.apps/echo created

这里为了体现strip_path的功能, 分别部署echo1,echo2两个deploy、svc

部署ingress

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
echo '
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
konghq.com/strip-path: "true" # 打开路径前缀strip.
name: my-app
namespace: echo
spec:
rules:
- host: myapp.example.com
http:
paths:
- path: /echo1/
backend:
serviceName: echo
servicePort: 8080
- path: /echo2/
backend:
serviceName: echo
servicePort: 8080
' | kubectl create -f -

请求

1
2
3
4
5
6
7
# PROXY_IP 为kong proxy svc 地址
export PROXY_IP=10.109.157.29
# 分别请求echo1/echo2
# echo1
curl -svX GET http://myapp.example.com/echo1/myapp/foo --resolve myapp.example.com:80:$PROXY_IP
# echo2
curl -svX GET http://myapp.example.com/echo2/myapp/foo --resolve myapp.example.com:80:$PROXY_IP

请求echo2返回:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Hostname: echo2-786fd5f6f5-tc596

Pod Information:
node name: g105e1900156
pod name: echo2-786fd5f6f5-tc596
pod namespace: echo
pod IP: 10.244.0.48 # echo2的pod的IP

Server values:
server_version=nginx: 1.13.3 - lua: 10008

Request Information:
client_address=::ffff:10.244.0.37
method=GET
real path=/myapp/foo # 真正请求到达pod时可以看到请求已经将echo2前缀给strip掉了.
query=
request_version=1.1
request_scheme=http
request_uri=http://myapp.example.com:8080/myapp/foo

Request Headers:
accept=*/*
connection=keep-alive
host=myapp.example.com
user-agent=curl/7.47.0
x-forwarded-for=10.244.0.1
x-forwarded-host=myapp.example.com
x-forwarded-port=8000
x-forwarded-prefix=/echo2/myapp/foo # 这里是真正的请求路径.
x-forwarded-proto=http
x-real-ip=10.244.0.1

Request Body:
-no body in request-

因此可以通过strip_path来实现每个应用使用特定的路径前缀.

在旧版本中,需要这样使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: "kong-demo"
configuration.konghq.com: "kongingress-demo"
name: demo-ingress
spec:
rules:
- http:
paths:
- backend:
serviceName: test-service
servicePort: 80
path: /
---
apiVersion: configuration.konghq.com/v1
kind: KongIngress
metadata:
name: kongingress-demo
route:
protocols:
- http
- https
strip_path: false # 在这里开始strip_path,然后在ingress上绑定kongingress

参考文章:

转载请注明原作者: 周淑科(https://izsk.me)

 wechat
Scan Me To Read on Phone
I know you won't do this,but what if you did?