- docker-compose
mkdir /data/kong -p
cd /data/kong
wget https://raw.githubusercontent.com/Kong/docker-kong/master/compose/docker-compose.yml
cat > /data/kong/POSTGRES_PASSWORD << EOF
kong2O21
EOF
docker-compose up -d
- docker
mkdir /data/kong-database -p
cd /data/kong-database
cat > /data/kong-database/start.sh << 'EOF'
docker run -d \
--restart=always \
--name kong-database \
-p 5432:5432 \
-e POSTGRES_USER=kong \
-e POSTGRES_DB=kong \
-e POSTGRES_PASSWORD='Kong2O21' \
-e PGDATA=/var/lib/postgresql/data/pgdata \
-v /etc/localtime:/etc/localtime \
-v /data/postgresql/data:/var/lib/postgresql/data \
postgres:10
EOF
bash /data/kong-database/start.sh
cat > /data/kong-database/init.sh << 'EOF'
docker run --rm \
-e "KONG_DATABASE=postgres" \
-e "KONG_PG_HOST=192.168.11.194" \
-e "KONG_PG_USER=kong" \
-e "KONG_PG_PASSWORD=Kong2O21" \
-e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" \
kong:2.5 kong migrations bootstrap
EOF
bash /data/kong-database/init.sh
mkdir /data/kong -p
cd /data/kong
cat > /data/kong/start.sh << 'EOF'
docker run -d \
--name kong \
--restart=always \
-e "KONG_DATABASE=postgres" \
-e "KONG_PG_HOST=192.168.11.194" \
-e "KONG_PG_PASSWORD=Kong2O21" \
-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" \
-v /etc/localtime:/etc/localtime \
-p 8000:8000 \
-p 8443:8443 \
-p 8001:8001 \
-p 8444:8444 \
kong:2.5
EOF
bash /data/kong/start.sh
# 迁移konga数据表到pgsql
docker run --rm \
--name konginit \
--network host \
pantsel/konga \
-c prepare \
-a postgres \
-u postgresql://kong:Kong2O21@192.168.11.194:5432/kong
# 启动konga
cat > /data/kong/konga.sh << 'EOF'
docker run -d \
--name konga \
--restart=always \
-p 1337:1337 \
-e "DB_ADAPTER=postgres" \
-e "DB_HOST=192.168.11.194" \
-e "DB_USER=kong" \
-e "DB_PASSWORD=Kong2O21" \
-e "DB_DATABASE=kong" \
-e "KONGA_HOOK_TIMEOUT=120000" \
-e "DB_PG_SCHEMA=public" \
-e "NODE_ENV=production" \
--name konga \
pantsel/konga
EOF
bash /data/kong/konga.sh
http://192.168.11.194:1337(konga工具有bug,在设置route时会报错)
http://192.168.11.194:8001/metrics
3、配置prometheus
#添加自动发现脚本
cat >> /data/prometheus/conf/prometheus.yml << 'EOF'
#kong自动发现
- job_name: 'kong'
file_sd_configs:
- files:
- /etc/prometheus/sd_config/kong.yaml
refresh_interval: 5s
relabel_configs:
- source_labels: [__address__]
regex: (.*)
target_label: instance
replacement: $1
- source_labels: [__address__]
regex: (.*):(.*)
target_label: __address__
replacement: $1:8001
EOF
#自动发现配置
cat > /data/prometheus/conf/sd_config/kong.yaml << 'EOF'
#kong自动发现
- labels:
project: kong
targets:
- 192.168.11.194:8001
EOF
grafanaid: 7424
测试:
#创建一个service
curl -i -X POST --url http://localhost:8001/services/ \
--data 'name=ifeng' \
--data 'url=https://www.ifeng.com'
#为service创建路由
curl -i -X POST --url http://localhost:8001/services/ifeng/routes \
--data 'paths[]=/ifeng'
#访问kong,测试是否转发
curl -i -X GET --url http://localhost:8000/ifeng
访问http://192.168.11.194:8000/ifeng/
参考:https://blog.csdn.net/xiaoliu598906167/article/details/85785420
https://blog.csdn.net/qism007/article/details/89842130
https://blog.catscarlet.com/201612202671.html