kong和apisix都是以nginx+lua为基础的网关应用,其实可以实现平滑迁移配置的。github有一个kong-to-apisix的项目,可以作为助力。
一.导出kong的配置文件
登录Kong服务器,执行 默认在目录下生成kong.yml文件
kong config db_export -c kong.conf
sz kong.yml
二.转换kong的配置文件为apisix格式的
#引入kong.yml
git clone https://github.com/api7/kong-to-apisix.git
cd kong-to-apisix/
go env -w GOPROXY=https://goproxy.cn #先安装好GO
make build
#注意如果kong的版本低于2.4,会有各种报错,哪行报错删掉哪行即可
./bin/kong-to-apisix migrate -i kong.yml -o apisix.yml
三.把apisix.yml配置写入到apisix
我看其他人都是说用官网推荐的stand-alone模式,但测试不可行,所以这里我用Python3脚本来写配置,登录apisix服务器执行
import requests
import yaml
import json
from time import sleep
kong_addr='http://x.x.x.x:8001'
apisix_addr='http://127.0.0.1:9080'
with open('apisix.yml') as f:
x=yaml.safe_load(f)
header={'X-API-KEY': 'edd1c9f034335f136f87ad84b625c8f1'}
upstream_info=x['upstreams']
service_info=x['services']
route_info=x['routes']
num=0
for upstream in upstream_info:
if('name' in upstream):
id=upstream['id']
#删除没有weight信息的node
del_list=[]
for i in upstream['nodes']:
if('weight' not in i):
del_list.append(i)
for i in del_list:
upstream['nodes'].remove(i)
#从kong接口取得healthcheck信息
kong_url=kong_addr+"/upstreams/"+id
kong_res=requests.get(kong_url).json()
upstream['checks']=kong_res['healthchecks']
if(upstream['checks']['active']['healthy']['interval']==0):
upstream['checks']['active']['healthy']['interval']=2
if(upstream['checks']['passive']['healthy']['successes']==0):
upstream['checks']['passive']['healthy']['successes']=1
if(upstream['checks']['passive']['unhealthy']['timeouts']==0):
upstream['checks']['passive']['unhealthy']['timeouts']=30
if(upstream['checks']['passive']['unhealthy']['http_failures']==0):
upstream['checks']['passive']['unhealthy']['http_failures']=30
if(upstream['checks']['passive']['unhealthy']['tcp_failures']==0):
upstream['checks']['passive']['unhealthy']['tcp_failures']=30
datapost=json.dumps(upstream)
url=apisix_addr+"/apisix/admin/upstreams/"+id
res=requests.put(url,data=datapost,headers=header)
if(res.status_code not in [200,201]):
print(url,res.status_code)
print(upstream,res.json())
sleep(1)
num+=1
print('upstream更新完成,更新了{}条'.format(num))
num=0
for service in service_info:
if('name' in service):
datapost=json.dumps(service)
id=service['id']
url=apisix_addr+"/apisix/admin/services/"+id
res=requests.put(url,data=datapost,headers=header)
if(res.status_code not in [200,201]):
print(url)
print(service,res.json())
sleep(1)
num+=1
print('service更新完成,更新了{}条'.format(num))
num=0
for route in route_info:
if('id' in route):
route['name']=route['id']
datapost=json.dumps(route)
id=route['id']
url=apisix_addr+"/apisix/admin/routes/"+id
res=requests.put(url,data=datapost,headers=header)
if(res.status_code not in [200,201]):
print(url)
print(route,res.json())
sleep(1)
num+=1
print('route更新完成,更新了{}条'.format(num))