• 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 - 安装

    1. # 安装
    2. brew install pcre openssl
    3. #查看安装路径
    4. brew list pcre openssl
    5. #我个人安装路径不是这里,是/opt/homebrew/路径下
    6. /usr/local/Cellar/pcre/8.44/bin/pcre-config
    7. /usr/local/Cellar/pcre/8.44/bin/pcregrep
    8. /usr/local/Cellar/pcre/8.44/bin/pcretest
    9. /usr/local/Cellar/pcre/8.44/include/ (6 files)
    10. /usr/local/Cellar/pcre/8.44/lib/libpcre.1.dylib
    11. /usr/local/Cellar/pcre/8.44/lib/libpcre16.0.dylib
    12. /usr/local/Cellar/pcre/8.44/lib/libpcre32.0.dylib
    13. /usr/local/Cellar/pcre/8.44/lib/libpcrecpp.0.dylib
    14. /usr/local/Cellar/pcre/8.44/lib/libpcreposix.0.dylib
    15. /usr/local/Cellar/pcre/8.44/lib/pkgconfig/ (5 files)
    16. /usr/local/Cellar/pcre/8.44/lib/ (10 other files)
    17. /usr/local/Cellar/pcre/8.44/share/doc/ (64 files)
    18. /usr/local/Cellar/pcre/8.44/share/man/ (103 files)
    19. /usr/local/Cellar/openssl@1.1/1.1.1g/.bottle/etc/ (7 files)
    20. /usr/local/Cellar/openssl@1.1/1.1.1g/bin/c_rehash
    21. /usr/local/Cellar/openssl@1.1/1.1.1g/bin/openssl
    22. /usr/local/Cellar/openssl@1.1/1.1.1g/include/openssl/ (104 files)
    23. /usr/local/Cellar/openssl@1.1/1.1.1g/lib/libcrypto.1.1.dylib
    24. /usr/local/Cellar/openssl@1.1/1.1.1g/lib/libssl.1.1.dylib
    25. /usr/local/Cellar/openssl@1.1/1.1.1g/lib/engines-1.1/ (2 files)
    26. /usr/local/Cellar/openssl@1.1/1.1.1g/lib/pkgconfig/ (3 files)
    27. /usr/local/Cellar/openssl@1.1/1.1.1g/lib/ (4 other files)
    28. /usr/local/Cellar/openssl@1.1/1.1.1g/share/doc/ (3964 files)
    29. /usr/local/Cellar/openssl@1.1/1.1.1g/share/man/ (3964 files)

    安装 lua 和 luarocks 

    1. brew install lua
    2. brew install luarocks
    3. # luarocks install 和 link的先后顺序,有可能调换
    4. brew link luarocks
    5. # 安装依赖库
    6. luarocks install lua-resty-jit-uuid
    7. #如果安装问题,请执行以下
    8. git clone git://github.com/diegonehab/luasocket.git
    9. cd luasocket
    10. make macosx
    11. sudo make install

    安装openresty

    1. brew install openresty/brew/openresty
    2. # 具体参考官网: http://openresty.org/cn/installation.html
    3. # 安装目录
    4. sudo find / -name openresty
    5. Password:
    6. #openresty的执行命令
    7. /usr/local/bin/openresty
    8. # nginx openresty 等的配置文件
    9. /usr/local/etc/openresty
    10. /usr/local/Homebrew/Library/Taps/openresty
    11. /usr/local/var/homebrew/linked/openresty
    12. # openresty 的站点目录?
    13. /usr/local/opt/openresty
    14. /usr/local/Cellar/openresty
    15. /usr/local/Cellar/openresty/1.15.8.3_1/bin/openresty
    16. /usr/local/Cellar/openresty@1.15.8.3/1.15.8.3/openresty
    17. /usr/local/Cellar/openresty@1.15.8.3/1.15.8.3/openresty/bin/openresty
    18. /usr/local/Cellar/openresty@1.15.8.3/1.15.8.3/bin/openresty

    安装postgresql

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

    安装kong 

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

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

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

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

    1. sudo mkdir -p /etc/kong
    2. sudo cp kong.conf.default /etc/kong/kong.conf

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

    其中我的配置如下

    1. database = postgres # Determines which of PostgreSQL or Cassandra
    2. pg_host = 127.0.0.1 # Host of the Postgres server.
    3. pg_port = 5432 # Port of the Postgres server.
    4. pg_timeout = 5000 # Defines the timeout (in ms), for connecting,
    5. pg_user = kong # Postgres user.
    6. pg_password = kong # Postgres user's password.
    7. pg_database = kong # The database name to connect to.
    8. pg_ssl = off # Toggles client-server TLS connections
    9. 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网关启动成功

    1. ⋊> ~ curl -i http://localhost:8001/
    2. HTTP/1.1 200 OK
    3. Date: Thu, 08 Mar 2018 08:49:08 GMT
    4. Content-Type: application/json; charset=utf-8
    5. Transfer-Encoding: chunked
    6. Connection: keep-alive
    7. Access-Control-Allow-Origin: *
    8. 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

    1. sudo find / -name tls.lua
    2. # 找到 的tls.lua文件
    3. /usr/local/Cellar/openresty@1.15.8.3/1.15.8.3/openresty/lualib/resty/kong/tls.lua
    4. # 拷贝tls.lua 文件到 /usr/local/share/lua/5.1/resty/kong,没有kong 文件夹,则创建一个
    5. 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

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

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

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

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

    1. Error: nginx configuration is invalid (exit code 1):
    2. 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)
    3. nginx: configuration file /opt/homebrew/opt/kong/nginx.conf test failed

    ssl证书失败

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

    1. pid pids/nginx.pid;
    2. error_log logs/error.log notice;
    3. # injected nginx_main_* directives
    4. daemon on;
    5. worker_processes auto;
    6. worker_rlimit_nofile 256;
    7. events {
    8. # injected nginx_events_* directives
    9. multi_accept on;
    10. worker_connections 256;
    11. }
    12. http {
    13. include 'nginx-kong.conf';
    14. }

    2 打开nginx-kong.conf

    看到需要这四个文件

    3 复制出来新增的文件 

    进入

    1. cd /opt/homebrew/opt/kong/ssl/ ##进入文件路径
    2. cp admin-kong-default.crt kong-default.crt #复制改名
    3. cp admin-kong-default.key kong-default.key
    4. cp admin-kong-default-ecdsa.crt kong-default-ecdsa.crt
    5. cp admin-kong-default-ecdsa.key kong-default-ecdsa.key
    sudo kong start -c /etc/kong/kong.conf
  • 相关阅读:
    兄弟机床联网
    从0开始学习JavaScript--JavaScript基础
    上传的文件资源不能被百度收录
    idea提交git项目,提交代码 点击commit一闪而过,没有反应的解决办法
    【TSP问题】基于遗传算法求解固定的开放式不返回多旅行推销员问题(M-TSP)附matlab代码
    MU editor IDE编辑器 二次开发记录与踩坑
    C 与 C++ 的真正区别在哪里?
    习题6-3 使用函数输出指定范围内的完数分数 20
    MyBatis的xml里#{}的参数为null报错、将null作为参数传递报错问题
    单独使用logback作为日志
  • 原文地址:https://blog.csdn.net/mocas_wang/article/details/126236140