MacOs下安装Kong网关

目录

Kong网关介绍

准备brew 安装

安装基本依赖

安装   pcre opensslOpenResty - 安装

安装 lua 和 luarocks 

安装openresty

安装postgresql

安装kong 

bug解决

Error: module 'resty.kong.tls' not found:

Database needs bootstrapping or is older than Kong 1.0

Error: nginx configuration is invalid (exit code 1):


Kong网关介绍

官网地址:API Gateway and Service Connectivity Platform | Kong Inc.

官网:https://konghq.com/

github: https://github.com/Kong

可以在空的官方文档中,选择mac 环境安装,当然也可以选择其他模式的安装
https://konghq.com/install/?itm_source=website&itm_medium=nav  

开源的kong 管理页面,非官方(两个都可以)

kong admin: pocketdigi · GitHub  kong-admin-ui

kongga:    GitHub - pantsel/konga: More than just another GUI to Kong Admin API

官方安装教程:Install Kong Gateway on macOS - v2.8.x | Kong Docs

准备brew 安装

mac 下安装,基本可以使用 brew, 但是之前下载的homebrew 源,是国外的,下载和更新很慢,建议使用如下命令,直接切换源(可选 中科大 或 清华源)

/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"

选中科大镜像源

安装基本依赖

Mac OS X (macOS) 用户安装过程中由于pcre和openssl可能出错,直接使用brew单独安装 OpenResty - 安装

安装   pcre opensslOpenResty - 安装

# 安装
brew install pcre openssl
#查看安装路径
brew list pcre openssl
 
#我个人安装路径不是这里,是/opt/homebrew/路径下
 
/usr/local/Cellar/pcre/8.44/bin/pcre-config
/usr/local/Cellar/pcre/8.44/bin/pcregrep
/usr/local/Cellar/pcre/8.44/bin/pcretest
/usr/local/Cellar/pcre/8.44/include/ (6 files)
/usr/local/Cellar/pcre/8.44/lib/libpcre.1.dylib
/usr/local/Cellar/pcre/8.44/lib/libpcre16.0.dylib
/usr/local/Cellar/pcre/8.44/lib/libpcre32.0.dylib
/usr/local/Cellar/pcre/8.44/lib/libpcrecpp.0.dylib
/usr/local/Cellar/pcre/8.44/lib/libpcreposix.0.dylib
/usr/local/Cellar/pcre/8.44/lib/pkgconfig/ (5 files)
/usr/local/Cellar/pcre/8.44/lib/ (10 other files)
/usr/local/Cellar/pcre/8.44/share/doc/ (64 files)
/usr/local/Cellar/pcre/8.44/share/man/ (103 files)
/usr/local/Cellar/openssl@1.1/1.1.1g/.bottle/etc/ (7 files)
/usr/local/Cellar/openssl@1.1/1.1.1g/bin/c_rehash
/usr/local/Cellar/openssl@1.1/1.1.1g/bin/openssl
/usr/local/Cellar/openssl@1.1/1.1.1g/include/openssl/ (104 files)
/usr/local/Cellar/openssl@1.1/1.1.1g/lib/libcrypto.1.1.dylib
/usr/local/Cellar/openssl@1.1/1.1.1g/lib/libssl.1.1.dylib
/usr/local/Cellar/openssl@1.1/1.1.1g/lib/engines-1.1/ (2 files)
/usr/local/Cellar/openssl@1.1/1.1.1g/lib/pkgconfig/ (3 files)
/usr/local/Cellar/openssl@1.1/1.1.1g/lib/ (4 other files)
/usr/local/Cellar/openssl@1.1/1.1.1g/share/doc/ (3964 files)
/usr/local/Cellar/openssl@1.1/1.1.1g/share/man/ (3964 files)

安装 lua 和 luarocks 

brew install lua
brew install luarocks
# luarocks install 和 link的先后顺序,有可能调换
brew link luarocks

 
# 安装依赖库
luarocks install lua-resty-jit-uuid


#如果安装问题,请执行以下
git clone git://github.com/diegonehab/luasocket.git
cd luasocket
make macosx
sudo make install

安装openresty

brew install openresty/brew/openresty
# 具体参考官网: http://openresty.org/cn/installation.html
 
# 安装目录
 sudo find / -name openresty
Password:
#openresty的执行命令
/usr/local/bin/openresty
# nginx openresty 等的配置文件
/usr/local/etc/openresty
/usr/local/Homebrew/Library/Taps/openresty
/usr/local/var/homebrew/linked/openresty
# openresty 的站点目录?
/usr/local/opt/openresty
/usr/local/Cellar/openresty
/usr/local/Cellar/openresty/1.15.8.3_1/bin/openresty
/usr/local/Cellar/openresty@1.15.8.3/1.15.8.3/openresty
/usr/local/Cellar/openresty@1.15.8.3/1.15.8.3/openresty/bin/openresty
/usr/local/Cellar/openresty@1.15.8.3/1.15.8.3/bin/openresty

安装postgresql

brew install postgresql
initdb /usr/local/var/postgres -E utf8
pg_ctl -D /usr/local/var/postgres -l logfile start
/usr/local/bin/psql -d postgres
​
#初始化kong的用户和数据库
CREATE USER kong; CREATE DATABASE kong OWNER kong;

安装kong 

brew tap kong/kong
brew install kong
 
 
 #配置环境变量
 
 vim ~/.bash_profile
 # kong #根据自己的安装路径选择
