为了账号安全,请及时绑定邮箱和手机立即绑定

Kong系列(一)——Kong集群部署

标签:
微服务 API

Kong 0.13 安装部署

机器准备

节点A:192.168.0.10
节点B:192.168.0.11

节点A,部署一个Kong Node和Postgresql
节点B, 部署一个Kong Node,数据库指向节点A的postgresql

安装

1. 安装数据库postgresql

  • 使用docker创建网络
docker network create kong-net
  • 将postgresql网络指定为新建的网络*(docker原来使用–link,新版本不建议使用,使用–network将两个容器指定到同一个网络来解决问题)*
docker run -d --name kong-database \
  --network=kong-net \
  -p 5432:5432 \
  -e "POSTGRES_USER=kong" \
  -e "POSTGRES_DB=kong" \
  postgres:9.6
  • 数据库迁移,将kong的数据库创建出来(指定到同一个网络)
docker run --rm \
 --network=kong-net \
 -e "KONG_DATABASE=postgres" \
 -e "KONG_PG_HOST=kong-database" \
 -e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" \
 kong:latest kong migrations up

2. postgresql主备热切换

TODO
为了高可用性,使用postgresql的流复制以及双机热备

3. kong集群

  • 节点A部署启动
docker run -d --name kong \
    --network=kong-net \
    -e "KONG_DATABASE=postgres" \
    -e "KONG_PG_HOST=kong-database" \
    -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" \
    -e "KONG_ADMIN_LISTEN_SSL=0.0.0.0:8444" \
    -p 8000:8000 \
    -p 8443:8443 \
    -p 8001:8001 \
    -p 8444:8444 \
    kong:latest
  • 节点B部署启动
docker run -d --name kong \
    -e "KONG_DATABASE=postgres" \
    -e "KONG_PG_HOST=192.168.0.10" \
    -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" \
    -e "KONG_ADMIN_LISTEN_SSL=0.0.0.0:8444" \
    -p 8000:8000 \
    -p 8443:8443 \
    -p 8001:8001 \
    -p 8444:8444 \
  • 添加测试

同步时间默认是5s,可以通过官网集群资料进行修改

	
节点A添加

curl -i -X POST --url http://localhost:8001/apis/ --data 'name=example-api' --data 'hosts=example.com' --data 'upstream_url=http://mockbin.org'

节点B查询

curl http://localhost:8001/apis/

>>>>>>
{
	"total": 1,
	"data": [{
		"created_at": 1527732097194,
		"strip_uri": true,
		"id": "41cbfb3b-eaa1-4a6c-ac19-c069f6daeb83",
		"hosts": ["example.com"],
		"name": "example-api",
		"http_if_terminated": false,
		"https_only": false,
		"retries": 5,
		"preserve_host": false,
		"upstream_connect_timeout": 60000,
		"upstream_read_timeout": 60000,
		"upstream_send_timeout": 60000,
		"upstream_url": "http:\/\/mockbin.org"
	}]
}

节点B添加

curl -i -X POST --url http://localhost:8001/apis/ --data 'name=example-api2' --data 'hosts=example.com' --data 'upstream_url=http://mockbin.org'

节点A查询
curl http://localhost:8001/apis/

>>>>>>
{
	"total": 2,
	"data": [{
		"created_at": 1527732097194,
		"strip_uri": true,
		"id": "41cbfb3b-eaa1-4a6c-ac19-c069f6daeb83",
		"hosts": ["example.com"],
		"name": "example-api",
		"http_if_terminated": false,
		"https_only": false,
		"retries": 5,
		"preserve_host": false,
		"upstream_connect_timeout": 60000,
		"upstream_read_timeout": 60000,
		"upstream_send_timeout": 60000,
		"upstream_url": "http:\/\/mockbin.org"
	}, {
		"created_at": 1527732578045,
		"strip_uri": true,
		"id": "29f12209-f45b-45dc-b6e1-f64e855d7387",
		"hosts": ["example.com"],
		"name": "example-api2",
		"http_if_terminated": false,
		"https_only": false,
		"retries": 5,
		"preserve_host": false,
		"upstream_connect_timeout": 60000,
		"upstream_read_timeout": 60000,
		"upstream_send_timeout": 60000,
		"upstream_url": "http:\/\/mockbin.org"
	}]
}

配置nginx负载Kong集群

对外提供9000端口,通过nginx的负载均衡,根据自身的需求,配置不同的算法,反向代理到kong的集群中

upstream kong {
   # 负载均衡算法,根据最少连接数
   least_conn;
   server 192.168.0.10:8000;
   server 192.168.0.11:8000;
}

server {
   # 这些端口要修改为实际情况
   listen 9100;
   #include snippets/ssl-api.example.com.conf;
   #include snippets/ssl-params.conf;
   # https 因为安全原因,需要禁用 gzip
   # 但是在一些场景下,不需要禁用
   # gzip off;
   gzip on;
   # 设置允许压缩的页面最小字节数
   gzip_min_length 1000;
   gzip_buffers 4 16k;
   gzip_http_version 1.1;
   # 1~9,默认为1,数值越大,压缩率越高,CPU占用越多,时间越久
   gzip_comp_level 3;
   gzip_vary on;
   # 禁用对 IE 6 使用 gzip 压缩
   gzip_disable "MSIE [1-6]\.";
   gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/javascript "application/javascript; charset=utf-8" application/xml application/xml+rss application/json "application/json; charset=utf-8" fo
nt/ttf font/otf image/svg+xml;

   # 设置最大允许的POST数据量,如果提交的文件超过这个值,会出现413错误
   client_max_body_size 20m;
   keepalive_timeout 15;

   # 不显示 nginx 的版本号
   server_tokens off;

   ## Individual nginx logs
   access_log  /var/log/nginx/access.log;
   error_log   /var/log/nginx/error.log;

   # 这里不能用 ^~ 因为后面跟的是字符串不是正则表达式
   # 只匹配符合规则的,其他都返回 404
   location / {
       # 这条命令会将 headers 中的 server 设置为 nginx 代理的后端网站所使用的
       proxy_set_header Host $http_host;
       proxy_redirect off;
       proxy_set_header X-Real-IP $remote_addr;
       proxy_set_header X-Scheme $scheme;
       proxy_pass http://kong;
       expires -1;
   }

   # 主页返回 404
   location = / {
       return 404;
   }
}

使用时,将所有额请求都转发到http://192.168.0.10:9100上,然后后续Kong会根据配置进行相应的处理以及相关的插件过滤

Kong-Dashboard辅助工具

安装

要将kong-dashboard和kong放在同一各network中(使用docker)
docker run -d --network kong-net --name kong-dashboard -p 8080:8080 pgbi/kong-dashboard start
–kong-url http://kong:8001
–basic-auth user=password

访问

http://192.168.0.1:8080 使用创建时的用户名和密码就可以登录进行管理了

点击查看更多内容
1人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
Python工程师
手记
粉丝
17
获赞与收藏
50

关注作者,订阅最新文章

阅读免费教程

感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消