19

Docker搭建Kong--配置Service并添加Key-auth-李佳良的博客

 4 years ago
source link: https://blog.51cto.com/13555423/2471028
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Docker搭建Kong--v0.14-配置Service并添加Key-auth

Kong 是在客户端和(微)服务间转发API通信的API网关,通过插件扩展功能。
概念术语
upstream: 是对上游服务器的抽象;
target: 代表了一个物理服务,是 ip + port 的抽象;
service: 是抽象层面的服务,他可以直接映射到一个物理服务(host 指向 ip + port),也可以指向一个 upstream 来做到负载均衡;
route: 是路由的抽象,他负责将实际的 request 映射到 service。
默认情况下,KONG监听的端口为:
8000: 此端口是KONG用来监听来自客户端传入的HTTP请求,并将此请求转发到上有服务器;
8443: 此端口是KONG用来监听来自客户端传入的HTTP请求的。它跟8000端口的功能类似,但是它只是用来监听HTTP请求的,没有转发功能。可以通过修改配置文件来禁止它;
8001: Admin API,通过此端口,管理者可以对KONG的监听服务进行配置;
8444: 通过此端口,管理者可以对HTTP请求进行监控.

环境部署
1.安装docker

export REGISTRY_MIRROR=https://registry.cn-hangzhou.aliyuncs.com
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum repolist
yum remove -y docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-selinux docker-engine-selinux docker-engine
yum install -y docker-ce-18.09.7 docker-ce-cli-18.09.7 containerd.io
systemctl start docker && systemctl status docker && systemctl daemon-reload

2.禁用防火墙

systemctl disable firewalld && systemctl stop firewalld && systemctl status firewalld
setenforce 0 && sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config

3.禁用交换分区,设置路由转发

swapoff -a && yes | cp /etc/fstab /etc/fstab_bak
cat /etc/fstab_bak | grep -v swap > /etc/fstab
cat /etc/fstab
sudo vi /etc/sysctl.conf
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1

安装
1.创建docker网络

docker network create kong-net

2.运行prostgres 9.6数据库镜像

docker run -d --name kong-database \
--network=kong-net \
-p 5432:5432 \
-e "POSTGRES_USER=kong" \
-e "POSTGRES_DB=kong" \
postgres:9.6

3.数据库准备,初始化Kong数据

docker run --rm \
--network=kong-net \
-e "KONG_DATABASE=postgres" \
-e "KONG_PG_HOST=kong-database" \
kong:0.14.1 kong migrations up

4.启动kong

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, 0.0.0.0:8444 ssl" \
-p 8000:8000 \
-p 8443:8443 \
-p 8001:8001 \
-p 8444:8444 \
kong:0.14.1

5.创建并运行Kong dashboard容器

docker run -d \
--network=kong-net \
--link kong:kong -p 8008:8080 pgbi/kong-dashboard start \
--kong-url http://kong:8001 \
--basic-auth kong=kong

然后访问 http://你的IP:8008,
登录账号为kong,密码为kong

Docker搭建Kong--v0.14-配置Service并添加Key-auth

1.为 http://mockbin.org 添加一个名为 example-service 的服务

使用Admin API添加服务,发出以下cURL请求以将您的第一个服务(指向Mockbin API)添加到Kong:

curl -i -X POST \
--url http://localhost:8001/services/ \
--data 'name=example-service'  --data 'url=http://mockbin.org'

2.为上面创建的服务添加一个路由

curl -i -X POST \
--url http://localhost:8001/services/example-service/routes \
--data 'hosts[]=example.com'

勾选"strip_path"选项,并点击”updata“

3. 运行下面的命令,会返回 http://mockbin.org 的信息

curl -i -X GET --url http://localhost:8000/ --header 'Host: example.com'
Docker搭建Kong--v0.14-配置Service并添加Key-auth

1.配置密钥身份验证插件

curl -i -X POST \
  --url http://localhost:8001/services/example-service/plugins/ \
  --data 'name=key-auth'

注意:此插件还接受一个config.key_names参数,默认为['apikey']。它是应该在请求期间包含apikey的标头和参数名称(均受支持)的列表

2.确认插件配置正确

curl -i -X GET --url http://localhost:8000/ --header 'Host: example.com'

由于您未指定所需的apikey标题或参数,因此响应应为401 Unauthorized

Docker搭建Kong--v0.14-配置Service并添加Key-auth

增加消费者

curl -i -X POST \
  --url http://localhost:8001/consumers/ \
  --data "username=Jason"

为上面的用户添加一个 key。下面命令中的 “ENTER_KEY_HERE” 需要替换成想要设置的密钥。

curl -i -X POST \
  --url http://localhost:8001/consumers/Jason/key-auth/ \
  --data 'key=ENTER_KEY_HERE'

在第3步的请求中添加 key 的信息后,可以正常访问了。命令如下:

curl -i -X GET \
  --url http://localhost:8000 \
  --header "Host: example.com" \
  --header "apikey: ENTER_KEY_HERE"
Docker搭建Kong--v0.14-配置Service并添加Key-auth

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK