php实战kong做微服务架构一(kong简介与安装)
序言
在一些分布式系统中,我们会在内部服务之前加一道屏障来保护我们的内部系统,并且可以统一管理内部服务。帮助开发者或用户更便捷的使用服务,不用考虑安全、流量等问题。
关于安全、限流、黑白名单等问题,我们统一在网关层对进行处理,内部服务只需关注业务实现。
在实际应用中,我们希望请求的API,网关对它们有统一的管理维护等功能,这样可以更好的管理项目。以后有新的服务只需在网关统一注册,就可以支持既有的安全、限流、黑白名单等功能。岂不美哉~~~
在当下已经有了众多优秀的项目帮助我们完成这一需求,例如kong,本文关于如何安装kong做了详细说明。
作业环境
Linux version 3.10.0-957.21.3.el7.x86_64
kong介绍
API网关项目,基于NGINX和Apache Cassandra或PostgreSQL构建的,提供RESTful接口,用于管理和配置服务,路由,插件和使用者。
允许开发者管理服务和API的整个生命周期,以及所有服务中代理组织的信息。
最重要的是,它使用户能够简化跨混合云和多云部署的API和微服务的管理。
使用kong的优势
分散应用程序或服务并过渡到微服务
打造完善的API开发人员生态系统
主动识别与API相关的异常和威胁
保护和管理API 或服务,并改善整个组织的API可见性
主要三组件
Kong Server:基于nginx的服务器,用来接收api请求。
Apache Cassandra或PostgreSQL:用来存储操作数据。
Kong Dashboard:UI管理工具。
安装
kong安装
yum install -y https://kong.bintray.com/kong-community-edition-rpm/centos/7/kong-community-edition-0.13.1.el7.noarch.rpm
PostgreSQL安装
下载安装
yum install https://download.postgresql.org/pub/repos/yum/9.5/redhat/rhel-7-x86_64/pgdg-centos95-9.5-3.noarch.rpm
yum install postgresql95 postgresql95-server
配置
PostgreSQL相关设置
初始化PostgreSQL数据库并启用自动启动
/usr/pgsql-9.5/bin/postgresql95-setup initdb
systemctl enable postgresql-9.5
systemctl start postgresql-9.5
切换到PostgreSQL用户并启动PostgreSQL
$ sudo -i -u postgres
$ psql
创建一个账号密码为kong的用户
$ psql>create user kong with password 'kong';
创建一个kong 数据库
$ psql>create database kong owner kong;
在IPv4下,本地连接替换ident为md5或者trust,允许kong用户在本地与数据库通信
修改/var/lib/pgsql/9.5/data/pg_hba.conf
把这个配置文件中的认证 METHOD的ident修改为trust或者md5
kong配置
配置文件位置: /etc/kong/kong.conf.default
cp /etc/kong/kong.conf.default /etc/kong/kong.conf
vi /etc/kong/kong.conf 修改以下内容
pg_host = 127.0.0.1 #数据库主机地址
pg_port = 5432 #数据库端口
pg_user = kong #数据库登录用户名
pg_password = kong #数据库登录密码
pg_database = kong #数据库名
配置文件位置: /usr/local/share/lua/5.1/kong/templates/kong_defaults.lua
修改以下内容
pg_password = kong #修改为自己的数据库密码
kong测试
初始化数据库
服务开启
curl测试
kong监听的端口
-
代理端口:Kong接收传入流量的地方。
8000: 监听来自客户端的http请求,将请求转发给提供对应服务的服务器。
8443: 监听来自客户端的https请求,将请求转发给提供对应服务的服务器。 -
管理员API:Kong公开其管理API的端口。因此,在生产中,应将此端口进行防火墙保护,以防止未经授权的访问。
8001:提供Kong的Admin API,可以使用该API通过HTTP操作Kong。
8443:提供相同的Kong Admin API,但使用HTTPS。
kong图形化管理
注意:如果没有npm,还需先安装:yum install npm
安装
-
安装dashboard
-
启动dashboard
kong-dashboard start --kong-url http://localhost:8001
-
访问
安全访问
- 用自定义端口启动 Kong Dashboard
kong-dashboard start \ --kong-url http://localhost:8001 \ --port [port] - 使用权限认证启动 Kong Dashboard
kong-dashboard start \ --kong-url http://kong:8001 \ --basic-auth user1=password1 user2=password2
安装遇到的问题以及解决方法
- 用户 “kong” Ident 认证失败(作者忘记修改身份验证了…)
解决方法:Error: [postgres error] could not retrieve current migrations: [postgres error] FATAL: Ident authentication failed for user "kong" Run with --v (verbose) or --vv (debug) for more details
修改/var/lib/pgsql/9.5/data/pg_hba.conf 把这个配置文件中的认证 METHOD的ident修改为trust或者md5
- npm下载太慢
解决方法:切换镜像源 没有使用cnpm.....总是不习惯 设置: npm config set registry https://registry.npm.taobao.org 然后再使用npm下载,眨眼间就下完了~~~
后期更新
上面版本有点低,特意在后期使用了新版本,更新本篇,以下时新版本安装过程。
kong 2.3.2
psql 13
kong安装
yum install https://bintray.com/kong/kong-gateway-rpm/download_file?file_path=centos%2F7%2Fkong-enterprise-edition-2.3.2.0.el7.noarch.rpm
PostgreSQL安装
下载安装
yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
yum install -y postgresql13-server postgresql13-contrib
配置
PostgreSQL相关设置
初始化PostgreSQL数据库并启用自动启动
/usr/pgsql-13/bin/postgresql-13-setup initdb
systemctl enable postgresql-13
systemctl start postgresql-13
状态查询
systemctl status postgresql-13
切换到PostgreSQL用户并启动PostgreSQL
$ sudo -i -u postgres
$ psql
创建一个账号密码为kong的用户
$ psql>create user kong with password 'kong';
创建一个kong 数据库
$ psql>create database kong owner kong;
kong配置
配置文件位置: /etc/kong/kong.conf.default
cp /etc/kong/kong.conf.default /etc/kong/kong.conf
vi /etc/kong/kong.conf 修改以下内容
database = postgres #存储方式
pg_host = 127.0.0.1 #数据库主机地址
pg_port = 5432 #数据库端口
pg_user = kong #数据库登录用户名
pg_password = kong #数据库登录密码
pg_database = kong #数据库名
配置文件位置: /usr/local/share/lua/5.1/kong/templates/kong_defaults.lua
修改以下内容
pg_password = kong #修改为自己的数据库密码
kong测试
初始化数据库
[root@monkey ~]# kong migrations bootstrap
Using Konnect Enterprise without a license
Bootstrapping database...
migrating core on database 'kong'...
core migrated up to: 000_base (executed)
core migrated up to: 003_100_to_110 (executed)
core migrated up to: 004_110_to_120 (executed)
core migrated up to: 005_120_to_130 (executed)
core migrated up to: 006_130_to_140 (executed)
core migrated up to: 007_140_to_150 (executed)
core migrated up to: 008_150_to_200 (executed)
core migrated up to: 009_200_to_210 (executed)
core migrated up to: 010_210_to_211 (executed)
core migrated up to: 011_212_to_213 (executed)
core migrated up to: 012_213_to_220 (executed)
core migrated up to: 013_220_to_230 (executed)
...此处省略n行...
enterprise.key-auth-enc migrated up to: 001_1500_to_2100 (executed)
79 migrations processed
79 executed
Database is up-to-date
服务开启
[root@monkey ~]# kong start -c /etc/kong/kong.conf
Using Konnect Enterprise without a license
2021/03/01 23:01:52 [warn] 12051#0: *2 [lua] plugins.lua:231: load_plugin(): plugin 'route-by-header' has been deprecated, context: ngx.timer
Kong started
这里提示route-by-header插件被弃用。此种默认提示,用官方的话讲,不想因为没提示造成技术债务…
curl测试
关于新版本管理UI工具
kong0.14版本开始使用konga