【微服務】部署Api網關Kong

Kong 是在客戶端和(微)服務間轉發 API 通訊的 API 網關,經過插件擴展功能。持久化支持Postgresql和Cassandra。鑑於作測試,使用搭建簡便的Postgresql做爲DB存儲。nginx

IP分配

192.168.0.181:CentOS7 部署Kong服務; 192.168.0.184:CentOS7 部署postgresql 10;sql

部署postgresql

首先在192.168.0.184上部署postgresql,命令以下:shell

yum install https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-centos10-10-1.noarch.rpm
 yum install postgresql10
 yum install postgresql10-server


 /usr/pgsql-10/bin/postgresql-10-setup initdb
 systemctl enable postgresql-10
 systemctl start postgresql-10

安裝完畢後進入db建立用戶;數據庫

#首先進入postgres用戶
su postgres

#進入sql命令行
/usr/pgsql-10/bin/psql

#建立用戶和庫
CREATE USER kong; CREATE DATABASE kong OWNER kong;

#修改用戶kong的密碼
ALTER USER kong WITH PASSWORD 'kong';

安裝kong

接下來在192.168.0.181上安裝kong,首先下載對應CentOS7的rpm文件:kong-community-edition-0.11.2.el7.noarch.rpmjson

yum install epel-release
yum install kong-community-edition-0.11.2.*.noarch.rpm --nogpgcheck

而後修改配置文件,默認在/etc/kong/kong.conf.default後端

database = postgres
pg_host = 192.168.0.184
pg_port = 5432
pg_user = kong
pg_password = kong
pg_database = kong

而後重命名爲kong.confcentos

再初始化dbapi

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

啓動:bash

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

kong開放4個端口,分別用途以下:cors

  • 8000: 監聽HTTP請求,向後端服務進行轉發;
  • 8443: 監聽HTTPS請求,同8000;
  • 8001: 用戶管理api訪問端口;
  • 8444: 監聽HTTPS用於管理api端口;

中止和reload命令:

kong stop
kong reload

Kong管理接口

狀態相關接口

