QPS 比 Kong 快10倍的开源网关-APISIX原创

# QPS 比 Kong 快10倍的开源网关-APISIX

Centos (opens new window) Docker (opens new window) APISIX (opens new window)

# APISIX 介绍

Apache APISIX 作为云原生架构的开源 API 网关,可以为海量 API 和微服务提供安全可靠的动态、高性能、可扩展的管理平台。 官网地址:https://apisix.apache.org/

# 架构

APISIX 分为Data Plane和Control Plane两个部分,Data Plane为提供API Gateway的端口,而Control Plane则时管理者进行日常配置的接口。

# 与kong 对比

APISIX 简介 (opens new window)

# apisix docker化部署

apisix 官方提供了apisix-docker (opens new window) 仓库,其中提供了Docker的构建和部署文件,但部署方法和说明不够明晰,特此写了此篇文档作以记录。

# 初始化系统及配置

为实现Docker数据变化的持久化,需要在容器宿主机的数据盘创建挂载目录。

# 数据持久化
mkdir -p /opt/data/apps/{apisix,etcd}
mkdir -p /opt/data/apps/etcd/data
chown -R 1001:1001 /opt/data/apps/etcd

# 创建自定义network
docker network create --driver bridge --subnet=172.18.1.0/24 --gateway=172.18.1.1 devops

# 由于apisix 的官方Docker镜像中并没有 config.yaml 文件,所以需要提前下载并配置。
# 下载 apisix 配置文件
# 源路径 https://github.com/apache/apisix-docker/blob/master/example/dashboard_conf/conf.yaml
wget https://www.nginxbar.com/scripts/apisix/apisix_conf.yaml -O /opt/data/apps/apisix/apisix_conf.yaml

# 下载 apisix Dahsboard 配置文件
# 源路径 https://github.com/apache/apisix-docker/tree/master/example/dashboard_conf
wget https://www.nginxbar.com/scripts/apisix/apisix_conf.yaml -O /opt/data/apps/apisix/apisix_conf.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# 单机环境部署

APISIX 部署一共需要apisix、Dashboard、ectd 3个组件,apisix 为服务网关,Dashboard 为Web管理服务,Etcd 为存储。

# 修改apisix config etcd 地址
sed -i "s#http://etcd:2379#http://127.0.0.1:2379#g" /opt/data/apps/apisix/apisix_conf.yaml

# 修改apisix Dahboard config etcd 地址
sed -i "s#http://etcd:2379#http://127.0.0.1:2379#g" /opt/data/apps/apisix/dashboard_conf.yaml

1
2
3
4
5
6

docker-compose 脚本

version: '3'
services:
  etcd:
    hostname: etcd
    image: bitnami/etcd:3.4.9
    container_name: etcd
    privileged: true
    volumes:
      - "/opt/data/apps/etcd/data:/opt/bitnami/etcd/data"
    environment:
      - "ETCD_ADVERTISE_CLIENT_URLS=http://0.0.0.0:2379"
      - "ETCD_LISTEN_CLIENT_URLS=http://0.0.0.0:2379"
      - "ETCD_LISTEN_PEER_URLS=http://0.0.0.0:2380"
      - "ETCD_INITIAL_ADVERTISE_PEER_URLS=http://0.0.0.0:2380"
      - "ALLOW_NONE_AUTHENTICATION=yes"
      - "ETCD_INITIAL_CLUSTER=node1=http://0.0.0.0:2380"
      - "ETCD_NAME=node1"
      - "ETCD_DATA_DIR=/opt/bitnami/etcd/data"
    ports:
      - 2379:2379
      - 2380:2380
    networks:
      - devops

  apisix-base:
    depends_on:
      - etcd
    hostname: apisix-base
    image: apache/apisix
    container_name: apisix-base
    volumes:
      - /opt/data/apps/apisix/logs:/usr/local/apisix/logs
      - /opt/data/apps/apisix/apisix_conf.yaml:/usr/local/apisix/conf/config.yaml
    ports:
      - 80:9080
      - 443:9443
      - 9091:9091
    networks:
      - devops

  apisix-dashboard:
    depends_on:
      - etcd
    hostname: apisix-dashboard
    container_name: apisix-dashboard
    image: apache/apisix-dashboard
    restart: always
    volumes:
      - /opt/data/apps/apisix/dashboard_conf.yaml:/usr/local/apisix-dashboard/conf/conf.yaml:ro
    ports:
      - "9090:9000/tcp"
    networks:
      - devops

# 加入已建好的网络
networks:
  devops:
    external: true  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

浏览器访问 http://IP:9090 ,默认用户名及密码 admin admin

# 集群环境部署

  • etcd 集群搭建 etcd IP 分别为172.85.0.10、172.85.0.11、172.85.0.12,分别在172.85.0.10、172.85.0.11、172.85.0.12 中执行如下shell 及docker-compose
