Apache APISIX安装指南:从源码到生产环境部署

Apache APISIX安装指南:从源码到生产环境部署

【免费下载链接】apisix The Cloud-Native API Gateway 【免费下载链接】apisix 项目地址: https://gitcode.com/GitHub_Trending/ap/apisix

概述

Apache APISIX是一个动态、实时、高性能的云原生API网关,提供丰富的流量管理功能,包括负载均衡、动态上游、金丝雀发布、熔断、认证、可观测性等。本文将详细介绍从源码编译到生产环境部署的完整安装流程。

系统要求

在开始安装前,请确保您的系统满足以下要求:

组件最低要求推荐配置
操作系统Linux (CentOS 7+, Ubuntu 16.04+, Debian 9+)Linux (Ubuntu 20.04+, CentOS 8+)
CPU2核4核及以上
内存2GB8GB及以上
存储10GB50GB及以上
网络千兆网卡万兆网卡

环境准备

1. 安装依赖项

首先安装系统级依赖包:

# Ubuntu/Debian
sudo apt-get update
sudo apt-get install -y git gcc g++ make curl wget unzip perl libpcre3-dev libssl-dev libyaml-dev

# CentOS/RHEL
sudo yum install -y git gcc gcc-c++ make curl wget unzip perl pcre-devel openssl-devel libyaml-devel

2. 安装OpenResty

APISIX基于OpenResty构建,需要先安装OpenResty:

# Ubuntu/Debian
wget -qO - https://openresty.org/package/pubkey.gpg | sudo apt-key add -
sudo apt-get install -y software-properties-common
sudo add-apt-repository -y "deb http://openresty.org/package/ubuntu $(lsb_release -sc) main"
sudo apt-get update
sudo apt-get install -y openresty

# CentOS/RHEL
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo
sudo yum install -y openresty

3. 安装LuaRocks

LuaRocks是Lua的包管理器:

wget https://luarocks.org/releases/luarocks-3.9.2.tar.gz
tar -xzf luarocks-3.9.2.tar.gz
cd luarocks-3.9.2
./configure --with-lua=/usr/local/openresty/luajit \
            --with-lua-include=/usr/local/openresty/luajit/include/luajit-2.1 \
            --lua-suffix=jit
make && sudo make install

源码安装APISIX

1. 克隆源码

# 指定版本
APISIX_VERSION='3.9.0'
git clone --depth 1 --branch ${APISIX_VERSION} https://gitcode.com/GitHub_Trending/ap/apisix.git apisix-${APISIX_VERSION}
cd apisix-${APISIX_VERSION}

2. 安装依赖

# 安装运行时依赖
make deps

# 安装APISIX
make install

安装过程流程图:

mermaid

3. 安装etcd

APISIX使用etcd作为配置中心:

ETCD_VERSION='3.5.4'
wget https://github.com/etcd-io/etcd/releases/download/v${ETCD_VERSION}/etcd-v${ETCD_VERSION}-linux-amd64.tar.gz
tar -xvf etcd-v${ETCD_VERSION}-linux-amd64.tar.gz
cd etcd-v${ETCD_VERSION}-linux-amd64
sudo cp -a etcd etcdctl /usr/bin/

# 启动etcd
nohup etcd >/tmp/etcd.log 2>&1 &

配置APISIX

1. 初始化配置

# 初始化NGINX配置和etcd
apisix init

2. 修改配置文件

编辑 conf/config.yaml 文件:

deployment:
  role: traditional
  role_traditional:
    config_provider: etcd
  etcd:
    host:
      - "http://127.0.0.1:2379"
  admin:
    admin_key:
      - name: "admin"
        key: "your-secure-key-here"  # 修改为安全的密钥
        role: admin

apisix:
  node_listen: 9080
  enable_admin: true
  admin_listen: 9180

3. 生成Nginx配置

# 测试配置
apisix test

# 生成最终配置
apisix init

启动和停止服务

启动APISIX

# 正常启动
apisix start

# 查看状态
apisix status

停止APISIX

# 优雅停止(处理完现有请求)
apisix quit

# 强制停止
apisix stop

重新加载配置

# 热重载配置(无需重启)
apisix reload

生产环境部署

1. 系统优化配置

