Kong系列 - 05 - 入门
程序员文章站
2022-06-03 21:16:22
...
client:指下游客户端。
service:指上游服务。
route:定义匹配客户端请求的规则,每个路由都与一个服务相关联,而服务可能有多个与之相关联的路由。
plugin:它是在代理生命周期中运行的业务逻辑,插件可以生效于全局或者特定的路由和服务。
consumer:消费者表示服务的消费者或者使用者,可能是一个用户,也可能是一个应用。
下面举一例子。
我们先部署一个echo服务。该服务没有对Kubernetes外暴露端口。如果想从Kubernetes集群之外访问该服务就需要通过Kong网关代理。
vi echo-server-service.yaml
apiVersion: v1
kind: Service
metadata:
labels:
app: echo
name: echo
spec:
ports:
- port: 8080
name: high
protocol: TCP
targetPort: 8080
- port: 80
name: low
protocol: TCP
targetPort: 8080
selector:
app: echo
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: echo
name: echo
spec:
replicas: 1
selector:
matchLabels:
app: echo
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: echo
spec:
containers:
- image: e2eteam/echoserver:2.2
name: echo
ports:
- containerPort: 8080
env:
- name: NODE_NAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
resources: {}
为了让Kong感知echo服务,需要在Kong中创建一个服务service,该服务指向echo服务。
curl -X POST \
--url http://192.168.1.55:32444/services/ \
--data 'name=echo-service' \
--data 'url=http://echo:8080' \
-s | python -m json.tool
{
"client_certificate": null,
"connect_timeout": 60000,
"created_at": 1576504925,
"host": "echo",
"id": "f15edf62-cb68-4279-a41f-7f5273ccd001",
"name": "echo-service",
"path": null,
"port": 8080,
"protocol": "http",
"read_timeout": 60000,
"retries": 5,
"tags": null,
"updated_at": 1576504925,
"write_timeout": 60000
}
查询服务services列表,可以看出刚才创建的服务service。
curl -X GET \
--url http://192.168.1.55:32444/services/ \
-s | python -m json.tool
{
"data": [
{
"client_certificate": null,
"connect_timeout": 60000,
"created_at": 1576504925,
"host": "echo",
"id": "f15edf62-cb68-4279-a41f-7f5273ccd001",
"name": "echo-service",
"path": null,
"port": 8080,
"protocol": "http",
"read_timeout": 60000,
"retries": 5,
"tags": null,
"updated_at": 1576504925,
"write_timeout": 60000
}
],
"next": null
}
为了让Kong知道客户端的哪些请求是访问echo服务的,需要为echo服务添加一条路由。每个服务是可以有多个路由与之对应。
curl -X POST \
--url http://192.168.1.55:32444/services/echo-service/routes \
--data 'name=echo-service-route' \
--data 'paths[]=/foo' \
-s | python -m json.tool
{
"created_at": 1576505187,
"destinations": null,
"headers": null,
"hosts": null,
"https_redirect_status_code": 426,
"id": "d152f741-9ed1-47d2-98bc-f8c438075e2e",
"methods": null,
"name": "echo-service-route",
"paths": [
"/foo"
],
"preserve_host": false,
"protocols": [
"http",
"https"
],
"regex_priority": 0,
"service": {
"id": "f15edf62-cb68-4279-a41f-7f5273ccd001"
},
"snis": null,
"sources": null,
"strip_path": true,
"tags": null,
"updated_at": 1576505187
}
测试一下,可以看出根据路由能够访问echo服务。
curl -iX GET --url http://192.168.1.55:32080/foo
HTTP/1.1 200 OK
Content-Type: text/plain; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Date: Mon, 16 Dec 2019 14:09:21 GMT
Server: echoserver
X-Kong-Upstream-Latency: 5
X-Kong-Proxy-Latency: 65
Via: kong/1.3.0
Hostname: echo-75cf96d976-pwbg7
Pod Information:
node name: k8s-node2
pod name: echo-75cf96d976-pwbg7
pod namespace: default
pod IP: 10.244.2.12
Server values:
server_version=nginx: 1.14.2 - lua: 10015
Request Information:
client_address=10.244.2.11
method=GET
real path=/
query=
request_version=1.1
request_scheme=http
request_uri=http://echo:8080/
Request Headers:
accept=*/*
connection=keep-alive
host=echo:8080
user-agent=curl/7.29.0
x-forwarded-for=10.244.0.0
x-forwarded-host=192.168.1.55
x-forwarded-port=8000
x-forwarded-proto=http
x-real-ip=10.244.0.0
Request Body:
-no body in request-
下一篇: KONG安装记录
推荐阅读
-
Mysql入门系列:MYSQL客户机程序2―增加错误检查[组图]_MySQL
-
【CSS3 入门教程系列】CSS3 Media Queries 实现响应式设计_html/css_WEB-ITnose
-
适合入门的php基础系列教程
-
Android控件系列之相册Gallery&Adapter适配器入门&控件缩放动画入门
-
Android控件系列之相册Gallery&Adapter适配器入门&控件缩放动画入门
-
ABP入门系列应用BootstrapTable表格插件
-
ABP入门系列之分页功能的实现
-
ABP入门系列之Json格式化
-
DesignPattern系列__05开闭原则
-
Spring Boot入门系列八(SpringBoot 整合Mybatis)