微服务网关--kong

Kong网关是一个轻量级、高性能、可拓展的云原生API网关。下面我们以docker的形式搭建kong环境。

1. 拉取kong-gateway镜像并打上标签

lzl@lzl:~$ docker pull kong/kong-gateway:2.6.0.1-alpine
2.6.0.1-alpine: Pulling from kong/kong-gateway
a0d0a0d46f8b: Already exists 
292d8c0f5367: Pulling fs layer 
8f939e93459a: Pulling fs layer 
8335045176a7: Pulling fs layer 
2.6.0.1-alpine: Pulling from kong/kong-gateway
a0d0a0d46f8b: Already exists 
292d8c0f5367: Pull complete 
8f939e93459a: Pull complete 
8335045176a7: Pull complete 
Digest: sha256:20d1f65138b36ffeadd6c63abe0dc1b496d42ab7bd49553328524d0bbf622026
Status: Downloaded newer image for kong/kong-gateway:2.6.0.1-alpine
docker.io/kong/kong-gateway:2.6.0.1-alpine

lzl@lzl:~$ docker tag kong/kong-gateway:2.6.0.1-alpine kong

2. 创建kong和其组件使用的网络

lzl@lzl:~$ docker network create kong-net
db4a092863e8dc77f26cf4aa43ffb62d09e19c1c66e9b15418d92277850c83a3
lzl@lzl:~$ docker network ls
NETWORK ID     NAME              DRIVER    SCOPE
505f6cc0e5b7   bridge            bridge    local
9027fdbdc8f6   docker_gwbridge   bridge    local
7a84b4fa35eb   host              host      local
db4a092863e8   kong-net          bridge    local
66b37b687b76   none              null      local

3.配置kong所使用的database

这里我们使用postgres,当然也可以使用别的数据库。

lzl@lzl:~$ docker run -d --name kong-database \
> --network=kong-net \
> -p 5432:5432 \
> -e "POSTGRES_USER=kong" \
> -e "POSTGRES_DB=kong" \
> -e "POSTGRES_PASSWORD=kong" \
> postgres:9.6
0d9691c833ab59555dadee339a1f7e15fcc4948793bede7a39112e9f39d62ee7

4. 正式启动kong之前需要迁移数据库

lzl@lzl:~$ docker run --rm \
> --network=kong-net \
> -e "KONG_DATABASE=postgres" \
> -e "KONG_PG_HOST=kong-database" \
> -e "KONG_PG_USER=kong" \
> -e "KONG_PG_PASSWORD=kong" \
> -e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" \
> kong:latest kong migrations bootstrap
Bootstrapping database...
migrating core on database 'kong'...
core migrated up to: 000_base (executed)
core migrated up to: 003_100_to_110 (executed)
core migrated up to: 004_110_to_120 (executed)
core migrated up to: 005_120_to_130 (executed)
···
···
migrating enterprise.response-transformer-advanced on database 'kong'...
enterprise.response-transformer-advanced migrated up to: 001_1500_to_2100 (executed)
82 migrations processed
82 executed
Database is up-to-date

5. 启动kong

lzl@lzl:~$ docker run -d --name kong \
> --network=kong-net \
> -e "KONG_DATABASE=postgres" \
> -e "KONG_PG_HOST=kong-database" \
> -e "KONG_PG_USER=kong" \
> -e "KONG_PG_PASSWORD=kong" \
> -e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" \
> -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
e549737ac5cd6bcc49bcf073619950402b40f312fbe7affc028c6b46039a7f20

到这里,kong-gateway就已经启动了。这里开放的几个端口说明一下:

端口 用途
8000 监听客户端传入的HTTP请求并进行转发
8443 监听客户端传入的HTTPS请求并进行转发
8001 Admin API,管理者通过这个端口对Kong的监听服务进行配置、插件设置、API的配置以及负载均衡等
8444 可通过此端口对HTTPS请求进行监控

6. 为配置kong的可视化界面konga配置数据库

进入到postgres中添加新的用户,并创建konga用的数据库。

lzl@lzl:~$ docker exec -it kong-database /bin/bash
root@0d9691c833ab:/# psql -U kong -W
Password for user kong: 
psql (9.6.24)
Type "help" for help.

kong=# create user konga with password 'konga';
CREATE ROLE
kong=# create database konga owner konga;
CREATE DATABASE
kong=# grant all privileges on database konga to konga;
GRANT
kong=# 

7. 预启动konga

这一步主要是为了konga配置数据库。

lzl@lzl:~$ docker run --rm pantsel/konga:latest \
> -c prepare \
> -a postgres \
> -u postgresql://konga:konga@10.0.20.25:5432/konga
debug: Preparing database...
Using postgres DB Adapter.
Database exists. Continue...
debug: Hook:api_health_checks:process() called
debug: Hook:health_checks:process() called
debug: Hook:start-scheduled-snapshots:process() called
debug: Hook:upstream_health_checks:process() called
debug: Hook:user_events_hook:process() called
debug: Seeding User...
debug: User seed planted
debug: Seeding Kongnode...
debug: Kongnode seed planted
debug: Seeding Emailtransport...
debug: Emailtransport seed planted
debug: Database migrations completed!

8. 启动konga

lzl@lzl:~$ docker run -d --name konga \--network=kong-net 
> -e "DB_ADAPTER=postgres" \
> -e "DB_HOST=10.0.20.25" \
> -e "DB_PORT=5432" \
> -e "DB_USER=konga" \
> -e "DB_PASSWORD=konga" \
> -e "DB_DATABASE=konga" \
> -e "DB_PG_SCHEMA=public" \
> -e "NODE_ENV=production" \
> -p 1337:1337 pantsel/konga
7031e0fb024b3c1919895b1f9ae516f06a3e95a805aee0076a3cfb99f3d889f5

# 开放这个端口,以免不能正常访问
lzl@lzl:~$ sudo iptables -A INPUT -p udp --dport 1337 -j ACCEPT
lzl@lzl:~$ sudo iptables -A INPUT -p tcp --dport 1337 -j ACCEPT

9. 现在就可以打开界面玩一玩了

创建连接,将konga连接到kong的API。这里注意Kong Admin URL就写http://kong:8001,我写http://localhost:8001怎么都连不上。

创建服务。这个服务可以是一个应用,也可以是某个接口。我把我的博客作为服务,让网关帮我做转发。

然后配置转发路由。这里输入完一定要按下回车。

现在访问:8000/blog端口会自动转到。但是目前还存在许多问题。

  • 使用kong之后我的宿主机无法访问虚拟机的内容了。
  • 对于kong网关的转发和路由机制还没搞清楚。

除此之外,kong-gateway还可以接入身份认证插件(如:JWT),链路追踪插件(如:zipkin),监控插件(如:prometheus),值得好好研究一下。

参考:

https://www.cnblogs.com/jerryqm/p/12901036.html

https://www.jianshu.com/p/551a4c61e224

自认为是幻象波普星的来客
Built with Hugo
主题 StackJimmy 设计