【kong 2.0.2研究】系列一:centos7手动指定安装kong

最近研究kong,这里记录一下

 

1、说明

Nginx、OpenRestry、Kong这三个项目紧密相连:

  1. Nginx是模块化设计的反向代理软件,C语言开发;
  2. OpenResty是以Nginx为核心的Web开发平台,可以解析执行Lua脚本(OpenResty与Lua的关系,类似于Jvm与Java,不过Java可以做的事情太多了,OpenResty主要用来做Web、API等);
  3. Kong是一个OpenResty应用,是一个api gateway,具有API管理和请求代理的功能。

 

1.1 git地址

https://github.com/Kong/kong

1.2 kong安装说明

https://docs.konghq.com/install/source/

 

1.3 openssl

https://www.openssl.org/source/

 

2 下载lua-kong-nginx-module

https://github.com/Kong/lua-kong-nginx-module

 

3. 安装OpenResty

https://openresty.org/en/download.html

./configure \
   --prefix=/home1/zcy/kong/openresty \
   --with-pcre-jit \
   --with-http_ssl_module \
   --with-http_realip_module \
   --with-http_stub_status_module \
   --with-http_v2_module \
   --with-openssl=/home1/zcy/kong/openssl-1.1.1c \
   --add-module=/home1/zcy/kong/lua-kong-nginx-module 

 

报错:you need to have ldconfig in your PATH env when enabling luajit

解决方法:编辑Makefile文件,将can_run("ldconfig")改成can_run("/sbin/ldconfig")

 

4 安装Luarocks

 ./configure \
   --lua-suffix=jit \
   --with-lua=/home1/zcy/kong/openresty/luajit \
   --with-lua-include=/home1/zcy/kong/openresty/luajit/include/luajit-2.1 \
   --prefix=/home1/zcy/kong/luarocks

 

配置环境:

export PATH=$PATH:/home1/zcy/kong/openresty/bin:/home1/zcy/kong/luarocks/bin:
export LUA_PATH="/home1/zcy/kong/luarocks/share/lua/5.1/?.lua;;/home1/zcy/kong/luarocks/share/lua/5.1/?/init.lua;"

 

5. 安装kong

luarocks 安装:
$luarocks make kong-*.rockspec

or

$luarocks install kong 2.0.2-0


或者
$ git clone [email protected]:Kong/kong.git
$ cd kong
$ [sudo] make install # this simply runs the `luarocks make kong-*.rockspec` command

 

install成功

lua-resty-lrucache 0.09-2 is now installed in /home1/zcy/kong/luarocks (license: 2bsd)

lua-resty-acme 0.4.3-1 depends on lua-resty-openssl >= 0.2.1-1 (0.4.4-1 installed)
lua-resty-acme 0.4.3-1 is now installed in /home1/zcy/kong/luarocks (license: BSD)

kong-plugin-acme 0.2.2-1 is now installed in /home1/zcy/kong/luarocks (license: Apache 2.0)

kong 2.0.2-0 is now installed in /home1/zcy/kong/luarocks (license: Apache 2.0)

 

创建kong的数据库:

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

启动kong:

./bin/kong start -c ./kong.conf

kong默认的代理地址是:

proxy_listen = 0.0.0.0:8000, 0.0.0.0:8443

默认的管理地址是:

admin_listen = 127.0.0.1:8001, 127.0.0.1:8444 ssl

 

验证Kong

如果一切顺利,你应该看到一条消息通知你Kong正在运行。

默认情况下,Kong监听以下端口:

  • 8000:监听来自Client的传入流量,并将其转发到上游服务
  • 8443:与8000端口类似,它监听HTTPS流量
  • 8001:Admin API用于配置Kong监听
  • 8444:Admin API监听HTTPS流量

 

6. 报错解决

6.1 编译报错,缺少lyaml

Error: Failed installing dependency: https://luarocks.org/lyaml-6.2.4-1.src.rock - Could not find library file for YAML
  No file libyaml.a in /usr/local/lib
  No file libyaml.a in /usr/local/lib64
  No file libyaml.so in /usr/local/lib
  No file libyaml.so in /usr/local/lib64
  No file matching libyaml.so.* in /usr/local/lib
  No file matching libyaml.so.* in /usr/local/lib64
  No file libyaml.a in /usr/lib
  No file libyaml.a in /usr/lib64
  No file libyaml.so in /usr/lib
  No file libyaml.so in /usr/lib64
  No file matching libyaml.so.* in /usr/lib
  No file matching libyaml.so.* in /usr/lib64
  No file libyaml.a in /lib
  No file libyaml.a in /lib64
  No file libyaml.so in /lib
  No file libyaml.so in /lib64
  No file matching libyaml.so.* in /lib
  No file matching libyaml.so.* in /lib64
You may have to install YAML in your system and/or pass YAML_DIR or YAML_LIBDIR to the luarocks command.
Example: luarocks install lyaml YAML_DIR=/usr/local
make: *** [install] Error 1

安装libyaml

# yum downgrade libyaml
# yum install libyaml-devel

 

6.2 启动报错

$ kong start -c ./kong.conf   报错


