声明:这是我在大学毕业后进入第一家互联网公司学习的内容
Kong是全球最受欢迎的开源API网关。专为多云和混合而建,针对微服务和分布式架构进行了优化。
功能
优点
个人理解:我理解的kong就是一个加强版的Nginx,有着比负载均衡器更为强大的功能(使用服务和路由对象公开服务、设置速率限制和代理缓存、通过密钥验证来保护服务、负载均衡流量)及可视化界面的配置
kong-admin
kong-proxy
konga-ui
一台服务器装PGSQL数据库(非必须)
一个K8S集群
集群均可以连外网
helm包如下
链接: https://pan.baidu.com/s/12IcxKic_fpVFfQzohiVLkw 提取码: iqcz 复制这段内容后打开百度网盘手机App,操作更方便哦
选择一台服务器装PGSQL数据库
建议使用Docker安装postgres。
# 创建docker网络
docker network create kong-net
# 创建数据库
docker run -d --restart always --name kong-database \
--network=kong-net \
-v /data/pgsql:/var/lib/postgresql/data \
-p 5432:5432 \
-e "POSTGRES_USER=kong" \
-e "POSTGRES_DB=kong" \
-e "POSTGRES_PASSWORD=kong" \
postgres:9.6
注意修改参数: POSTGRES_USER,POSTGRES_PASSWORD。
# 初始化Kong连接数据库的默认结构
docker run --rm \
--network=kong-net \
-e "KONG_DATABASE=postgres" \
-e "KONG_PG_HOST=kong-database" \
-e "KONG_PG_USER=kong" \
-e "KONG_PG_PASSWORD=kong" \
-e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" \
kong:latest kong migrations bootstrap
注意修改参数:KONG_PG_HOST,KONG_PG_PASSWORD
修改kong项目 helm部署文件夹里的values.yaml文件
采用k8s外的postgres数据库,因此关闭heml脚本中的自动创建数据库
env:
database: "postgres"
PG_HOST: "172.31.21.32"
PG_PASSWORD: "kong"
CASSANDRA_CONTACT_POINTS: "kong-database"
runMigrations: false
admin:
# 控制k8s 是否生成Service
enabled: true
http:
# enabled 控制 deployment 是否生成相应 docker http containerPort,如使用nodeport
enabled: true
servicePort: 8001
containerPort: 8001
# 如使用阿里云SLB直接到nodeport 请设置nodePort端口,保证集群内唯一
nodePort: 30001
tls:
enabled: true
servicePort: 8444
containerPort: 8444
# 生产环境 如借助阿里云SLB服务,设置NodePort模式,
type: NodePort
ingress:
# ingress 部分根据实际情况配置域名和端口,建议打开;如使用nodeport模式,使用阿里云slb服务,可以设置为False。
enabled: False
proxy:
enabled: true
http:
enabled: true
servicePort: 80
containerPort: 8000
# 如使用阿里云SLB直接到nodeport 请设置nodePort端口,保证集群内唯一
nodePort: 30000
tls:
enabled: true
servicePort: 443
containerPort: 8443
# 生产环境 如借助阿里云SLB服务,设置NodePort模式,
type: NodePort
ingress:
# ingress 部分根据实际情况配置域名和端口,建议打开;如使用nodeport模式,使用阿里云slb服务,可以设置为False。
enabled: false
ingressController: true
进入kong的文件夹
[root@localhost kong]tree
├── kong-k8s
│ ├── Chart.yaml
│ ├── FAQs.md
│ ├── README.md
│ ├── templates
│ │ ├── admission-webhook.yaml
│ │ ├── config-custom-server-blocks.yaml
│ │ ├── config-dbless.yaml
│ │ ├── controller-rbac-resources.yaml
│ │ ├── controller-service-account.yaml
│ │ ├── custom-resource-definitions.yaml
│ │ ├── deployment.yaml
│ │ ├── _helpers.tpl
│ │ ├── ingress-admin.yaml
│ │ ├── ingress-manager.yaml
│ │ ├── ingress-portal-api.yaml
│ │ ├── ingress-portal.yaml
│ │ ├── ingress-proxy.yaml
│ │ ├── migrations-post-upgrade.yaml
│ │ ├── migrations-pre-upgrade.yaml
│ │ ├── migrations.yaml
│ │ ├── NOTES.txt
│ │ ├── pdb.yaml
│ │ ├── psp.yaml
│ │ ├── service-kong-admin.yaml
│ │ ├── service-kong-manager.yaml
│ │ ├── service-kong-portal-api.yaml
│ │ ├── service-kong-portal.yaml
│ │ ├── service-kong-proxy.yaml
│ │ └── servicemonitor.yaml
│ └── values.yaml
# 第一次部署
helm install -n kong ./kong-k8s --values ./kong-k8s/values.yaml --namespace kong
如果你要将kong部署到一个单独的命名空间,请指定namespace
可选参数 --namespace kong
# 修改yaml文件,或者更新镜像
helm upgrade kong ./kong-k8s --values ./kong-k8s/values.yaml --namespace kong
可选参数 --namespace kong
CREATE DATABASE “konga” WITH ENCODING=‘UTF8’;
docker run --rm pantsel/konga:latest -c prepare -a {
{adapter}} -u {
{connection-uri}}
argument description default
-c command -
-a adapter (can be postgres or mysql) -
-u full database connection url -
例如我的示例命令:
docker run --rm --network=kong-net pantsel/konga:latest -c prepare -a postgres -u postgresql://kong:[email protected]:5432/konga
helm install -n konga-ui ./konga-ui-k8s --values ./konga-ui-k8s/values.yaml --namespace kong
PGSQL为9.6 10 的这2个版本可以执行 初始化Kong连接数据库的默认结构
PGSQL:12版本目前不支持,具体原因未知
现象如下:你数据库迁移命令成功
但是实际上表却没有创建
migrating rate-limiting on database 'kong'...
rate-limiting migrated up to: 000_base_rate_limiting (executed)
rate-limiting migrated up to: 003_10_to_112 (executed)
rate-limiting migrated up to: 004_200_to_210 (executed)
migrating hmac-auth on database 'kong'...
hmac-auth migrated up to: 000_base_hmac_auth (executed)
hmac-auth migrated up to: 002_130_to_140 (executed)
hmac-auth migrated up to: 003_200_to_210 (executed)
migrating oauth2 on database 'kong'...
oauth2 migrated up to: 000_base_oauth2 (executed)
oauth2 migrated up to: 003_130_to_140 (executed)
oauth2 migrated up to: 004_200_to_210 (executed)
migrating ip-restriction on database 'kong'...
ip-restriction migrated up to: 001_200_to_210 (executed)
migrating jwt on database 'kong'...
jwt migrated up to: 000_base_jwt (executed)
jwt migrated up to: 002_130_to_140 (executed)
jwt migrated up to: 003_200_to_210 (executed)
migrating basic-auth on database 'kong'...
basic-auth migrated up to: 000_base_basic_auth (executed)
basic-auth migrated up to: 002_130_to_140 (executed)
basic-auth migrated up to: 003_200_to_210 (executed)
migrating key-auth on database 'kong'...
key-auth migrated up to: 000_base_key_auth (executed)
key-auth migrated up to: 002_130_to_140 (executed)
key-auth migrated up to: 003_200_to_210 (executed)
migrating session on database 'kong'...
session migrated up to: 000_base_session (executed)
migrating acl on database 'kong'...
acl migrated up to: 000_base_acl (executed)
acl migrated up to: 002_130_to_140 (executed)
acl migrated up to: 003_200_to_210 (executed)
migrating response-ratelimiting on database 'kong'...
response-ratelimiting migrated up to: 000_base_response_rate_limiting (executed)
migrating bot-detection on database 'kong'...
bot-detection migrated up to: 001_200_to_210 (executed)
migrating acme on database 'kong'...
acme migrated up to: 000_base_acme (executed)
34 migrations processed
34 executed
Database is up-to-date
如果初始化的数据库不是以容器方式运行(例如购买rds的PGSQL)
那么你需要提前创建数据库和用户kong 然后直接执行docker初始化命令,并且参数需要去掉
–network=
并且修改KONG_PG_HOST的实际IP
docker run --rm \
-e "KONG_DATABASE=postgres" \
-e "KONG_PG_HOST=数据库的实际ip" \
-e "KONG_PG_USER=kong" \
-e "KONG_PG_PASSWORD=kong" \
-e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" \
kong:latest kong migrations bootstrap
部署kong的时候需要初始化一个容器 waiting for db
我用PGSQL10版本报错如下
Run with --v (verbose) or --vv (debug) for more details
waiting for db
Error: [PostgreSQL error] failed to retrieve PostgreSQL server_version_num: timeout
连接超时,然后我试了下ping pgsql的ip是可以的
突然灵机一动
RDS一般都要添加白名单的,我添加了试下,成功
kong-install
kong-github
版权声明:
原创不易,洗文可耻。除非注明,本博文章均为原创,转载请以链接形式标明本文地址。