export PATH=$PATH:/opt/homebrew/Cellar/openresty@1.19.3.2/1.19.3.2/openresty/bin
export PATH=$PATH:/opt/homebrew/Cellar/kong/2.8.1/bin
#kong end

从kong 的github 上下载kong.conf.default 文件

https://github.com/Kong/kong/blob/master/kong.conf.default

并拷贝到 /etc/kong/ 目录下,命名为kong.conf, 修改kong.conf配置

sudo mkdir -p /etc/kong

sudo cp kong.conf.default /etc/kong/kong.conf

注意:这个kong.conf配置文件的信息需要修改,其中包括配置postgres数据库的地址账号密码等信息。

其中我的配置如下

database = postgres             # Determines which of PostgreSQL or Cassandra

pg_host = 127.0.0.1             # Host of the Postgres server.
pg_port = 5432                    # Port of the Postgres server.
pg_timeout = 5000               # Defines the timeout (in ms), for connecting,

pg_user = kong                    # Postgres user.
pg_password = kong             # Postgres user's password.
pg_database = kong             # The database name to connect to.

pg_ssl = off                         # Toggles client-server TLS connections
                                
pg_ssl_verify = off               # Toggles server certificate verification if

运行Kong migrations 进行Database初始化操作

kong migrations up -c /etc/kong/kong.conf

运行启动Kong网关

kong start -c /etc/kong/kong.conf

验证Kong网关启动成功

⋊> ~ curl -i http://localhost:8001/                                                                                                                                                                                                                              
HTTP/1.1 200 OK
Date: Thu, 08 Mar 2018 08:49:08 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/0.12.2

访问127.0.0.1:8001,出现一大串Json数据,说明Kong网关已经启动成功。如下图:

 至此,本地搭建Kong网关的步骤已经全部完成。

bug解决

启动:kong start -c /etc/kong/kong.conf

Error: module 'resty.kong.tls' not found:

原因:是kong启动时会在预先设置的路径中加载tls.lua和init.lua模块,而搜索完所有路径都没有发现需要的模块就会报错,可能是在安装过程中依赖的lua模块没有放到对应的路径下。原文链接:https://blog.csdn.net/zzhongcy/article/details/105100678
解决方法:拷贝https://github.com/Kong/lua-kong-nginx-module里的lualib/resty/kong/tls.lua到 luarocks/share/lua/5.1/resty/kong/tls.lua

我的解决方案:

使用find命令在根目录下找kong缺失的模块文件(如:find / -name tls.lua),将找到后的模块文件放到kong启动加载时检索的目录下,或使用建立软连接的方式即可。

find / -name tls.lua 找到 tls.lua 文件,然后拷贝到/usr/local/share/lua/5.1/resty/kong

sudo find / -name tls.lua
# 找到 的tls.lua文件
/usr/local/Cellar/openresty@1.15.8.3/1.15.8.3/openresty/lualib/resty/kong/tls.lua
# 拷贝tls.lua 文件到 /usr/local/share/lua/5.1/resty/kong,没有kong 文件夹,则创建一个
sudo cp /usr/local/Cellar/openresty@1.15.8.3/1.15.8.3/openresty/lualib/resty/kong/tls.lua /usr/local/share/lua/5.1/resty/kong

Database needs bootstrapping or is older than Kong 1.0

Error: /usr/local/share/lua/5.1/kong/cmd/utils/migrations.lua:16: Database needs bootstrapping or is older than Kong 1.0.
 
To start a new installation from scratch, run 'kong migrations bootstrap'.
 
To migrate from a version older than 1.0, migrated to Kong 1.5.0 first.
If you still have 'apis' entities, you can convert them to Routes and Services
using the 'kong migrations migrate-apis' command in Kong 1.5.0.

解决:这一步出错,有可能是我上面流程中的第6步忘了

# 执行命令
kong migrations bootstrap 
# 启动kong
sudo kong start -c /etc/kong/kong.conf
Kong started
 
# 访问: http://127.0.0.1:8001/
出现很多json 串,标识安装成功

Error: nginx configuration is invalid (exit code 1):

Error: nginx configuration is invalid (exit code 1):
nginx: [emerg] cannot load certificate "/opt/homebrew/opt/kong/ssl/kong-default.crt": BIO_new_file() failed (SSL: error:02001002:system library:fopen:No such file or directory:fopen('/opt/homebrew/opt/kong/ssl/kong-default.crt','r') error:2006D080:BIO routines:BIO_new_file:no such file)
nginx: configuration file /opt/homebrew/opt/kong/nginx.conf test failed

ssl证书失败

1 查看/opt/homebrew/opt/kong/nginx.conf配置

pid pids/nginx.pid;
error_log logs/error.log notice;

# injected nginx_main_* directives
daemon on;
worker_processes auto;
worker_rlimit_nofile 256;

events {
    # injected nginx_events_* directives
    multi_accept on;
    worker_connections 256;
}

http {
    include 'nginx-kong.conf';
}

2 打开nginx-kong.conf

看到需要这四个文件

3 复制出来新增的文件 

进入

cd /opt/homebrew/opt/kong/ssl/  ##进入文件路径
cp admin-kong-default.crt kong-default.crt #复制改名
 cp admin-kong-default.key kong-default.key
cp admin-kong-default-ecdsa.crt kong-default-ecdsa.crt 
cp admin-kong-default-ecdsa.key kong-default-ecdsa.key
sudo kong start -c /etc/kong/kong.conf

版权声明:本文为mocas_wang原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。