mkdir -p /opt/data/apps/etcd/data
chown -R 1001:1001 /opt/data/apps/etcd

# 设定etcd 节点名,根据实际情况修改
echo "NODE_NAME=node2" >.env
# 设定etcd 集群节点名称
echo "CLUSTER_NODE=node1=http://172.85.0.10:2380,node2=http://172.85.0.11:2380,node3=http://172.85.0.12:2380" >>.env

# 设定当前节点IP
node_ip=`ifconfig |grep "inet 172.16" |awk '{print $2}'`
echo "NODE_IP=$node_ip" >>.env
1
2
3
4
5
6
7
8
9
10
11

etcd docker-compose 脚本

version: '3'
services:
  etcd:
    hostname: etcd
    image: bitnami/etcd:3.4.9
    container_name: etcd
    privileged: true
    network_mode: host
    volumes:
      - "/opt/data/apps/etcd/data:/opt/bitnami/etcd/data"
    environment:
      - "ETCD_ADVERTISE_CLIENT_URLS=http://${NODE_IP}:2379"
      - "ETCD_LISTEN_CLIENT_URLS=http://0.0.0.0:2379"
      - "ETCD_LISTEN_PEER_URLS=http://0.0.0.0:2380"
      - "ETCD_INITIAL_ADVERTISE_PEER_URLS=http://${NODE_IP}:2380"
      - "ALLOW_NONE_AUTHENTICATION=yes"
      - "ETCD_INITIAL_CLUSTER=${CLUSTER_NODE}"
      - "ETCD_NAME=${NODE_NAME}"
      - "ETCD_DATA_DIR=/opt/bitnami/etcd/data"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  • apisix 组件部署 apisix-base 可以在多台docker服务节点上部署,实现多台负载的效果,apisix-dashboard 仅需使用一个即可。
# 修改apisix config etcd 地址
sed -i "s#http://etcd:2379#http://172.85.0.10:2379#g" /opt/data/apps/apisix/apisix_conf.yaml
sed -i '/  prefix:/i\    - "http:\/\/172.85.0.11:2379"' /opt/data/apps/apisix/apisix_conf.yaml
sed -i '/  prefix:/i\    - "http:\/\/172.85.0.12:2379"' /opt/data/apps/apisix/apisix_conf.yaml

# 修改apisix Dahboard config etcd 地址
sed -i "s#http://etcd:2379#http://172.85.0.10:2379#g" /opt/data/apps/apisix/dashboard_conf.yaml
sed -i '/172.85.0.10:2379"/a\      - "http:\/\/172.85.0.11:2379"' /opt/data/apps/apisix/dashboard_conf.yaml
sed -i '/172.85.0.11:2379"/a\      - "http:\/\/172.85.0.12:2379"' /opt/data/apps/apisix/dashboard_conf.yaml

1
2
3
4
5
6
7
8
9
10

docker-compose 脚本

version: '3'
services:
  apisix-base:
    hostname: apisix-base
    image: apache/apisix
    container_name: apisix-base
    volumes:
      - /opt/data/apps/apisix/logs:/usr/local/apisix/logs
      - /opt/data/apps/apisix/apisix_conf.yaml:/usr/local/apisix/conf/config.yaml
    # 生产环境建议主机模式
    # network_mode: host
    ports:
      - 80:9080
      - 443:9443
      - 9091:9091
    networks:
      - devops

  apisix-dashboard:
    hostname: apisix-dashboard
    container_name: apisix-dashboard
    image: apache/apisix-dashboard
    restart: always
    volumes:
      - /opt/data/apps/apisix/dashboard_conf.yaml:/usr/local/apisix-dashboard/conf/conf.yaml:ro
    ports:
      - "9090:9000/tcp"
    networks:
      - devops

# 加入已建好的网络
networks:
  devops:
    external: true  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

# 接入测试

key=`cat /opt/data/apps/apisix/apisix_conf.yaml |grep key |grep -v "admin"|head -n 1 |awk '{print $2}'`

# 通过一个api即可快速创建API
curl "http://127.0.0.1/apisix/admin/routes/1" -H "X-API-KEY: $key" -X PUT -d '
{
  "methods": ["ALL"],
  "name":"skywalking",
  "host": "skywalking.nginxbar.com",
  "uri": "/*",
  "upstream": {
    "type": "roundrobin",
    "nodes": {
      "172.85.0.41:8080": 1
    }
  }
}'

# 查看 routes
curl "http://127.0.0.1/apisix/admin/routes/" -H "X-API-KEY: $key"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
上次更新: 2022/12/05, 22:29:05

Initializing...

最近更新
01
git的tag与branch 原创
05-21
02
阿里云SLS日志服务的数据脱敏及安全管理 原创
03-21
03
云平台的成本管理 原创
03-13
更多文章>
×