一:起kong容器
docker network create kong-net #新建子网
docker run -d --name kong \
--network=kong-net \
-v "kong-vol:/usr/local/kong/declarative" \
-e "KONG_DATABASE=off" \
-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
二:生成kong.conf和kong.yml并修改
docker exec -it kong kong config init /home/kong/kong.yml #初始化出kong.yml
##替换kong.yml内容如下
_format_version: '1.1'
routes:
- name: route.sooc
protocols:
- http
paths:
- /api/sooc
strip_path: true
preserve_host: false
regex_priority: 1
service: service.sooc
- name: route.coos
protocols:
- http
paths:
- /api/coos
strip_path: true
preserve_host: false
regex_priority: 1
service: service.coos
- name: route.coop
protocols:
- http
paths:
- /api/coop
strip_path: true
preserve_host: false
regex_priority: 1
service: service.coop
- name: route.csop
protocols:
- http
paths:
- /api/csop
strip_path: true
preserve_host: false
regex_priority: 1
service: service.csop
- name: route.cmos
protocols:
- http
paths:
- /api/cmos
strip_path: true
preserve_host: false
regex_priority: 1
service: service.cmos
- name: route.csos
protocols:
- http
paths:
- /api/csos
strip_path: true
preserve_host: false
regex_priority: 1
service: service.csos
- name: route.cmop
protocols:
- http
paths:
- /api/cmop
strip_path: true
preserve_host: false
regex_priority: 1
service: service.cmop
services:
- name: service.sooc
host: upstream.sooc
protocol: http
port: 80
connect_timeout: 60000
read_timeout: 60000
write_timeout: 60000
retries: 5
- name: service.coos
host: upstream.coos
protocol: http
port: 80
connect_timeout: 60000
read_timeout: 60000
write_timeout: 60000
retries: 5
- name: service.coop
host: upstream.coop
protocol: http
port: 80
connect_timeout: 60000
read_timeout: 60000
write_timeout: 60000
retries: 5
- name: service.csop
host: upstream.csop
protocol: http
port: 80
connect_timeout: 60000
read_timeout: 60000
write_timeout: 60000
retries: 5
- name: service.cmos
host: upstream.cmos
protocol: http
port: 80
connect_timeout: 60000
read_timeout: 60000
write_timeout: 60000
retries: 5
- name: service.csos
host: upstream.csos
protocol: http
port: 80
connect_timeout: 60000
read_timeout: 60000
write_timeout: 60000
retries: 5
- name: service.cmop
host: upstream.cmop
protocol: http
port: 80
connect_timeout: 60000
read_timeout: 60000
write_timeout: 60000
retries: 5
upstreams:
- name: upstream.sooc
algorithm: round-robin
- name: upstream.coos
algorithm: round-robin
- name: upstream.coop
algorithm: round-robin
- name: upstream.csop
algorithm: round-robin
- name: upstream.cmos
algorithm: round-robin
- name: upstream.csos
algorithm: round-robin
- name: upstream.cmop
algorithm: round-robin
targets:
- upstream: upstream.sooc
target: xxxxxxxx:port
weight: 100
- upstream: upstream.coos
target: xxxxxxxx:port
weight: 100
- upstream: upstream.coop
target: xxxxxxxx:port
weight: 100
- upstream: upstream.csop
target: xxxxxxxx:port
weight: 100
- upstream: upstream.cmos
target: xxxxxxxx:port
weight: 100
- upstream: upstream.csos
target: xxxxxxxx:port
weight: 100
- upstream: upstream.cmop
target: xxxxxxxx:port
weight: 100
docker exec -it -u root 93a4f8c026c4 /bin/bash #以root用户进入,否则无权限
cp -rp /etc/kong/kong.conf.default /etc/kong/kong.conf
将kong.conf文件database改为database = off ,将declarative_config改为declarative_config = /home/kong/kong.yml
###commit成新的镜像并save保存
docker commit 93a4f8c026c4 kong:202108251623
docker save cf6b7eca8102 >/root/kong-202108251623.tar
三:接口路由测试
#!/bin/bash
# @Time : 20210824
# @Author : wulei
# @File : init_kong
# 初始化kong路由配置信息
addr=localhost:8001
curl -X POST http://localhost:8001/upstreams --data "name=upstream.sooc"
curl -X POST http://localhost:8001/upstreams/upstream.sooc/targets --data "target=xxxxxxip-addr:port" --data "weight=100"
curl -X POST http://localhost:8001/services/ --data 'name=service.sooc' --data 'host=upstream.sooc'
curl -X POST http://localhost:8001/services/service.sooc/routes --data "paths[]=/api/sooc" --data "name=route.sooc"
curl -X POST http://localhost:8001/upstreams --data "name=upstream.cmop"
curl -X POST http://localhost:8001/upstreams/upstream.cmop/targets --data "target=xxxxxxip-addr:port" --data "weight=100"
curl -X POST http://localhost:8001/services/ --data 'name=service.cmop' --data 'host=upstream.cmop'
curl -X POST http://localhost:8001/services/service.cmop/routes --data "paths[]=/api/cmop" --data "name=route.cmop"
curl -X POST http://localhost:8001/upstreams --data "name=upstream.coos"
curl -X POST http://localhost:8001/upstreams/upstream.coos/targets --data "target=xxxxxxip-addr:port" --data "weight=100"
curl -X POST http://localhost:8001/services/ --data 'name=service.coos' --data 'host=upstream.coos'
curl -X POST http://localhost:8001/services/service.coos/routes --data "paths[]=/api/coos" --data "name=route.coos"
curl -X POST http://localhost:8001/upstreams --data "name=upstream.coop"
curl -X POST http://localhost:8001/upstreams/upstream.coop/targets --data "target=xxxxxxip-addr:port" --data "weight=100"
curl -X POST http://localhost:8001/services/ --data 'name=service.coop' --data 'host=upstream.coop'
curl -X POST http://localhost:8001/services/service.coop/routes --data "paths[]=/api/coop" --data "name=route.coop"
curl -X POST http://localhost:8001/upstreams --data "name=upstream.csp"
curl -X POST http://localhost:8001/upstreams/upstream.csp/targets --data "target=xxxxxxip-addr:port" --data "weight=100"
curl -X POST http://localhost:8001/services/ --data 'name=service.csp' --data 'host=upstream.csp'
curl -X POST http://localhost:8001/services/service.csp/routes --data "paths[]=/api/csp" --data "name=route.csp"
curl -X POST http://localhost:8001/upstreams --data "name=upstream.cmos"
curl -X POST http://localhost:8001/upstreams/upstream.cmos/targets --data "target=xxxxxxip-addr:port" --data "weight=100"
curl -X POST http://localhost:8001/services/ --data 'name=service.cmos' --data 'host=upstream.cmos'
curl -X POST http://localhost:8001/services/service.cmos/routes --data "paths[]=/api/cmos" --data "name=route.cmos"
curl -X POST http://localhost:8001/upstreams --data "name=upstream.csos"
curl -X POST http://localhost:8001/upstreams/upstream.csos/targets --data "target=xxxxxxip-addr:port" --data "weight=100"
curl -X POST http://localhost:8001/services/ --data 'name=service.csos' --data 'host=upstream.csos'
curl -X POST http://localhost:8001/services/service.csos/routes --data "paths[]=/api/csos" --data "name=route.csos"