JAVA和Nginx 教程大全

网站首页 > 精选教程 正文

Kong系列(一)——Kong安装 空安装视频

wys521 2024-10-23 15:23:48 精选教程 24 ℃ 0 评论

Kong是一个基于Nginx的开源网关系统,参考官网来部署一套网关系统来使用。

机器准备


节点A:192.168.0.10

节点B:192.168.0.11

节点A,部署一个Kong Node和Postgresql

节点B, 部署一个Kong Node,数据库指向节点A的postgresql


安装

1.安装数据库postgresql

  • 使用docker创建网络
  • docker network create kong-net

  • 将postgresql网络指定为新建的网络(docker原来使用–link,新版本不建议使用,使用–network将两个容器指定到同一个网络来解决问题
  • docker run -d --name kong-database \
     --network=kong-net \
     -p 5432:5432 \
     -e "POSTGRES_USER=kong" \
     -e "POSTGRES_DB=kong" \
     postgres:9.6
  • 数据库迁移,将kong的数据库创建出来(指定到同一个网络)
  • docker run --rm \
     --network=kong-net \
     -e "KONG_DATABASE=postgres" \
     -e "KONG_PG_HOST=kong-database" \
     -e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" \
     kong:latest kong migrations up

    2.postgresql主备热切换

    TODO
    为了高可用性,使用postgresql的流复制以及双机热备

    3.kong集群

  • 节点A部署启动
  • docker run -d --name kong \
     --network=kong-net \
     -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
  • 节点B部署启动
  • docker run -d --name kong \
     -e "KONG_DATABASE=postgres" \
     -e "KONG_PG_HOST=192.168.0.10" \
     -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 
  • 添加测试
  • 同步时间默认是5s,可以通过官网集群资料进行修改

    节点A添加
    
    curl -i -X POST --url http://localhost:8001/apis/ --data 'name=example-api' --data 'hosts=example.com' --data 'upstream_url=http://mockbin.org'
    
    节点B查询
    
    curl http://localhost:8001/apis/
    
    >>>>>>
    {
    	"total": 1,
    	"data": [{
    		"created_at": 1527732097194,
    		"strip_uri": true,
    		"id": "41cbfb3b-eaa1-4a6c-ac19-c069f6daeb83",
    		"hosts": ["example.com"],
    		"name": "example-api",
    		"http_if_terminated": false,
    		"https_only": false,
    		"retries": 5,
    		"preserve_host": false,
    		"upstream_connect_timeout": 60000,
    		"upstream_read_timeout": 60000,
    		"upstream_send_timeout": 60000,
    		"upstream_url": "http:\/\/mockbin.org"
    	}]
    }
    
    节点B添加
    
    curl -i -X POST --url http://localhost:8001/apis/ --data 'name=example-api2' --data 'hosts=example.com' --data 'upstream_url=http://mockbin.org'
    
    节点A查询
    curl http://localhost:8001/apis/
    
    >>>>>>
    {
    	"total": 2,
    	"data": [{
    		"created_at": 1527732097194,
    		"strip_uri": true,
    		"id": "41cbfb3b-eaa1-4a6c-ac19-c069f6daeb83",
    		"hosts": ["example.com"],
    		"name": "example-api",
    		"http_if_terminated": false,
    		"https_only": false,
    		"retries": 5,
    		"preserve_host": false,
    		"upstream_connect_timeout": 60000,
    		"upstream_read_timeout": 60000,
    		"upstream_send_timeout": 60000,
    		"upstream_url": "http:\/\/mockbin.org"
    	}, {
    		"created_at": 1527732578045,
    		"strip_uri": true,
    		"id": "29f12209-f45b-45dc-b6e1-f64e855d7387",
    		"hosts": ["example.com"],
    		"name": "example-api2",
    		"http_if_terminated": false,
    		"https_only": false,
    		"retries": 5,
    		"preserve_host": false,
    		"upstream_connect_timeout": 60000,
    		"upstream_read_timeout": 60000,
    		"upstream_send_timeout": 60000,
    		"upstream_url": "http:\/\/mockbin.org"
    	}]
    }

    配置nginx负载Kong集群

    对外提供9000端口,通过nginx的负载均衡,根据自身的需求,配置不同的算法,反向代理到kong的集群中

    upstream kong {
     # 负载均衡算法,根据最少连接数
     least_conn;
     server 192.168.0.10:8000;
     server 192.168.0.11:8000;
    }
    
    server {
     # 这些端口要修改为实际情况
     listen 9100;
     #include snippets/ssl-api.example.com.conf;
     #include snippets/ssl-params.conf;
     # https 因为安全原因,需要禁用 gzip
     # 但是在一些场景下,不需要禁用
     # gzip off;
     gzip on;
     # 设置允许压缩的页面最小字节数
     gzip_min_length 1000;
     gzip_buffers 4 16k;
     gzip_http_version 1.1;
     # 1~9,默认为1,数值越大,压缩率越高,CPU占用越多,时间越久
     gzip_comp_level 3;
     gzip_vary on;
     # 禁用对 IE 6 使用 gzip 压缩
     gzip_disable "MSIE [1-6]\.";
     gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/javascript "application/javascript; charset=utf-8" application/xml application/xml+rss application/json "application/json; charset=utf-8" fo
    nt/ttf font/otf image/svg+xml;
    
     # 设置最大允许的POST数据量,如果提交的文件超过这个值,会出现413错误
     client_max_body_size 20m;
     keepalive_timeout 15;
    
     # 不显示 nginx 的版本号
     server_tokens off;
    
     ## Individual nginx logs
     access_log /var/log/nginx/access.log;
     error_log /var/log/nginx/error.log;
    
     # 这里不能用 ^~ 因为后面跟的是字符串不是正则表达式
     # 只匹配符合规则的,其他都返回 404
     location / {
     # 这条命令会将 headers 中的 server 设置为 nginx 代理的后端网站所使用的
     proxy_set_header Host $http_host;
     proxy_redirect off;
     proxy_set_header X-Real-IP $remote_addr;
     proxy_set_header X-Scheme $scheme;
     proxy_pass http://kong;
     expires -1;
     }
    
     # 主页返回 404
     location = / {
     return 404;
     }
    }

    使用时,将所有额请求都转发到http://192.168.0.10:9100上,然后后续Kong会根据配置进行相应的处理以及相关的插件过滤

    Tags:

    本文暂时没有评论,来添加一个吧(●'◡'●)

    欢迎 发表评论:

    最近发表
    标签列表