docker部署kong+Consul
配置要求
4核8G内存 100G硬盘
环境搭建
需要安装的组件有
- Consul 服务注册与发现组件
- Kong 服务网关
- PostgreSQL Kong网关的数据存储
- Konga kong网关的UI
创建docker网络
首选,需要创建network,让各个容器能够相互通讯
docker network create --driver bridge --subnet 172.20.0.0/24 --gateway 172.20.0.1 kong-net
部署Consul
我们先部署Consul
docker run -d \
--name consul \
--network=kong-net \
--ip=172.20.0.3 \
-v /data/consul.d:/consul/config.d \
-p 8500:8500 \
-p 8600:8600 \
-p 8600:8600/udp \
-p 8300:8300 \
-p 8301:8301 \
-p 8301:8301/udp \
-p 8302:8302 \
-p 8302:8302/udp \
consul agent \
-server -bootstrap -ui \
-config-dir=/consul/config.d \
-client=0.0.0.0
8500 : http 端口,用于 http 接口和 web ui访问;
8300 : server rpc 端口,同一数据中心 consul server 之间通过该端口通信
8301 : serf lan 端口,同一数据中心 consul client 通过该端口通信;
用于处理当前datacenter中LAN的gossip通信;
8302 : serf wan 端口,不同数据中心 consul server 通过该端口通信; agent Server使用,处理与其他datacenter的gossip通信;
8600 : dns 端口,用于已注册的服务发现;
安装完后,访问127.20.0.1:8500
查看服务信息;
测试手动注册一个服务,创建一个test1.json文件,然后将其放到config.d目录中
{
"services": [
{
"id": "test1",
"name": "test1-service",
"tags": [
"c-test1"
],
"address": "172.20.0.10",
"port": 80,
"checks": [
{
"http": "http://172.20.0.10/health",
"tls_skip_verify": false,
"method": "Get",
"interval": "10s",
"timeout": "1s"
}
]
}
]
}
测试一下DNS发现,可以发现answer secion出现上面配置的两台机器;
dig @172.20.0.1 -p 8600 test1.service.consul SRV
开始部署KONG
安装PostgreSQL
docker run -d --name kong-database \
--network=kong-net \
--ip=172.20.0.4 \
-p 5432:5432 \
-e "POSTGRES_USER=kong" \
-e "POSTGRES_DB=kong" \
-e "POSTGRES_PASSWORD=kong" \
postgres:9.6
迁移数据库
初始化数据库, 注意:由于使用了自定义的网络(–network),所以容器之间可以通过服务名相互访问。
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:1.5.1 kong migrations bootstrap
安装KONG
docker run -d --name kong \
--network=kong-net \
--ip=172.20.0.5 \
-e "KONG_DATABASE=postgres" \
-e "KONG_PG_HOST=172.20.0.4" \
-e "KONG_PG_USER=kong" \
-e "KONG_PG_PASSWORD=kong" \
-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" \
-e "KONG_PROXY_LISTEN=0.0.0.0:8000, 0.0.0.0:9080 http2, 0.0.0.0:9081 http2 ssl" \
-e "KONG_DNS_RESOLVER=172.20.0.3:8600" \
-e "KONG_DNS_ORDER=SRV,LAST,A,CNAME" \
-p 8000:8000 \
-p 9080:9080 \
-p 8443:8443 \
-p 8001:8001 \
-p 127.0.0.1:8444:8444 \
kong:1.5.1
端口说明:
8001: 管理端api http访问端口
8444: 管理端api ssl访问端口
8000:http访问端口(http)
8443:http访问端口(ssl)
9080: grpc端口(http2)
安装完成后,访问172.18.0.1:8001 可看到服务相关信息
注意,由于配置DNS解析,参数KONG_PG_HOST如果指定容器名称,可能无法访问,这里使用容器地址;
KONG_DNS_RESOLVER
环境变量为DNS解析地址,如果有多个集群地址,逗号隔开;
然后安装UI, 这里使用konga
docker run -d --name konga -p 1337:1337 --network kong-net pantsel/konga
安装成功后,访问: http://172.18.0.1:1337/
对外暴露端口
- consul
8500 : http 端口,用于 http 接口和 web ui访问;
8600 : dns 端口,用于已注册的服务发现; - kong
8001: 管理端api http访问端口
8000:http访问端口(http)
9080: grpc端口(http2) - konga
1337: ui服务
备忘
通过curl向kong添加service和route
http
服务
curl -i -X POST \
--url http://localhost:8001/services/ \
--data 'name=lmcrm-http' \
--data 'protocol=http' \
--data 'host=lmcrm-http.service.consul' \
--data 'retries=2'
路由
curl -i -X POST \
--url http://localhost:8001/services/lmcrm-http/routes \
--data 'name=lmcrm-http-router' \
--data 'paths[]=/lmcrm' \
--data 'strip_path=false' \
--data 'protocols[]=http' \
--data 'protocols[]=https'
grpc
服务
curl -i -X POST \
--url http://localhost:8001/services/ \
--data 'name=lmcrm-grpc' \
--data 'protocol=grpc' \
--data 'host=lmcrm-grpc.service.consul' \
--data 'port=21015' \
--data 'retries=2'
路由
curl -i -X POST \
--url http://localhost:8001/services/lmcrm-grpc/routes \
--data 'name=lmcrm-grpc-router' \
--data 'paths[]=/lmcrm.Lmcrm' \
--data 'strip_path=false' \
--data 'protocols[]=grpc'