Ref: https://docs.konghq.com/install/docker
安装Docker
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum -y install docker-ce
systemctl daemon-reload && systemctl restart docker
部署Kong
创建网络和存储卷
docker network create kong-net
docker volume create kong-vol
配置文件
cd /var/lib/docker/volumes/kong-vol/_data/
cat > apis.yml <<EOF
_format_version: '1.1'
routes:
- name: route-httpbin-1
protocols:
- http
- https
hosts:
- route-httpbin-1.local
paths:
- /
strip_path: true
preserve_host: false
regex_priority: 0
service: service-httpbin
tags:
- "group-1"
- "api-1"
- name: route-httpbin-2
protocols:
- http
- https
hosts:
- route-httpbin-2.local
paths:
- /httpbin
strip_path: true
preserve_host: false
regex_priority: 0
service: service-httpbin
tags:
- "group-2"
- "api-2"
- name: route-httpbin-3
protocols:
- http
- https
hosts:
- route-httpbin-3.local
paths:
- /anything
- /get
strip_path: false
preserve_host: false
regex_priority: 0
service: service-httpbin
tags:
- "group-3"
- "api-3"
- name: route-httpbin-4
protocols:
- http
- https
hosts:
- route-httpbin-4.local
paths:
- /
strip_path: false
preserve_host: false
regex_priority: 0
service: service-httpbin
tags:
- "group-4"
- "api-4"
- name: route-httpbin-5
protocols:
- http
- https
hosts:
- route-httpbin-5.local
paths:
- /
strip_path: false
preserve_host: false
regex_priority: 0
service: service-httpbin
tags:
- "group-5"
- "api-5"
- name: route-ip
protocols:
- http
- https
hosts:
- route-ip.local
paths:
- /
strip_path: false
preserve_host: false
regex_priority: 0
service: service-ip
tags:
- "group-5"
- "api-5"
services:
- name: service-httpbin
host: upstream-httpbin
protocol: http
port: 80
connect_timeout: 60000
read_timeout: 60000
write_timeout: 60000
retries: 5
- name: service-ip
host: upstream-ip-v1
protocol: http
port: 80
connect_timeout: 60000
read_timeout: 60000
write_timeout: 60000
retries: 5
upstreams:
- name: upstream-httpbin
algorithm: round-robin
tags:
- "v1"
- name: upstream-ip-v1
algorithm: round-robin
tags:
- "v1"
- name: upstream-ip-v2
algorithm: round-robin
tags:
- "v2"
- name: upstream-ip-v3
algorithm: round-robin
tags:
- "v3"
targets:
- upstream: upstream-httpbin
tags:
- "zone1"
target: 35.170.216.115:80
weight: 100
- upstream: upstream-ip-v1
tags:
- "zone1"
target: httpbin.org:80
weight: 100
- upstream: upstream-ip-v2
tags:
- "zone1"
target: ipconfig.io:80
weight: 100
- upstream: upstream-ip-v3
tags:
- "zone1"
target: ipconfig.io:80
weight: 100
plugins:
- name: correlation-id
config:
header_name: "X-SF-Request-Id"
echo_downstream: true
- name: request-size-limiting
config:
allowed_payload_size: 1
size_unit: megabytes
- name: request-size-limiting
route: route-httpbin-5
config:
allowed_payload_size: 4
size_unit: megabytes
- name: request-termination
config:
status_code: 403
message: access forbidden, too many users are connected
route: route-httpbin-4
- name: prometheus
- name: error-log
- name: lambda-deploy
config:
header_name: X-SF-Lambda-Deploy
lambdas:
- lambda: return kong.request.get_header("service-version") == "v1"
upstream_name: upstream-ip-v1
- lambda: return kong.request.get_header("service-version") == "v2"
upstream_name: upstream-ip-v2
- lambda: return kong.request.get_header("service-version") == "v3"
upstream_name: upstream-ip-v3
default_upstream_name: upstream-ip-v1
EOF
启动
docker run -d --name kong \
--network=kong-net \
-v "kong-vol:/usr/local/kong/declarative" \
-e "KONG_DATABASE=off" \
-e "KONG_DECLARATIVE_CONFIG=/usr/local/kong/declarative/apis.yml" \
-e "KONG_LUA_PACKAGE_PATH=/usr/local/kong/declarative/kong-plugins/?.lua" \
-e "KONG_PLUGINS=bundled,lambda-deploy,error-log" \
-e "KONG_PROXY_ACCESS_LOG=/dev/stdout" \
-e "KONG_ADMIN_ACCESS_LOG=/dev/stdout" \
-e "KONG_PROXY_ERROR_LOG=/dev/stderr" \
-e "KONG_ADMIN_ERROR_LOG=/dev/stderr" \
-e "KONG_ADMIN_LISTEN=0.0.0.0:8001, 0.0.0.0:8444 ssl" \
-p 8000:8000 \
-p 8443:8443 \
-p 8001:8001 \
-p 8444:8444 \
kong:latest
测试
# status code: 417
dd if=/dev/zero of=2m.data bs=1M count=2
curl -i -H "host: route-httpbin-2.local" -F "config=@2m.data" \
http://127.0.0.1:8000/httpbin/post
# status code: 404
curl -i -H "host: route-httpbin-3.local" -F "config=@2m.data" \
http://127.0.0.1:8000/post
# status code: 403
curl -i -H "host: route-httpbin-4.local" \
http://127.0.0.1:8000/anything
停止
docker rm -f kong
网友评论