APISIX 是一个云原生、高性能、可扩展的微服务 API 网关。
它是基于 OpenResty 和 etcd 来实现,和传统 API 网关相比,APISIX 具备动态路由和插件热加载,特别适合微服务体系下的 API 管理。
服务器三台
CentOS:7.6 内核:3.1
IP:172.18.235.156/24 , 172.18.235.154/24, 172.18.235.155/24
注意:
1、三台服务器相同的操作,在配置etcd时需要更改IP 。
2、关闭服务器防火墙,selinux
1、配置依赖
1.1、安装 epel, luarocks 需要它
]# wget http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
]# rpm -ivh epel-release-latest-7.noarch.rpm
1.2 安装 etcd
]# mkdir -pv /opt/src
]# cd /opt/src
]# wget https://github.com/etcd-io/etcd/releases/download/v3.4.13/etcd-v3.4.13-linux-amd64.tar.gz
]# tar -xf etcd-v3.4.13-linux-amd64.tar.gz -C /opt
]# mv etcd-v3.4.13-linux-amd64 etcd-v3.4.13
]# ln -sf /opt/etcd-v3.4.13 /opt/etcd
]# cd /opt/etcd
]#cat etcd.sh ##编写etcd 启动脚本(172.18.235.156)
#!/bin/sh
./etcd --name="node1" --data-dir="/opt/etcd/default.etcd" --listen-peer-urls="http://172.18.235.156:2380" --listen-client-urls="http://172.18.235.156:2379,http://localhost:2379" --initial-advertise-peer-urls="http://172.18.235.156:2380" --advertise-client-urls="http://172.18.235.156:2379" --initial-cluster="node1=http://172.18.235.156:2380,node2=http://172.18.235.154:2380,node3=http://172.18.235.155:2380" --initial-cluster-token="etcd-cluster" --initial-cluster-state="new" >> /opt/etcd/etcd.log 2>&1 &
]# cat etcd.sh ##编写etcd 启动脚本(172.18.235.154)
#!/bin/sh
./etcd --name="node1" --data-dir="/opt/etcd/default.etcd" --listen-peer-urls="http://172.18.235.156:2380" --listen-client-urls="http://172.18.235.156:2379,http://localhost:2379" --initial-advertise-peer-urls="http://172.18.235.156:2380" --advertise-client-urls="http://172.18.235.156:2379" --initial-cluster="node1=http://172.18.235.156:2380,node2=http://172.18.235.154:2380,node3=http://172.18.235.155:2380" --initial-cluster-token="etcd-cluster" --initial-cluster-state="new" >> /opt/etcd/etcd.log 2>&1 &
]# cat etcd.sh ##编写etcd 启动脚本(172.18.235.155)
#!/bin/sh
./etcd --name="node3" --data-dir="/opt/etcd/default.etcd" --listen-peer-urls="http://172.18.235.155:2380" --listen-client-urls="http://172.18.235.155:2379,http://localhost:2379" --initial-advertise-peer-urls="http://172.18.235.155:2380" --advertise-client-urls="http://172.18.235.155:2379" --initial-cluster="node1=http://172.18.235.156:2380,node2=http://172.18.235.154:2380,node3=http://172.18.235.155:2380" --initial-cluster-token="etcd-cluster" --initial-cluster-state="new" >> /usr/local/etcd-v3.4.13-linux-amd64/etcd.log 2>&1 &
启动:
]# bash etcd.sh
验证:
]# ./etcdctl member list
1.3、部署OpenResty
]# yum install yum-utils
]# yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo
]# yum install -y openresty curl git gcc luarocks lua-devel gcc-c++ ##安装 OpenResty 和 编译工具
1.4、安装go、yarn、node(软件先下载,并上传至服务器)
]# cd /opt/src
]# tar -xf yarn-v1.22.4.tar.gz -C /opt/
]# ln -sf /opt/yarn-v1.22.4/ /opt/yarn
]# tar -xf node-v12.16.1-linux-x64.tar.xz -C /opt/yarn
]# mv /opt/node-v12.16.1-linux-x64/ /opt/node-v12.16.1
]# ln -sf /opt/node-v12.16.1 /opt/node
]# tar -xf go1.13.linux-amd64.tar.gz -C /opt/
]# mv go go-v1.13
]# ln -sf /opt/go-v1.13 /opt/go
]# vim /etc/profile ##文件最后添加
。。。。。。。。省略。。。。。。
PATH=/opt/yarn/bin/:$PATH
PATH=/opt/node/bin/:$PATH
PATH=$HOME/bin:$HOME/.local/bin:$PATH
export PATH=$PATH:/opt/go/bin
export GOROOT=/opt/go
export GOPATH=$HOME/go
export PATH=$PATH:$HOME/go/bin
]# source /etc/profile
2、安装apisix
]# mkdir -pv /opt/apisix-2.0
]# cd /opt/src
]# wget https://downloads.apache.org/apisix/2.0/apache-apisix-2.0-src.tgz
]# tar zxvf apache-apisix-2.0-src.tgz -C /opt/apisix-2.0
]# ln -sf /opt/apisix-v2.0 /opt/apisix
]# cd /opt/apisix
]# ./bin/apisix start
验证:
]# ps -ef | grep apisix
]# netstat -tnulp ## 9080、9443 端口
3、安装apisix-dashboard 插件
]# cd /opt/
]# git clone https://github.com/apache/apisix-dashboard.git
]# cd apisix-dashboard
]# go env -w GO111MODULE=on
]# export APIX_ETCD_ENDPOINTS="127.0.0.1:2379"
]# go env -w GOPROXY=https://goproxy.cn,direct
]# vim api/run.sh
# config
export SYSLOG_HOST=0.0.0.0 ##修改IP,将127.0.0.1改成 0.0.0.0
]# nohuo ./api/run.sh > /dev/null &
]# yarn install
]# yarn build ##如果此步成功,在当前目录下生成一个 dist 目录,里面有web 程序
验证:
]# ps -ef | grep manager-api
]# netstat -tnulp ##8080 端口
浏览器访问: http://IP:8080 ##账号密码默认admin,登录进去后添加内容,然后再打开其它两台服务器的web 页面会看到刚添加进去的内容