一、安装 Docker
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum -y install docker-ce
systemctl daemon-reload && systemctl restart docker
二、安装 Httpbin
port: 80
docker run -itd -p 8081:80 --name httpbin-1 kennethreitz/httpbin
docker run -itd -p 8082:80 --name httpbin-2 kennethreitz/httpbin
docker run -itd -p 8083:80 --name httpbin-3 kennethreitz/httpbin
三、安装 Kong
sudo yum install -y wget jq
wget https://bintray.com/kong/kong-rpm/rpm -O bintray-kong-kong-rpm.repo
export major_version=`grep -oE '[0-9]+\.[0-9]+' /etc/redhat-release | cut -d "." -f1`
sed -i -e 's/baseurl.*/&\/centos\/'$major_version''/ bintray-kong-kong-rpm.repo
sudo mv bintray-kong-kong-rpm.repo /etc/yum.repos.d/
sudo yum install -y kong
四、配置 routes&services&plugins
cat > /tmp/apis.yaml <<EOF
_format_version: '1.1'
routes:
- name: route-httpbin
protocols:
- http
paths:
- /httpbin
strip_path: true
preserve_host: false
regex_priority: 1
service: service-httpbin
services:
- name: service-httpbin
host: upstream-httpbin
protocol: http
port: 80
connect_timeout: 60000
read_timeout: 60000
write_timeout: 60000
retries: 5
upstreams:
- name: upstream-httpbin
algorithm: round-robin
algorithm: round-robin
algorithm: round-robin
healthchecks:
active:
https_verify_certificate: false
type: http
http_path: "/status/500"
timeout: 30
concurrency: 3
healthy:
http_statuses:
- 200
- 302
- 301
successes: 3
interval: 0
unhealthy:
tcp_failures: 3
timeouts: 3
http_failures: 3
http_statuses:
- 429
- 404
- 500
- 501
- 502
- 503
- 504
- 505
interval: 0
passive:
type: http
healthy:
successes: 3
http_statuses:
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 226
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
unhealthy:
http_failures: 3
tcp_failures: 3
timeouts: 3
http_statuses:
- 429
- 500
- 503
threshold: 0
tags:
- user-level
- low-priority
targets:
- upstream: upstream-httpbin
target: 127.0.0.1:8081
weight: 100
- upstream: upstream-httpbin
target: 127.0.0.1:8082
weight: 100
- upstream: upstream-httpbin
target: 127.0.0.1:8083
weight: 100
EOF
五、启动 Kong
# 配置
cp /etc/kong/kong.conf.default /etc/kong/kong.conf
# 使用db-lessmode
sed -i "/^#database/i\database = off" /etc/kong/kong.conf
sed -i "/^#declarative_config/i\declarative_config = /tmp/apis.yaml" /etc/kong/kong.conf
# 启动
kong start
# 验证
curl http://127.0.0.1:8001 2>/dev/null | jq .configuration.database
curl http://127.0.0.1:8000/httpbin/ip
curl http://127.0.0.1:8000/httpbin/status/500
curl http://127.0.0.1:8000/httpbin/delay/80000
# 查看upstream健康状态
curl http://127.0.0.1:8001/upstreams/f348e902-7c2e-5cf7-a8d7-f30ec5e6b12d/health
网友评论