美文网首页架构我爱编程
kong-api-gateway的环境搭建与使用

kong-api-gateway的环境搭建与使用

作者: 二月江北 | 来源:发表于2018-05-28 00:48 被阅读92次

    摘要

    微服务架构,restful接口必然是分散的,如何统一这些分散的API对外提供统一的访问地址呢?Spring Cloud官方是spring cloud zuul,但是我们公司用的是kong,一开始接触到kong感到陌生,后来自己搭建了一次,觉得它就是个nginx。
    kong api gateway的官方地址: https://getkong.org

    docker命令行安装步骤

    1. 安装Kong的数据库
    sudo docker run -d --name kong-database \
    -p 5432:5432 \
    -e "POSTGRES_USER=kong" \
    -e "POSTGRES_DB=kong" \
    postgres:9.5
    
    -e:设置容器的环境变量
    
    1. 准备kong的数据库 ,这个命令会对数据库进行初始化
    sudo docker run --rm \
    --link kong-database:kong-database \
    -e "KONG_DATABASE=postgres" \
    -e "KONG_PG_HOST=kong-database" \
    -e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" \
    kong:latest kong migrations up
    

    --rm: 启动运行命令就自动删除该容器

    1. 启动kong
    sudo docker run -d --name kong \
        --link kong-database:kong-database \
        -e "KONG_DATABASE=postgres" \
        -e "KONG_PG_HOST=kong-database" \
        -e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" \
        -e "KONG_PROXY_ACCESS_LOG=/dev/stdout" \
        -e "KONG_ADMIN_ACCESS_LOG=/dev/stdout" \
        -e "KONG_PROXY_ERROR_LOG=/dev/stderr" \
        -e "KONG_ADMIN_ERROR_LOG=/dev/stderr" \
        -e "KONG_ADMIN_LISTEN=0.0.0.0:8001" \
        -e "KONG_ADMIN_LISTEN_SSL=0.0.0.0:8444" \
        -p 8000:8000 \
        -p 8443:8443 \
        -p 8001:8001 \
        -p 8444:8444 \
        kong:latest
    
    1. 安装kong-dashboard
    sudo docker run -d -p 8089:8080 -link kong pgbi/kong-dashboard start --kong-url http://kong:8001
    

    浏览器访问 http://<本机IP>:8089 即可。

    image

    rancher下的安装步骤

    1.点击创建服务

    image

    2.将下面的docker-compose编排文件,保存到本地,命名为docker-compose.yml,然后在rancher中打开即可。

    version: '2'
    services:
      kong-database:
        image: postgres:9.5
        environment:
          POSTGRES_DB: kong
          POSTGRES_USER: kong
        stdin_open: true
        tty: true
        ports:
        - 5432:5432/tcp
        labels:
          io.rancher.container.pull_image: always
      kong-dashboard:
        image: pgbi/kong-dashboard
        stdin_open: true
        tty: true
        links:
        - kong:kong
        ports:
        - 8090:8080/tcp
        command:
        - start
        - --kong-url
        - http://kong:8001
        labels:
          io.rancher.container.pull_image: always
      kong:
        image: kong:latest
        environment:
          KONG_ADMIN_ACCESS_LOG: /dev/stdout
          KONG_ADMIN_ERROR_LOG: /dev/stderr
          KONG_ADMIN_LISTEN: 0.0.0.0:8001
          KONG_ADMIN_LISTEN_SSL: 0.0.0.0:8444
          KONG_CASSANDRA_CONTACT_POINTS: kong-database
          KONG_DATABASE: postgres
          KONG_PG_HOST: kong-database
          KONG_PROXY_ACCESS_LOG: /dev/stdout
          KONG_PROXY_ERROR_LOG: /dev/stderr
        stdin_open: true
        tty: true
        links:
        - kong-database:kong-database
        ports:
        - 8000:8000/tcp
        - 8443:8443/tcp
        - 8001:8001/tcp
        - 8444:8444/tcp
        labels:
          io.rancher.container.pull_image: always
    

    3.等待一会,kong这个容器可能要重启两次,大约一分钟就好,之后删除kong-test,这个是为了初花数据库用的。浏览器访问http://<本机IP>:8090.

    kong的使用

    nginx代理配置

    由于kong本身就是一个nginx,但是它默认监听的端口是8090(这个8090是我自己映射的,容器默认是8000,具体端口要看你自己怎么映射哦,你也可以直接映射到80,),并不是80,我们对外的时候不可能提供8090这个端口的,所以需要在物理机上再加一层nginx,把访问80端口的APi转发到8090.nginx配置如下:

    server {
    
        listen     80;
        server_name    <你的域名>;
        #ssl on;
        access_log  /var/log/nginx/kong_dashboard_access.log;
        error_log   /var/log/nginx/kong_dashboard_error.log;
    
        location / {
            proxy_connect_timeout 300s;
            proxy_send_timeout 300s;
            proxy_read_timeout 300s;
            client_max_body_size 500m;
            proxy_pass   http://localhost:8090/;
            proxy_set_header  X-Real-IP         $remote_addr; # pass on real client's IP
                proxy_set_header  X-Forwarded-For   $proxy_add_x_forwarded_for;
                proxy_set_header  X-Forwarded-Proto $scheme;
        }
    }
    
    

    API的配置

    kong要代理服务,一种是通过域名或IP的方式代理多个服务。而当前我们的服务是部署在docker容器上的,这时我们可以采用容器链接的方式。

     kong:
        image: kong:latest
        environment:
          KONG_ADMIN_ACCESS_LOG: /dev/stdout
          KONG_ADMIN_ERROR_LOG: /dev/stderr
          KONG_ADMIN_LISTEN: 0.0.0.0:8001
          KONG_ADMIN_LISTEN_SSL: 0.0.0.0:8444
          KONG_CASSANDRA_CONTACT_POINTS: kong-database
          KONG_DATABASE: postgres
          KONG_PG_HOST: kong-database
          KONG_PROXY_ACCESS_LOG: /dev/stdout
          KONG_PROXY_ERROR_LOG: /dev/stderr
        stdin_open: true
        tty: true
        links:
        - kong-database:kong-database
        external_links:
        - huijubao/staticWeb:staticweb
        - huijubao/message:message
        - huijubao/product:product
        - huijubao/task:task
        - huijubao/payment:payment
        - huijubao/app:app
        - huijubao/route:route
        - huijubao/crm:crm
        - huijubao/policy:policy
        ports:
        - 8000:8000/tcp
        - 8443:8443/tcp
        - 8001:8001/tcp
        - 8444:8444/tcp
        labels:
          io.rancher.container.pull_image: always
    

    添加了一系列的external_links容器链接。这些容器在rancher中的关系如下:

    image

    也可以选择升级容器,如下图:

    image

    api的配置如下:

    image

    参考博客: https://www.cnblogs.com/xiaoEight/p/5560514.html

    相关文章

      网友评论

      • 6ac218feb25b:您好,你们公司是怎么解决kong与用户服务交互的。你们自己单独开发了插件吗?

      本文标题:kong-api-gateway的环境搭建与使用

      本文链接:https://www.haomeiwen.com/subject/qxqwjftx.html