# Kong 安装和部署指南

# Kong 安装和部署指南

Kong 是一个云原生、快速、可扩展的微服务抽象层(API 网关)。以下是 Kong 的安装和部署方法：

## 1. 安装 Kong

### 在 Linux 上安装

#### Ubuntu/Debian
```bash
# 添加 Kong 仓库
sudo apt-get update
sudo apt-get install -y apt-transport-https curl
echo "deb https://kong.bintray.com/kong-deb `lsb_release -sc` main" | sudo tee -a /etc/apt/sources.list
curl -o bintray.key https://bintray.com/user/downloadSubjectPublicKey?username=bintray
sudo apt-key add bintray.key
sudo apt-get update

# 安装 Kong
sudo apt-get install -y kong
```

#### CentOS/RHEL
```bash
# 添加 Kong 仓库
sudo yum install -y wget
wget https://bintray.com/kong/kong-rpm/rpm -O bintray-kong-kong-rpm.repo
sudo mv bintray-kong-kong-rpm.repo /etc/yum.repos.d/
sudo yum update -y

# 安装 Kong
sudo yum install -y kong
```

### 使用 Docker
```bash
# 创建 Docker 网络
docker network create kong-net

# 启动 PostgreSQL 容器
docker run -d --name kong-database \
  --network=kong-net \
  -p 5432:5432 \
  -e POSTGRES_USER=kong \
  -e POSTGRES_DB=kong \
  -e POSTGRES_PASSWORD=kong \
  postgres:9.6

# 初始化数据库
docker run --rm \
  --network=kong-net \
  -e "KONG_DATABASE=postgres" \
  -e "KONG_PG_HOST=kong-database" \
  -e "KONG_PG_PASSWORD=kong" \
  kong:latest kong migrations bootstrap

# 启动 Kong
docker run -d --name kong \
  --network=kong-net \
  -e "KONG_DATABASE=postgres" \
  -e "KONG_PG_HOST=kong-database" \
  -e "KONG_PG_PASSWORD=kong" \
  -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:latest
```

## 2. 配置 Kong

### 基本配置
编辑 `/etc/kong/kong.conf` 或设置环境变量：

```bash
# 数据库配置
KONG_DATABASE=postgres  # 或 cassandra
KONG_PG_HOST=localhost
KONG_PG_PORT=5432
KONG_PG_USER=kong
KONG_PG_PASSWORD=kong
KONG_PG_DATABASE=kong

# 代理配置
KONG_PROXY_LISTEN=0.0.0.0:8000, 0.0.0.0:8443 ssl
KONG_ADMIN_LISTEN=0.0.0.0:8001, 0.0.0.0:8444 ssl
```

### 初始化数据库
```bash
kong migrations bootstrap [-c /path/to/kong.conf]
```

### 启动 Kong
```bash
kong start [-c /path/to/kong.conf]
```

## 3. 验证安装

检查 Kong 是否运行：
```bash
curl -i http://localhost:8001/
```

应该看到 Kong 的欢迎信息。

## 4. 基本使用

### 添加服务
```bash
curl -i -X POST \
  --url http://localhost:8001/services/ \
  --data 'name=example-service' \
  --data 'url=http://mockbin.org'
```

### 添加路由
```bash
curl -i -X POST \
  --url http://localhost:8001/services/example-service/routes \
  --data 'hosts[]=example.com'
```

### 测试代理
```bash
curl -i -X GET \
  --url http://localhost:8000/ \
  --header 'Host: example.com'
```

## 5. 生产环境部署建议

1. **使用 PostgreSQL 集群**：确保数据库高可用
2. **配置 Kong 集群**：多个 Kong 节点共享同一个数据库
3. **启用 TLS**：配置 SSL 证书
4. **设置监控**：使用 Prometheus 或 Datadog 监控 Kong
5. **配置日志**：设置适当的日志级别和日志轮转
6. **使用负载均衡器**：在 Kong 节点前放置负载均衡器

## 6. 常见插件

Kong 的强大之处在于其插件系统，常用插件包括：
- **认证**：Keycloak, JWT, OAuth2, Basic Auth
- **安全**：CORS, IP Restriction, Bot Detection
- **流量控制**：Rate Limiting, Request Size Limiting
- **监控**：Prometheus, Datadog
- **日志**：File Log, HTTP Log, TCP Log

希望这个指南能帮助您顺利安装和部署 Kong！如需更详细的配置，请参考 Kong 官方文档。