安装
version: '3' services: kong-database: image: postgres:9.6 container_name: kong-database ports: - 5432:5432 environment: - POSTGRES_USER=kong - POSTGRES_DB=kong - POSTGRES_PASSWORD=kong networks: - kong-net volumes: - "db-data-kong-postgres:/var/lib/postgresql/data" kong-migrations: image: kong environment: - KONG_DATABASE=postgres - KONG_PG_HOST=kong-database - KONG_PG_PASSWORD=kong - KONG_CASSANDRA_CONTACT_POINTS=kong-database command: kong migrations bootstrap restart: on-failure networks: - kong-net depends_on: - kong-database kong: image: kong container_name: kong environment: - LC_CTYPE=en_US.UTF-8 - LC_ALL=en_US.UTF-8 - KONG_DATABASE=postgres - KONG_PG_HOST=kong-database - KONG_PG_USER=kong - KONG_PG_PASSWORD=kong - KONG_CASSANDRA_CONTACT_POINTS=kong-database - KONG_PROXY_ACCESS_LOG=/dev/stdout - KONG_ADMIN_ACCESS_LOG=/dev/stdout - KONG_PROXY_ERROR_LOG=/dev/stderr - KONG_ADMIN_ERROR_LOG=/dev/stderr - KONG_ADMIN_LISTEN=0.0.0.0:8001, 0.0.0.0:8444 ssl restart: on-failure ports: - 8000:8000 - 8443:8443 - 8001:8001 - 8444:8444 links: - kong-database:kong-database networks: - kong-net depends_on: - kong-migrations konga: image: pantsel/konga ports: - 1337:1337 links: - kong:kong container_name: konga environment: - NODE_ENV=production volumes: db-data-kong-postgres: networks: kong-net: external: false
测试接口地址
http://192.168.452:9999
原始nginx配置
upstream passportUpstream { server localhost:9999 weight=100; } server { listen 8000; location /hello { proxy_pass http://passportUpstream; } }
使用API进行配置
使用 curl 命令, 仅作为例子
# 配置 upstream curl -X POST http://localhost:8001/upstreams --data "name=passportUpstream" # 配置 target curl -X POST http://localhost:8001/upstreams/passport/targets --data "target=localhost:8080" --data "weight=100" # 配置 service curl -X POST http://localhost:8001/services --data "name=getUserInfo" --data "host=passportUpstream" # 配置 route curl -X POST http://localhost:8001/routes --data "paths[]=/user" --data "service.id=8695cc65-16c1-43b1-95a1-5d30d0a50409" curl -X POST http://localhost:8001/routes --data "hosts[]=*.example.com,test.com,*.abc.com" --data "service.id=8695cc65-16c1-43b1-95a1-5d30d0a50409"
使用httpie 命令
# 配置 upstream echo '{"name":"passportUpstream"}' | http POST http://localhost:8001/upstreams # 配置 target echo '{"target":"192.168.4.52:9999","weight":100}' | http POST http://localhost:8001/upstreams/passportUpstream/targets # 配置 service echo '{"name":"getLogs","host":"passportUpstream"}' | http POST http://localhost:8001/services # 配置 route 获取上一步返回的serivce id 作为参数传入 echo '{"paths":["/user"],"service":{"id":"1e6da5de-be0b-49a4-9a6b-764c2a03b906"}}' | http POST http://localhost:8001/routes
对应关系
Upstream : target -> 1:n
Service:Upstream -> 1:1 or 1:0 (Service 可以直接指向具体的Target,相当于不做负载均衡)
Service : Route -> 1:n
测试
echo '{"message":"2021-10-20-10:18"}' | http POST http://localhost:8000/user/logs -v
结果
POST /user/logs HTTP/1.1 Accept: application/json, */*;q=0.5 Accept-Encoding: gzip, deflate, br Connection: keep-alive Content-Length: 31 Content-Type: application/json Host: localhost:8000 User-Agent: HTTPie/2.5.0 { "message": "2021-10-20-10:18" }
输出配置
获取主机IP
ip addr show docker0
5: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
link/ether 02:42:ce:e4:26:c7 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
valid_lft forever preferred_lft forever
# 配置 upstream echo '{"name":"passportUpstream"}' | http POST http://localhost:8001/upstreams # 配置 target echo '{"target":"192.168.4.52:9999","weight":100}' | http POST http://localhost:8001/upstreams/passportUpstream/targets # 配置 service echo '{"name":"admin-api","host":"localhost","port":"8001"}' | http POST http://localhost:8001/services # 配置 route 获取上一步返回的serivce id 作为参数传入 echo '{"paths":["/admin-api"],"service":{"id":"81ac656d-cd6e-42c8-94d9-a5a85effa14a"}}' | http POST http://localhost:8001/admin-api/routes