# 调整系统参数
echo "net.core.somaxconn = 65535" | sudo tee -a /etc/sysctl.conf
echo "net.ipv4.tcp_max_syn_backlog = 65535" | sudo tee -a /etc/sysctl.conf
echo "fs.file-max = 2097152" | sudo tee -a /etc/sysctl.conf
sudo sysctl -p

# 调整进程限制
echo "* soft nofile 65535" | sudo tee -a /etc/security/limits.conf
echo "* hard nofile 65535" | sudo tee -a /etc/security/limits.conf

2. 配置Systemd服务

创建 /etc/systemd/system/apisix.service

[Unit]
Description=Apache APISIX
After=network.target

[Service]
Type=forking
User=root
Group=root
ExecStart=/usr/bin/apisix start
ExecStop=/usr/bin/apisix quit
ExecReload=/usr/bin/apisix reload
Restart=on-failure
RestartSec=5s

[Install]
WantedBy=multi-user.target

启用并启动服务:

sudo systemctl daemon-reload
sudo systemctl enable apisix
sudo systemctl start apisix
sudo systemctl status apisix

3. 配置日志轮转

创建 /etc/logrotate.d/apisix

/usr/local/apisix/logs/*.log {
    daily
    missingok
    rotate 30
    compress
    delaycompress
    notifempty
    copytruncate
    dateext
    dateformat -%Y%m%d
}

高可用部署架构

mermaid

集群部署配置

修改 config.yaml 支持集群:

deployment:
  role: traditional
  role_traditional:
    config_provider: etcd
  etcd:
    host:
      - "http://etcd1:2379"
      - "http://etcd2:2379"
      - "http://etcd3:2379"
    prefix: "/apisix"
    timeout: 30

监控和运维

1. 启用Prometheus监控

# 在config.yaml中添加
plugins:
  - prometheus

plugin_attr:
  prometheus:
    export_addr:
      ip: "0.0.0.0"
      port: 9091

2. 健康检查配置

# 创建健康检查脚本
cat > /usr/local/bin/apisix-healthcheck.sh << 'EOF'
#!/bin/bash
curl -f http://127.0.0.1:9080/apisix/status || exit 1
EOF

chmod +x /usr/local/bin/apisix-healthcheck.sh

故障排除

常见问题解决

问题解决方案
端口冲突修改 config.yaml 中的监听端口
etcd连接失败检查etcd服务状态和网络连通性
权限问题确保APISIX有足够的文件系统权限
内存不足调整Nginx worker进程数量

日志分析

# 查看错误日志
tail -f /usr/local/apisix/logs/error.log

# 查看访问日志
tail -f /usr/local/apisix/logs/access.log

# 查看启动日志
journalctl -u apisix -f

安全加固

1. 防火墙配置

# 只开放必要端口
sudo ufw allow 9080/tcp  # APISIX监听端口
sudo ufw allow 9180/tcp  # 管理端口(建议内网)
sudo ufw allow 2379/tcp  # etcd端口(集群内部)

2. SSL/TLS配置

# 启用SSL
ssl:
  enable: true
  listen: 9443
  ssl_cert: /path/to/cert.pem
  ssl_cert_key: /path/to/key.pem

性能优化

1. Nginx优化配置

nginx_config:
  worker_processes: auto
  worker_connections: 4096
  keepalive_timeout: 60s
  client_header_timeout: 60s
  client_body_timeout: 60s
  send_timeout: 60s

2. 缓存优化

plugin_attr:
  prometheus:
    metrics_interval: 5
  limit-count:
    count: 100
    time_window: 60
    key: remote_addr
    rejected_code: 503

总结

通过本文的详细指南,您应该能够成功完成Apache APISIX从源码编译到生产环境部署的全过程。APISIX作为一个高性能的API网关,在现代微服务架构中扮演着重要角色。正确的安装和配置是确保系统稳定运行的基础。

记住定期更新APISIX版本以获得最新的功能和安全补丁,同时建立完善的监控和告警机制,确保网关服务的高可用性。

【免费下载链接】apisix The Cloud-Native API Gateway 【免费下载链接】apisix 项目地址: https://gitcode.com/GitHub_Trending/ap/apisix

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值