查看kong信息
GET http://192.168.0.181:8001/
  • available_on_server: 查看開啓的插件列表
  • enabled_in_cluster:查看集羣中開啓插件的信息
{
    "version": "0.11.2", 
    "plugins": {
        "enabled_in_cluster": [ ], 
        "available_on_server": {
            "response-transformer": true, 
            "correlation-id": true, 
            "statsd": true, 
            "jwt": true, 
            "cors": true, 
            "basic-auth": true, 
            "key-auth": true, 
            "ldap-auth": true, 
            "oauth2": true, 
            "http-log": true, 
            "ip-restriction": true, 
            "hmac-auth": true, 
            "request-size-limiting": true, 
            "datadog": true, 
            "tcp-log": true, 
            "request-transformer": true, 
            "file-log": true, 
            "acl": true, 
            "bot-detection": true, 
            "loggly": true, 
            "galileo": true, 
            "syslog": true, 
            "udp-log": true, 
            "response-ratelimiting": true, 
            "aws-lambda": true, 
            "runscope": true, 
            "rate-limiting": true, 
            "request-termination": true
        }
    }, 
    "tagline": "Welcome to kong", 
    "configuration": {
        "error_default_type": "text/plain", 
        "admin_listen": "0.0.0.0:8001", 
        "lua_ssl_verify_depth": 1, 
        "trusted_ips": { }, 
        "prefix": "/usr/local/kong", 
        "nginx_conf": "/usr/local/kong/nginx.conf", 
        "cassandra_username": "kong", 
        "proxy_ip": "0.0.0.0", 
        "ssl_cert_key": "/usr/local/kong/ssl/kong-default.key", 
        "admin_ssl_cert_key": "/usr/local/kong/ssl/admin-kong-default.key", 
        "dns_resolver": { }, 
        "pg_user": "kong", 
        "mem_cache_size": "128m", 
        "server_tokens": true, 
        "proxy_ssl_port": 8443, 
        "admin_ip": "0.0.0.0", 
        "custom_plugins": { }, 
        "pg_host": "192.168.0.184", 
        "nginx_acc_logs": "/usr/local/kong/logs/access.log", 
        "proxy_listen": "0.0.0.0:8000", 
        "client_ssl_cert_default": "/usr/local/kong/ssl/kong-default.crt", 
        "cassandra_ssl": false, 
        "dns_no_sync": false, 
        "db_update_propagation": 0, 
        "nginx_err_logs": "/usr/local/kong/logs/error.log", 
        "cassandra_port": 9042, 
        "dns_order": [
            "LAST", 
            "SRV", 
            "A", 
            "CNAME"
        ], 
        "admin_ssl_ip": "0.0.0.0", 
        "dns_stale_ttl": 4, 
        "nginx_optimizations": true, 
        "proxy_ssl_ip": "0.0.0.0", 
        "database": "postgres", 
        "ssl": true, 
        "pg_database": "kong", 
        "nginx_worker_processes": "auto", 
        "admin_ssl_port": 8444, 
        "lua_package_cpath": "", 
        "admin_port": 8001, 
        "nginx_pid": "/usr/local/kong/pids/nginx.pid", 
        "upstream_keepalive": 60, 
        "proxy_access_log": "logs/access.log", 
        "ssl_ciphers": "ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256", 
        "ssl_cert_csr_default": "/usr/local/kong/ssl/kong-default.csr", 
        "admin_access_log": "logs/admin_access.log", 
        "dns_error_ttl": 1, 
        "client_ssl_cert_csr_default": "/usr/local/kong/ssl/kong-default.csr", 
        "pg_password": "******", 
        "client_ssl": false, 
        "cassandra_contact_points": [
            "127.0.0.1"
        ], 
        "admin_ssl_cert_csr_default": "/usr/local/kong/ssl/admin-kong-default.csr", 
        "lua_socket_pool_size": 30, 
        "real_ip_header": "X-Real-IP", 
        "ssl_cipher_suite": "modern", 
        "cassandra_consistency": "ONE", 
        "http2": false, 
        "proxy_listen_ssl": "0.0.0.0:8443", 
        "client_max_body_size": "0", 
        "admin_error_log": "logs/error.log", 
        "pg_ssl_verify": false, 
        "dns_not_found_ttl": 30, 
        "pg_ssl": false, 
        "proxy_error_log": "logs/error.log", 
        "proxy_port": 8000, 
        "cassandra_repl_strategy": "SimpleStrategy", 
        "latency_tokens": true, 
        "admin_listen_ssl": "0.0.0.0:8444", 
        "admin_ssl_cert": "/usr/local/kong/ssl/admin-kong-default.crt", 
        "admin_http2": false, 
        "nginx_kong_conf": "/usr/local/kong/nginx-kong.conf", 
        "cassandra_schema_consensus_timeout": 10000, 
        "dns_hostsfile": "/etc/hosts", 
        "log_level": "notice", 
        "cassandra_timeout": 5000, 
        "ssl_cert": "/usr/local/kong/ssl/kong-default.crt", 
        "admin_ssl": true, 
        "admin_ssl_cert_key_default": "/usr/local/kong/ssl/admin-kong-default.key", 
        "cassandra_ssl_verify": false, 
        "db_cache_ttl": 3600, 
        "cassandra_lb_policy": "RoundRobin", 
        "real_ip_recursive": "off", 
        "cassandra_repl_factor": 1, 
        "client_ssl_cert_key_default": "/usr/local/kong/ssl/kong-default.key", 
        "nginx_daemon": "on", 
        "anonymous_reports": true, 
        "kong_env": "/usr/local/kong/.kong_env", 
        "cassandra_data_centers": [
            "dc1:2", 
            "dc2:3"
        ], 
        "pg_port": 5432, 
        "plugins": {
            "response-transformer": true, 
            "correlation-id": true, 
            "statsd": true, 
            "jwt": true, 
            "cors": true, 
            "basic-auth": true, 
            "key-auth": true, 
            "ldap-auth": true, 
            "request-termination": true, 
            "http-log": true, 
            "rate-limiting": true, 
            "hmac-auth": true, 
            "runscope": true, 
            "datadog": true, 
            "tcp-log": true, 
            "aws-lambda": true, 
            "response-ratelimiting": true, 
            "bot-detection": true, 
            "request-size-limiting": true, 
            "syslog": true, 
            "galileo": true, 
            "loggly": true, 
            "udp-log": true, 
            "file-log": true, 
            "request-transformer": true, 
            "acl": true, 
            "ip-restriction": true, 
            "oauth2": true
        }, 
        "client_body_buffer_size": "8k", 
        "nginx_admin_acc_logs": "/usr/local/kong/logs/admin_access.log", 
        "admin_ssl_cert_default": "/usr/local/kong/ssl/admin-kong-default.crt", 
        "db_update_frequency": 5, 
        "cassandra_keyspace": "kong", 
        "ssl_cert_default": "/usr/local/kong/ssl/kong-default.crt", 
        "ssl_cert_key_default": "/usr/local/kong/ssl/kong-default.key", 
        "lua_package_path": "./?.lua;./?/init.lua;"
    }, 
    "lua_version": "LuaJIT 2.1.0-beta2", 
    "prng_seeds": {
        "pid: 15093": 961939718379
    }, 
    "timers": {
        "pending": 5, 
        "running": 0
    }, 
    "hostname": "tree81"
}
獲取kong狀態
GET http://192.168.0.181:8001/status
{
    "database": {
        "reachable": true #數據庫的鏈接狀態
    },
    "server": {
        "connections_writing": 1, #正在響應的請求數
        "total_requests": 8,  #總請求數
        "connections_handled": 7, #處理鏈接總數
        "connections_accepted": 7, #客戶端鏈接總數
        "connections_reading": 0, #正在讀取請求頭的鏈接數
        "connections_active": 1, #活動鏈接數,含等待
        "connections_waiting": 0 #正在等待請求的鏈接數
    }
}
相關文章
相關標籤/搜索