Error: module 'resty.kong.tls' not found:No LuaRocks module found for resty.kong.tls
        no field package.preload['resty.kong.tls']
        no file './resty/kong/tls.lua'
        no file './resty/kong/tls/init.lua'
        no file '/home1/zcy/kong/openresty/site/lualib/resty/kong/tls.ljbc'
        no file '/home1/zcy/kong/openresty/site/lualib/resty/kong/tls/init.ljbc'
        no file '/home1/zcy/kong/openresty/lualib/resty/kong/tls.ljbc'
        no file '/home1/zcy/kong/openresty/lualib/resty/kong/tls/init.ljbc'
        no file '/home1/zcy/kong/openresty/site/lualib/resty/kong/tls.lua'
        no file '/home1/zcy/kong/openresty/site/lualib/resty/kong/tls/init.lua'
        no file '/home1/zcy/kong/openresty/lualib/resty/kong/tls.lua'
        no file '/home1/zcy/kong/openresty/lualib/resty/kong/tls/init.lua'
        no file '/home1/zcy/kong/luarocks/share/lua/5.1/resty/kong/tls.lua'
        no file './resty/kong/tls.lua'
        no file '/home1/zcy/kong/openresty/luajit/share/luajit-2.1.0-beta3/resty/kong/tls.lua'
        no file '/usr/local/share/lua/5.1/resty/kong/tls.lua'
        no file '/usr/local/share/lua/5.1/resty/kong/tls/init.lua'
        no file '/home1/zcy/kong/openresty/luajit/share/lua/5.1/resty/kong/tls.lua'
        no file '/home1/zcy/kong/openresty/luajit/share/lua/5.1/resty/kong/tls/init.lua'
        no file '/home1/zcy/kong/luarocks/share/lua/5.1/resty/kong/tls/init.lua'
        no file '/home1/zcy/.luarocks/share/lua/5.1/resty/kong/tls.lua'
        no file '/home1/zcy/.luarocks/share/lua/5.1/resty/kong/tls/init.lua'
        no file '/home1/zcy/kong/openresty/site/lualib/resty/kong/tls.so'
        no file '/home1/zcy/kong/openresty/lualib/resty/kong/tls.so'
        no file './resty/kong/tls.so'
        no file '/usr/local/lib/lua/5.1/resty/kong/tls.so'
        no file '/home1/zcy/kong/openresty/luajit/lib/lua/5.1/resty/kong/tls.so'
        no file '/usr/local/lib/lua/5.1/loadall.so'
        no file '/home1/zcy/.luarocks/lib/lua/5.1/resty/kong/tls.so'
        no file '/home1/zcy/kong/luarocks/lib/lua/5.1/resty/kong/tls.so'
        no file '/home1/zcy/kong/openresty/site/lualib/resty.so'
        no file '/home1/zcy/kong/openresty/lualib/resty.so'
        no file './resty.so'
        no file '/usr/local/lib/lua/5.1/resty.so'
        no file '/home1/zcy/kong/openresty/luajit/lib/lua/5.1/resty.so'
        no file '/usr/local/lib/lua/5.1/loadall.so'
        no file '/home1/zcy/.luarocks/lib/lua/5.1/resty.so'
        no file '/home1/zcy/kong/luarocks/lib/lua/5.1/resty.so'

  Run with --v (verbose) or --vv (debug) for more details

 

原因:是kong启动时会在预先设置的路径中加载tls.lua和init.lua模块,而搜索完所有路径都没有发现需要的模块就会报错,可能是在安装过程中依赖的lua模块没有放到对应的路径下。

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

拷贝https://github.com/Kong/lua-kong-nginx-module里的lualib/resty/kong/tls.lua到  luarocks/share/lua/5.1/resty/kong/tls.lua

 

failed to wait for schmeaconsensus:timeout

 

使用kong migrations bootstrap进行初始化的过程中,出现如下所示”failed to wait for schmeaconsensus:timeout”的错误。

原因:kong使用的数据库采用的是多台服务器集群部署的方式。在初始化过程中会进行一系列的创建数据库、表、字段等一系列操作,并要在集群之间完成数据的同步一致化。可能网络、磁盘IO等原因,未在设定的时间内完成数据一致化,就会报错。

解决方法:kong migrations bootstrap初始化命令后边可以跟一些选项参数,其中--db-timeout选项是用来指定超时时间的,如果不手动指定该选项,默认参数值为60(单位为秒)。因此可以手动指定该选项,并设置一个更长时间的参数。

 

8 命令

kong start -c ./kong.conf 

kong stop -p ./

 

9 其他问题参考:

https://www.lijiaocn.com/%E9%97%AE%E9%A2%98/2018/09/29/kong-usage-problem-and-solution.html

 

参考:

https://blog.csdn.net/lijiaocn/article/details/83004672

https://promagic.cn/apiwangguankongshiyongjiaochengyinginxopenrestyhekongdejibengainianyushiyongfangfa.shtml

https://bbs.huaweicloud.com/forum/thread-39375-1-1.html

 

请我喝咖啡

如果觉得文章写得不错,能对你有帮助,可以扫描我的微信二维码请我喝咖啡哦~~哈哈~~

你可能感兴趣的:(Nginx,Kong)