• kong的安装以及简单配置


        1、下载镜像

    2、安装

    2.1、创建网络

    2.2、安装postgres,kong依赖于postgres

    2.3、初始化kong数据表信息

    2.4、启动kong

    2.5、初始化konga数据信息

    2.6、启动konga

    2.7、创建连接节点

    2.8、创建Upstreams

    2.9、配置services和routes

    3.0、验证

    3.1、其它


    1、下载镜像

    1. docker pull postgres:9.6-bullseye
    2. docker pull kong/kong-gateway:2.4.1.0-alpine
    3. docker pull pantsel/konga:0.14.9
    • 这3个镜像的版本必须匹配,否则初始化kong、konga数据时会出现各种问题。

    2、安装

    2.1、创建网络

    docker network create yanshao_net

    2.2、安装postgres,kong依赖于postgres

    1. docker run -d --network=yanshao_net --name postgres \
    2. -p 5432:5432 \
    3. -e "POSTGRES_USER=kong" \
    4. -e "POSTGRES_DB=kong" \
    5. -e "POSTGRES_PASSWORD=kong" \
    6. postgres:9.6-bullseye

    备注:

    • postgres默认端口5432,通过 -p 5432:5432,将5432暴露出来,当前主机可访问,否则只能在容器内访问。
    • -e "POSTGRES_USER=kong"、e "POSTGRES_DB=kong"、-e "POSTGRES_PASSWORD=kong"创建指定的库、用户名、密码
    • 启动成功之后,在数据库客户端检查postgres是否安装成功

     2.3、初始化kong数据表信息

    1. docker run --rm --network=yanshao_net \
    2. -e "KONG_DATABASE=postgres" \
    3. -e "KONG_PG_HOST=postgres" \
    4. -e "KONG_PG_PASSWORD=kong" \
    5. -e "KONG_PASSWORD=kong" \
    6. kong/kong-gateway:2.4.1.0-alpine kong migrations bootstrap
    •   -e "KONG_PG_HOST=postgres":是启动posgres数据库时,--name指定的名称
    •   -e "KONG_DATABASE=postgres": 指定当前kong使用的数据库类型,这里就是postgres
    •   -e "KONG_PG_PASSWORD=kong" : 数据库密码
    •   -e "KONG_PASSWORD=kong":数据库用户名
    1. Bootstrapping database...
    2. migrating core on database 'kong'...
    3. core migrated up to: 000_base (executed)
    4. core migrated up to: 003_100_to_110 (executed)
    5. core migrated up to: 004_110_to_120 (executed)
    6. core migrated up to: 005_120_to_130 (executed)
    7. core migrated up to: 006_130_to_140 (executed)
    8. core migrated up to: 007_140_to_150 (executed)
    9. core migrated up to: 008_150_to_200 (executed)
    10. core migrated up to: 009_200_to_210 (executed)
    11. core migrated up to: 010_210_to_211 (executed)
    12. core migrated up to: 011_212_to_213 (executed)
    13. core migrated up to: 012_213_to_220 (executed)
    14. core migrated up to: 013_220_to_230 (executed)
    15. migrating acl on database 'kong'...
    16. acl migrated up to: 000_base_acl (executed)
    17. acl migrated up to: 002_130_to_140 (executed)
    18. acl migrated up to: 003_200_to_210 (executed)
    19. acl migrated up to: 004_212_to_213 (executed)
    20. migrating acme on database 'kong'...
    21. acme migrated up to: 000_base_acme (executed)
    22. migrating basic-auth on database 'kong'...
    23. basic-auth migrated up to: 000_base_basic_auth (executed)
    24. basic-auth migrated up to: 002_130_to_140 (executed)
    25. basic-auth migrated up to: 003_200_to_210 (executed)
    26. migrating bot-detection on database 'kong'...
    27. bot-detection migrated up to: 001_200_to_210 (executed)
    28. migrating canary on database 'kong'...
    29. canary migrated up to: 001_200_to_210 (executed)
    30. migrating degraphql on database 'kong'...
    31. degraphql migrated up to: 000_base (executed)
    32. migrating graphql-rate-limiting-advanced on database 'kong'...
    33. graphql-rate-limiting-advanced migrated up to: 000_base_gql_rate_limiting (executed)
    34. migrating hmac-auth on database 'kong'...
    35. hmac-auth migrated up to: 000_base_hmac_auth (executed)
    36. hmac-auth migrated up to: 002_130_to_140 (executed)
    37. hmac-auth migrated up to: 003_200_to_210 (executed)
    38. migrating ip-restriction on database 'kong'...
    39. ip-restriction migrated up to: 001_200_to_210 (executed)
    40. migrating jwt on database 'kong'...
    41. jwt migrated up to: 000_base_jwt (executed)
    42. jwt migrated up to: 002_130_to_140 (executed)
    43. jwt migrated up to: 003_200_to_210 (executed)
    44. migrating jwt-signer on database 'kong'...
    45. jwt-signer migrated up to: 000_base_jwt_signer (executed)
    46. jwt-signer migrated up to: 001_200_to_210 (executed)
    47. migrating key-auth on database 'kong'...
    48. key-auth migrated up to: 000_base_key_auth (executed)
    49. key-auth migrated up to: 002_130_to_140 (executed)
    50. key-auth migrated up to: 003_200_to_210 (executed)
    51. migrating key-auth-enc on database 'kong'...
    52. key-auth-enc migrated up to: 000_base_key_auth_enc (executed)
    53. key-auth-enc migrated up to: 001_200_to_210 (executed)
    54. migrating mtls-auth on database 'kong'...
    55. mtls-auth migrated up to: 000_base_mtls_auth (executed)
    56. mtls-auth migrated up to: 001_200_to_210 (executed)
    57. mtls-auth migrated up to: 002_2200_to_2300 (executed)
    58. migrating oauth2 on database 'kong'...
    59. oauth2 migrated up to: 000_base_oauth2 (executed)
    60. oauth2 migrated up to: 003_130_to_140 (executed)
    61. oauth2 migrated up to: 004_200_to_210 (executed)
    62. oauth2 migrated up to: 005_210_to_211 (executed)
    63. migrating openid-connect on database 'kong'...
    64. openid-connect migrated up to: 000_base_openid_connect (executed)
    65. openid-connect migrated up to: 001_14_to_15 (executed)
    66. openid-connect migrated up to: 002_200_to_210 (executed)
    67. migrating proxy-cache-advanced on database 'kong'...
    68. proxy-cache-advanced migrated up to: 001_035_to_050 (executed)
    69. migrating rate-limiting on database 'kong'...
    70. rate-limiting migrated up to: 000_base_rate_limiting (executed)
    71. rate-limiting migrated up to: 003_10_to_112 (executed)
    72. rate-limiting migrated up to: 004_200_to_210 (executed)
    73. migrating response-ratelimiting on database 'kong'...
    74. response-ratelimiting migrated up to: 000_base_response_rate_limiting (executed)
    75. migrating session on database 'kong'...
    76. session migrated up to: 000_base_session (executed)
    77. session migrated up to: 001_add_ttl_index (executed)
    78. migrating vault-auth on database 'kong'...
    79. vault-auth migrated up to: 000_base_vault_auth (executed)
    80. migrating enterprise on database 'kong'...
    81. enterprise migrated up to: 000_base (executed)
    82. enterprise migrated up to: 006_1301_to_1500 (executed)
    83. enterprise migrated up to: 006_1301_to_1302 (executed)
    84. enterprise migrated up to: 010_1500_to_2100 (executed)
    85. enterprise migrated up to: 007_1500_to_1504 (executed)
    86. enterprise migrated up to: 008_1504_to_1505 (executed)
    87. enterprise migrated up to: 007_1500_to_2100 (executed)
    88. enterprise migrated up to: 009_1506_to_1507 (executed)
    89. enterprise migrated up to: 009_2100_to_2200 (executed)
    90. enterprise migrated up to: 010_2200_to_2211 (executed)
    91. enterprise migrated up to: 010_2200_to_2300 (executed)
    92. enterprise migrated up to: 010_2200_to_2300_1 (executed)
    93. migrating enterprise.acl on database 'kong'...
    94. enterprise.acl migrated up to: 001_1500_to_2100 (executed)
    95. migrating enterprise.basic-auth on database 'kong'...
    96. enterprise.basic-auth migrated up to: 001_1500_to_2100 (executed)
    97. migrating enterprise.hmac-auth on database 'kong'...
    98. enterprise.hmac-auth migrated up to: 001_1500_to_2100 (executed)
    99. migrating enterprise.jwt on database 'kong'...
    100. enterprise.jwt migrated up to: 001_1500_to_2100 (executed)
    101. migrating enterprise.key-auth on database 'kong'...
    102. enterprise.key-auth migrated up to: 001_1500_to_2100 (executed)
    103. migrating enterprise.key-auth-enc on database 'kong'...
    104. enterprise.key-auth-enc migrated up to: 001_1500_to_2100 (executed)
    105. migrating enterprise.mtls-auth on database 'kong'...
    106. enterprise.mtls-auth migrated up to: 001_1500_to_2100 (executed)
    107. enterprise.mtls-auth migrated up to: 002_2200_to_2300 (executed)
    108. migrating enterprise.oauth2 on database 'kong'...
    109. enterprise.oauth2 migrated up to: 001_1500_to_2100 (executed)
    110. enterprise.oauth2 migrated up to: 002_2200_to_2211 (executed)
    111. migrating enterprise.request-transformer-advanced on database 'kong'...
    112. enterprise.request-transformer-advanced migrated up to: 001_1500_to_2100 (executed)
    113. migrating enterprise.response-transformer-advanced on database 'kong'...
    114. enterprise.response-transformer-advanced migrated up to: 001_1500_to_2100 (executed)
    115. 80 migrations processed
    116. 80 executed
    • 成功之后,可以在客户端看到创建了74张表

     2.4、启动kong

    1. docker run -d --name kong-ee --network=yanshao_net \
    2. -e "KONG_DATABASE=postgres" \
    3. -e "KONG_PG_HOST=postgres" \
    4. -e "KONG_PG_PASSWORD=kong" \
    5. -e "KONG_PROXY_ACCESS_LOG=/dev/stdout" \
    6. -e "KONG_ADMIN_ACCESS_LOG=/dev/stdout" \
    7. -e "KONG_PROXY_ERROR_LOG=/dev/stderr" \
    8. -e "KONG_ADMIN_ERROR_LOG=/dev/stderr" \
    9. -e "KONG_ADMIN_LISTEN=0.0.0.0:8001" \
    10. -e "KONG_ADMIN_GUI_URL=http://127.0.0.1:9002" \
    11. -p 9000:8000 \
    12. -p 9443:8443 \
    13. -p 9001:8001 \
    14. -p 9444:8444 \
    15. -p 9002:8002 \
    16. kong/kong-gateway:2.4.1.0-alpine

    kong有5个端口需要对外暴露

    • 8000:对应的http请求代理端口,后面配置代理时,需要用到它,这里对外暴露端口改成了9000
    • 8001:  http接口的管理端口
    • 8443:  对应的https请求的代理端口
    • 8444:  https接口的管理端口
    • 8002:  对api做了一些数据分析
    • kong启动之后,可以在浏览器输入:http://127.0.0.1:9002/overview,可以通过这个链接,检查kong是否安装成功

    2.5、初始化konga数据信息

    docker run --rm --network=yanshao_net  pantsel/konga:latest -c prepare -a postgres -u postgresql://kong:kong@postgres:5432/kong
    1. debug: Preparing database...
    2. Using postgres DB Adapter.
    3. Database exists. Continue...
    4. debug: Hook:api_health_checks:process() called
    5. debug: Hook:health_checks:process() called
    6. debug: Hook:start-scheduled-snapshots:process() called
    7. debug: Hook:upstream_health_checks:process() called
    8. debug: Hook:user_events_hook:process() called
    9. debug: Seeding User...
    10. debug: User seed planted
    11. debug: Seeding Kongnode...
    12. debug: Kongnode seed planted
    13. debug: Seeding Emailtransport...
    14. debug: Emailtransport seed planted
    15. debug: Database migrations completed!
    • 初始化成功之后,在postgres新增了11张表(现在是85张表,之前kong初始化时,创建了74张) 

    2.6、启动konga

    1. docker run -d -p 1337:1337 --network yanshao_net --name konga \
    2. -e "DB_ADAPTER=postgres" \
    3. -e "DB_URI=postgresql://kong:kong@postgres:5432/kong" \
    4. -e "DB_PASSWORD=kong" \
    5. -e "NODE_ENV=production" \
    6. pantsel/konga:0.14.9
    • konga默认端口是1337,这里也需要对外暴露
    • 启动成功之后,输入http://127.0.0.1:1337/register,检查konga是否安装成功
    • 注册konga管理员账号

     2.7、创建连接节点

    • ip是当前主机ip地址,端口是9001(默认是8001,本文对外映射时设置成9001了)

    2.8、创建Upstreams

    • 输入name,然后提交
    • 点击刚添加的upstream点击DETAILS添加targets,然后点击ADD TARGET输入target(ip+port)后点击SUBMIT TARGET即可,ip为本地电脑ip保证kong容器内可访问,端口为本地服务端口

    •  本地起一个简单的springboot服务,上面配置的ip + 端口,可直接访问这个springboot服务

     2.9、配置services和routes

    •  输入name、host(可以与前面Upstreams的name保持一致,也可以跟前面配置的ip保持一致)
    • protocol(协议类型,这里是http)
    • port这里是9000,kong启动之后,http的代理端口是8000,启动时做了对外映射,所以这里是9000
    • service保存后,点击service name进入详情页面,然后点击Routes添加路由规则
    • 点击ADD ROUTE输入name、paths(输入转发规则需按回车才生效),Strip Path(是否把匹配成功的paths删除后在转发后端服务器.),Preserve Host(转发后端是否带host参数,默认不带,Protocols 只保留http即可,最后点击SUBMIT ROUTE

     

    3.0、验证

    1. @RestController
    2. @RequestMapping("/user")
    3. public class UserController {
    4. @PostMapping("/v1/userInfo")
    5. public Map userInfo1(){
    6. Map res = new HashMap<>();
    7. res.put("version","v1");
    8. res.put("userName","Tiger");
    9. res.put("address","beijing");
    10. return res;
    11. }
    12. }

     到这里,说明kong可以配置代理并且使用了!

    3.1、其它

    • postgres的版本最好选择9.6.X(尝试过12.XX以上的版本都失败了,貌似konga最新的版本只能使用9.6.X以下的版本)
    • kong/kong-gateway最新版本需要当前主机安装openssl,而且需要安装openssl.rand等命令(尝试过,失败了)
    • pantsel/konga无法兼容12.0以上的版本(10.0和11.0没有试过),会有ssl验证的问题
  • 相关阅读:
    POJ3104Drying题解
    Unity关键词语音识别
    正点原子嵌入式linux驱动开发——RGB转HDMI
    034:vue项目利用qrcodejs2生成二维码示例
    c++ 学习 之 继承中 父类子类构造函数和析构函数的调用顺序
    C#控件开源库:MetroFramework的下载
    【JavaEE重点知识归纳】第5节:方法
    关于.NET代码质量度量和分析的一些总结
    【kali-漏洞扫描】(2.1)Nessus下载安装(上)
    Spring反序列化JNDI分析
  • 原文地址:https://blog.csdn.net/qq_31289187/article/details/127683144