做者: 李佶澳 转载请保留:原文地址 发布时间:2018-09-29 15:41:50 +0800css
说明
Nginx、OpenRestry、Kong这三个项目紧密相连: Nginx是模块化设计的反向代理软件,C语言开发; OpenResty是以Nginx为核心的Web开发平台,能够解析执行Lua脚本(OpenResty与Lua的关系,相似于Jvm与Java,不过Java能够作的事情太多了,OpenResty主要用来作Web、API等); Kong是一个OpenResty应用,是一个api gateway,具备API管理和请求代理的功能。html
Nginx
Nginx是HTTP Server、反向代理服务器、邮件代理服务器、通用的TCP/UDP代理服务器。nginx features详细列出了nginx的功能特性。nginx
Nginx配置文件,指令与变量
Nginx的配置文件由单指令(simple directive)
和块指令(block directive)
组成,单指令只有一行,以“;”结尾,块指令后面是用“{ }”包裹的多行内容。git
有些块指令后的花括号中能够继续包含单指令,这样的块指令被成为配置上下文(context)
,这样的指令有:events、http、server、location等。github
context是嵌套的,最外层的context是main context
,配置文件中不在{}
的中指令都是位于main context
中。web
events和http指令位于main context,server位于http context,location位于server context:正则表达式
-
-
-
-
-
配置文件示例见: Beginner’s Guide,例如:sql
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
location ~ \.(gif|jpg|png)$ {
-
-
-
-
-
-
-
-
-
-
-
-
fastcgi_pass localhost:
9000;
-
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
-
fastcgi_param QUERY_STRING $query_string;
-
-
-
上面的例子中的proxy_pass
和factcgi_pass
分别是nginx的http proxy module和http fastcgi moudle中指令。docker
Nginx有不少的module,在Nginx Documents中能够查看每一个modules的用法。数据库
Nginx: Alphabetical index of directives中列出了Nginx的全部指令。
Nginx: Alphabetical index of variables中列出了能够在配置文件中使用的全部变量。
在查看Nginx指令用法的时候,注意指令的context:
-
-
-
Context: http, server, location,
if in location # 可使用gzip指令的地方
Nginx做为TCP/UDP负载均衡器
Nginx本来只能作7层(http)代理,在1.9.0版本中增长了4层(TCP/UDP)代理功能。
4层代理功能在Nginx的ngx_stream_core_module模块中实现,但默认没有编译,须要在编译时指定: –with-stream。
使用配置以下:
-
-
-
error_log /var/
log/nginx/error.log info;
-
-
-
-
-
-
-
-
hash $remote_addr consistent;
-
-
server backend1.example.com:12345 weight=5;
-
server 127.0.0.1:12345 max_fails=3 fail_timeout=30s;
-
server unix:/tmp/backend3;
-
-
-
-
server 192.168.0.1:53535;
-
server dns.example.com:53;
-
-
-
-
-
proxy_connect_timeout
1s;
-
-
-
-
-
-
listen
127.0.0.1:53 udp reuseport;
-
-
-
-
-
-
-
proxy_pass unix:/tmp/stream.socket;
-
-
Nginx模块
理解Nginx Module很重要,由于后面的OpenResty就是标准的Nginx加上不少Nginx Module。
Nginx是用C语言开发软件,采用模块化设计,能够经过开发模块扩展Nginx的功能。
Nginx Development guide中介绍了Nginx模块开发的方法Nginx Module develop。
插件能够编译成.so之后动态加载,也能够直接编译到nginx中,编译是经过--add-module
指定要集成的模块。
例如lua-nginx-module:
-
./configure --prefix=
/opt/nginx \
-
--
with-ld-opt="-Wl,-rpath,/path/to/luajit-or-lua/lib" \
-
--add-
module=/path/to/ngx_devel_kit \
-
--add-
module=/path/to/lua-nginx-module
OpenResty
OpenResty是一个集成了Nginx、LuaJIT和其它不少moudels的平台,用来托管完整的web应用——包含业务逻辑,而不单纯是静态文件服务器: OpenResty® aims to run your server-side web app completely in the Nginx server, leveraging Nginx’s event model to do non-blocking I/O not only with the HTTP clients, but also with remote backends like MySQL, PostgreSQL, Memcached, and Redis.
OpenResty Components中列出了OpenResty集成的组件,数量很多,这里就不列出来了。
先经过OpenResty Getting Started感觉一下OpenResty是咋回事。
OpenResty安装
Centos安装方式:
-
sudo yum
install yum-utils
-
-
sudo yum
install openresty
-
sudo yum
install openresty-resty
经过源代码编译:
-
-
tar -xvf openresty
-1.13.6.2.tar.gz
-
-
./configure --
with-pcre-jit --with-http_ssl_module --with-http_realip_module --with-http_stub_status_module --with-http_v2_module
-
-
-
export PATH=/usr/local/openresty/bin:$PATH
为了后面顺利的使用kong,configure时要指定kong依赖的模块。
都包含如下文件:
-
$ tree -L 2 /usr/local/openresty/
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
注意openresty命令就是nginx命令,OpenResty能够理解为一个集成了不少模块的定制版nginx:
-
-
nginx version: openresty/1.13.6.2
-
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
-
-
-
-
-v :
show version and exit
-
-V :
show version and configure options then exit
-
-t :
test configuration and exit
-
-T :
test configuration, dump it and exit
-
-q : suppress non-
error messages during configuration testing
-
-s signal : send signal
to a master process: stop, quit, reopen, reload
-
-p prefix :
set prefix path (default: /usr/local/openresty/nginx/)
-
-c filename :
set configuration file (default: conf/nginx.conf)
-
-g directives :
set global directives out of configuration file
能够在openresty的配置文件中写入lua代码:
-
-
-
error_log logs/
error.log;
-
-
-
-
-
-
-
-
-
-
ngx.say(
"<p>hello, world</p>")
-
-
-
-
启动:
openresty -p `pwd` -c nginx.conf
而后访问”127.0.0.1:8080”,能够看到输出:
-
-
Kong
Kong是一个OpenResty应用,用来管理api。
Kong编译安装
Kong编译安装时须要先安装有OpenResty。
还须要lua包管理工具luarocks:
-
-
./configure --lua-suffix=jit --
with-lua=/usr/local/openresty/luajit --with-lua-include=/usr/local/openresty/luajit/include/luajit-2.1
-
下载kong代码编译:
-
-
-
编译完成以后会在当前目录生成一个bin目录:
-
-
查看bin/kong的内容,能够发现这是一个用resty执行的脚本文件:
-
-
-
-
require "luarocks.loader"
-
-
package.path =
"./?.lua;./?/init.lua;" .. package.path
-
-
require("kong.cmd.init")(arg)
启动Kong
先准备数据库,kong支持PostgreSQL和Cassandra 3.x.x,这里使用PostgreSQL(须要版本在9.4及以上):
注意,若是使用其它版本的PostgreSQL,将下面的9.6换成对应版本号。
-
yum
install https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm
-
-
yum
install postgresql96-server
-
export PATH=$PATH:/usr/pgsql-9.6/bin/
-
postgresql96-setup initdb
-
systemctl
start postgresql-9.6
-
-
-
CREATE USER kong; CREATE DATABASE kong OWNER kong;
-
alter user kong with encrypted password '123456';
-
在/var/lib/pgsql/9.6/data/pg_hba.conf的开始处
添加规则下面规则:
host kong kong 127.0.0.1/32 md5
而后重启PostgreSQL
,确保下面的命令能登录PostgreSQL:
-
-
-
-
-
-
PostgreSQL的部署使用和经过密码登录方式的设置参考:PostgresSQL数据库的基本使用、PostgreSQL的用户究竟是这么回事?新用户怎样才能用密码登录?。
准备kong的配置文件,
-
cp kong.conf.default kong.conf
-
# 在
kong.conf中填入数据地址、用户、密码等
建立kong的数据库:
./bin/kong migrations up -c ./kong.conf
启动kong:
./bin/kong start -c ./kong.conf
kong默认的代理地址是:
proxy_listen = 0.0.0.0:8000, 0.0.0.0:8443
默认的管理地址是:
admin_listen = 127.0.0.1:8001, 127.0.0.1:8444 ssl
返回的是json字符串:
-
$ curl -i
http://localhost:8001/
-
-
Date: Sat, 29 Sep 2018 08:56:51 GMT
-
Content-
Type: application/json; charset=utf-8
-
-
Access-Control-Allow-
Origin: *
-
-
-
-
{
"plugins":{"enabled_in_cluster":[],"availab...
-
部署Kong Dashboard
PGBI/kong-dashboard是一个第三方的Dashboard。
-
-
-
Kong的使用
中止:
kong stop
从新加载:
kong reload
注册API:添加服务、配置路由
添加服务Configuring a Service。
添加一个名为example-service
的服务,服务地址是http://mockbin.org
:
-
-
-
--
data 'name=example-service' \
-
--
data 'url=http://mockbin.org'
执行后返回:
-
-
"connect_timeout": 60000,
-
"created_at": 1538213979,
-
-
"id": "ebed2707-e2fb-4694-9e8e-fb66fe9dd7c8",
-
"name": "example-service",
-
-
-
-
-
-
"updated_at": 1538213979,
-
-
为example-service
添加一个route
,知足route的请求将被转发给example-service,执行:
-
-
-
--
data 'hosts[]=example.com'
这里配置的route条件是:host为example.com。
返回:
-
-
"created_at": 1538185340,
-
-
-
-
"id": "4738ae2c-b64a-4fe5-9e2a-5855e769a9e8",
-
-
-
-
-
-
-
-
-
-
"id": "ebed2707-e2fb-4694-9e8e-fb66fe9dd7c8"
-
-
-
-
这时候访问kong的proxy地址
时,若是host为example.com
,请求被转发到http://mockbin.org
:
-
-
-
--header
'Host: example.com'
能够在/etc/hostsname中将example.com地址配置为kong所在的机器的地址:
10.10.192.35 example.com
而后就能够经过example.com:8000
打开http://mockbin.org。
插件启用方法
插件是用来扩展API的,例如为API添加认证、设置ACL、限制速率等、集成oauth、ldap等。
Kong Plugins中列出了已有的全部插件。
这里演示key-auth插件的用法,Kong Enabling Plugins,。
-
-
-
返回:
-
-
-
-
"hide_credentials": false,
-
-
-
-
-
-
-
"created_at": 1538218948000,
-
-
"id": "f25f3952-d0d4-4923-baac-860554fc2fc1",
-
-
"service_id": "ebed2707-e2fb-4694-9e8e-fb66fe9dd7c8"
-
这时候直接访问example.com,会返回401:
-
-
-
> --header
'Host: example.com'
-
HTTP/
1.1 401 Unauthorized
-
Date: Sat, 29 Sep 2018 11:03:55 GMT
-
Content-Type: application/json; charset=utf
-8
-
-
WWW-Authenticate: Key realm=
"kong"
-
-
在kong中建立一个名为Jason的用户:
-
-
-
返回:
-
-
"created_at": 1538219225,
-
-
"id": "f2450962-e4bb-477f-8df6-85984eb94e09",
-
-
将Jason的密码设置为123456:
-
-
-
返回:
-
-
"consumer_id": "f2450962-e4bb-477f-8df6-85984eb94e09",
-
"created_at": 1538219311000,
-
"id": "0332d36f-61b9-425a-b563-510c11a85e85",
-
-
这时候能够用Jason的key访问API:
-
-
-
--header
"Host: example.com" \
-
--header
"apikey: 123456"
返回的是mockbin.org的首页。
key-auth插件的详细用法参考Kong Plugin: key-auth。插件的做用范围能够是全局(global)、服务(service)、路由(router)。
启用key-auth后,经过认证的请求被转发给上游服务时,key-auth会增设下面的字段:
-
X-Consumer-ID, the ID
of the Consumer on Kong
-
X-Consumer-
Custom-ID, the custom_id of the Consumer (if set)
-
X-Consumer-Username, the username
of the Consumer (if set)
-
X-Credential-Username, the username
of the Credential (only if the consumer is not the
-
X-Anonymous-Consumer, will be
set to true when authentication failed, and the
Kong的插件
Kong Plugins中列出了已有的全部插件,有些插件只能在企业版使用,有些插件是社区成员开发的,大部分是Kong公司开发,并集成到社区版中。
下面是社区版集成的、Kong公司维护的插件(2018-09-30 14:33:03):
认证插件:
-
-
-
-
-
-
安全插件:
-
-
-
流控插件:
-
-
-
-
-
微服务插件:
-
-
-
-
分析和监控插件:
-
-
-
内容修改插件(Transformations):
-
-
-
日志插件:
-
-
-
-
-
-
-
Kong与Kubernetes的集成
通过前面的学习,对Api网关是什么,以及Kong可以作什么已经有了足够的了解。如今Kubernetes一统计算资源与应用发布编排的趋势已经造成,咱们更关心Kong可否和Kubernetes结合。
Kong是一个Api网关,也是一个特性更丰富的反向代理,既然它有代理流量的功能,那么能不能直接成为Kubernetes的流量入口?使Kubernetes内部的服务都经过Kong发布。
Kong实现了一个Kubernetes Ingress Controller来作这件事。在Kubernetes中部署kong的方法见Kong CE or EE on Kubernetes。
这部份内容比较多,单独开篇了: Kubernetes与API网关Kong的集成。
遇到的问题
ERROR: module ‘socket’ not found:No LuaRocks module found for socket
启动的时候:
-
-
-
ERROR: ./kong/globalpatches.lua:
63: module 'socket' not found:No LuaRocks module found for socket
-
这是由于编译kong以后,从新编译了luarocks,而且将luarocks安装在了其它位置。从新编译kong以后解决。
ERROR: function to_regclass(unknown) does not exist (8)
建立数据库的时候:
-
# kong migrations up -c ./kong.conf
-
-
[postgres
error] could not retrieve current migrations: [postgres error] ERROR: function to_regclass(unknown) does not exist (8)
-
这是由于PostgreSQL的版本过低了,to_regclass
在PostgreSQL 9.4及以上的版本中才存在。
-
yum
install https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm
-
-
yum
install postgresql96-server
nginx: [emerg] unknown directive "real_ip_header" in /usr/local/kong/nginx-kong.conf:73
这是由于编译的openresty的时候,没有指定--with-http_realip_module
,从新编译安装:
-
-
-
make
install //默认安装在/usr/local/bin/openresty
-
export PATH=/usr/local/openresty/bin:$PATH
参考
- nginx website
- OpenResty website
- Kong website
- Kong Compile Source
- nginx features
- nginx documentation
- Nginx Example Configuration & Directives
- Nginx: Alphabetical index of directives
- Nginx: Alphabetical index of variables
- Beginner’s Guide
- Nginx: ngx_http_fastcgi_module
- Nginx: ngx_http_proxy_module
- Nginx Documents
- Nginx: Module ngx_stream_core_module
- OpenResty website
- OpenResty Components
- OpenResty Getting Started
- Nginx Development guide
- Nginx Module develop
- PostgreSQL的用户究竟是这么回事?新用户怎样才能用密码登录?
- PostgresSQL数据库的基本使用
- Kong Enabling Plugins
- Kong Plugin: key-auth
- Kong Plugins
- Kong CE or EE on Kubernetes
- Kong/kubernetes-ingress-controller
- PGBI/kong-dashboard
限时活动,每邀请一人即返回25元!

相关文章
《深刻剖析Kubernetes》专栏的阅读笔记(持续更新)
Kubernetes网络方案Flannel的学习笔记
《左耳听风》陈皓专栏的阅读笔记(持续更新)
Kubernetes1.12从零开始(五):本身动手部署Kubernetes(待续)
Kubernetes1.12从零开始(四):必须先讲一下基本概念
Kubernetes1.12从零开始(三):用minikube与kubeadm部署
Kubernetes1.12从零开始(二):部署环境准备
Kubernetes1.12从零开始(一):官方文档汇总
API网关Kong与Kubernetes的集成方法
PostgreSQL的用户究竟是这么回事?新用户怎样才能用密码登录?