APISIX网关试水
基本概念
- router:路由规则,对请求进行匹配,并中转到对应的后端服务,如无特殊要求,可绕过service直接配置upstream;
- upstream:虚拟host抽象,负责再对个节点之间进行负载均衡(可对应到nginx的upstream)
- service:对upstream进行抽象,通常就是对一个upstream的引用,具体用处是?——参考kong的设计,这里service可以是基于服务发现得到的,不需要对应到upstream配置;同时可以配置service级别的插件,被多个router复用;此时如果router也有配置相同插件,则会覆盖service级别插件;同一个service可以指向到不同的upstream(如AB测试)
- consumer:消费者,实际就是kong的租户概念;
- plugins:router维度 or 全局 的请求处理逻辑扩展;选择优先级是 Consumer > Route > Service 。——实际还有all service场景的插件!
整个路由及插件的设计跟kong基本一致。
部署架构
多集群部署,通过etcd隔离实现。需要在控制面适配,配置多个etcd集群实现。
etcd管理
安装(appops账号)
wget https://github.com/etcd-io/etcd/releases/download/v3.4.13/etcd-v3.4.13-linux-amd64.tar.gz tar -xvf etcd-v3.4.13-linux-amd64.tar.gz
部署路径:/home/appops/etcd-v3.4.13-linux-amd64
开发集群:
xxx
启动命令分别为:
nohup ./etcd –name db-node1
–initial-advertise-peer-urls http://10.xxx:2380 –listen-peer-urls http://10.xxx:2380
–advertise-client-urls http://10.xxx:2379 –listen-client-urls http://10.xxx:2379
–initial-cluster db-node1=http://10.xxx:2380,db-node2=http://10.xxx:2380,
–initial-cluster-state new –initial-cluster-token qiye-mask &
nohup ./etcd –name db-node2
–initial-advertise-peer-urls http://10.150.0.2:2380 –listen-peer-urls http://10.150.0.2:2380
–advertise-client-urls http://10.150.0.2:2379 –listen-client-urls http://10.150.0.2:2379
–initial-cluster db-node1=http://10.150.0.4:2380,db-node2=http://10.150.0.2:2380,
–initial-cluster-state new –initial-cluster-token qiye-mask &
线上集群:
nohup ./etcd –name db-online-node1 \ –initial-advertise-peer-urls http://10.yyy:2380 –listen-peer-urls http://10.yyy:2380 \ –advertise-client-urls http://10.yyy:2379 –listen-client-urls http://10.yyy:2379 \ –initial-cluster db-online-node1=http://10.yyy:2380,db-online-node2=http://10.xxx:2380, \ –initial-cluster-state new –initial-cluster-token qiye-mask &
nohup ./etcd –name db-online-node2 \ –initial-advertise-peer-urls http://10.xxx:2380 –listen-peer-urls http://10.xxx:2380 \ –advertise-client-urls http://10.xxx:2379 –listen-client-urls http://10.xxx:2379 \ –initial-cluster db-online-node1=http://10.yyy:2380,db-online-node2=http://10.xxx:2380, \ –initial-cluster-state new –initial-cluster-token qiye-mask &
安全策略:
开启root账号的密码验证
./etcdctl –endpoints http://10.yyy:2380 user add root
./etcdctl –endpoints http://10.yyy:2380 role add root –user="root”
./etcdctl –endpoints http://10.yyy:2380 user grant-role root root –user="root”
./etcdctl –endpoints http://10.yyy:2380,http://10.xxx:2380 auth enable –user="root”
注意:
需要同步调整dashboard跟gateway配置;
控制面
打包机:
xxx
工具安装
- golang安装(root账户)
https://golang.org/dl/go1.16.linux-amd64.tar.gz
tar -C /usr/local -xzf go1.16.linux-amd64.tar.gz
export PATH=$PATH:/usr/local/go/bin (添加到.bashrc) - nodejs安装(root账户)
https://nodejs.org/dist/v14.16.0/node-v14.16.0-linux-x64.tar.xz
mkdir -p /usr/local/lib/nodejs
tar -xJvf node-v14.16.0-linux-x64.tar.xz -C /usr/local/lib/nodejs
export PATH=$PATH:/usr/local/lib/nodejs/node-v14.16.0-linux-x64/bin(添加到.bashrc) - yarn安装(root账户)
npm install -g yarn yarn set version latest - 源码编译(appops账户)
go env -w GOPROXY=https://goproxy.cn,direct
make build
mv ./output ../apisix-dashboard
配置(appops账户)
api/conf/conf.yaml
- 监听IP,PORT;
- etcd地址;
- 日志路径;
- 平台登录账户密码;
- 插件名;
api/conf/schema.json 增加插件schema——此处不是直接从网关拉取,重复工作。。。
服务启停(appops账户)
- 停止:cd ./apisix-dashboard && ./manager-api stop
- 启动:cd ./apisix-dashboard && nohup ./manager-api &
数据面
编译打包
-
依赖安装 操作系统:Debian9
发布目录: /home/appops/qiye-mask -
1,openresty,lurocks安装(root账号)
wget -qO - https://openresty.org/package/pubkey.gpg | sudo apt-key add - apt-get -y install software-properties-common add-apt-repository -y “deb http://openresty.org/package/debian $(lsb_release -sc) openresty” apt-get update
apt-get install -y git openresty curl make openresty-openssl111-dev mv /usr/local/openresty/openssl111/ /usr/local/openresty/openssl/
curl https://raw.githubusercontent.com/apache/apisix/master/utils/linux-install-luarocks.sh -sL | bash -
-
2,lua依赖安装(appops账号)
cd /home/appops/qiye-mask && make deps (此时所有lua依赖都会存放到/home/appops/qiye-mask/deps下) -
3,etcd安装(appops账号)
参考:etcd管理 -
deb安装包制作
打包目录:/home/appops/qiye-mask-package -
目录结构
./home/appops/qiye-mask:即对应实际网关lua源码目录,手动将源码以及lua依赖都拷贝过来,包括(apisix,bin,conf,deps)
./usr/local/openresty:openresty运行环境相关内容,通过前面安装阶段已经存在于当前机器对应目录中,直接拷贝过来即可;
./DEBIAN/control:记录基本信息
./DEBIAN/postinst:安装完成后需执行的动作;(当前包括/home/appops/qiye-mask目录权限设置,以及/usr/local/bin/下软链接创建)
-
打包命令(appops账号)
cd /home/appops && dpkg -b qiye-mask-package qiye-mask-0.0.1.deb -
安装命令(root账号)
dpkg -i qiye-mask-0.0.1.deb -
服务启停
启动:cd /home/appops/qiye-mask & ./bin/apisix start
关闭:cd /home/appops/qiye-mask & ./bin/apisix stop
Eureka集成
discovery:
eureka:
host: # it’s possible to define multiple eureka hosts addresses of the same eureka cluster.
- “http://10.150.0.8:8660/eureka/”
- “http://10.150.0.3:8660/eureka/”
prefix: “/eureka/”
fetch_interval: 30 # 30s
weight: 100 # default weight for node
timeout:
connect: 2000 # 2000ms
send: 2000 # 2000ms
read: 5000 # 5000ms
插件相关
参考官网插件开发指南