一、安装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
二、安装Httpbin
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
docker run -itd -p 80:80 kennethreitz/httpbin
curl http://127.0.0.1:80/ip
三、configure kong
cp /etc/kong/kong.conf.default /etc/kong/kong.conf
sed -i "/^#database/i\database = off" /etc/kong/kong.conf
sed -i "/^#nginx_worker_processes/i\nginx_worker_processes = 1" /etc/kong/kong.conf
sed -i "/^#declarative_config/i\declarative_config = /tmp/apis.yaml" /etc/kong/kong.conf
四、configure 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
targets:
- upstream: upstream-httpbin
target: 127.0.0.1:80
weight: 100
plugins:
- config:
generator: tracker
header_name: X-Kong-Request-ID
echo_downstream: true
name: correlation-id
EOF
五、start Kong
kong start
六、update declarative config file
cat > /tmp/loop_update.sh <<EOF
#!/bin/bash
while true
do
echo "config:"`date`
curl -F "config=@/tmp/apis.yaml" http://127.0.0.1:8001/config
done
EOF
chmod +x /tmp/loop_update.sh
/tmp/loop_update.sh
七、open a new terminal and send request
cat > /tmp/loop_request.sh <<EOF
#!/bin/bash
while true
do
curl -i http://127.0.0.1:8000/httpbin/ip
done
EOF
chmod +x /tmp/loop_request.sh
/tmp/loop_request.sh
八、after a while, view logs/access.log
cat /usr/local/kong/logs/admin_access.log | wc -l
cat /usr/local/kong/logs/access.log | grep 200 | wc -l
cat /usr/local/kong/logs/access.log | grep 404 | wc -l
cat /usr/local/kong/logs/access.log | grep 503 | wc -l
网友评论